blob: a2610f667548d2a8a4e5840766502f2abf0c6b22 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
20 "strings"
21 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090022
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090027 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090028)
29
Jaewoong Jungd6585fe2019-06-18 13:09:13 -070030var buildDir string
31
Jaewoong Jung9c49b282020-05-14 14:15:24 -070032type testCustomizer func(fs map[string][]byte, config android.Config)
33
34func testApex(t *testing.T, bp string, handlers ...testCustomizer) *android.TestContext {
Jaewoong Jungd6585fe2019-06-18 13:09:13 -070035 var config android.Config
36 config, buildDir = setup(t)
Jaewoong Jung9c49b282020-05-14 14:15:24 -070037 for _, handler := range handlers {
38 tempFS := map[string][]byte{}
39 handler(tempFS, config)
40 }
Jiyong Park25fc6a92018-11-18 18:02:45 +090041 defer teardown(buildDir)
42
43 ctx := android.NewTestArchContext()
Alex Light0851b882019-02-07 13:20:53 -080044 ctx.RegisterModuleType("apex", android.ModuleFactoryAdaptor(apexBundleFactory))
45 ctx.RegisterModuleType("apex_test", android.ModuleFactoryAdaptor(testApexBundleFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090046 ctx.RegisterModuleType("apex_key", android.ModuleFactoryAdaptor(apexKeyFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090047 ctx.RegisterModuleType("apex_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070048 ctx.RegisterModuleType("prebuilt_apex", android.ModuleFactoryAdaptor(PrebuiltFactory))
Jaewoong Jung9c49b282020-05-14 14:15:24 -070049 ctx.RegisterModuleType("apex_set", android.ModuleFactoryAdaptor(apexSetFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090050 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Park25fc6a92018-11-18 18:02:45 +090051
52 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
53 ctx.TopDown("apex_deps", apexDepsMutator)
54 ctx.BottomUp("apex", apexMutator)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070055 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
56 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090057 })
58
59 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
60 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +090061 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +090062 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090063 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090064 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090065 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +090066 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park04480cf2019-02-06 00:16:29 +090067 ctx.RegisterModuleType("sh_binary", android.ModuleFactoryAdaptor(android.ShBinaryFactory))
Jiyong Parkb2742fd2019-02-11 11:38:15 +090068 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(java.AndroidAppCertificateFactory))
Jiyong Park809bb722019-02-13 21:33:49 +090069 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070070 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
71 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
72 })
Jiyong Park25fc6a92018-11-18 18:02:45 +090073 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +090074 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090075 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +090076 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090077 ctx.BottomUp("version", cc.VersionMutator).Parallel()
78 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
79 })
80
81 ctx.Register()
82
83 bp = bp + `
84 toolchain_library {
85 name: "libcompiler_rt-extras",
86 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090087 vendor_available: true,
88 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090089 }
90
91 toolchain_library {
92 name: "libatomic",
93 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090094 vendor_available: true,
95 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090096 }
97
98 toolchain_library {
99 name: "libgcc",
100 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900101 vendor_available: true,
102 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900103 }
104
105 toolchain_library {
Yi Kongb0e95c12019-05-07 04:46:14 +0000106 name: "libgcc_stripped",
107 src: "",
108 vendor_available: true,
109 recovery_available: true,
110 }
111
112 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900113 name: "libclang_rt.builtins-aarch64-android",
114 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900115 vendor_available: true,
116 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900117 }
118
119 toolchain_library {
120 name: "libclang_rt.builtins-arm-android",
121 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900122 vendor_available: true,
123 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900124 }
125
126 cc_object {
127 name: "crtbegin_so",
128 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900129 vendor_available: true,
130 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900131 }
132
133 cc_object {
134 name: "crtend_so",
135 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900136 vendor_available: true,
137 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900138 }
139
Alex Light3d673592019-01-18 14:37:31 -0800140 cc_object {
141 name: "crtbegin_static",
142 stl: "none",
143 }
144
145 cc_object {
146 name: "crtend_android",
147 stl: "none",
148 }
149
Jiyong Parkda6eb592018-12-19 17:12:36 +0900150 llndk_library {
151 name: "libc",
152 symbol_file: "",
153 }
154
155 llndk_library {
156 name: "libm",
157 symbol_file: "",
158 }
159
160 llndk_library {
161 name: "libdl",
162 symbol_file: "",
163 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900164 `
165
166 ctx.MockFileSystem(map[string][]byte{
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900167 "Android.bp": []byte(bp),
168 "build/target/product/security": nil,
169 "apex_manifest.json": nil,
Jiyong Park809bb722019-02-13 21:33:49 +0900170 "AndroidManifest.xml": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900171 "system/sepolicy/apex/myapex-file_contexts": nil,
172 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
173 "system/sepolicy/apex/otherapex-file_contexts": nil,
174 "mylib.cpp": nil,
175 "myprebuilt": nil,
176 "my_include": nil,
177 "vendor/foo/devkeys/test.x509.pem": nil,
178 "vendor/foo/devkeys/test.pk8": nil,
179 "testkey.x509.pem": nil,
180 "testkey.pk8": nil,
181 "testkey.override.x509.pem": nil,
182 "testkey.override.pk8": nil,
183 "vendor/foo/devkeys/testkey.avbpubkey": nil,
184 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park52818fc2019-03-18 12:01:38 +0900185 "NOTICE": nil,
186 "custom_notice": nil,
Jiyong Park67882562019-03-21 01:11:21 +0900187 "testkey2.avbpubkey": nil,
188 "testkey2.pem": nil,
Jiyong Parkc95714e2019-03-29 14:23:10 +0900189 "myapex-arm64.apex": nil,
190 "myapex-arm.apex": nil,
Jaewoong Jung9c49b282020-05-14 14:15:24 -0700191 "myapex.apks": nil,
Jiyong Parkd37a8822019-04-18 17:25:49 +0900192 "frameworks/base/api/current.txt": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900193 })
194 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
195 android.FailIfErrored(t, errs)
196 _, errs = ctx.PrepareBuildActions(config)
197 android.FailIfErrored(t, errs)
198
199 return ctx
200}
201
202func setup(t *testing.T) (config android.Config, buildDir string) {
203 buildDir, err := ioutil.TempDir("", "soong_apex_test")
204 if err != nil {
205 t.Fatal(err)
206 }
207
208 config = android.TestArchConfig(buildDir, nil)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900209 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
Jiyong Park9335a262018-12-24 11:31:58 +0900210 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900211 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
Jiyong Parkd37a8822019-04-18 17:25:49 +0900212 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
213 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900214 return
215}
216
217func teardown(buildDir string) {
218 os.RemoveAll(buildDir)
219}
220
221// ensure that 'result' contains 'expected'
222func ensureContains(t *testing.T, result string, expected string) {
223 if !strings.Contains(result, expected) {
224 t.Errorf("%q is not found in %q", expected, result)
225 }
226}
227
228// ensures that 'result' does not contain 'notExpected'
229func ensureNotContains(t *testing.T, result string, notExpected string) {
230 if strings.Contains(result, notExpected) {
231 t.Errorf("%q is found in %q", notExpected, result)
232 }
233}
234
235func ensureListContains(t *testing.T, result []string, expected string) {
236 if !android.InList(expected, result) {
237 t.Errorf("%q is not found in %v", expected, result)
238 }
239}
240
241func ensureListNotContains(t *testing.T, result []string, notExpected string) {
242 if android.InList(notExpected, result) {
243 t.Errorf("%q is found in %v", notExpected, result)
244 }
245}
246
247// Minimal test
248func TestBasicApex(t *testing.T) {
249 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900250 apex_defaults {
251 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900252 manifest: ":myapex.manifest",
253 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900254 key: "myapex.key",
255 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800256 multilib: {
257 both: {
258 binaries: ["foo",],
259 }
260 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261 }
262
Jiyong Park30ca9372019-02-07 16:27:23 +0900263 apex {
264 name: "myapex",
265 defaults: ["myapex-defaults"],
266 }
267
Jiyong Park25fc6a92018-11-18 18:02:45 +0900268 apex_key {
269 name: "myapex.key",
270 public_key: "testkey.avbpubkey",
271 private_key: "testkey.pem",
272 }
273
Jiyong Park809bb722019-02-13 21:33:49 +0900274 filegroup {
275 name: "myapex.manifest",
276 srcs: ["apex_manifest.json"],
277 }
278
279 filegroup {
280 name: "myapex.androidmanifest",
281 srcs: ["AndroidManifest.xml"],
282 }
283
Jiyong Park25fc6a92018-11-18 18:02:45 +0900284 cc_library {
285 name: "mylib",
286 srcs: ["mylib.cpp"],
287 shared_libs: ["mylib2"],
288 system_shared_libs: [],
289 stl: "none",
290 }
291
Alex Light3d673592019-01-18 14:37:31 -0800292 cc_binary {
293 name: "foo",
294 srcs: ["mylib.cpp"],
295 compile_multilib: "both",
296 multilib: {
297 lib32: {
298 suffix: "32",
299 },
300 lib64: {
301 suffix: "64",
302 },
303 },
304 symlinks: ["foo_link_"],
305 symlink_preferred_arch: true,
306 system_shared_libs: [],
307 static_executable: true,
308 stl: "none",
309 }
310
Jiyong Park25fc6a92018-11-18 18:02:45 +0900311 cc_library {
312 name: "mylib2",
313 srcs: ["mylib.cpp"],
314 system_shared_libs: [],
315 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900316 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317 }
318 `)
319
320 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900321
322 optFlags := apexRule.Args["opt_flags"]
323 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700324 // Ensure that the NOTICE output is being packaged as an asset.
325 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900326
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 copyCmds := apexRule.Args["copy_commands"]
328
329 // Ensure that main rule creates an output
330 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
331
332 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900333 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900334
335 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900336 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337
338 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800339 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
340 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800341
342 // Ensure that the platform variant ends with _core_shared
343 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
344 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800345
346 // Ensure that all symlinks are present.
347 found_foo_link_64 := false
348 found_foo := false
349 for _, cmd := range strings.Split(copyCmds, " && ") {
350 if strings.HasPrefix(cmd, "ln -s foo64") {
351 if strings.HasSuffix(cmd, "bin/foo") {
352 found_foo = true
353 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
354 found_foo_link_64 = true
355 }
356 }
357 }
358 good := found_foo && found_foo_link_64
359 if !good {
360 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
361 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900362
Jaewoong Jung5c6572e2019-06-17 17:40:56 -0700363 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("mergeNoticesRule")
364 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jungd6585fe2019-06-18 13:09:13 -0700365 if len(noticeInputs) != 2 {
366 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900367 }
368 ensureListContains(t, noticeInputs, "NOTICE")
369 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800370}
371
372func TestBasicZipApex(t *testing.T) {
373 ctx := testApex(t, `
374 apex {
375 name: "myapex",
376 key: "myapex.key",
377 payload_type: "zip",
378 native_shared_libs: ["mylib"],
379 }
380
381 apex_key {
382 name: "myapex.key",
383 public_key: "testkey.avbpubkey",
384 private_key: "testkey.pem",
385 }
386
387 cc_library {
388 name: "mylib",
389 srcs: ["mylib.cpp"],
390 shared_libs: ["mylib2"],
391 system_shared_libs: [],
392 stl: "none",
393 }
394
395 cc_library {
396 name: "mylib2",
397 srcs: ["mylib.cpp"],
398 system_shared_libs: [],
399 stl: "none",
400 }
401 `)
402
403 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
404 copyCmds := zipApexRule.Args["copy_commands"]
405
406 // Ensure that main rule creates an output
407 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
408
409 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900410 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800411
412 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900413 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800414
415 // Ensure that both direct and indirect deps are copied into apex
416 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
417 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900418}
419
420func TestApexWithStubs(t *testing.T) {
421 ctx := testApex(t, `
422 apex {
423 name: "myapex",
424 key: "myapex.key",
425 native_shared_libs: ["mylib", "mylib3"],
426 }
427
428 apex_key {
429 name: "myapex.key",
430 public_key: "testkey.avbpubkey",
431 private_key: "testkey.pem",
432 }
433
434 cc_library {
435 name: "mylib",
436 srcs: ["mylib.cpp"],
437 shared_libs: ["mylib2", "mylib3"],
438 system_shared_libs: [],
439 stl: "none",
440 }
441
442 cc_library {
443 name: "mylib2",
444 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900445 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900446 system_shared_libs: [],
447 stl: "none",
448 stubs: {
449 versions: ["1", "2", "3"],
450 },
451 }
452
453 cc_library {
454 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900455 srcs: ["mylib.cpp"],
456 shared_libs: ["mylib4"],
457 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900458 stl: "none",
459 stubs: {
460 versions: ["10", "11", "12"],
461 },
462 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900463
464 cc_library {
465 name: "mylib4",
466 srcs: ["mylib.cpp"],
467 system_shared_libs: [],
468 stl: "none",
469 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900470 `)
471
472 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
473 copyCmds := apexRule.Args["copy_commands"]
474
475 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800476 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900477
478 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800479 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900480
481 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800482 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900483
Jiyong Parkda6eb592018-12-19 17:12:36 +0900484 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900485
486 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900487 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900488 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900489 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900490
491 // 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 +0900492 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900493 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900494 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900495
496 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900497 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900498 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900499
500 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900501 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 +0900502}
503
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900504func TestApexWithExplicitStubsDependency(t *testing.T) {
505 ctx := testApex(t, `
506 apex {
507 name: "myapex",
508 key: "myapex.key",
509 native_shared_libs: ["mylib"],
510 }
511
512 apex_key {
513 name: "myapex.key",
514 public_key: "testkey.avbpubkey",
515 private_key: "testkey.pem",
516 }
517
518 cc_library {
519 name: "mylib",
520 srcs: ["mylib.cpp"],
521 shared_libs: ["libfoo#10"],
522 system_shared_libs: [],
523 stl: "none",
524 }
525
526 cc_library {
527 name: "libfoo",
528 srcs: ["mylib.cpp"],
529 shared_libs: ["libbar"],
530 system_shared_libs: [],
531 stl: "none",
532 stubs: {
533 versions: ["10", "20", "30"],
534 },
535 }
536
537 cc_library {
538 name: "libbar",
539 srcs: ["mylib.cpp"],
540 system_shared_libs: [],
541 stl: "none",
542 }
543
544 `)
545
546 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
547 copyCmds := apexRule.Args["copy_commands"]
548
549 // Ensure that direct non-stubs dep is always included
550 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
551
552 // Ensure that indirect stubs dep is not included
553 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
554
555 // Ensure that dependency of stubs is not included
556 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
557
Jiyong Parkda6eb592018-12-19 17:12:36 +0900558 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900559
560 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900561 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900562 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900563 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900564
Jiyong Parkda6eb592018-12-19 17:12:36 +0900565 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900566
567 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
568 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
569}
570
Jiyong Park25fc6a92018-11-18 18:02:45 +0900571func TestApexWithSystemLibsStubs(t *testing.T) {
572 ctx := testApex(t, `
573 apex {
574 name: "myapex",
575 key: "myapex.key",
576 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
577 }
578
579 apex_key {
580 name: "myapex.key",
581 public_key: "testkey.avbpubkey",
582 private_key: "testkey.pem",
583 }
584
585 cc_library {
586 name: "mylib",
587 srcs: ["mylib.cpp"],
588 shared_libs: ["libdl#27"],
589 stl: "none",
590 }
591
592 cc_library_shared {
593 name: "mylib_shared",
594 srcs: ["mylib.cpp"],
595 shared_libs: ["libdl#27"],
596 stl: "none",
597 }
598
599 cc_library {
600 name: "libc",
601 no_libgcc: true,
602 nocrt: true,
603 system_shared_libs: [],
604 stl: "none",
605 stubs: {
606 versions: ["27", "28", "29"],
607 },
608 }
609
610 cc_library {
611 name: "libm",
612 no_libgcc: true,
613 nocrt: true,
614 system_shared_libs: [],
615 stl: "none",
616 stubs: {
617 versions: ["27", "28", "29"],
618 },
619 }
620
621 cc_library {
622 name: "libdl",
623 no_libgcc: true,
624 nocrt: true,
625 system_shared_libs: [],
626 stl: "none",
627 stubs: {
628 versions: ["27", "28", "29"],
629 },
630 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900631
632 cc_library {
633 name: "libBootstrap",
634 srcs: ["mylib.cpp"],
635 stl: "none",
636 bootstrap: true,
637 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900638 `)
639
640 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
641 copyCmds := apexRule.Args["copy_commands"]
642
643 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800644 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900645 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
646 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900647
648 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900649 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900650
Jiyong Parkda6eb592018-12-19 17:12:36 +0900651 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
652 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
653 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900654
655 // For dependency to libc
656 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900657 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.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, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900660 // ... Cflags from stub is correctly exported to mylib
661 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
662 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
663
664 // For dependency to libm
665 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900666 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900667 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900668 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669 // ... and is not compiling with the stub
670 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
671 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
672
673 // For dependency to libdl
674 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900675 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900676 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900677 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
678 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900679 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900680 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900681 // ... Cflags from stub is correctly exported to mylib
682 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
683 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900684
685 // Ensure that libBootstrap is depending on the platform variant of bionic libs
686 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
687 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
688 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
689 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900690}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900691
692func TestFilesInSubDir(t *testing.T) {
693 ctx := testApex(t, `
694 apex {
695 name: "myapex",
696 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900697 native_shared_libs: ["mylib"],
698 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900699 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900700 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900701 }
702
703 apex_key {
704 name: "myapex.key",
705 public_key: "testkey.avbpubkey",
706 private_key: "testkey.pem",
707 }
708
709 prebuilt_etc {
710 name: "myetc",
711 src: "myprebuilt",
712 sub_dir: "foo/bar",
713 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900714
715 cc_library {
716 name: "mylib",
717 srcs: ["mylib.cpp"],
718 relative_install_path: "foo/bar",
719 system_shared_libs: [],
720 stl: "none",
721 }
722
723 cc_binary {
724 name: "mybin",
725 srcs: ["mylib.cpp"],
726 relative_install_path: "foo/bar",
727 system_shared_libs: [],
728 static_executable: true,
729 stl: "none",
730 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900731 `)
732
733 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
734 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
735
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900736 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900737 ensureListContains(t, dirs, "etc")
738 ensureListContains(t, dirs, "etc/foo")
739 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900740 ensureListContains(t, dirs, "lib64")
741 ensureListContains(t, dirs, "lib64/foo")
742 ensureListContains(t, dirs, "lib64/foo/bar")
743 ensureListContains(t, dirs, "lib")
744 ensureListContains(t, dirs, "lib/foo")
745 ensureListContains(t, dirs, "lib/foo/bar")
746
Jiyong Parkbd13e442019-03-15 18:10:35 +0900747 ensureListContains(t, dirs, "bin")
748 ensureListContains(t, dirs, "bin/foo")
749 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900750}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900751
752func TestUseVendor(t *testing.T) {
753 ctx := testApex(t, `
754 apex {
755 name: "myapex",
756 key: "myapex.key",
757 native_shared_libs: ["mylib"],
758 use_vendor: true,
759 }
760
761 apex_key {
762 name: "myapex.key",
763 public_key: "testkey.avbpubkey",
764 private_key: "testkey.pem",
765 }
766
767 cc_library {
768 name: "mylib",
769 srcs: ["mylib.cpp"],
770 shared_libs: ["mylib2"],
771 system_shared_libs: [],
772 vendor_available: true,
773 stl: "none",
774 }
775
776 cc_library {
777 name: "mylib2",
778 srcs: ["mylib.cpp"],
779 system_shared_libs: [],
780 vendor_available: true,
781 stl: "none",
782 }
783 `)
784
785 inputsList := []string{}
786 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
787 for _, implicit := range i.Implicits {
788 inputsList = append(inputsList, implicit.String())
789 }
790 }
791 inputsString := strings.Join(inputsList, " ")
792
793 // ensure that the apex includes vendor variants of the direct and indirect deps
794 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
795 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
796
797 // ensure that the apex does not include core variants
798 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
799 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
800}
Jiyong Park16e91a02018-12-20 18:18:08 +0900801
802func TestStaticLinking(t *testing.T) {
803 ctx := testApex(t, `
804 apex {
805 name: "myapex",
806 key: "myapex.key",
807 native_shared_libs: ["mylib"],
808 }
809
810 apex_key {
811 name: "myapex.key",
812 public_key: "testkey.avbpubkey",
813 private_key: "testkey.pem",
814 }
815
816 cc_library {
817 name: "mylib",
818 srcs: ["mylib.cpp"],
819 system_shared_libs: [],
820 stl: "none",
821 stubs: {
822 versions: ["1", "2", "3"],
823 },
824 }
825
826 cc_binary {
827 name: "not_in_apex",
828 srcs: ["mylib.cpp"],
829 static_libs: ["mylib"],
830 static_executable: true,
831 system_shared_libs: [],
832 stl: "none",
833 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900834 `)
835
836 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
837
838 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800839 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900840}
Jiyong Park9335a262018-12-24 11:31:58 +0900841
842func TestKeys(t *testing.T) {
843 ctx := testApex(t, `
844 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900845 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900846 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900847 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900848 native_shared_libs: ["mylib"],
849 }
850
851 cc_library {
852 name: "mylib",
853 srcs: ["mylib.cpp"],
854 system_shared_libs: [],
855 stl: "none",
856 }
857
858 apex_key {
859 name: "myapex.key",
860 public_key: "testkey.avbpubkey",
861 private_key: "testkey.pem",
862 }
863
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900864 android_app_certificate {
865 name: "myapex.certificate",
866 certificate: "testkey",
867 }
868
869 android_app_certificate {
870 name: "myapex.certificate.override",
871 certificate: "testkey.override",
872 }
873
Jiyong Park9335a262018-12-24 11:31:58 +0900874 `)
875
876 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900877 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900878
879 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
880 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
881 "vendor/foo/devkeys/testkey.avbpubkey")
882 }
883 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
884 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
885 "vendor/foo/devkeys/testkey.pem")
886 }
887
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900888 // check the APK certs. It should be overridden to myapex.certificate.override
889 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
890 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900891 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900892 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900893 }
894}
Jiyong Park58e364a2019-01-19 19:24:06 +0900895
896func TestMacro(t *testing.T) {
897 ctx := testApex(t, `
898 apex {
899 name: "myapex",
900 key: "myapex.key",
901 native_shared_libs: ["mylib"],
902 }
903
904 apex {
905 name: "otherapex",
906 key: "myapex.key",
907 native_shared_libs: ["mylib"],
908 }
909
910 apex_key {
911 name: "myapex.key",
912 public_key: "testkey.avbpubkey",
913 private_key: "testkey.pem",
914 }
915
916 cc_library {
917 name: "mylib",
918 srcs: ["mylib.cpp"],
919 system_shared_libs: [],
920 stl: "none",
921 }
922 `)
923
924 // non-APEX variant does not have __ANDROID__APEX__ defined
925 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
926 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
927 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
928
929 // APEX variant has __ANDROID_APEX__=<apexname> defined
930 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
931 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
932 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
933
934 // APEX variant has __ANDROID_APEX__=<apexname> defined
935 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
936 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
937 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
938}
Jiyong Park7e636d02019-01-28 16:16:54 +0900939
940func TestHeaderLibsDependency(t *testing.T) {
941 ctx := testApex(t, `
942 apex {
943 name: "myapex",
944 key: "myapex.key",
945 native_shared_libs: ["mylib"],
946 }
947
948 apex_key {
949 name: "myapex.key",
950 public_key: "testkey.avbpubkey",
951 private_key: "testkey.pem",
952 }
953
954 cc_library_headers {
955 name: "mylib_headers",
956 export_include_dirs: ["my_include"],
957 system_shared_libs: [],
958 stl: "none",
959 }
960
961 cc_library {
962 name: "mylib",
963 srcs: ["mylib.cpp"],
964 system_shared_libs: [],
965 stl: "none",
966 header_libs: ["mylib_headers"],
967 export_header_lib_headers: ["mylib_headers"],
968 stubs: {
969 versions: ["1", "2", "3"],
970 },
971 }
972
973 cc_library {
974 name: "otherlib",
975 srcs: ["mylib.cpp"],
976 system_shared_libs: [],
977 stl: "none",
978 shared_libs: ["mylib"],
979 }
980 `)
981
982 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
983
984 // Ensure that the include path of the header lib is exported to 'otherlib'
985 ensureContains(t, cFlags, "-Imy_include")
986}
Alex Light9670d332019-01-29 18:07:33 -0800987
Alex Light0851b882019-02-07 13:20:53 -0800988func TestNonTestApex(t *testing.T) {
989 ctx := testApex(t, `
990 apex {
991 name: "myapex",
992 key: "myapex.key",
993 native_shared_libs: ["mylib_common"],
994 }
995
996 apex_key {
997 name: "myapex.key",
998 public_key: "testkey.avbpubkey",
999 private_key: "testkey.pem",
1000 }
1001
1002 cc_library {
1003 name: "mylib_common",
1004 srcs: ["mylib.cpp"],
1005 system_shared_libs: [],
1006 stl: "none",
1007 }
1008 `)
1009
1010 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1011 apexRule := module.Rule("apexRule")
1012 copyCmds := apexRule.Args["copy_commands"]
1013
1014 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1015 t.Log("Apex was a test apex!")
1016 t.Fail()
1017 }
1018 // Ensure that main rule creates an output
1019 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1020
1021 // Ensure that apex variant is created for the direct dep
1022 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1023
1024 // Ensure that both direct and indirect deps are copied into apex
1025 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1026
1027 // Ensure that the platform variant ends with _core_shared
1028 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1029
1030 if !android.InAnyApex("mylib_common") {
1031 t.Log("Found mylib_common not in any apex!")
1032 t.Fail()
1033 }
1034}
1035
1036func TestTestApex(t *testing.T) {
1037 if android.InAnyApex("mylib_common_test") {
1038 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!")
1039 }
1040 ctx := testApex(t, `
1041 apex_test {
1042 name: "myapex",
1043 key: "myapex.key",
1044 native_shared_libs: ["mylib_common_test"],
1045 }
1046
1047 apex_key {
1048 name: "myapex.key",
1049 public_key: "testkey.avbpubkey",
1050 private_key: "testkey.pem",
1051 }
1052
1053 cc_library {
1054 name: "mylib_common_test",
1055 srcs: ["mylib.cpp"],
1056 system_shared_libs: [],
1057 stl: "none",
1058 }
1059 `)
1060
1061 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1062 apexRule := module.Rule("apexRule")
1063 copyCmds := apexRule.Args["copy_commands"]
1064
1065 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1066 t.Log("Apex was not a test apex!")
1067 t.Fail()
1068 }
1069 // Ensure that main rule creates an output
1070 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1071
1072 // Ensure that apex variant is created for the direct dep
1073 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1074
1075 // Ensure that both direct and indirect deps are copied into apex
1076 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1077
1078 // Ensure that the platform variant ends with _core_shared
1079 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1080
1081 if android.InAnyApex("mylib_common_test") {
1082 t.Log("Found mylib_common_test in some apex!")
1083 t.Fail()
1084 }
1085}
1086
Alex Light9670d332019-01-29 18:07:33 -08001087func TestApexWithTarget(t *testing.T) {
1088 ctx := testApex(t, `
1089 apex {
1090 name: "myapex",
1091 key: "myapex.key",
1092 multilib: {
1093 first: {
1094 native_shared_libs: ["mylib_common"],
1095 }
1096 },
1097 target: {
1098 android: {
1099 multilib: {
1100 first: {
1101 native_shared_libs: ["mylib"],
1102 }
1103 }
1104 },
1105 host: {
1106 multilib: {
1107 first: {
1108 native_shared_libs: ["mylib2"],
1109 }
1110 }
1111 }
1112 }
1113 }
1114
1115 apex_key {
1116 name: "myapex.key",
1117 public_key: "testkey.avbpubkey",
1118 private_key: "testkey.pem",
1119 }
1120
1121 cc_library {
1122 name: "mylib",
1123 srcs: ["mylib.cpp"],
1124 system_shared_libs: [],
1125 stl: "none",
1126 }
1127
1128 cc_library {
1129 name: "mylib_common",
1130 srcs: ["mylib.cpp"],
1131 system_shared_libs: [],
1132 stl: "none",
1133 compile_multilib: "first",
1134 }
1135
1136 cc_library {
1137 name: "mylib2",
1138 srcs: ["mylib.cpp"],
1139 system_shared_libs: [],
1140 stl: "none",
1141 compile_multilib: "first",
1142 }
1143 `)
1144
1145 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1146 copyCmds := apexRule.Args["copy_commands"]
1147
1148 // Ensure that main rule creates an output
1149 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1150
1151 // Ensure that apex variant is created for the direct dep
1152 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1153 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1154 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1155
1156 // Ensure that both direct and indirect deps are copied into apex
1157 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1158 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1159 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1160
1161 // Ensure that the platform variant ends with _core_shared
1162 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1163 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1164 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1165}
Jiyong Park04480cf2019-02-06 00:16:29 +09001166
1167func TestApexWithShBinary(t *testing.T) {
1168 ctx := testApex(t, `
1169 apex {
1170 name: "myapex",
1171 key: "myapex.key",
1172 binaries: ["myscript"],
1173 }
1174
1175 apex_key {
1176 name: "myapex.key",
1177 public_key: "testkey.avbpubkey",
1178 private_key: "testkey.pem",
1179 }
1180
1181 sh_binary {
1182 name: "myscript",
1183 src: "mylib.cpp",
1184 filename: "myscript.sh",
1185 sub_dir: "script",
1186 }
1187 `)
1188
1189 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1190 copyCmds := apexRule.Args["copy_commands"]
1191
1192 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1193}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001194
1195func TestApexInProductPartition(t *testing.T) {
1196 ctx := testApex(t, `
1197 apex {
1198 name: "myapex",
1199 key: "myapex.key",
1200 native_shared_libs: ["mylib"],
1201 product_specific: true,
1202 }
1203
1204 apex_key {
1205 name: "myapex.key",
1206 public_key: "testkey.avbpubkey",
1207 private_key: "testkey.pem",
1208 product_specific: true,
1209 }
1210
1211 cc_library {
1212 name: "mylib",
1213 srcs: ["mylib.cpp"],
1214 system_shared_libs: [],
1215 stl: "none",
1216 }
1217 `)
1218
1219 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1220 expected := "target/product/test_device/product/apex"
1221 actual := apex.installDir.RelPathString()
1222 if actual != expected {
1223 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1224 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001225}
Jiyong Park67882562019-03-21 01:11:21 +09001226
1227func TestApexKeyFromOtherModule(t *testing.T) {
1228 ctx := testApex(t, `
1229 apex_key {
1230 name: "myapex.key",
1231 public_key: ":my.avbpubkey",
1232 private_key: ":my.pem",
1233 product_specific: true,
1234 }
1235
1236 filegroup {
1237 name: "my.avbpubkey",
1238 srcs: ["testkey2.avbpubkey"],
1239 }
1240
1241 filegroup {
1242 name: "my.pem",
1243 srcs: ["testkey2.pem"],
1244 }
1245 `)
1246
1247 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1248 expected_pubkey := "testkey2.avbpubkey"
1249 actual_pubkey := apex_key.public_key_file.String()
1250 if actual_pubkey != expected_pubkey {
1251 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1252 }
1253 expected_privkey := "testkey2.pem"
1254 actual_privkey := apex_key.private_key_file.String()
1255 if actual_privkey != expected_privkey {
1256 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1257 }
1258}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001259
1260func TestPrebuilt(t *testing.T) {
1261 ctx := testApex(t, `
1262 prebuilt_apex {
1263 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09001264 arch: {
1265 arm64: {
1266 src: "myapex-arm64.apex",
1267 },
1268 arm: {
1269 src: "myapex-arm.apex",
1270 },
1271 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001272 }
1273 `)
1274
1275 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1276
Jiyong Parkc95714e2019-03-29 14:23:10 +09001277 expectedInput := "myapex-arm64.apex"
1278 if prebuilt.inputApex.String() != expectedInput {
1279 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
1280 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001281}
Nikita Ioffeed75f612019-04-04 18:09:48 +01001282
1283func TestPrebuiltFilenameOverride(t *testing.T) {
1284 ctx := testApex(t, `
1285 prebuilt_apex {
1286 name: "myapex",
1287 src: "myapex-arm.apex",
1288 filename: "notmyapex.apex",
1289 }
1290 `)
1291
1292 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1293
1294 expected := "notmyapex.apex"
1295 if p.installFilename != expected {
1296 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
1297 }
1298}
Jaewoong Jung9c49b282020-05-14 14:15:24 -07001299
1300// TODO(jungjw): Move this to proptools
1301func intPtr(i int) *int {
1302 return &i
1303}
1304
1305func TestApexSet(t *testing.T) {
1306 ctx := testApex(t, `
1307 apex_set {
1308 name: "myapex",
1309 set: "myapex.apks",
1310 filename: "foo_v2.apex",
1311 overrides: ["foo"],
1312 }
1313 `, func(fs map[string][]byte, config android.Config) {
1314 config.TestProductVariables.Platform_sdk_version = intPtr(30)
1315 config.TestProductVariables.DeviceArch = proptools.StringPtr("arm")
1316 config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm64")
1317 })
1318
1319 m := ctx.ModuleForTests("myapex", "android_common")
1320
1321 // Check extract_apks tool parameters.
1322 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
1323 actual := extractedApex.Args["abis"]
1324 expected := "ARMEABI_V7A,ARM64_V8A"
1325 if actual != expected {
1326 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
1327 }
1328 actual = extractedApex.Args["sdk-version"]
1329 expected = "30"
1330 if actual != expected {
1331 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
1332 }
1333}