blob: 5fc07384ddad2e6f651563341672995c090ba7bd [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"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070020 "reflect"
Jiyong Park25fc6a92018-11-18 18:02:45 +090021 "strings"
22 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090023
24 "github.com/google/blueprint/proptools"
25
26 "android/soong/android"
27 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090028 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090029)
30
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070031var buildDir string
32
Jooyung Han5c998b92019-06-27 11:30:33 +090033func testApexError(t *testing.T, pattern, bp string) {
34 ctx, config := testApexContext(t, bp)
35 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
36 if len(errs) > 0 {
37 android.FailIfNoMatchingErrors(t, pattern, errs)
38 return
39 }
40 _, errs = ctx.PrepareBuildActions(config)
41 if len(errs) > 0 {
42 android.FailIfNoMatchingErrors(t, pattern, errs)
43 return
44 }
45
46 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
47}
48
Jaewoong Jung22f7d182019-07-16 18:25:41 -070049func testApex(t *testing.T, bp string) (*android.TestContext, android.Config) {
Jooyung Han5c998b92019-06-27 11:30:33 +090050 ctx, config := testApexContext(t, bp)
51 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
52 android.FailIfErrored(t, errs)
53 _, errs = ctx.PrepareBuildActions(config)
54 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070055 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090056}
57
58func testApexContext(t *testing.T, bp string) (*android.TestContext, android.Config) {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -070059 config := android.TestArchConfig(buildDir, nil)
60 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
61 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
62 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
63 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
64 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jiyong Park25fc6a92018-11-18 18:02:45 +090065
66 ctx := android.NewTestArchContext()
Alex Light0851b882019-02-07 13:20:53 -080067 ctx.RegisterModuleType("apex", android.ModuleFactoryAdaptor(apexBundleFactory))
68 ctx.RegisterModuleType("apex_test", android.ModuleFactoryAdaptor(testApexBundleFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090069 ctx.RegisterModuleType("apex_key", android.ModuleFactoryAdaptor(apexKeyFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090070 ctx.RegisterModuleType("apex_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070071 ctx.RegisterModuleType("prebuilt_apex", android.ModuleFactoryAdaptor(PrebuiltFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090072 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Park25fc6a92018-11-18 18:02:45 +090073
74 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
75 ctx.TopDown("apex_deps", apexDepsMutator)
76 ctx.BottomUp("apex", apexMutator)
Jooyung Han5c998b92019-06-27 11:30:33 +090077 ctx.BottomUp("apex_uses", apexUsesMutator)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070078 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
79 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090080 })
81
82 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
83 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +090084 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +090085 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090086 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Roland Levillain630846d2019-06-26 12:48:34 +010087 ctx.RegisterModuleType("cc_test", android.ModuleFactoryAdaptor(cc.TestFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090088 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090089 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +090090 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park04480cf2019-02-06 00:16:29 +090091 ctx.RegisterModuleType("sh_binary", android.ModuleFactoryAdaptor(android.ShBinaryFactory))
Jiyong Parkb2742fd2019-02-11 11:38:15 +090092 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(java.AndroidAppCertificateFactory))
Jiyong Park809bb722019-02-13 21:33:49 +090093 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070094 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
95 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
96 })
Jiyong Park25fc6a92018-11-18 18:02:45 +090097 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +090098 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090099 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900100 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900101 ctx.BottomUp("version", cc.VersionMutator).Parallel()
102 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
103 })
104
105 ctx.Register()
106
107 bp = bp + `
108 toolchain_library {
109 name: "libcompiler_rt-extras",
110 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900111 vendor_available: true,
112 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900113 }
114
115 toolchain_library {
116 name: "libatomic",
117 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900118 vendor_available: true,
119 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900120 }
121
122 toolchain_library {
123 name: "libgcc",
124 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900125 vendor_available: true,
126 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900127 }
128
129 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700130 name: "libgcc_stripped",
131 src: "",
132 vendor_available: true,
133 recovery_available: true,
134 }
135
136 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900137 name: "libclang_rt.builtins-aarch64-android",
138 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900139 vendor_available: true,
140 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900141 }
142
143 toolchain_library {
144 name: "libclang_rt.builtins-arm-android",
145 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900146 vendor_available: true,
147 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900148 }
149
150 cc_object {
151 name: "crtbegin_so",
152 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900153 vendor_available: true,
154 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900155 }
156
157 cc_object {
158 name: "crtend_so",
159 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900160 vendor_available: true,
161 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900162 }
163
Alex Light3d673592019-01-18 14:37:31 -0800164 cc_object {
165 name: "crtbegin_static",
166 stl: "none",
167 }
168
169 cc_object {
170 name: "crtend_android",
171 stl: "none",
172 }
173
Jiyong Parkda6eb592018-12-19 17:12:36 +0900174 llndk_library {
175 name: "libc",
176 symbol_file: "",
177 }
178
179 llndk_library {
180 name: "libm",
181 symbol_file: "",
182 }
183
184 llndk_library {
185 name: "libdl",
186 symbol_file: "",
187 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900188 `
189
190 ctx.MockFileSystem(map[string][]byte{
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900191 "Android.bp": []byte(bp),
Dan Willemsen412160e2019-04-09 21:36:26 -0700192 "build/make/target/product/security": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900193 "apex_manifest.json": nil,
Jiyong Park809bb722019-02-13 21:33:49 +0900194 "AndroidManifest.xml": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900195 "system/sepolicy/apex/myapex-file_contexts": nil,
196 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
197 "system/sepolicy/apex/otherapex-file_contexts": nil,
Jooyung Han5c998b92019-06-27 11:30:33 +0900198 "system/sepolicy/apex/commonapex-file_contexts": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900199 "mylib.cpp": nil,
Roland Levillain630846d2019-06-26 12:48:34 +0100200 "mytest.cpp": nil,
Jooyung Han5c998b92019-06-27 11:30:33 +0900201 "mylib_common.cpp": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900202 "myprebuilt": nil,
203 "my_include": nil,
204 "vendor/foo/devkeys/test.x509.pem": nil,
205 "vendor/foo/devkeys/test.pk8": nil,
206 "testkey.x509.pem": nil,
207 "testkey.pk8": nil,
208 "testkey.override.x509.pem": nil,
209 "testkey.override.pk8": nil,
210 "vendor/foo/devkeys/testkey.avbpubkey": nil,
211 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park52818fc2019-03-18 12:01:38 +0900212 "NOTICE": nil,
213 "custom_notice": nil,
Jiyong Park67882562019-03-21 01:11:21 +0900214 "testkey2.avbpubkey": nil,
215 "testkey2.pem": nil,
Jiyong Parkc95714e2019-03-29 14:23:10 +0900216 "myapex-arm64.apex": nil,
217 "myapex-arm.apex": nil,
Jiyong Park71b519d2019-04-18 17:25:49 +0900218 "frameworks/base/api/current.txt": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900219 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900220
Jooyung Han5c998b92019-06-27 11:30:33 +0900221 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900222}
223
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700224func setUp() {
225 var err error
226 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900227 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700228 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900230}
231
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700232func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900233 os.RemoveAll(buildDir)
234}
235
236// ensure that 'result' contains 'expected'
237func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900238 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900239 if !strings.Contains(result, expected) {
240 t.Errorf("%q is not found in %q", expected, result)
241 }
242}
243
244// ensures that 'result' does not contain 'notExpected'
245func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900246 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 if strings.Contains(result, notExpected) {
248 t.Errorf("%q is found in %q", notExpected, result)
249 }
250}
251
252func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900253 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900254 if !android.InList(expected, result) {
255 t.Errorf("%q is not found in %v", expected, result)
256 }
257}
258
259func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900260 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261 if android.InList(notExpected, result) {
262 t.Errorf("%q is found in %v", notExpected, result)
263 }
264}
265
266// Minimal test
267func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700268 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900269 apex_defaults {
270 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900271 manifest: ":myapex.manifest",
272 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 key: "myapex.key",
274 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800275 multilib: {
276 both: {
277 binaries: ["foo",],
278 }
279 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 }
281
Jiyong Park30ca9372019-02-07 16:27:23 +0900282 apex {
283 name: "myapex",
284 defaults: ["myapex-defaults"],
285 }
286
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 apex_key {
288 name: "myapex.key",
289 public_key: "testkey.avbpubkey",
290 private_key: "testkey.pem",
291 }
292
Jiyong Park809bb722019-02-13 21:33:49 +0900293 filegroup {
294 name: "myapex.manifest",
295 srcs: ["apex_manifest.json"],
296 }
297
298 filegroup {
299 name: "myapex.androidmanifest",
300 srcs: ["AndroidManifest.xml"],
301 }
302
Jiyong Park25fc6a92018-11-18 18:02:45 +0900303 cc_library {
304 name: "mylib",
305 srcs: ["mylib.cpp"],
306 shared_libs: ["mylib2"],
307 system_shared_libs: [],
308 stl: "none",
309 }
310
Alex Light3d673592019-01-18 14:37:31 -0800311 cc_binary {
312 name: "foo",
313 srcs: ["mylib.cpp"],
314 compile_multilib: "both",
315 multilib: {
316 lib32: {
317 suffix: "32",
318 },
319 lib64: {
320 suffix: "64",
321 },
322 },
323 symlinks: ["foo_link_"],
324 symlink_preferred_arch: true,
325 system_shared_libs: [],
326 static_executable: true,
327 stl: "none",
328 }
329
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 cc_library {
331 name: "mylib2",
332 srcs: ["mylib.cpp"],
333 system_shared_libs: [],
334 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900335 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900336 }
337 `)
338
339 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900340
341 optFlags := apexRule.Args["opt_flags"]
342 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700343 // Ensure that the NOTICE output is being packaged as an asset.
344 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900345
Jiyong Park25fc6a92018-11-18 18:02:45 +0900346 copyCmds := apexRule.Args["copy_commands"]
347
348 // Ensure that main rule creates an output
349 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
350
351 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900352 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353
354 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900355 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900356
357 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800358 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
359 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800360
361 // Ensure that the platform variant ends with _core_shared
362 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
363 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800364
365 // Ensure that all symlinks are present.
366 found_foo_link_64 := false
367 found_foo := false
368 for _, cmd := range strings.Split(copyCmds, " && ") {
369 if strings.HasPrefix(cmd, "ln -s foo64") {
370 if strings.HasSuffix(cmd, "bin/foo") {
371 found_foo = true
372 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
373 found_foo_link_64 = true
374 }
375 }
376 }
377 good := found_foo && found_foo_link_64
378 if !good {
379 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
380 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900381
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700382 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("mergeNoticesRule")
383 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700384 if len(noticeInputs) != 2 {
385 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900386 }
387 ensureListContains(t, noticeInputs, "NOTICE")
388 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800389}
390
391func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700392 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800393 apex {
394 name: "myapex",
395 key: "myapex.key",
396 payload_type: "zip",
397 native_shared_libs: ["mylib"],
398 }
399
400 apex_key {
401 name: "myapex.key",
402 public_key: "testkey.avbpubkey",
403 private_key: "testkey.pem",
404 }
405
406 cc_library {
407 name: "mylib",
408 srcs: ["mylib.cpp"],
409 shared_libs: ["mylib2"],
410 system_shared_libs: [],
411 stl: "none",
412 }
413
414 cc_library {
415 name: "mylib2",
416 srcs: ["mylib.cpp"],
417 system_shared_libs: [],
418 stl: "none",
419 }
420 `)
421
422 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
423 copyCmds := zipApexRule.Args["copy_commands"]
424
425 // Ensure that main rule creates an output
426 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
427
428 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900429 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800430
431 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900432 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800433
434 // Ensure that both direct and indirect deps are copied into apex
435 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
436 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900437}
438
439func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700440 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900441 apex {
442 name: "myapex",
443 key: "myapex.key",
444 native_shared_libs: ["mylib", "mylib3"],
445 }
446
447 apex_key {
448 name: "myapex.key",
449 public_key: "testkey.avbpubkey",
450 private_key: "testkey.pem",
451 }
452
453 cc_library {
454 name: "mylib",
455 srcs: ["mylib.cpp"],
456 shared_libs: ["mylib2", "mylib3"],
457 system_shared_libs: [],
458 stl: "none",
459 }
460
461 cc_library {
462 name: "mylib2",
463 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900464 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900465 system_shared_libs: [],
466 stl: "none",
467 stubs: {
468 versions: ["1", "2", "3"],
469 },
470 }
471
472 cc_library {
473 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900474 srcs: ["mylib.cpp"],
475 shared_libs: ["mylib4"],
476 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900477 stl: "none",
478 stubs: {
479 versions: ["10", "11", "12"],
480 },
481 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900482
483 cc_library {
484 name: "mylib4",
485 srcs: ["mylib.cpp"],
486 system_shared_libs: [],
487 stl: "none",
488 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900489 `)
490
491 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
492 copyCmds := apexRule.Args["copy_commands"]
493
494 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800495 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900496
497 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800498 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900499
500 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800501 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900502
Jiyong Parkda6eb592018-12-19 17:12:36 +0900503 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900504
505 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900506 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900507 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900508 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900509
510 // 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 +0900511 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900512 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900513 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900514
515 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900516 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900517 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900518
519 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900520 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 +0900521}
522
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900523func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700524 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900525 apex {
526 name: "myapex",
527 key: "myapex.key",
528 native_shared_libs: ["mylib"],
529 }
530
531 apex_key {
532 name: "myapex.key",
533 public_key: "testkey.avbpubkey",
534 private_key: "testkey.pem",
535 }
536
537 cc_library {
538 name: "mylib",
539 srcs: ["mylib.cpp"],
540 shared_libs: ["libfoo#10"],
541 system_shared_libs: [],
542 stl: "none",
543 }
544
545 cc_library {
546 name: "libfoo",
547 srcs: ["mylib.cpp"],
548 shared_libs: ["libbar"],
549 system_shared_libs: [],
550 stl: "none",
551 stubs: {
552 versions: ["10", "20", "30"],
553 },
554 }
555
556 cc_library {
557 name: "libbar",
558 srcs: ["mylib.cpp"],
559 system_shared_libs: [],
560 stl: "none",
561 }
562
563 `)
564
565 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
566 copyCmds := apexRule.Args["copy_commands"]
567
568 // Ensure that direct non-stubs dep is always included
569 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
570
571 // Ensure that indirect stubs dep is not included
572 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
573
574 // Ensure that dependency of stubs is not included
575 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
576
Jiyong Parkda6eb592018-12-19 17:12:36 +0900577 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900578
579 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900580 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900581 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900582 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900583
Jiyong Parkda6eb592018-12-19 17:12:36 +0900584 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900585
586 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
587 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
588}
589
Jiyong Park25fc6a92018-11-18 18:02:45 +0900590func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700591 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900592 apex {
593 name: "myapex",
594 key: "myapex.key",
595 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
596 }
597
598 apex_key {
599 name: "myapex.key",
600 public_key: "testkey.avbpubkey",
601 private_key: "testkey.pem",
602 }
603
604 cc_library {
605 name: "mylib",
606 srcs: ["mylib.cpp"],
607 shared_libs: ["libdl#27"],
608 stl: "none",
609 }
610
611 cc_library_shared {
612 name: "mylib_shared",
613 srcs: ["mylib.cpp"],
614 shared_libs: ["libdl#27"],
615 stl: "none",
616 }
617
618 cc_library {
619 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700620 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900621 nocrt: true,
622 system_shared_libs: [],
623 stl: "none",
624 stubs: {
625 versions: ["27", "28", "29"],
626 },
627 }
628
629 cc_library {
630 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700631 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900632 nocrt: true,
633 system_shared_libs: [],
634 stl: "none",
635 stubs: {
636 versions: ["27", "28", "29"],
637 },
638 }
639
640 cc_library {
641 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700642 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900643 nocrt: true,
644 system_shared_libs: [],
645 stl: "none",
646 stubs: {
647 versions: ["27", "28", "29"],
648 },
649 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900650
651 cc_library {
652 name: "libBootstrap",
653 srcs: ["mylib.cpp"],
654 stl: "none",
655 bootstrap: true,
656 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900657 `)
658
659 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
660 copyCmds := apexRule.Args["copy_commands"]
661
662 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800663 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900664 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
665 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900666
667 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900668 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669
Jiyong Parkda6eb592018-12-19 17:12:36 +0900670 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
671 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
672 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900673
674 // For dependency to libc
675 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900676 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900677 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900678 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900679 // ... Cflags from stub is correctly exported to mylib
680 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
681 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
682
683 // For dependency to libm
684 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900685 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900686 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900687 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900688 // ... and is not compiling with the stub
689 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
690 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
691
692 // For dependency to libdl
693 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900694 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900695 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900696 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
697 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900698 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900699 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900700 // ... Cflags from stub is correctly exported to mylib
701 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
702 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900703
704 // Ensure that libBootstrap is depending on the platform variant of bionic libs
705 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
706 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
707 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
708 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900709}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900710
711func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700712 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900713 apex {
714 name: "myapex",
715 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900716 native_shared_libs: ["mylib"],
717 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900718 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900719 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900720 }
721
722 apex_key {
723 name: "myapex.key",
724 public_key: "testkey.avbpubkey",
725 private_key: "testkey.pem",
726 }
727
728 prebuilt_etc {
729 name: "myetc",
730 src: "myprebuilt",
731 sub_dir: "foo/bar",
732 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900733
734 cc_library {
735 name: "mylib",
736 srcs: ["mylib.cpp"],
737 relative_install_path: "foo/bar",
738 system_shared_libs: [],
739 stl: "none",
740 }
741
742 cc_binary {
743 name: "mybin",
744 srcs: ["mylib.cpp"],
745 relative_install_path: "foo/bar",
746 system_shared_libs: [],
747 static_executable: true,
748 stl: "none",
749 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900750 `)
751
752 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
753 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
754
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900755 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900756 ensureListContains(t, dirs, "etc")
757 ensureListContains(t, dirs, "etc/foo")
758 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900759 ensureListContains(t, dirs, "lib64")
760 ensureListContains(t, dirs, "lib64/foo")
761 ensureListContains(t, dirs, "lib64/foo/bar")
762 ensureListContains(t, dirs, "lib")
763 ensureListContains(t, dirs, "lib/foo")
764 ensureListContains(t, dirs, "lib/foo/bar")
765
Jiyong Parkbd13e442019-03-15 18:10:35 +0900766 ensureListContains(t, dirs, "bin")
767 ensureListContains(t, dirs, "bin/foo")
768 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900769}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900770
771func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700772 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +0900773 apex {
774 name: "myapex",
775 key: "myapex.key",
776 native_shared_libs: ["mylib"],
777 use_vendor: true,
778 }
779
780 apex_key {
781 name: "myapex.key",
782 public_key: "testkey.avbpubkey",
783 private_key: "testkey.pem",
784 }
785
786 cc_library {
787 name: "mylib",
788 srcs: ["mylib.cpp"],
789 shared_libs: ["mylib2"],
790 system_shared_libs: [],
791 vendor_available: true,
792 stl: "none",
793 }
794
795 cc_library {
796 name: "mylib2",
797 srcs: ["mylib.cpp"],
798 system_shared_libs: [],
799 vendor_available: true,
800 stl: "none",
801 }
802 `)
803
804 inputsList := []string{}
805 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
806 for _, implicit := range i.Implicits {
807 inputsList = append(inputsList, implicit.String())
808 }
809 }
810 inputsString := strings.Join(inputsList, " ")
811
812 // ensure that the apex includes vendor variants of the direct and indirect deps
813 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
814 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
815
816 // ensure that the apex does not include core variants
817 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
818 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
819}
Jiyong Park16e91a02018-12-20 18:18:08 +0900820
Jooyung Han5c998b92019-06-27 11:30:33 +0900821func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
822 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
823 apex {
824 name: "myapex",
825 key: "myapex.key",
826 native_shared_libs: ["mylib"],
827 use_vendor: true,
828 }
829
830 apex_key {
831 name: "myapex.key",
832 public_key: "testkey.avbpubkey",
833 private_key: "testkey.pem",
834 }
835
836 cc_library {
837 name: "mylib",
838 srcs: ["mylib.cpp"],
839 system_shared_libs: [],
840 stl: "none",
841 }
842 `)
843}
844
Jiyong Park16e91a02018-12-20 18:18:08 +0900845func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700846 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +0900847 apex {
848 name: "myapex",
849 key: "myapex.key",
850 native_shared_libs: ["mylib"],
851 }
852
853 apex_key {
854 name: "myapex.key",
855 public_key: "testkey.avbpubkey",
856 private_key: "testkey.pem",
857 }
858
859 cc_library {
860 name: "mylib",
861 srcs: ["mylib.cpp"],
862 system_shared_libs: [],
863 stl: "none",
864 stubs: {
865 versions: ["1", "2", "3"],
866 },
867 }
868
869 cc_binary {
870 name: "not_in_apex",
871 srcs: ["mylib.cpp"],
872 static_libs: ["mylib"],
873 static_executable: true,
874 system_shared_libs: [],
875 stl: "none",
876 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900877 `)
878
879 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
880
881 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800882 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900883}
Jiyong Park9335a262018-12-24 11:31:58 +0900884
885func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700886 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +0900887 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900888 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900889 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900890 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900891 native_shared_libs: ["mylib"],
892 }
893
894 cc_library {
895 name: "mylib",
896 srcs: ["mylib.cpp"],
897 system_shared_libs: [],
898 stl: "none",
899 }
900
901 apex_key {
902 name: "myapex.key",
903 public_key: "testkey.avbpubkey",
904 private_key: "testkey.pem",
905 }
906
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900907 android_app_certificate {
908 name: "myapex.certificate",
909 certificate: "testkey",
910 }
911
912 android_app_certificate {
913 name: "myapex.certificate.override",
914 certificate: "testkey.override",
915 }
916
Jiyong Park9335a262018-12-24 11:31:58 +0900917 `)
918
919 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900920 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900921
922 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
923 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
924 "vendor/foo/devkeys/testkey.avbpubkey")
925 }
926 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
927 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
928 "vendor/foo/devkeys/testkey.pem")
929 }
930
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900931 // check the APK certs. It should be overridden to myapex.certificate.override
932 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
933 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900934 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900935 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900936 }
937}
Jiyong Park58e364a2019-01-19 19:24:06 +0900938
939func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700940 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +0900941 apex {
942 name: "myapex",
943 key: "myapex.key",
944 native_shared_libs: ["mylib"],
945 }
946
947 apex {
948 name: "otherapex",
949 key: "myapex.key",
950 native_shared_libs: ["mylib"],
951 }
952
953 apex_key {
954 name: "myapex.key",
955 public_key: "testkey.avbpubkey",
956 private_key: "testkey.pem",
957 }
958
959 cc_library {
960 name: "mylib",
961 srcs: ["mylib.cpp"],
962 system_shared_libs: [],
963 stl: "none",
964 }
965 `)
966
967 // non-APEX variant does not have __ANDROID__APEX__ defined
968 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
969 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
970 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
971
972 // APEX variant has __ANDROID_APEX__=<apexname> defined
973 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
974 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
975 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
976
977 // APEX variant has __ANDROID_APEX__=<apexname> defined
978 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
979 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
980 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
981}
Jiyong Park7e636d02019-01-28 16:16:54 +0900982
983func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700984 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +0900985 apex {
986 name: "myapex",
987 key: "myapex.key",
988 native_shared_libs: ["mylib"],
989 }
990
991 apex_key {
992 name: "myapex.key",
993 public_key: "testkey.avbpubkey",
994 private_key: "testkey.pem",
995 }
996
997 cc_library_headers {
998 name: "mylib_headers",
999 export_include_dirs: ["my_include"],
1000 system_shared_libs: [],
1001 stl: "none",
1002 }
1003
1004 cc_library {
1005 name: "mylib",
1006 srcs: ["mylib.cpp"],
1007 system_shared_libs: [],
1008 stl: "none",
1009 header_libs: ["mylib_headers"],
1010 export_header_lib_headers: ["mylib_headers"],
1011 stubs: {
1012 versions: ["1", "2", "3"],
1013 },
1014 }
1015
1016 cc_library {
1017 name: "otherlib",
1018 srcs: ["mylib.cpp"],
1019 system_shared_libs: [],
1020 stl: "none",
1021 shared_libs: ["mylib"],
1022 }
1023 `)
1024
1025 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1026
1027 // Ensure that the include path of the header lib is exported to 'otherlib'
1028 ensureContains(t, cFlags, "-Imy_include")
1029}
Alex Light9670d332019-01-29 18:07:33 -08001030
Alex Light0851b882019-02-07 13:20:53 -08001031func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001032 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001033 apex {
1034 name: "myapex",
1035 key: "myapex.key",
1036 native_shared_libs: ["mylib_common"],
1037 }
1038
1039 apex_key {
1040 name: "myapex.key",
1041 public_key: "testkey.avbpubkey",
1042 private_key: "testkey.pem",
1043 }
1044
1045 cc_library {
1046 name: "mylib_common",
1047 srcs: ["mylib.cpp"],
1048 system_shared_libs: [],
1049 stl: "none",
1050 }
1051 `)
1052
1053 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1054 apexRule := module.Rule("apexRule")
1055 copyCmds := apexRule.Args["copy_commands"]
1056
1057 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1058 t.Log("Apex was a test apex!")
1059 t.Fail()
1060 }
1061 // Ensure that main rule creates an output
1062 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1063
1064 // Ensure that apex variant is created for the direct dep
1065 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1066
1067 // Ensure that both direct and indirect deps are copied into apex
1068 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1069
1070 // Ensure that the platform variant ends with _core_shared
1071 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1072
1073 if !android.InAnyApex("mylib_common") {
1074 t.Log("Found mylib_common not in any apex!")
1075 t.Fail()
1076 }
1077}
1078
1079func TestTestApex(t *testing.T) {
1080 if android.InAnyApex("mylib_common_test") {
1081 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!")
1082 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001083 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001084 apex_test {
1085 name: "myapex",
1086 key: "myapex.key",
1087 native_shared_libs: ["mylib_common_test"],
1088 }
1089
1090 apex_key {
1091 name: "myapex.key",
1092 public_key: "testkey.avbpubkey",
1093 private_key: "testkey.pem",
1094 }
1095
1096 cc_library {
1097 name: "mylib_common_test",
1098 srcs: ["mylib.cpp"],
1099 system_shared_libs: [],
1100 stl: "none",
1101 }
1102 `)
1103
1104 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1105 apexRule := module.Rule("apexRule")
1106 copyCmds := apexRule.Args["copy_commands"]
1107
1108 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1109 t.Log("Apex was not a test apex!")
1110 t.Fail()
1111 }
1112 // Ensure that main rule creates an output
1113 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1114
1115 // Ensure that apex variant is created for the direct dep
1116 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1117
1118 // Ensure that both direct and indirect deps are copied into apex
1119 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1120
1121 // Ensure that the platform variant ends with _core_shared
1122 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1123
1124 if android.InAnyApex("mylib_common_test") {
1125 t.Log("Found mylib_common_test in some apex!")
1126 t.Fail()
1127 }
1128}
1129
Alex Light9670d332019-01-29 18:07:33 -08001130func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001131 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08001132 apex {
1133 name: "myapex",
1134 key: "myapex.key",
1135 multilib: {
1136 first: {
1137 native_shared_libs: ["mylib_common"],
1138 }
1139 },
1140 target: {
1141 android: {
1142 multilib: {
1143 first: {
1144 native_shared_libs: ["mylib"],
1145 }
1146 }
1147 },
1148 host: {
1149 multilib: {
1150 first: {
1151 native_shared_libs: ["mylib2"],
1152 }
1153 }
1154 }
1155 }
1156 }
1157
1158 apex_key {
1159 name: "myapex.key",
1160 public_key: "testkey.avbpubkey",
1161 private_key: "testkey.pem",
1162 }
1163
1164 cc_library {
1165 name: "mylib",
1166 srcs: ["mylib.cpp"],
1167 system_shared_libs: [],
1168 stl: "none",
1169 }
1170
1171 cc_library {
1172 name: "mylib_common",
1173 srcs: ["mylib.cpp"],
1174 system_shared_libs: [],
1175 stl: "none",
1176 compile_multilib: "first",
1177 }
1178
1179 cc_library {
1180 name: "mylib2",
1181 srcs: ["mylib.cpp"],
1182 system_shared_libs: [],
1183 stl: "none",
1184 compile_multilib: "first",
1185 }
1186 `)
1187
1188 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1189 copyCmds := apexRule.Args["copy_commands"]
1190
1191 // Ensure that main rule creates an output
1192 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1193
1194 // Ensure that apex variant is created for the direct dep
1195 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1196 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1197 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1198
1199 // Ensure that both direct and indirect deps are copied into apex
1200 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1201 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1202 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1203
1204 // Ensure that the platform variant ends with _core_shared
1205 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1206 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1207 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1208}
Jiyong Park04480cf2019-02-06 00:16:29 +09001209
1210func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001211 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09001212 apex {
1213 name: "myapex",
1214 key: "myapex.key",
1215 binaries: ["myscript"],
1216 }
1217
1218 apex_key {
1219 name: "myapex.key",
1220 public_key: "testkey.avbpubkey",
1221 private_key: "testkey.pem",
1222 }
1223
1224 sh_binary {
1225 name: "myscript",
1226 src: "mylib.cpp",
1227 filename: "myscript.sh",
1228 sub_dir: "script",
1229 }
1230 `)
1231
1232 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1233 copyCmds := apexRule.Args["copy_commands"]
1234
1235 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1236}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001237
1238func TestApexInProductPartition(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001239 ctx, _ := testApex(t, `
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001240 apex {
1241 name: "myapex",
1242 key: "myapex.key",
1243 native_shared_libs: ["mylib"],
1244 product_specific: true,
1245 }
1246
1247 apex_key {
1248 name: "myapex.key",
1249 public_key: "testkey.avbpubkey",
1250 private_key: "testkey.pem",
1251 product_specific: true,
1252 }
1253
1254 cc_library {
1255 name: "mylib",
1256 srcs: ["mylib.cpp"],
1257 system_shared_libs: [],
1258 stl: "none",
1259 }
1260 `)
1261
1262 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1263 expected := "target/product/test_device/product/apex"
1264 actual := apex.installDir.RelPathString()
1265 if actual != expected {
1266 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1267 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001268}
Jiyong Park67882562019-03-21 01:11:21 +09001269
1270func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001271 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09001272 apex_key {
1273 name: "myapex.key",
1274 public_key: ":my.avbpubkey",
1275 private_key: ":my.pem",
1276 product_specific: true,
1277 }
1278
1279 filegroup {
1280 name: "my.avbpubkey",
1281 srcs: ["testkey2.avbpubkey"],
1282 }
1283
1284 filegroup {
1285 name: "my.pem",
1286 srcs: ["testkey2.pem"],
1287 }
1288 `)
1289
1290 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1291 expected_pubkey := "testkey2.avbpubkey"
1292 actual_pubkey := apex_key.public_key_file.String()
1293 if actual_pubkey != expected_pubkey {
1294 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1295 }
1296 expected_privkey := "testkey2.pem"
1297 actual_privkey := apex_key.private_key_file.String()
1298 if actual_privkey != expected_privkey {
1299 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1300 }
1301}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001302
1303func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001304 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001305 prebuilt_apex {
1306 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09001307 arch: {
1308 arm64: {
1309 src: "myapex-arm64.apex",
1310 },
1311 arm: {
1312 src: "myapex-arm.apex",
1313 },
1314 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001315 }
1316 `)
1317
1318 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1319
Jiyong Parkc95714e2019-03-29 14:23:10 +09001320 expectedInput := "myapex-arm64.apex"
1321 if prebuilt.inputApex.String() != expectedInput {
1322 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
1323 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001324}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001325
1326func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001327 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001328 prebuilt_apex {
1329 name: "myapex",
1330 src: "myapex-arm.apex",
1331 filename: "notmyapex.apex",
1332 }
1333 `)
1334
1335 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1336
1337 expected := "notmyapex.apex"
1338 if p.installFilename != expected {
1339 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
1340 }
1341}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07001342
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001343func TestPrebuiltOverrides(t *testing.T) {
1344 ctx, config := testApex(t, `
1345 prebuilt_apex {
1346 name: "myapex.prebuilt",
1347 src: "myapex-arm.apex",
1348 overrides: [
1349 "myapex",
1350 ],
1351 }
1352 `)
1353
1354 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
1355
1356 expected := []string{"myapex"}
1357 actual := android.AndroidMkEntriesForTest(t, config, "", p).EntryMap["LOCAL_OVERRIDES_PACKAGES"]
1358 if !reflect.DeepEqual(actual, expected) {
1359 t.Errorf("Incorrect LOCAL_OVERRIDES_PACKAGES value '%s', expected '%s'", actual, expected)
1360 }
1361}
1362
Roland Levillain630846d2019-06-26 12:48:34 +01001363func TestApexWithTests(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001364 ctx, _ := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01001365 apex_test {
1366 name: "myapex",
1367 key: "myapex.key",
1368 tests: [
1369 "mytest",
1370 ],
1371 }
1372
1373 apex_key {
1374 name: "myapex.key",
1375 public_key: "testkey.avbpubkey",
1376 private_key: "testkey.pem",
1377 }
1378
1379 cc_test {
1380 name: "mytest",
1381 gtest: false,
1382 srcs: ["mytest.cpp"],
1383 relative_install_path: "test",
1384 system_shared_libs: [],
1385 static_executable: true,
1386 stl: "none",
1387 }
1388 `)
1389
1390 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1391 copyCmds := apexRule.Args["copy_commands"]
1392
1393 // Ensure that test dep is copied into apex.
1394 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
1395}
1396
Jooyung Han5c998b92019-06-27 11:30:33 +09001397func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001398 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09001399 apex {
1400 name: "myapex",
1401 key: "myapex.key",
1402 native_shared_libs: ["mylib"],
1403 uses: ["commonapex"],
1404 }
1405
1406 apex {
1407 name: "commonapex",
1408 key: "myapex.key",
1409 native_shared_libs: ["libcommon"],
1410 provide_cpp_shared_libs: true,
1411 }
1412
1413 apex_key {
1414 name: "myapex.key",
1415 public_key: "testkey.avbpubkey",
1416 private_key: "testkey.pem",
1417 }
1418
1419 cc_library {
1420 name: "mylib",
1421 srcs: ["mylib.cpp"],
1422 shared_libs: ["libcommon"],
1423 system_shared_libs: [],
1424 stl: "none",
1425 }
1426
1427 cc_library {
1428 name: "libcommon",
1429 srcs: ["mylib_common.cpp"],
1430 system_shared_libs: [],
1431 stl: "none",
1432 }
1433 `)
1434
1435 module1 := ctx.ModuleForTests("myapex", "android_common_myapex")
1436 apexRule1 := module1.Rule("apexRule")
1437 copyCmds1 := apexRule1.Args["copy_commands"]
1438
1439 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex")
1440 apexRule2 := module2.Rule("apexRule")
1441 copyCmds2 := apexRule2.Args["copy_commands"]
1442
1443 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1444 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_core_shared_commonapex")
1445 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
1446 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
1447 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
1448}
1449
1450func TestApexUsesFailsIfNotProvided(t *testing.T) {
1451 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
1452 apex {
1453 name: "myapex",
1454 key: "myapex.key",
1455 uses: ["commonapex"],
1456 }
1457
1458 apex {
1459 name: "commonapex",
1460 key: "myapex.key",
1461 }
1462
1463 apex_key {
1464 name: "myapex.key",
1465 public_key: "testkey.avbpubkey",
1466 private_key: "testkey.pem",
1467 }
1468 `)
1469 testApexError(t, `uses: "commonapex" is not a provider`, `
1470 apex {
1471 name: "myapex",
1472 key: "myapex.key",
1473 uses: ["commonapex"],
1474 }
1475
1476 cc_library {
1477 name: "commonapex",
1478 system_shared_libs: [],
1479 stl: "none",
1480 }
1481
1482 apex_key {
1483 name: "myapex.key",
1484 public_key: "testkey.avbpubkey",
1485 private_key: "testkey.pem",
1486 }
1487 `)
1488}
1489
1490func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
1491 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
1492 apex {
1493 name: "myapex",
1494 key: "myapex.key",
1495 use_vendor: true,
1496 uses: ["commonapex"],
1497 }
1498
1499 apex {
1500 name: "commonapex",
1501 key: "myapex.key",
1502 provide_cpp_shared_libs: true,
1503 }
1504
1505 apex_key {
1506 name: "myapex.key",
1507 public_key: "testkey.avbpubkey",
1508 private_key: "testkey.pem",
1509 }
1510 `)
1511}
1512
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07001513func TestMain(m *testing.M) {
1514 run := func() int {
1515 setUp()
1516 defer tearDown()
1517
1518 return m.Run()
1519 }
1520
1521 os.Exit(run())
1522}