blob: 46eabe1d771c46bc2e0d40ce8fb8b96561998b86 [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 (
Jaewoong Jung939ebd52019-03-26 15:07:36 -070018 "bufio"
19 "bytes"
Jiyong Park25fc6a92018-11-18 18:02:45 +090020 "io/ioutil"
21 "os"
22 "strings"
23 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090024
25 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
28 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090029 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090030)
31
32func testApex(t *testing.T, bp string) *android.TestContext {
33 config, buildDir := setup(t)
34 defer teardown(buildDir)
35
36 ctx := android.NewTestArchContext()
Alex Light0851b882019-02-07 13:20:53 -080037 ctx.RegisterModuleType("apex", android.ModuleFactoryAdaptor(apexBundleFactory))
38 ctx.RegisterModuleType("apex_test", android.ModuleFactoryAdaptor(testApexBundleFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090039 ctx.RegisterModuleType("apex_key", android.ModuleFactoryAdaptor(apexKeyFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090040 ctx.RegisterModuleType("apex_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070041 ctx.RegisterModuleType("prebuilt_apex", android.ModuleFactoryAdaptor(PrebuiltFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090042 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Park25fc6a92018-11-18 18:02:45 +090043
44 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
45 ctx.TopDown("apex_deps", apexDepsMutator)
46 ctx.BottomUp("apex", apexMutator)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070047 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
48 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090049 })
50
51 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
52 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +090053 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +090054 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090055 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090056 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090057 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +090058 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park04480cf2019-02-06 00:16:29 +090059 ctx.RegisterModuleType("sh_binary", android.ModuleFactoryAdaptor(android.ShBinaryFactory))
Jiyong Parkb2742fd2019-02-11 11:38:15 +090060 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(java.AndroidAppCertificateFactory))
Jiyong Park809bb722019-02-13 21:33:49 +090061 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070062 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
63 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
64 })
Jiyong Park25fc6a92018-11-18 18:02:45 +090065 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +090066 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090067 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +090068 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090069 ctx.BottomUp("version", cc.VersionMutator).Parallel()
70 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
71 })
72
73 ctx.Register()
74
75 bp = bp + `
76 toolchain_library {
77 name: "libcompiler_rt-extras",
78 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090079 vendor_available: true,
80 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090081 }
82
83 toolchain_library {
84 name: "libatomic",
85 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090086 vendor_available: true,
87 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090088 }
89
90 toolchain_library {
91 name: "libgcc",
92 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090093 vendor_available: true,
94 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090095 }
96
97 toolchain_library {
98 name: "libclang_rt.builtins-aarch64-android",
99 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900100 vendor_available: true,
101 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900102 }
103
104 toolchain_library {
105 name: "libclang_rt.builtins-arm-android",
106 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900107 vendor_available: true,
108 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900109 }
110
111 cc_object {
112 name: "crtbegin_so",
113 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900114 vendor_available: true,
115 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900116 }
117
118 cc_object {
119 name: "crtend_so",
120 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900121 vendor_available: true,
122 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900123 }
124
Alex Light3d673592019-01-18 14:37:31 -0800125 cc_object {
126 name: "crtbegin_static",
127 stl: "none",
128 }
129
130 cc_object {
131 name: "crtend_android",
132 stl: "none",
133 }
134
Jiyong Parkda6eb592018-12-19 17:12:36 +0900135 llndk_library {
136 name: "libc",
137 symbol_file: "",
138 }
139
140 llndk_library {
141 name: "libm",
142 symbol_file: "",
143 }
144
145 llndk_library {
146 name: "libdl",
147 symbol_file: "",
148 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900149 `
150
151 ctx.MockFileSystem(map[string][]byte{
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900152 "Android.bp": []byte(bp),
153 "build/target/product/security": nil,
154 "apex_manifest.json": nil,
Jiyong Park809bb722019-02-13 21:33:49 +0900155 "AndroidManifest.xml": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900156 "system/sepolicy/apex/myapex-file_contexts": nil,
157 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
158 "system/sepolicy/apex/otherapex-file_contexts": nil,
159 "mylib.cpp": nil,
160 "myprebuilt": nil,
161 "my_include": nil,
162 "vendor/foo/devkeys/test.x509.pem": nil,
163 "vendor/foo/devkeys/test.pk8": nil,
164 "testkey.x509.pem": nil,
165 "testkey.pk8": nil,
166 "testkey.override.x509.pem": nil,
167 "testkey.override.pk8": nil,
168 "vendor/foo/devkeys/testkey.avbpubkey": nil,
169 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park52818fc2019-03-18 12:01:38 +0900170 "NOTICE": nil,
171 "custom_notice": nil,
Jiyong Park67882562019-03-21 01:11:21 +0900172 "testkey2.avbpubkey": nil,
173 "testkey2.pem": nil,
Jiyong Parkc95714e2019-03-29 14:23:10 +0900174 "myapex-arm64.apex": nil,
175 "myapex-arm.apex": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900176 })
177 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
178 android.FailIfErrored(t, errs)
179 _, errs = ctx.PrepareBuildActions(config)
180 android.FailIfErrored(t, errs)
181
182 return ctx
183}
184
185func setup(t *testing.T) (config android.Config, buildDir string) {
186 buildDir, err := ioutil.TempDir("", "soong_apex_test")
187 if err != nil {
188 t.Fatal(err)
189 }
190
191 config = android.TestArchConfig(buildDir, nil)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900192 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
Jiyong Park9335a262018-12-24 11:31:58 +0900193 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900194 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
Jiyong Park25fc6a92018-11-18 18:02:45 +0900195 return
196}
197
198func teardown(buildDir string) {
199 os.RemoveAll(buildDir)
200}
201
202// ensure that 'result' contains 'expected'
203func ensureContains(t *testing.T, result string, expected string) {
204 if !strings.Contains(result, expected) {
205 t.Errorf("%q is not found in %q", expected, result)
206 }
207}
208
209// ensures that 'result' does not contain 'notExpected'
210func ensureNotContains(t *testing.T, result string, notExpected string) {
211 if strings.Contains(result, notExpected) {
212 t.Errorf("%q is found in %q", notExpected, result)
213 }
214}
215
216func ensureListContains(t *testing.T, result []string, expected string) {
217 if !android.InList(expected, result) {
218 t.Errorf("%q is not found in %v", expected, result)
219 }
220}
221
222func ensureListNotContains(t *testing.T, result []string, notExpected string) {
223 if android.InList(notExpected, result) {
224 t.Errorf("%q is found in %v", notExpected, result)
225 }
226}
227
228// Minimal test
229func TestBasicApex(t *testing.T) {
230 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900231 apex_defaults {
232 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900233 manifest: ":myapex.manifest",
234 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900235 key: "myapex.key",
236 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800237 multilib: {
238 both: {
239 binaries: ["foo",],
240 }
241 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242 }
243
Jiyong Park30ca9372019-02-07 16:27:23 +0900244 apex {
245 name: "myapex",
246 defaults: ["myapex-defaults"],
247 }
248
Jiyong Park25fc6a92018-11-18 18:02:45 +0900249 apex_key {
250 name: "myapex.key",
251 public_key: "testkey.avbpubkey",
252 private_key: "testkey.pem",
253 }
254
Jiyong Park809bb722019-02-13 21:33:49 +0900255 filegroup {
256 name: "myapex.manifest",
257 srcs: ["apex_manifest.json"],
258 }
259
260 filegroup {
261 name: "myapex.androidmanifest",
262 srcs: ["AndroidManifest.xml"],
263 }
264
Jiyong Park25fc6a92018-11-18 18:02:45 +0900265 cc_library {
266 name: "mylib",
267 srcs: ["mylib.cpp"],
268 shared_libs: ["mylib2"],
269 system_shared_libs: [],
270 stl: "none",
271 }
272
Alex Light3d673592019-01-18 14:37:31 -0800273 cc_binary {
274 name: "foo",
275 srcs: ["mylib.cpp"],
276 compile_multilib: "both",
277 multilib: {
278 lib32: {
279 suffix: "32",
280 },
281 lib64: {
282 suffix: "64",
283 },
284 },
285 symlinks: ["foo_link_"],
286 symlink_preferred_arch: true,
287 system_shared_libs: [],
288 static_executable: true,
289 stl: "none",
290 }
291
Jiyong Park25fc6a92018-11-18 18:02:45 +0900292 cc_library {
293 name: "mylib2",
294 srcs: ["mylib.cpp"],
295 system_shared_libs: [],
296 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900297 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900298 }
299 `)
300
301 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900302
303 optFlags := apexRule.Args["opt_flags"]
304 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
305
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306 copyCmds := apexRule.Args["copy_commands"]
307
308 // Ensure that main rule creates an output
309 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
310
311 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900312 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313
314 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900315 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900316
317 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800318 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
319 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800320
321 // Ensure that the platform variant ends with _core_shared
322 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
323 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800324
325 // Ensure that all symlinks are present.
326 found_foo_link_64 := false
327 found_foo := false
328 for _, cmd := range strings.Split(copyCmds, " && ") {
329 if strings.HasPrefix(cmd, "ln -s foo64") {
330 if strings.HasSuffix(cmd, "bin/foo") {
331 found_foo = true
332 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
333 found_foo_link_64 = true
334 }
335 }
336 }
337 good := found_foo && found_foo_link_64
338 if !good {
339 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
340 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900341
342 apexMergeNoticeRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexMergeNoticeRule")
343 noticeInputs := strings.Split(apexMergeNoticeRule.Args["inputs"], " ")
344 if len(noticeInputs) != 3 {
345 t.Errorf("number of input notice files: expected = 3, actual = %d", len(noticeInputs))
346 }
347 ensureListContains(t, noticeInputs, "NOTICE")
348 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800349}
350
351func TestBasicZipApex(t *testing.T) {
352 ctx := testApex(t, `
353 apex {
354 name: "myapex",
355 key: "myapex.key",
356 payload_type: "zip",
357 native_shared_libs: ["mylib"],
358 }
359
360 apex_key {
361 name: "myapex.key",
362 public_key: "testkey.avbpubkey",
363 private_key: "testkey.pem",
364 }
365
366 cc_library {
367 name: "mylib",
368 srcs: ["mylib.cpp"],
369 shared_libs: ["mylib2"],
370 system_shared_libs: [],
371 stl: "none",
372 }
373
374 cc_library {
375 name: "mylib2",
376 srcs: ["mylib.cpp"],
377 system_shared_libs: [],
378 stl: "none",
379 }
380 `)
381
382 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
383 copyCmds := zipApexRule.Args["copy_commands"]
384
385 // Ensure that main rule creates an output
386 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
387
388 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900389 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800390
391 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900392 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800393
394 // Ensure that both direct and indirect deps are copied into apex
395 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
396 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900397}
398
399func TestApexWithStubs(t *testing.T) {
400 ctx := testApex(t, `
401 apex {
402 name: "myapex",
403 key: "myapex.key",
404 native_shared_libs: ["mylib", "mylib3"],
405 }
406
407 apex_key {
408 name: "myapex.key",
409 public_key: "testkey.avbpubkey",
410 private_key: "testkey.pem",
411 }
412
413 cc_library {
414 name: "mylib",
415 srcs: ["mylib.cpp"],
416 shared_libs: ["mylib2", "mylib3"],
417 system_shared_libs: [],
418 stl: "none",
419 }
420
421 cc_library {
422 name: "mylib2",
423 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900424 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900425 system_shared_libs: [],
426 stl: "none",
427 stubs: {
428 versions: ["1", "2", "3"],
429 },
430 }
431
432 cc_library {
433 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900434 srcs: ["mylib.cpp"],
435 shared_libs: ["mylib4"],
436 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900437 stl: "none",
438 stubs: {
439 versions: ["10", "11", "12"],
440 },
441 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900442
443 cc_library {
444 name: "mylib4",
445 srcs: ["mylib.cpp"],
446 system_shared_libs: [],
447 stl: "none",
448 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900449 `)
450
451 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
452 copyCmds := apexRule.Args["copy_commands"]
453
454 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800455 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900456
457 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800458 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900459
460 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800461 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900462
Jiyong Parkda6eb592018-12-19 17:12:36 +0900463 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900464
465 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900466 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900467 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900468 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900469
470 // 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 +0900471 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900472 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900473 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900474
475 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900476 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900477 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900478
479 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900480 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 +0900481}
482
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900483func TestApexWithExplicitStubsDependency(t *testing.T) {
484 ctx := testApex(t, `
485 apex {
486 name: "myapex",
487 key: "myapex.key",
488 native_shared_libs: ["mylib"],
489 }
490
491 apex_key {
492 name: "myapex.key",
493 public_key: "testkey.avbpubkey",
494 private_key: "testkey.pem",
495 }
496
497 cc_library {
498 name: "mylib",
499 srcs: ["mylib.cpp"],
500 shared_libs: ["libfoo#10"],
501 system_shared_libs: [],
502 stl: "none",
503 }
504
505 cc_library {
506 name: "libfoo",
507 srcs: ["mylib.cpp"],
508 shared_libs: ["libbar"],
509 system_shared_libs: [],
510 stl: "none",
511 stubs: {
512 versions: ["10", "20", "30"],
513 },
514 }
515
516 cc_library {
517 name: "libbar",
518 srcs: ["mylib.cpp"],
519 system_shared_libs: [],
520 stl: "none",
521 }
522
523 `)
524
525 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
526 copyCmds := apexRule.Args["copy_commands"]
527
528 // Ensure that direct non-stubs dep is always included
529 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
530
531 // Ensure that indirect stubs dep is not included
532 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
533
534 // Ensure that dependency of stubs is not included
535 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
536
Jiyong Parkda6eb592018-12-19 17:12:36 +0900537 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900538
539 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900540 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900541 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900542 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900543
Jiyong Parkda6eb592018-12-19 17:12:36 +0900544 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900545
546 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
547 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
548}
549
Jiyong Park25fc6a92018-11-18 18:02:45 +0900550func TestApexWithSystemLibsStubs(t *testing.T) {
551 ctx := testApex(t, `
552 apex {
553 name: "myapex",
554 key: "myapex.key",
555 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
556 }
557
558 apex_key {
559 name: "myapex.key",
560 public_key: "testkey.avbpubkey",
561 private_key: "testkey.pem",
562 }
563
564 cc_library {
565 name: "mylib",
566 srcs: ["mylib.cpp"],
567 shared_libs: ["libdl#27"],
568 stl: "none",
569 }
570
571 cc_library_shared {
572 name: "mylib_shared",
573 srcs: ["mylib.cpp"],
574 shared_libs: ["libdl#27"],
575 stl: "none",
576 }
577
578 cc_library {
579 name: "libc",
580 no_libgcc: true,
581 nocrt: true,
582 system_shared_libs: [],
583 stl: "none",
584 stubs: {
585 versions: ["27", "28", "29"],
586 },
587 }
588
589 cc_library {
590 name: "libm",
591 no_libgcc: true,
592 nocrt: true,
593 system_shared_libs: [],
594 stl: "none",
595 stubs: {
596 versions: ["27", "28", "29"],
597 },
598 }
599
600 cc_library {
601 name: "libdl",
602 no_libgcc: true,
603 nocrt: true,
604 system_shared_libs: [],
605 stl: "none",
606 stubs: {
607 versions: ["27", "28", "29"],
608 },
609 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900610
611 cc_library {
612 name: "libBootstrap",
613 srcs: ["mylib.cpp"],
614 stl: "none",
615 bootstrap: true,
616 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900617 `)
618
619 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
620 copyCmds := apexRule.Args["copy_commands"]
621
622 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800623 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900624 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
625 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900626
627 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900628 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900629
Jiyong Parkda6eb592018-12-19 17:12:36 +0900630 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
631 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
632 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900633
634 // For dependency to libc
635 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900636 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900637 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900638 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900639 // ... Cflags from stub is correctly exported to mylib
640 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
641 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
642
643 // For dependency to libm
644 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900645 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900646 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900647 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900648 // ... and is not compiling with the stub
649 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
650 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
651
652 // For dependency to libdl
653 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900654 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900655 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900656 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
657 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900658 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900659 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900660 // ... Cflags from stub is correctly exported to mylib
661 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
662 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900663
664 // Ensure that libBootstrap is depending on the platform variant of bionic libs
665 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
666 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
667 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
668 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900670
671func TestFilesInSubDir(t *testing.T) {
672 ctx := testApex(t, `
673 apex {
674 name: "myapex",
675 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900676 native_shared_libs: ["mylib"],
677 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900678 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900679 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900680 }
681
682 apex_key {
683 name: "myapex.key",
684 public_key: "testkey.avbpubkey",
685 private_key: "testkey.pem",
686 }
687
688 prebuilt_etc {
689 name: "myetc",
690 src: "myprebuilt",
691 sub_dir: "foo/bar",
692 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900693
694 cc_library {
695 name: "mylib",
696 srcs: ["mylib.cpp"],
697 relative_install_path: "foo/bar",
698 system_shared_libs: [],
699 stl: "none",
700 }
701
702 cc_binary {
703 name: "mybin",
704 srcs: ["mylib.cpp"],
705 relative_install_path: "foo/bar",
706 system_shared_libs: [],
707 static_executable: true,
708 stl: "none",
709 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900710 `)
711
712 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
713 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
714
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900715 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900716 ensureListContains(t, dirs, "etc")
717 ensureListContains(t, dirs, "etc/foo")
718 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900719 ensureListContains(t, dirs, "lib64")
720 ensureListContains(t, dirs, "lib64/foo")
721 ensureListContains(t, dirs, "lib64/foo/bar")
722 ensureListContains(t, dirs, "lib")
723 ensureListContains(t, dirs, "lib/foo")
724 ensureListContains(t, dirs, "lib/foo/bar")
725
Jiyong Parkbd13e442019-03-15 18:10:35 +0900726 ensureListContains(t, dirs, "bin")
727 ensureListContains(t, dirs, "bin/foo")
728 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900729}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900730
731func TestUseVendor(t *testing.T) {
732 ctx := testApex(t, `
733 apex {
734 name: "myapex",
735 key: "myapex.key",
736 native_shared_libs: ["mylib"],
737 use_vendor: true,
738 }
739
740 apex_key {
741 name: "myapex.key",
742 public_key: "testkey.avbpubkey",
743 private_key: "testkey.pem",
744 }
745
746 cc_library {
747 name: "mylib",
748 srcs: ["mylib.cpp"],
749 shared_libs: ["mylib2"],
750 system_shared_libs: [],
751 vendor_available: true,
752 stl: "none",
753 }
754
755 cc_library {
756 name: "mylib2",
757 srcs: ["mylib.cpp"],
758 system_shared_libs: [],
759 vendor_available: true,
760 stl: "none",
761 }
762 `)
763
764 inputsList := []string{}
765 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
766 for _, implicit := range i.Implicits {
767 inputsList = append(inputsList, implicit.String())
768 }
769 }
770 inputsString := strings.Join(inputsList, " ")
771
772 // ensure that the apex includes vendor variants of the direct and indirect deps
773 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
774 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
775
776 // ensure that the apex does not include core variants
777 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
778 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
779}
Jiyong Park16e91a02018-12-20 18:18:08 +0900780
781func TestStaticLinking(t *testing.T) {
782 ctx := testApex(t, `
783 apex {
784 name: "myapex",
785 key: "myapex.key",
786 native_shared_libs: ["mylib"],
787 }
788
789 apex_key {
790 name: "myapex.key",
791 public_key: "testkey.avbpubkey",
792 private_key: "testkey.pem",
793 }
794
795 cc_library {
796 name: "mylib",
797 srcs: ["mylib.cpp"],
798 system_shared_libs: [],
799 stl: "none",
800 stubs: {
801 versions: ["1", "2", "3"],
802 },
803 }
804
805 cc_binary {
806 name: "not_in_apex",
807 srcs: ["mylib.cpp"],
808 static_libs: ["mylib"],
809 static_executable: true,
810 system_shared_libs: [],
811 stl: "none",
812 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900813 `)
814
815 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
816
817 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800818 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900819}
Jiyong Park9335a262018-12-24 11:31:58 +0900820
821func TestKeys(t *testing.T) {
822 ctx := testApex(t, `
823 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900824 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900825 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900826 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900827 native_shared_libs: ["mylib"],
828 }
829
830 cc_library {
831 name: "mylib",
832 srcs: ["mylib.cpp"],
833 system_shared_libs: [],
834 stl: "none",
835 }
836
837 apex_key {
838 name: "myapex.key",
839 public_key: "testkey.avbpubkey",
840 private_key: "testkey.pem",
841 }
842
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900843 android_app_certificate {
844 name: "myapex.certificate",
845 certificate: "testkey",
846 }
847
848 android_app_certificate {
849 name: "myapex.certificate.override",
850 certificate: "testkey.override",
851 }
852
Jiyong Park9335a262018-12-24 11:31:58 +0900853 `)
854
855 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900856 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900857
858 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
859 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
860 "vendor/foo/devkeys/testkey.avbpubkey")
861 }
862 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
863 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
864 "vendor/foo/devkeys/testkey.pem")
865 }
866
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900867 // check the APK certs. It should be overridden to myapex.certificate.override
868 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
869 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900870 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900871 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900872 }
873}
Jiyong Park58e364a2019-01-19 19:24:06 +0900874
875func TestMacro(t *testing.T) {
876 ctx := testApex(t, `
877 apex {
878 name: "myapex",
879 key: "myapex.key",
880 native_shared_libs: ["mylib"],
881 }
882
883 apex {
884 name: "otherapex",
885 key: "myapex.key",
886 native_shared_libs: ["mylib"],
887 }
888
889 apex_key {
890 name: "myapex.key",
891 public_key: "testkey.avbpubkey",
892 private_key: "testkey.pem",
893 }
894
895 cc_library {
896 name: "mylib",
897 srcs: ["mylib.cpp"],
898 system_shared_libs: [],
899 stl: "none",
900 }
901 `)
902
903 // non-APEX variant does not have __ANDROID__APEX__ defined
904 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
905 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
906 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
907
908 // APEX variant has __ANDROID_APEX__=<apexname> defined
909 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
910 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
911 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
912
913 // APEX variant has __ANDROID_APEX__=<apexname> defined
914 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
915 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
916 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
917}
Jiyong Park7e636d02019-01-28 16:16:54 +0900918
919func TestHeaderLibsDependency(t *testing.T) {
920 ctx := testApex(t, `
921 apex {
922 name: "myapex",
923 key: "myapex.key",
924 native_shared_libs: ["mylib"],
925 }
926
927 apex_key {
928 name: "myapex.key",
929 public_key: "testkey.avbpubkey",
930 private_key: "testkey.pem",
931 }
932
933 cc_library_headers {
934 name: "mylib_headers",
935 export_include_dirs: ["my_include"],
936 system_shared_libs: [],
937 stl: "none",
938 }
939
940 cc_library {
941 name: "mylib",
942 srcs: ["mylib.cpp"],
943 system_shared_libs: [],
944 stl: "none",
945 header_libs: ["mylib_headers"],
946 export_header_lib_headers: ["mylib_headers"],
947 stubs: {
948 versions: ["1", "2", "3"],
949 },
950 }
951
952 cc_library {
953 name: "otherlib",
954 srcs: ["mylib.cpp"],
955 system_shared_libs: [],
956 stl: "none",
957 shared_libs: ["mylib"],
958 }
959 `)
960
961 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
962
963 // Ensure that the include path of the header lib is exported to 'otherlib'
964 ensureContains(t, cFlags, "-Imy_include")
965}
Alex Light9670d332019-01-29 18:07:33 -0800966
Alex Light0851b882019-02-07 13:20:53 -0800967func TestNonTestApex(t *testing.T) {
968 ctx := testApex(t, `
969 apex {
970 name: "myapex",
971 key: "myapex.key",
972 native_shared_libs: ["mylib_common"],
973 }
974
975 apex_key {
976 name: "myapex.key",
977 public_key: "testkey.avbpubkey",
978 private_key: "testkey.pem",
979 }
980
981 cc_library {
982 name: "mylib_common",
983 srcs: ["mylib.cpp"],
984 system_shared_libs: [],
985 stl: "none",
986 }
987 `)
988
989 module := ctx.ModuleForTests("myapex", "android_common_myapex")
990 apexRule := module.Rule("apexRule")
991 copyCmds := apexRule.Args["copy_commands"]
992
993 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
994 t.Log("Apex was a test apex!")
995 t.Fail()
996 }
997 // Ensure that main rule creates an output
998 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
999
1000 // Ensure that apex variant is created for the direct dep
1001 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1002
1003 // Ensure that both direct and indirect deps are copied into apex
1004 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1005
1006 // Ensure that the platform variant ends with _core_shared
1007 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1008
1009 if !android.InAnyApex("mylib_common") {
1010 t.Log("Found mylib_common not in any apex!")
1011 t.Fail()
1012 }
1013}
1014
1015func TestTestApex(t *testing.T) {
1016 if android.InAnyApex("mylib_common_test") {
1017 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!")
1018 }
1019 ctx := testApex(t, `
1020 apex_test {
1021 name: "myapex",
1022 key: "myapex.key",
1023 native_shared_libs: ["mylib_common_test"],
1024 }
1025
1026 apex_key {
1027 name: "myapex.key",
1028 public_key: "testkey.avbpubkey",
1029 private_key: "testkey.pem",
1030 }
1031
1032 cc_library {
1033 name: "mylib_common_test",
1034 srcs: ["mylib.cpp"],
1035 system_shared_libs: [],
1036 stl: "none",
1037 }
1038 `)
1039
1040 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1041 apexRule := module.Rule("apexRule")
1042 copyCmds := apexRule.Args["copy_commands"]
1043
1044 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1045 t.Log("Apex was not a test apex!")
1046 t.Fail()
1047 }
1048 // Ensure that main rule creates an output
1049 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1050
1051 // Ensure that apex variant is created for the direct dep
1052 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1053
1054 // Ensure that both direct and indirect deps are copied into apex
1055 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1056
1057 // Ensure that the platform variant ends with _core_shared
1058 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1059
1060 if android.InAnyApex("mylib_common_test") {
1061 t.Log("Found mylib_common_test in some apex!")
1062 t.Fail()
1063 }
1064}
1065
Alex Light9670d332019-01-29 18:07:33 -08001066func TestApexWithTarget(t *testing.T) {
1067 ctx := testApex(t, `
1068 apex {
1069 name: "myapex",
1070 key: "myapex.key",
1071 multilib: {
1072 first: {
1073 native_shared_libs: ["mylib_common"],
1074 }
1075 },
1076 target: {
1077 android: {
1078 multilib: {
1079 first: {
1080 native_shared_libs: ["mylib"],
1081 }
1082 }
1083 },
1084 host: {
1085 multilib: {
1086 first: {
1087 native_shared_libs: ["mylib2"],
1088 }
1089 }
1090 }
1091 }
1092 }
1093
1094 apex_key {
1095 name: "myapex.key",
1096 public_key: "testkey.avbpubkey",
1097 private_key: "testkey.pem",
1098 }
1099
1100 cc_library {
1101 name: "mylib",
1102 srcs: ["mylib.cpp"],
1103 system_shared_libs: [],
1104 stl: "none",
1105 }
1106
1107 cc_library {
1108 name: "mylib_common",
1109 srcs: ["mylib.cpp"],
1110 system_shared_libs: [],
1111 stl: "none",
1112 compile_multilib: "first",
1113 }
1114
1115 cc_library {
1116 name: "mylib2",
1117 srcs: ["mylib.cpp"],
1118 system_shared_libs: [],
1119 stl: "none",
1120 compile_multilib: "first",
1121 }
1122 `)
1123
1124 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1125 copyCmds := apexRule.Args["copy_commands"]
1126
1127 // Ensure that main rule creates an output
1128 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1129
1130 // Ensure that apex variant is created for the direct dep
1131 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1132 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1133 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1134
1135 // Ensure that both direct and indirect deps are copied into apex
1136 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1137 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1138 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1139
1140 // Ensure that the platform variant ends with _core_shared
1141 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1142 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1143 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1144}
Jiyong Park04480cf2019-02-06 00:16:29 +09001145
1146func TestApexWithShBinary(t *testing.T) {
1147 ctx := testApex(t, `
1148 apex {
1149 name: "myapex",
1150 key: "myapex.key",
1151 binaries: ["myscript"],
1152 }
1153
1154 apex_key {
1155 name: "myapex.key",
1156 public_key: "testkey.avbpubkey",
1157 private_key: "testkey.pem",
1158 }
1159
1160 sh_binary {
1161 name: "myscript",
1162 src: "mylib.cpp",
1163 filename: "myscript.sh",
1164 sub_dir: "script",
1165 }
1166 `)
1167
1168 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1169 copyCmds := apexRule.Args["copy_commands"]
1170
1171 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1172}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001173
1174func TestApexInProductPartition(t *testing.T) {
1175 ctx := testApex(t, `
1176 apex {
1177 name: "myapex",
1178 key: "myapex.key",
1179 native_shared_libs: ["mylib"],
1180 product_specific: true,
1181 }
1182
1183 apex_key {
1184 name: "myapex.key",
1185 public_key: "testkey.avbpubkey",
1186 private_key: "testkey.pem",
1187 product_specific: true,
1188 }
1189
1190 cc_library {
1191 name: "mylib",
1192 srcs: ["mylib.cpp"],
1193 system_shared_libs: [],
1194 stl: "none",
1195 }
1196 `)
1197
1198 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1199 expected := "target/product/test_device/product/apex"
1200 actual := apex.installDir.RelPathString()
1201 if actual != expected {
1202 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1203 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001204}
Jiyong Park67882562019-03-21 01:11:21 +09001205
1206func TestApexKeyFromOtherModule(t *testing.T) {
1207 ctx := testApex(t, `
1208 apex_key {
1209 name: "myapex.key",
1210 public_key: ":my.avbpubkey",
1211 private_key: ":my.pem",
1212 product_specific: true,
1213 }
1214
1215 filegroup {
1216 name: "my.avbpubkey",
1217 srcs: ["testkey2.avbpubkey"],
1218 }
1219
1220 filegroup {
1221 name: "my.pem",
1222 srcs: ["testkey2.pem"],
1223 }
1224 `)
1225
1226 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1227 expected_pubkey := "testkey2.avbpubkey"
1228 actual_pubkey := apex_key.public_key_file.String()
1229 if actual_pubkey != expected_pubkey {
1230 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1231 }
1232 expected_privkey := "testkey2.pem"
1233 actual_privkey := apex_key.private_key_file.String()
1234 if actual_privkey != expected_privkey {
1235 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1236 }
1237}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001238
1239func TestPrebuilt(t *testing.T) {
1240 ctx := testApex(t, `
1241 prebuilt_apex {
1242 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09001243 arch: {
1244 arm64: {
1245 src: "myapex-arm64.apex",
1246 },
1247 arm: {
1248 src: "myapex-arm.apex",
1249 },
1250 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001251 key: "myapex.key"
1252 }
1253
1254 apex_key {
1255 name: "myapex.key",
1256 public_key: "testkey.avbpubkey",
1257 private_key: "testkey.pem",
1258 product_specific: true,
1259 }
1260 `)
1261
1262 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1263
Jiyong Parkc95714e2019-03-29 14:23:10 +09001264 expectedInput := "myapex-arm64.apex"
1265 if prebuilt.inputApex.String() != expectedInput {
1266 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
1267 }
1268
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001269 // Check if the key module is added as a required module.
1270 buf := &bytes.Buffer{}
1271 prebuilt.AndroidMk().Extra[0](buf, nil)
1272 found := false
1273 scanner := bufio.NewScanner(bytes.NewReader(buf.Bytes()))
1274 expected := "myapex.key"
1275 for scanner.Scan() {
1276 line := scanner.Text()
1277 tok := strings.Split(line, " := ")
1278 if tok[0] == "LOCAL_REQUIRED_MODULES" {
1279 found = true
1280 if tok[1] != "myapex.key" {
1281 t.Errorf("Unexpected LOCAL_REQUIRED_MODULES '%s', expected '%s'", tok[1], expected)
1282 }
1283 }
1284 }
1285 if !found {
1286 t.Errorf("Couldn't find a LOCAL_REQUIRED_MODULES entry")
1287 }
1288}