blob: 45c715f07a802cdbc6461224a11c54e0d0cb4ca2 [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))
Jiyong Park7f7766d2019-07-25 22:02:35 +090094 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(java.LibraryFactory))
Jiyong Park9e6c2422019-08-09 20:39:45 +090095 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(java.ImportFactory))
Jiyong Park7f7766d2019-07-25 22:02:35 +090096
Jaewoong Jung939ebd52019-03-26 15:07:36 -070097 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
98 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
99 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900100 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900101 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900102 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900103 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Roland Levillain9b5fde92019-06-28 15:41:19 +0100104 ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900105 ctx.BottomUp("version", cc.VersionMutator).Parallel()
106 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
107 })
108
109 ctx.Register()
110
111 bp = bp + `
112 toolchain_library {
113 name: "libcompiler_rt-extras",
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: "libatomic",
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 toolchain_library {
127 name: "libgcc",
128 src: "",
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 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700134 name: "libgcc_stripped",
135 src: "",
136 vendor_available: true,
137 recovery_available: true,
138 }
139
140 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900141 name: "libclang_rt.builtins-aarch64-android",
142 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900143 vendor_available: true,
144 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900145 }
146
147 toolchain_library {
148 name: "libclang_rt.builtins-arm-android",
149 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900150 vendor_available: true,
151 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900152 }
153
154 cc_object {
155 name: "crtbegin_so",
156 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900157 vendor_available: true,
158 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900159 }
160
161 cc_object {
162 name: "crtend_so",
163 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900164 vendor_available: true,
165 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900166 }
167
Alex Light3d673592019-01-18 14:37:31 -0800168 cc_object {
169 name: "crtbegin_static",
170 stl: "none",
171 }
172
173 cc_object {
174 name: "crtend_android",
175 stl: "none",
176 }
177
Jiyong Parkda6eb592018-12-19 17:12:36 +0900178 llndk_library {
179 name: "libc",
180 symbol_file: "",
181 }
182
183 llndk_library {
184 name: "libm",
185 symbol_file: "",
186 }
187
188 llndk_library {
189 name: "libdl",
190 symbol_file: "",
191 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900192 `
193
194 ctx.MockFileSystem(map[string][]byte{
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900195 "Android.bp": []byte(bp),
Dan Willemsen412160e2019-04-09 21:36:26 -0700196 "build/make/target/product/security": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900197 "apex_manifest.json": nil,
Jiyong Park809bb722019-02-13 21:33:49 +0900198 "AndroidManifest.xml": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900199 "system/sepolicy/apex/myapex-file_contexts": nil,
200 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
201 "system/sepolicy/apex/otherapex-file_contexts": nil,
Jooyung Han5c998b92019-06-27 11:30:33 +0900202 "system/sepolicy/apex/commonapex-file_contexts": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900203 "mylib.cpp": nil,
Jooyung Han5c998b92019-06-27 11:30:33 +0900204 "mylib_common.cpp": nil,
Roland Levillain9b5fde92019-06-28 15:41:19 +0100205 "mytest.cpp": nil,
206 "mytest1.cpp": nil,
207 "mytest2.cpp": nil,
208 "mytest3.cpp": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900209 "myprebuilt": nil,
210 "my_include": nil,
Jiyong Park7f7766d2019-07-25 22:02:35 +0900211 "foo/bar/MyClass.java": nil,
Jiyong Park9e6c2422019-08-09 20:39:45 +0900212 "prebuilt.jar": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900213 "vendor/foo/devkeys/test.x509.pem": nil,
214 "vendor/foo/devkeys/test.pk8": nil,
215 "testkey.x509.pem": nil,
216 "testkey.pk8": nil,
217 "testkey.override.x509.pem": nil,
218 "testkey.override.pk8": nil,
219 "vendor/foo/devkeys/testkey.avbpubkey": nil,
220 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park52818fc2019-03-18 12:01:38 +0900221 "NOTICE": nil,
222 "custom_notice": nil,
Jiyong Park67882562019-03-21 01:11:21 +0900223 "testkey2.avbpubkey": nil,
224 "testkey2.pem": nil,
Jiyong Parkc95714e2019-03-29 14:23:10 +0900225 "myapex-arm64.apex": nil,
226 "myapex-arm.apex": nil,
Jiyong Park71b519d2019-04-18 17:25:49 +0900227 "frameworks/base/api/current.txt": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900228 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229
Jooyung Han5c998b92019-06-27 11:30:33 +0900230 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900231}
232
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700233func setUp() {
234 var err error
235 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900236 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700237 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900239}
240
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700241func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242 os.RemoveAll(buildDir)
243}
244
245// ensure that 'result' contains 'expected'
246func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900247 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900248 if !strings.Contains(result, expected) {
249 t.Errorf("%q is not found in %q", expected, result)
250 }
251}
252
253// ensures that 'result' does not contain 'notExpected'
254func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900255 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900256 if strings.Contains(result, notExpected) {
257 t.Errorf("%q is found in %q", notExpected, result)
258 }
259}
260
261func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900262 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900263 if !android.InList(expected, result) {
264 t.Errorf("%q is not found in %v", expected, result)
265 }
266}
267
268func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900269 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900270 if android.InList(notExpected, result) {
271 t.Errorf("%q is found in %v", notExpected, result)
272 }
273}
274
Jooyung Hane1633032019-08-01 17:41:43 +0900275func ensureListEmpty(t *testing.T, result []string) {
276 t.Helper()
277 if len(result) > 0 {
278 t.Errorf("%q is expected to be empty", result)
279 }
280}
281
Jiyong Park25fc6a92018-11-18 18:02:45 +0900282// Minimal test
283func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700284 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900285 apex_defaults {
286 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900287 manifest: ":myapex.manifest",
288 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900289 key: "myapex.key",
290 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800291 multilib: {
292 both: {
293 binaries: ["foo",],
294 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900295 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900296 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900297 }
298
Jiyong Park30ca9372019-02-07 16:27:23 +0900299 apex {
300 name: "myapex",
301 defaults: ["myapex-defaults"],
302 }
303
Jiyong Park25fc6a92018-11-18 18:02:45 +0900304 apex_key {
305 name: "myapex.key",
306 public_key: "testkey.avbpubkey",
307 private_key: "testkey.pem",
308 }
309
Jiyong Park809bb722019-02-13 21:33:49 +0900310 filegroup {
311 name: "myapex.manifest",
312 srcs: ["apex_manifest.json"],
313 }
314
315 filegroup {
316 name: "myapex.androidmanifest",
317 srcs: ["AndroidManifest.xml"],
318 }
319
Jiyong Park25fc6a92018-11-18 18:02:45 +0900320 cc_library {
321 name: "mylib",
322 srcs: ["mylib.cpp"],
323 shared_libs: ["mylib2"],
324 system_shared_libs: [],
325 stl: "none",
326 }
327
Alex Light3d673592019-01-18 14:37:31 -0800328 cc_binary {
329 name: "foo",
330 srcs: ["mylib.cpp"],
331 compile_multilib: "both",
332 multilib: {
333 lib32: {
334 suffix: "32",
335 },
336 lib64: {
337 suffix: "64",
338 },
339 },
340 symlinks: ["foo_link_"],
341 symlink_preferred_arch: true,
342 system_shared_libs: [],
343 static_executable: true,
344 stl: "none",
345 }
346
Jiyong Park25fc6a92018-11-18 18:02:45 +0900347 cc_library {
348 name: "mylib2",
349 srcs: ["mylib.cpp"],
350 system_shared_libs: [],
351 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900352 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900354
355 java_library {
356 name: "myjar",
357 srcs: ["foo/bar/MyClass.java"],
358 sdk_version: "none",
359 system_modules: "none",
360 compile_dex: true,
361 static_libs: ["myotherjar"],
362 }
363
364 java_library {
365 name: "myotherjar",
366 srcs: ["foo/bar/MyClass.java"],
367 sdk_version: "none",
368 system_modules: "none",
369 compile_dex: true,
370 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900371
372 java_import {
373 name: "myprebuiltjar",
374 jars: ["prebuilt.jar"],
375 installable: true,
376 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900377 `)
378
379 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900380
381 optFlags := apexRule.Args["opt_flags"]
382 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700383 // Ensure that the NOTICE output is being packaged as an asset.
384 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900385
Jiyong Park25fc6a92018-11-18 18:02:45 +0900386 copyCmds := apexRule.Args["copy_commands"]
387
388 // Ensure that main rule creates an output
389 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
390
391 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900392 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900393 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900394 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900395
396 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900397 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900398 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399
400 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800401 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
402 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900403 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900404 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900405 // .. but not for java libs
406 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800407
Jiyong Park7f7766d2019-07-25 22:02:35 +0900408 // Ensure that the platform variant ends with _core_shared or _common
Logan Chien3aeedc92018-12-26 15:32:21 +0800409 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
410 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900411 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
412 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900413 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800414
415 // Ensure that all symlinks are present.
416 found_foo_link_64 := false
417 found_foo := false
418 for _, cmd := range strings.Split(copyCmds, " && ") {
419 if strings.HasPrefix(cmd, "ln -s foo64") {
420 if strings.HasSuffix(cmd, "bin/foo") {
421 found_foo = true
422 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
423 found_foo_link_64 = true
424 }
425 }
426 }
427 good := found_foo && found_foo_link_64
428 if !good {
429 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
430 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900431
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700432 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("mergeNoticesRule")
433 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700434 if len(noticeInputs) != 2 {
435 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900436 }
437 ensureListContains(t, noticeInputs, "NOTICE")
438 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800439}
440
441func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700442 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800443 apex {
444 name: "myapex",
445 key: "myapex.key",
446 payload_type: "zip",
447 native_shared_libs: ["mylib"],
448 }
449
450 apex_key {
451 name: "myapex.key",
452 public_key: "testkey.avbpubkey",
453 private_key: "testkey.pem",
454 }
455
456 cc_library {
457 name: "mylib",
458 srcs: ["mylib.cpp"],
459 shared_libs: ["mylib2"],
460 system_shared_libs: [],
461 stl: "none",
462 }
463
464 cc_library {
465 name: "mylib2",
466 srcs: ["mylib.cpp"],
467 system_shared_libs: [],
468 stl: "none",
469 }
470 `)
471
472 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
473 copyCmds := zipApexRule.Args["copy_commands"]
474
475 // Ensure that main rule creates an output
476 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
477
478 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900479 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800480
481 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900482 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800483
484 // Ensure that both direct and indirect deps are copied into apex
485 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
486 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900487}
488
489func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700490 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900491 apex {
492 name: "myapex",
493 key: "myapex.key",
494 native_shared_libs: ["mylib", "mylib3"],
495 }
496
497 apex_key {
498 name: "myapex.key",
499 public_key: "testkey.avbpubkey",
500 private_key: "testkey.pem",
501 }
502
503 cc_library {
504 name: "mylib",
505 srcs: ["mylib.cpp"],
506 shared_libs: ["mylib2", "mylib3"],
507 system_shared_libs: [],
508 stl: "none",
509 }
510
511 cc_library {
512 name: "mylib2",
513 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900514 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900515 system_shared_libs: [],
516 stl: "none",
517 stubs: {
518 versions: ["1", "2", "3"],
519 },
520 }
521
522 cc_library {
523 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900524 srcs: ["mylib.cpp"],
525 shared_libs: ["mylib4"],
526 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900527 stl: "none",
528 stubs: {
529 versions: ["10", "11", "12"],
530 },
531 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900532
533 cc_library {
534 name: "mylib4",
535 srcs: ["mylib.cpp"],
536 system_shared_libs: [],
537 stl: "none",
538 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900539 `)
540
541 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
542 copyCmds := apexRule.Args["copy_commands"]
543
544 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800545 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900546
547 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800548 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900549
550 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800551 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900552
Jiyong Parkda6eb592018-12-19 17:12:36 +0900553 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900554
555 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900556 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900557 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900558 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900559
560 // 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 +0900561 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900562 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900563 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900564
565 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900566 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900567 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900568
569 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900570 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 +0900571}
572
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900573func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700574 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900575 apex {
576 name: "myapex",
577 key: "myapex.key",
578 native_shared_libs: ["mylib"],
579 }
580
581 apex_key {
582 name: "myapex.key",
583 public_key: "testkey.avbpubkey",
584 private_key: "testkey.pem",
585 }
586
587 cc_library {
588 name: "mylib",
589 srcs: ["mylib.cpp"],
590 shared_libs: ["libfoo#10"],
591 system_shared_libs: [],
592 stl: "none",
593 }
594
595 cc_library {
596 name: "libfoo",
597 srcs: ["mylib.cpp"],
598 shared_libs: ["libbar"],
599 system_shared_libs: [],
600 stl: "none",
601 stubs: {
602 versions: ["10", "20", "30"],
603 },
604 }
605
606 cc_library {
607 name: "libbar",
608 srcs: ["mylib.cpp"],
609 system_shared_libs: [],
610 stl: "none",
611 }
612
613 `)
614
615 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
616 copyCmds := apexRule.Args["copy_commands"]
617
618 // Ensure that direct non-stubs dep is always included
619 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
620
621 // Ensure that indirect stubs dep is not included
622 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
623
624 // Ensure that dependency of stubs is not included
625 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
626
Jiyong Parkda6eb592018-12-19 17:12:36 +0900627 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900628
629 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900630 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900631 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900632 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900633
Jiyong Parkda6eb592018-12-19 17:12:36 +0900634 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900635
636 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
637 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
638}
639
Jiyong Park25fc6a92018-11-18 18:02:45 +0900640func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700641 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900642 apex {
643 name: "myapex",
644 key: "myapex.key",
645 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
646 }
647
648 apex_key {
649 name: "myapex.key",
650 public_key: "testkey.avbpubkey",
651 private_key: "testkey.pem",
652 }
653
654 cc_library {
655 name: "mylib",
656 srcs: ["mylib.cpp"],
657 shared_libs: ["libdl#27"],
658 stl: "none",
659 }
660
661 cc_library_shared {
662 name: "mylib_shared",
663 srcs: ["mylib.cpp"],
664 shared_libs: ["libdl#27"],
665 stl: "none",
666 }
667
668 cc_library {
669 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700670 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900671 nocrt: true,
672 system_shared_libs: [],
673 stl: "none",
674 stubs: {
675 versions: ["27", "28", "29"],
676 },
677 }
678
679 cc_library {
680 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700681 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900682 nocrt: true,
683 system_shared_libs: [],
684 stl: "none",
685 stubs: {
686 versions: ["27", "28", "29"],
687 },
688 }
689
690 cc_library {
691 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700692 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900693 nocrt: true,
694 system_shared_libs: [],
695 stl: "none",
696 stubs: {
697 versions: ["27", "28", "29"],
698 },
699 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900700
701 cc_library {
702 name: "libBootstrap",
703 srcs: ["mylib.cpp"],
704 stl: "none",
705 bootstrap: true,
706 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900707 `)
708
709 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
710 copyCmds := apexRule.Args["copy_commands"]
711
712 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800713 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900714 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
715 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900716
717 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900718 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900719
Jiyong Parkda6eb592018-12-19 17:12:36 +0900720 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
721 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
722 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900723
724 // For dependency to libc
725 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900726 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900727 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900728 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900729 // ... Cflags from stub is correctly exported to mylib
730 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
731 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
732
733 // For dependency to libm
734 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900735 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900736 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900737 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900738 // ... and is not compiling with the stub
739 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
740 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
741
742 // For dependency to libdl
743 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900744 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900745 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900746 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
747 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900748 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900749 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900750 // ... Cflags from stub is correctly exported to mylib
751 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
752 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900753
754 // Ensure that libBootstrap is depending on the platform variant of bionic libs
755 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
756 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
757 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
758 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900759}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900760
761func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700762 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900763 apex {
764 name: "myapex",
765 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900766 native_shared_libs: ["mylib"],
767 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900768 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900769 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900770 }
771
772 apex_key {
773 name: "myapex.key",
774 public_key: "testkey.avbpubkey",
775 private_key: "testkey.pem",
776 }
777
778 prebuilt_etc {
779 name: "myetc",
780 src: "myprebuilt",
781 sub_dir: "foo/bar",
782 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900783
784 cc_library {
785 name: "mylib",
786 srcs: ["mylib.cpp"],
787 relative_install_path: "foo/bar",
788 system_shared_libs: [],
789 stl: "none",
790 }
791
792 cc_binary {
793 name: "mybin",
794 srcs: ["mylib.cpp"],
795 relative_install_path: "foo/bar",
796 system_shared_libs: [],
797 static_executable: true,
798 stl: "none",
799 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900800 `)
801
802 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
803 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
804
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900805 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900806 ensureListContains(t, dirs, "etc")
807 ensureListContains(t, dirs, "etc/foo")
808 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900809 ensureListContains(t, dirs, "lib64")
810 ensureListContains(t, dirs, "lib64/foo")
811 ensureListContains(t, dirs, "lib64/foo/bar")
812 ensureListContains(t, dirs, "lib")
813 ensureListContains(t, dirs, "lib/foo")
814 ensureListContains(t, dirs, "lib/foo/bar")
815
Jiyong Parkbd13e442019-03-15 18:10:35 +0900816 ensureListContains(t, dirs, "bin")
817 ensureListContains(t, dirs, "bin/foo")
818 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900819}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900820
821func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700822 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +0900823 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 shared_libs: ["mylib2"],
840 system_shared_libs: [],
841 vendor_available: true,
842 stl: "none",
843 }
844
845 cc_library {
846 name: "mylib2",
847 srcs: ["mylib.cpp"],
848 system_shared_libs: [],
849 vendor_available: true,
850 stl: "none",
851 }
852 `)
853
854 inputsList := []string{}
855 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
856 for _, implicit := range i.Implicits {
857 inputsList = append(inputsList, implicit.String())
858 }
859 }
860 inputsString := strings.Join(inputsList, " ")
861
862 // ensure that the apex includes vendor variants of the direct and indirect deps
863 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
864 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
865
866 // ensure that the apex does not include core variants
867 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
868 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
869}
Jiyong Park16e91a02018-12-20 18:18:08 +0900870
Jooyung Han5c998b92019-06-27 11:30:33 +0900871func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
872 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
873 apex {
874 name: "myapex",
875 key: "myapex.key",
876 native_shared_libs: ["mylib"],
877 use_vendor: true,
878 }
879
880 apex_key {
881 name: "myapex.key",
882 public_key: "testkey.avbpubkey",
883 private_key: "testkey.pem",
884 }
885
886 cc_library {
887 name: "mylib",
888 srcs: ["mylib.cpp"],
889 system_shared_libs: [],
890 stl: "none",
891 }
892 `)
893}
894
Jiyong Park16e91a02018-12-20 18:18:08 +0900895func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700896 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +0900897 apex {
898 name: "myapex",
899 key: "myapex.key",
900 native_shared_libs: ["mylib"],
901 }
902
903 apex_key {
904 name: "myapex.key",
905 public_key: "testkey.avbpubkey",
906 private_key: "testkey.pem",
907 }
908
909 cc_library {
910 name: "mylib",
911 srcs: ["mylib.cpp"],
912 system_shared_libs: [],
913 stl: "none",
914 stubs: {
915 versions: ["1", "2", "3"],
916 },
917 }
918
919 cc_binary {
920 name: "not_in_apex",
921 srcs: ["mylib.cpp"],
922 static_libs: ["mylib"],
923 static_executable: true,
924 system_shared_libs: [],
925 stl: "none",
926 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900927 `)
928
929 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
930
931 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800932 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900933}
Jiyong Park9335a262018-12-24 11:31:58 +0900934
935func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700936 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +0900937 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900938 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900939 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900940 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900941 native_shared_libs: ["mylib"],
942 }
943
944 cc_library {
945 name: "mylib",
946 srcs: ["mylib.cpp"],
947 system_shared_libs: [],
948 stl: "none",
949 }
950
951 apex_key {
952 name: "myapex.key",
953 public_key: "testkey.avbpubkey",
954 private_key: "testkey.pem",
955 }
956
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900957 android_app_certificate {
958 name: "myapex.certificate",
959 certificate: "testkey",
960 }
961
962 android_app_certificate {
963 name: "myapex.certificate.override",
964 certificate: "testkey.override",
965 }
966
Jiyong Park9335a262018-12-24 11:31:58 +0900967 `)
968
969 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900970 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900971
972 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
973 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
974 "vendor/foo/devkeys/testkey.avbpubkey")
975 }
976 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
977 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
978 "vendor/foo/devkeys/testkey.pem")
979 }
980
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900981 // check the APK certs. It should be overridden to myapex.certificate.override
982 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
983 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900984 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900985 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900986 }
987}
Jiyong Park58e364a2019-01-19 19:24:06 +0900988
989func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700990 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +0900991 apex {
992 name: "myapex",
993 key: "myapex.key",
994 native_shared_libs: ["mylib"],
995 }
996
997 apex {
998 name: "otherapex",
999 key: "myapex.key",
1000 native_shared_libs: ["mylib"],
1001 }
1002
1003 apex_key {
1004 name: "myapex.key",
1005 public_key: "testkey.avbpubkey",
1006 private_key: "testkey.pem",
1007 }
1008
1009 cc_library {
1010 name: "mylib",
1011 srcs: ["mylib.cpp"],
1012 system_shared_libs: [],
1013 stl: "none",
1014 }
1015 `)
1016
1017 // non-APEX variant does not have __ANDROID__APEX__ defined
1018 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1019 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1020 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
1021
1022 // APEX variant has __ANDROID_APEX__=<apexname> defined
1023 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
1024 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1025 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
1026
1027 // APEX variant has __ANDROID_APEX__=<apexname> defined
1028 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
1029 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1030 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
1031}
Jiyong Park7e636d02019-01-28 16:16:54 +09001032
1033func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001034 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001035 apex {
1036 name: "myapex",
1037 key: "myapex.key",
1038 native_shared_libs: ["mylib"],
1039 }
1040
1041 apex_key {
1042 name: "myapex.key",
1043 public_key: "testkey.avbpubkey",
1044 private_key: "testkey.pem",
1045 }
1046
1047 cc_library_headers {
1048 name: "mylib_headers",
1049 export_include_dirs: ["my_include"],
1050 system_shared_libs: [],
1051 stl: "none",
1052 }
1053
1054 cc_library {
1055 name: "mylib",
1056 srcs: ["mylib.cpp"],
1057 system_shared_libs: [],
1058 stl: "none",
1059 header_libs: ["mylib_headers"],
1060 export_header_lib_headers: ["mylib_headers"],
1061 stubs: {
1062 versions: ["1", "2", "3"],
1063 },
1064 }
1065
1066 cc_library {
1067 name: "otherlib",
1068 srcs: ["mylib.cpp"],
1069 system_shared_libs: [],
1070 stl: "none",
1071 shared_libs: ["mylib"],
1072 }
1073 `)
1074
1075 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1076
1077 // Ensure that the include path of the header lib is exported to 'otherlib'
1078 ensureContains(t, cFlags, "-Imy_include")
1079}
Alex Light9670d332019-01-29 18:07:33 -08001080
Jooyung Hane1633032019-08-01 17:41:43 +09001081func TestDependenciesInApexManifest(t *testing.T) {
1082 ctx, _ := testApex(t, `
1083 apex {
1084 name: "myapex_nodep",
1085 key: "myapex.key",
1086 native_shared_libs: ["lib_nodep"],
1087 compile_multilib: "both",
1088 file_contexts: "myapex",
1089 }
1090
1091 apex {
1092 name: "myapex_dep",
1093 key: "myapex.key",
1094 native_shared_libs: ["lib_dep"],
1095 compile_multilib: "both",
1096 file_contexts: "myapex",
1097 }
1098
1099 apex {
1100 name: "myapex_provider",
1101 key: "myapex.key",
1102 native_shared_libs: ["libfoo"],
1103 compile_multilib: "both",
1104 file_contexts: "myapex",
1105 }
1106
1107 apex {
1108 name: "myapex_selfcontained",
1109 key: "myapex.key",
1110 native_shared_libs: ["lib_dep", "libfoo"],
1111 compile_multilib: "both",
1112 file_contexts: "myapex",
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: "lib_nodep",
1123 srcs: ["mylib.cpp"],
1124 system_shared_libs: [],
1125 stl: "none",
1126 }
1127
1128 cc_library {
1129 name: "lib_dep",
1130 srcs: ["mylib.cpp"],
1131 shared_libs: ["libfoo"],
1132 system_shared_libs: [],
1133 stl: "none",
1134 }
1135
1136 cc_library {
1137 name: "libfoo",
1138 srcs: ["mytest.cpp"],
1139 stubs: {
1140 versions: ["1"],
1141 },
1142 system_shared_libs: [],
1143 stl: "none",
1144 }
1145 `)
1146
1147 names := func(s string) (ns []string) {
1148 for _, n := range strings.Split(s, " ") {
1149 if len(n) > 0 {
1150 ns = append(ns, n)
1151 }
1152 }
1153 return
1154 }
1155
1156 var injectRule android.TestingBuildParams
1157 var provideNativeLibs, requireNativeLibs []string
1158
1159 injectRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("injectApexDependency")
1160 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1161 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1162 ensureListEmpty(t, provideNativeLibs)
1163 ensureListEmpty(t, requireNativeLibs)
1164
1165 injectRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("injectApexDependency")
1166 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1167 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1168 ensureListEmpty(t, provideNativeLibs)
1169 ensureListContains(t, requireNativeLibs, "libfoo.so")
1170
1171 injectRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("injectApexDependency")
1172 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1173 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1174 ensureListContains(t, provideNativeLibs, "libfoo.so")
1175 ensureListEmpty(t, requireNativeLibs)
1176
1177 injectRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("injectApexDependency")
1178 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1179 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1180 ensureListContains(t, provideNativeLibs, "libfoo.so")
1181 ensureListEmpty(t, requireNativeLibs)
1182}
1183
Alex Light0851b882019-02-07 13:20:53 -08001184func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001185 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001186 apex {
1187 name: "myapex",
1188 key: "myapex.key",
1189 native_shared_libs: ["mylib_common"],
1190 }
1191
1192 apex_key {
1193 name: "myapex.key",
1194 public_key: "testkey.avbpubkey",
1195 private_key: "testkey.pem",
1196 }
1197
1198 cc_library {
1199 name: "mylib_common",
1200 srcs: ["mylib.cpp"],
1201 system_shared_libs: [],
1202 stl: "none",
1203 }
1204 `)
1205
1206 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1207 apexRule := module.Rule("apexRule")
1208 copyCmds := apexRule.Args["copy_commands"]
1209
1210 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1211 t.Log("Apex was a test apex!")
1212 t.Fail()
1213 }
1214 // Ensure that main rule creates an output
1215 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1216
1217 // Ensure that apex variant is created for the direct dep
1218 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1219
1220 // Ensure that both direct and indirect deps are copied into apex
1221 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1222
1223 // Ensure that the platform variant ends with _core_shared
1224 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1225
1226 if !android.InAnyApex("mylib_common") {
1227 t.Log("Found mylib_common not in any apex!")
1228 t.Fail()
1229 }
1230}
1231
1232func TestTestApex(t *testing.T) {
1233 if android.InAnyApex("mylib_common_test") {
1234 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!")
1235 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001236 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001237 apex_test {
1238 name: "myapex",
1239 key: "myapex.key",
1240 native_shared_libs: ["mylib_common_test"],
1241 }
1242
1243 apex_key {
1244 name: "myapex.key",
1245 public_key: "testkey.avbpubkey",
1246 private_key: "testkey.pem",
1247 }
1248
1249 cc_library {
1250 name: "mylib_common_test",
1251 srcs: ["mylib.cpp"],
1252 system_shared_libs: [],
1253 stl: "none",
1254 }
1255 `)
1256
1257 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1258 apexRule := module.Rule("apexRule")
1259 copyCmds := apexRule.Args["copy_commands"]
1260
1261 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1262 t.Log("Apex was not a test apex!")
1263 t.Fail()
1264 }
1265 // Ensure that main rule creates an output
1266 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1267
1268 // Ensure that apex variant is created for the direct dep
1269 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1270
1271 // Ensure that both direct and indirect deps are copied into apex
1272 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1273
1274 // Ensure that the platform variant ends with _core_shared
1275 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1276
1277 if android.InAnyApex("mylib_common_test") {
1278 t.Log("Found mylib_common_test in some apex!")
1279 t.Fail()
1280 }
1281}
1282
Alex Light9670d332019-01-29 18:07:33 -08001283func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001284 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08001285 apex {
1286 name: "myapex",
1287 key: "myapex.key",
1288 multilib: {
1289 first: {
1290 native_shared_libs: ["mylib_common"],
1291 }
1292 },
1293 target: {
1294 android: {
1295 multilib: {
1296 first: {
1297 native_shared_libs: ["mylib"],
1298 }
1299 }
1300 },
1301 host: {
1302 multilib: {
1303 first: {
1304 native_shared_libs: ["mylib2"],
1305 }
1306 }
1307 }
1308 }
1309 }
1310
1311 apex_key {
1312 name: "myapex.key",
1313 public_key: "testkey.avbpubkey",
1314 private_key: "testkey.pem",
1315 }
1316
1317 cc_library {
1318 name: "mylib",
1319 srcs: ["mylib.cpp"],
1320 system_shared_libs: [],
1321 stl: "none",
1322 }
1323
1324 cc_library {
1325 name: "mylib_common",
1326 srcs: ["mylib.cpp"],
1327 system_shared_libs: [],
1328 stl: "none",
1329 compile_multilib: "first",
1330 }
1331
1332 cc_library {
1333 name: "mylib2",
1334 srcs: ["mylib.cpp"],
1335 system_shared_libs: [],
1336 stl: "none",
1337 compile_multilib: "first",
1338 }
1339 `)
1340
1341 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1342 copyCmds := apexRule.Args["copy_commands"]
1343
1344 // Ensure that main rule creates an output
1345 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1346
1347 // Ensure that apex variant is created for the direct dep
1348 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1349 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1350 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1351
1352 // Ensure that both direct and indirect deps are copied into apex
1353 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1354 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1355 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1356
1357 // Ensure that the platform variant ends with _core_shared
1358 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1359 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1360 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1361}
Jiyong Park04480cf2019-02-06 00:16:29 +09001362
1363func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001364 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09001365 apex {
1366 name: "myapex",
1367 key: "myapex.key",
1368 binaries: ["myscript"],
1369 }
1370
1371 apex_key {
1372 name: "myapex.key",
1373 public_key: "testkey.avbpubkey",
1374 private_key: "testkey.pem",
1375 }
1376
1377 sh_binary {
1378 name: "myscript",
1379 src: "mylib.cpp",
1380 filename: "myscript.sh",
1381 sub_dir: "script",
1382 }
1383 `)
1384
1385 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1386 copyCmds := apexRule.Args["copy_commands"]
1387
1388 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1389}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001390
1391func TestApexInProductPartition(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001392 ctx, _ := testApex(t, `
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001393 apex {
1394 name: "myapex",
1395 key: "myapex.key",
1396 native_shared_libs: ["mylib"],
1397 product_specific: true,
1398 }
1399
1400 apex_key {
1401 name: "myapex.key",
1402 public_key: "testkey.avbpubkey",
1403 private_key: "testkey.pem",
1404 product_specific: true,
1405 }
1406
1407 cc_library {
1408 name: "mylib",
1409 srcs: ["mylib.cpp"],
1410 system_shared_libs: [],
1411 stl: "none",
1412 }
1413 `)
1414
1415 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1416 expected := "target/product/test_device/product/apex"
1417 actual := apex.installDir.RelPathString()
1418 if actual != expected {
1419 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1420 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001421}
Jiyong Park67882562019-03-21 01:11:21 +09001422
1423func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001424 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09001425 apex_key {
1426 name: "myapex.key",
1427 public_key: ":my.avbpubkey",
1428 private_key: ":my.pem",
1429 product_specific: true,
1430 }
1431
1432 filegroup {
1433 name: "my.avbpubkey",
1434 srcs: ["testkey2.avbpubkey"],
1435 }
1436
1437 filegroup {
1438 name: "my.pem",
1439 srcs: ["testkey2.pem"],
1440 }
1441 `)
1442
1443 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1444 expected_pubkey := "testkey2.avbpubkey"
1445 actual_pubkey := apex_key.public_key_file.String()
1446 if actual_pubkey != expected_pubkey {
1447 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1448 }
1449 expected_privkey := "testkey2.pem"
1450 actual_privkey := apex_key.private_key_file.String()
1451 if actual_privkey != expected_privkey {
1452 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1453 }
1454}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001455
1456func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001457 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001458 prebuilt_apex {
1459 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09001460 arch: {
1461 arm64: {
1462 src: "myapex-arm64.apex",
1463 },
1464 arm: {
1465 src: "myapex-arm.apex",
1466 },
1467 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001468 }
1469 `)
1470
1471 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1472
Jiyong Parkc95714e2019-03-29 14:23:10 +09001473 expectedInput := "myapex-arm64.apex"
1474 if prebuilt.inputApex.String() != expectedInput {
1475 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
1476 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001477}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001478
1479func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001480 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001481 prebuilt_apex {
1482 name: "myapex",
1483 src: "myapex-arm.apex",
1484 filename: "notmyapex.apex",
1485 }
1486 `)
1487
1488 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1489
1490 expected := "notmyapex.apex"
1491 if p.installFilename != expected {
1492 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
1493 }
1494}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07001495
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001496func TestPrebuiltOverrides(t *testing.T) {
1497 ctx, config := testApex(t, `
1498 prebuilt_apex {
1499 name: "myapex.prebuilt",
1500 src: "myapex-arm.apex",
1501 overrides: [
1502 "myapex",
1503 ],
1504 }
1505 `)
1506
1507 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
1508
1509 expected := []string{"myapex"}
1510 actual := android.AndroidMkEntriesForTest(t, config, "", p).EntryMap["LOCAL_OVERRIDES_PACKAGES"]
1511 if !reflect.DeepEqual(actual, expected) {
1512 t.Errorf("Incorrect LOCAL_OVERRIDES_PACKAGES value '%s', expected '%s'", actual, expected)
1513 }
1514}
1515
Roland Levillain630846d2019-06-26 12:48:34 +01001516func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01001517 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01001518 apex_test {
1519 name: "myapex",
1520 key: "myapex.key",
1521 tests: [
1522 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01001523 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01001524 ],
1525 }
1526
1527 apex_key {
1528 name: "myapex.key",
1529 public_key: "testkey.avbpubkey",
1530 private_key: "testkey.pem",
1531 }
1532
1533 cc_test {
1534 name: "mytest",
1535 gtest: false,
1536 srcs: ["mytest.cpp"],
1537 relative_install_path: "test",
1538 system_shared_libs: [],
1539 static_executable: true,
1540 stl: "none",
1541 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01001542
1543 cc_test {
1544 name: "mytests",
1545 gtest: false,
1546 srcs: [
1547 "mytest1.cpp",
1548 "mytest2.cpp",
1549 "mytest3.cpp",
1550 ],
1551 test_per_src: true,
1552 relative_install_path: "test",
1553 system_shared_libs: [],
1554 static_executable: true,
1555 stl: "none",
1556 }
Roland Levillain630846d2019-06-26 12:48:34 +01001557 `)
1558
1559 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1560 copyCmds := apexRule.Args["copy_commands"]
1561
1562 // Ensure that test dep is copied into apex.
1563 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01001564
1565 // Ensure that test deps built with `test_per_src` are copied into apex.
1566 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
1567 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
1568 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01001569
1570 // Ensure the module is correctly translated.
1571 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1572 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
1573 name := apexBundle.BaseModuleName()
1574 prefix := "TARGET_"
1575 var builder strings.Builder
1576 data.Custom(&builder, name, prefix, "", data)
1577 androidMk := builder.String()
1578 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest\n")
1579 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest1\n")
1580 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest2\n")
1581 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest3\n")
1582 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.apex_manifest.json\n")
1583 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.apex_pubkey\n")
1584 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01001585}
1586
Jooyung Han5c998b92019-06-27 11:30:33 +09001587func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001588 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09001589 apex {
1590 name: "myapex",
1591 key: "myapex.key",
1592 native_shared_libs: ["mylib"],
1593 uses: ["commonapex"],
1594 }
1595
1596 apex {
1597 name: "commonapex",
1598 key: "myapex.key",
1599 native_shared_libs: ["libcommon"],
1600 provide_cpp_shared_libs: true,
1601 }
1602
1603 apex_key {
1604 name: "myapex.key",
1605 public_key: "testkey.avbpubkey",
1606 private_key: "testkey.pem",
1607 }
1608
1609 cc_library {
1610 name: "mylib",
1611 srcs: ["mylib.cpp"],
1612 shared_libs: ["libcommon"],
1613 system_shared_libs: [],
1614 stl: "none",
1615 }
1616
1617 cc_library {
1618 name: "libcommon",
1619 srcs: ["mylib_common.cpp"],
1620 system_shared_libs: [],
1621 stl: "none",
1622 }
1623 `)
1624
1625 module1 := ctx.ModuleForTests("myapex", "android_common_myapex")
1626 apexRule1 := module1.Rule("apexRule")
1627 copyCmds1 := apexRule1.Args["copy_commands"]
1628
1629 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex")
1630 apexRule2 := module2.Rule("apexRule")
1631 copyCmds2 := apexRule2.Args["copy_commands"]
1632
1633 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1634 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_core_shared_commonapex")
1635 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
1636 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
1637 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
1638}
1639
1640func TestApexUsesFailsIfNotProvided(t *testing.T) {
1641 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
1642 apex {
1643 name: "myapex",
1644 key: "myapex.key",
1645 uses: ["commonapex"],
1646 }
1647
1648 apex {
1649 name: "commonapex",
1650 key: "myapex.key",
1651 }
1652
1653 apex_key {
1654 name: "myapex.key",
1655 public_key: "testkey.avbpubkey",
1656 private_key: "testkey.pem",
1657 }
1658 `)
1659 testApexError(t, `uses: "commonapex" is not a provider`, `
1660 apex {
1661 name: "myapex",
1662 key: "myapex.key",
1663 uses: ["commonapex"],
1664 }
1665
1666 cc_library {
1667 name: "commonapex",
1668 system_shared_libs: [],
1669 stl: "none",
1670 }
1671
1672 apex_key {
1673 name: "myapex.key",
1674 public_key: "testkey.avbpubkey",
1675 private_key: "testkey.pem",
1676 }
1677 `)
1678}
1679
1680func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
1681 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
1682 apex {
1683 name: "myapex",
1684 key: "myapex.key",
1685 use_vendor: true,
1686 uses: ["commonapex"],
1687 }
1688
1689 apex {
1690 name: "commonapex",
1691 key: "myapex.key",
1692 provide_cpp_shared_libs: true,
1693 }
1694
1695 apex_key {
1696 name: "myapex.key",
1697 public_key: "testkey.avbpubkey",
1698 private_key: "testkey.pem",
1699 }
1700 `)
1701}
1702
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07001703func TestMain(m *testing.M) {
1704 run := func() int {
1705 setUp()
1706 defer tearDown()
1707
1708 return m.Run()
1709 }
1710
1711 os.Exit(run())
1712}