blob: 38d2bf2f75f78b04ae7af3dfd5deb10bc01b04c4 [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))
95
Jaewoong Jung939ebd52019-03-26 15:07:36 -070096 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
97 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
98 })
Jiyong Park25fc6a92018-11-18 18:02:45 +090099 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900100 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900101 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900102 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Roland Levillain9b5fde92019-06-28 15:41:19 +0100103 ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900104 ctx.BottomUp("version", cc.VersionMutator).Parallel()
105 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
106 })
107
108 ctx.Register()
109
110 bp = bp + `
111 toolchain_library {
112 name: "libcompiler_rt-extras",
113 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900114 vendor_available: true,
115 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900116 }
117
118 toolchain_library {
119 name: "libatomic",
120 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900121 vendor_available: true,
122 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900123 }
124
125 toolchain_library {
126 name: "libgcc",
127 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900128 vendor_available: true,
129 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900130 }
131
132 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700133 name: "libgcc_stripped",
134 src: "",
135 vendor_available: true,
136 recovery_available: true,
137 }
138
139 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900140 name: "libclang_rt.builtins-aarch64-android",
141 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900142 vendor_available: true,
143 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900144 }
145
146 toolchain_library {
147 name: "libclang_rt.builtins-arm-android",
148 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900149 vendor_available: true,
150 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900151 }
152
153 cc_object {
154 name: "crtbegin_so",
155 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900156 vendor_available: true,
157 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900158 }
159
160 cc_object {
161 name: "crtend_so",
162 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900163 vendor_available: true,
164 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900165 }
166
Alex Light3d673592019-01-18 14:37:31 -0800167 cc_object {
168 name: "crtbegin_static",
169 stl: "none",
170 }
171
172 cc_object {
173 name: "crtend_android",
174 stl: "none",
175 }
176
Jiyong Parkda6eb592018-12-19 17:12:36 +0900177 llndk_library {
178 name: "libc",
179 symbol_file: "",
180 }
181
182 llndk_library {
183 name: "libm",
184 symbol_file: "",
185 }
186
187 llndk_library {
188 name: "libdl",
189 symbol_file: "",
190 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900191 `
192
193 ctx.MockFileSystem(map[string][]byte{
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900194 "Android.bp": []byte(bp),
Dan Willemsen412160e2019-04-09 21:36:26 -0700195 "build/make/target/product/security": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900196 "apex_manifest.json": nil,
Jiyong Park809bb722019-02-13 21:33:49 +0900197 "AndroidManifest.xml": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900198 "system/sepolicy/apex/myapex-file_contexts": nil,
199 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
200 "system/sepolicy/apex/otherapex-file_contexts": nil,
Jooyung Han5c998b92019-06-27 11:30:33 +0900201 "system/sepolicy/apex/commonapex-file_contexts": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900202 "mylib.cpp": nil,
Jooyung Han5c998b92019-06-27 11:30:33 +0900203 "mylib_common.cpp": nil,
Roland Levillain9b5fde92019-06-28 15:41:19 +0100204 "mytest.cpp": nil,
205 "mytest1.cpp": nil,
206 "mytest2.cpp": nil,
207 "mytest3.cpp": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900208 "myprebuilt": nil,
209 "my_include": nil,
Jiyong Park7f7766d2019-07-25 22:02:35 +0900210 "foo/bar/MyClass.java": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900211 "vendor/foo/devkeys/test.x509.pem": nil,
212 "vendor/foo/devkeys/test.pk8": nil,
213 "testkey.x509.pem": nil,
214 "testkey.pk8": nil,
215 "testkey.override.x509.pem": nil,
216 "testkey.override.pk8": nil,
217 "vendor/foo/devkeys/testkey.avbpubkey": nil,
218 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park52818fc2019-03-18 12:01:38 +0900219 "NOTICE": nil,
220 "custom_notice": nil,
Jiyong Park67882562019-03-21 01:11:21 +0900221 "testkey2.avbpubkey": nil,
222 "testkey2.pem": nil,
Jiyong Parkc95714e2019-03-29 14:23:10 +0900223 "myapex-arm64.apex": nil,
224 "myapex-arm.apex": nil,
Jiyong Park71b519d2019-04-18 17:25:49 +0900225 "frameworks/base/api/current.txt": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900226 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900227
Jooyung Han5c998b92019-06-27 11:30:33 +0900228 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229}
230
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700231func setUp() {
232 var err error
233 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900234 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700235 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900236 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900237}
238
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700239func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900240 os.RemoveAll(buildDir)
241}
242
243// ensure that 'result' contains 'expected'
244func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900245 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900246 if !strings.Contains(result, expected) {
247 t.Errorf("%q is not found in %q", expected, result)
248 }
249}
250
251// ensures that 'result' does not contain 'notExpected'
252func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900253 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900254 if strings.Contains(result, notExpected) {
255 t.Errorf("%q is found in %q", notExpected, result)
256 }
257}
258
259func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900260 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261 if !android.InList(expected, result) {
262 t.Errorf("%q is not found in %v", expected, result)
263 }
264}
265
266func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900267 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900268 if android.InList(notExpected, result) {
269 t.Errorf("%q is found in %v", notExpected, result)
270 }
271}
272
Jooyung Hane1633032019-08-01 17:41:43 +0900273func ensureListEmpty(t *testing.T, result []string) {
274 t.Helper()
275 if len(result) > 0 {
276 t.Errorf("%q is expected to be empty", result)
277 }
278}
279
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280// Minimal test
281func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700282 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900283 apex_defaults {
284 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900285 manifest: ":myapex.manifest",
286 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 key: "myapex.key",
288 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800289 multilib: {
290 both: {
291 binaries: ["foo",],
292 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900293 },
294 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900295 }
296
Jiyong Park30ca9372019-02-07 16:27:23 +0900297 apex {
298 name: "myapex",
299 defaults: ["myapex-defaults"],
300 }
301
Jiyong Park25fc6a92018-11-18 18:02:45 +0900302 apex_key {
303 name: "myapex.key",
304 public_key: "testkey.avbpubkey",
305 private_key: "testkey.pem",
306 }
307
Jiyong Park809bb722019-02-13 21:33:49 +0900308 filegroup {
309 name: "myapex.manifest",
310 srcs: ["apex_manifest.json"],
311 }
312
313 filegroup {
314 name: "myapex.androidmanifest",
315 srcs: ["AndroidManifest.xml"],
316 }
317
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318 cc_library {
319 name: "mylib",
320 srcs: ["mylib.cpp"],
321 shared_libs: ["mylib2"],
322 system_shared_libs: [],
323 stl: "none",
324 }
325
Alex Light3d673592019-01-18 14:37:31 -0800326 cc_binary {
327 name: "foo",
328 srcs: ["mylib.cpp"],
329 compile_multilib: "both",
330 multilib: {
331 lib32: {
332 suffix: "32",
333 },
334 lib64: {
335 suffix: "64",
336 },
337 },
338 symlinks: ["foo_link_"],
339 symlink_preferred_arch: true,
340 system_shared_libs: [],
341 static_executable: true,
342 stl: "none",
343 }
344
Jiyong Park25fc6a92018-11-18 18:02:45 +0900345 cc_library {
346 name: "mylib2",
347 srcs: ["mylib.cpp"],
348 system_shared_libs: [],
349 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900350 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900351 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900352
353 java_library {
354 name: "myjar",
355 srcs: ["foo/bar/MyClass.java"],
356 sdk_version: "none",
357 system_modules: "none",
358 compile_dex: true,
359 static_libs: ["myotherjar"],
360 }
361
362 java_library {
363 name: "myotherjar",
364 srcs: ["foo/bar/MyClass.java"],
365 sdk_version: "none",
366 system_modules: "none",
367 compile_dex: true,
368 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 `)
370
371 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900372
373 optFlags := apexRule.Args["opt_flags"]
374 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700375 // Ensure that the NOTICE output is being packaged as an asset.
376 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900377
Jiyong Park25fc6a92018-11-18 18:02:45 +0900378 copyCmds := apexRule.Args["copy_commands"]
379
380 // Ensure that main rule creates an output
381 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
382
383 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900384 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900385 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900386
387 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900388 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900389 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900390
391 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800392 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
393 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900394 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
395 // .. but not for java libs
396 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800397
Jiyong Park7f7766d2019-07-25 22:02:35 +0900398 // Ensure that the platform variant ends with _core_shared or _common
Logan Chien3aeedc92018-12-26 15:32:21 +0800399 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
400 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900401 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
402 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800403
404 // Ensure that all symlinks are present.
405 found_foo_link_64 := false
406 found_foo := false
407 for _, cmd := range strings.Split(copyCmds, " && ") {
408 if strings.HasPrefix(cmd, "ln -s foo64") {
409 if strings.HasSuffix(cmd, "bin/foo") {
410 found_foo = true
411 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
412 found_foo_link_64 = true
413 }
414 }
415 }
416 good := found_foo && found_foo_link_64
417 if !good {
418 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
419 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900420
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700421 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("mergeNoticesRule")
422 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700423 if len(noticeInputs) != 2 {
424 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900425 }
426 ensureListContains(t, noticeInputs, "NOTICE")
427 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800428}
429
430func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700431 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800432 apex {
433 name: "myapex",
434 key: "myapex.key",
435 payload_type: "zip",
436 native_shared_libs: ["mylib"],
437 }
438
439 apex_key {
440 name: "myapex.key",
441 public_key: "testkey.avbpubkey",
442 private_key: "testkey.pem",
443 }
444
445 cc_library {
446 name: "mylib",
447 srcs: ["mylib.cpp"],
448 shared_libs: ["mylib2"],
449 system_shared_libs: [],
450 stl: "none",
451 }
452
453 cc_library {
454 name: "mylib2",
455 srcs: ["mylib.cpp"],
456 system_shared_libs: [],
457 stl: "none",
458 }
459 `)
460
461 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
462 copyCmds := zipApexRule.Args["copy_commands"]
463
464 // Ensure that main rule creates an output
465 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
466
467 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900468 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800469
470 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900471 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800472
473 // Ensure that both direct and indirect deps are copied into apex
474 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
475 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900476}
477
478func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700479 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900480 apex {
481 name: "myapex",
482 key: "myapex.key",
483 native_shared_libs: ["mylib", "mylib3"],
484 }
485
486 apex_key {
487 name: "myapex.key",
488 public_key: "testkey.avbpubkey",
489 private_key: "testkey.pem",
490 }
491
492 cc_library {
493 name: "mylib",
494 srcs: ["mylib.cpp"],
495 shared_libs: ["mylib2", "mylib3"],
496 system_shared_libs: [],
497 stl: "none",
498 }
499
500 cc_library {
501 name: "mylib2",
502 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900503 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900504 system_shared_libs: [],
505 stl: "none",
506 stubs: {
507 versions: ["1", "2", "3"],
508 },
509 }
510
511 cc_library {
512 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900513 srcs: ["mylib.cpp"],
514 shared_libs: ["mylib4"],
515 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900516 stl: "none",
517 stubs: {
518 versions: ["10", "11", "12"],
519 },
520 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900521
522 cc_library {
523 name: "mylib4",
524 srcs: ["mylib.cpp"],
525 system_shared_libs: [],
526 stl: "none",
527 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900528 `)
529
530 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
531 copyCmds := apexRule.Args["copy_commands"]
532
533 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800534 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900535
536 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800537 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900538
539 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800540 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900541
Jiyong Parkda6eb592018-12-19 17:12:36 +0900542 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900543
544 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900545 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900546 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900547 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900548
549 // 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 +0900550 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900551 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900552 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900553
554 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900555 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900556 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900557
558 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900559 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 +0900560}
561
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900562func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700563 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900564 apex {
565 name: "myapex",
566 key: "myapex.key",
567 native_shared_libs: ["mylib"],
568 }
569
570 apex_key {
571 name: "myapex.key",
572 public_key: "testkey.avbpubkey",
573 private_key: "testkey.pem",
574 }
575
576 cc_library {
577 name: "mylib",
578 srcs: ["mylib.cpp"],
579 shared_libs: ["libfoo#10"],
580 system_shared_libs: [],
581 stl: "none",
582 }
583
584 cc_library {
585 name: "libfoo",
586 srcs: ["mylib.cpp"],
587 shared_libs: ["libbar"],
588 system_shared_libs: [],
589 stl: "none",
590 stubs: {
591 versions: ["10", "20", "30"],
592 },
593 }
594
595 cc_library {
596 name: "libbar",
597 srcs: ["mylib.cpp"],
598 system_shared_libs: [],
599 stl: "none",
600 }
601
602 `)
603
604 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
605 copyCmds := apexRule.Args["copy_commands"]
606
607 // Ensure that direct non-stubs dep is always included
608 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
609
610 // Ensure that indirect stubs dep is not included
611 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
612
613 // Ensure that dependency of stubs is not included
614 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
615
Jiyong Parkda6eb592018-12-19 17:12:36 +0900616 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900617
618 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900619 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900620 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900621 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900622
Jiyong Parkda6eb592018-12-19 17:12:36 +0900623 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900624
625 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
626 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
627}
628
Jiyong Park25fc6a92018-11-18 18:02:45 +0900629func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700630 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900631 apex {
632 name: "myapex",
633 key: "myapex.key",
634 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
635 }
636
637 apex_key {
638 name: "myapex.key",
639 public_key: "testkey.avbpubkey",
640 private_key: "testkey.pem",
641 }
642
643 cc_library {
644 name: "mylib",
645 srcs: ["mylib.cpp"],
646 shared_libs: ["libdl#27"],
647 stl: "none",
648 }
649
650 cc_library_shared {
651 name: "mylib_shared",
652 srcs: ["mylib.cpp"],
653 shared_libs: ["libdl#27"],
654 stl: "none",
655 }
656
657 cc_library {
658 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700659 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900660 nocrt: true,
661 system_shared_libs: [],
662 stl: "none",
663 stubs: {
664 versions: ["27", "28", "29"],
665 },
666 }
667
668 cc_library {
669 name: "libm",
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: "libdl",
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 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900689
690 cc_library {
691 name: "libBootstrap",
692 srcs: ["mylib.cpp"],
693 stl: "none",
694 bootstrap: true,
695 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900696 `)
697
698 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
699 copyCmds := apexRule.Args["copy_commands"]
700
701 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800702 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900703 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
704 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900705
706 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900707 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900708
Jiyong Parkda6eb592018-12-19 17:12:36 +0900709 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
710 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
711 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900712
713 // For dependency to libc
714 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900715 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900716 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900717 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900718 // ... Cflags from stub is correctly exported to mylib
719 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
720 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
721
722 // For dependency to libm
723 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900724 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900725 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900726 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900727 // ... and is not compiling with the stub
728 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
729 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
730
731 // For dependency to libdl
732 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900733 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900734 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900735 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
736 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900737 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900738 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900739 // ... Cflags from stub is correctly exported to mylib
740 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
741 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900742
743 // Ensure that libBootstrap is depending on the platform variant of bionic libs
744 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
745 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
746 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
747 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900748}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900749
750func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700751 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900752 apex {
753 name: "myapex",
754 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900755 native_shared_libs: ["mylib"],
756 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900757 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900758 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900759 }
760
761 apex_key {
762 name: "myapex.key",
763 public_key: "testkey.avbpubkey",
764 private_key: "testkey.pem",
765 }
766
767 prebuilt_etc {
768 name: "myetc",
769 src: "myprebuilt",
770 sub_dir: "foo/bar",
771 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900772
773 cc_library {
774 name: "mylib",
775 srcs: ["mylib.cpp"],
776 relative_install_path: "foo/bar",
777 system_shared_libs: [],
778 stl: "none",
779 }
780
781 cc_binary {
782 name: "mybin",
783 srcs: ["mylib.cpp"],
784 relative_install_path: "foo/bar",
785 system_shared_libs: [],
786 static_executable: true,
787 stl: "none",
788 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900789 `)
790
791 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
792 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
793
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900794 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900795 ensureListContains(t, dirs, "etc")
796 ensureListContains(t, dirs, "etc/foo")
797 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900798 ensureListContains(t, dirs, "lib64")
799 ensureListContains(t, dirs, "lib64/foo")
800 ensureListContains(t, dirs, "lib64/foo/bar")
801 ensureListContains(t, dirs, "lib")
802 ensureListContains(t, dirs, "lib/foo")
803 ensureListContains(t, dirs, "lib/foo/bar")
804
Jiyong Parkbd13e442019-03-15 18:10:35 +0900805 ensureListContains(t, dirs, "bin")
806 ensureListContains(t, dirs, "bin/foo")
807 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900808}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900809
810func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700811 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +0900812 apex {
813 name: "myapex",
814 key: "myapex.key",
815 native_shared_libs: ["mylib"],
816 use_vendor: true,
817 }
818
819 apex_key {
820 name: "myapex.key",
821 public_key: "testkey.avbpubkey",
822 private_key: "testkey.pem",
823 }
824
825 cc_library {
826 name: "mylib",
827 srcs: ["mylib.cpp"],
828 shared_libs: ["mylib2"],
829 system_shared_libs: [],
830 vendor_available: true,
831 stl: "none",
832 }
833
834 cc_library {
835 name: "mylib2",
836 srcs: ["mylib.cpp"],
837 system_shared_libs: [],
838 vendor_available: true,
839 stl: "none",
840 }
841 `)
842
843 inputsList := []string{}
844 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
845 for _, implicit := range i.Implicits {
846 inputsList = append(inputsList, implicit.String())
847 }
848 }
849 inputsString := strings.Join(inputsList, " ")
850
851 // ensure that the apex includes vendor variants of the direct and indirect deps
852 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
853 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
854
855 // ensure that the apex does not include core variants
856 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
857 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
858}
Jiyong Park16e91a02018-12-20 18:18:08 +0900859
Jooyung Han5c998b92019-06-27 11:30:33 +0900860func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
861 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
862 apex {
863 name: "myapex",
864 key: "myapex.key",
865 native_shared_libs: ["mylib"],
866 use_vendor: true,
867 }
868
869 apex_key {
870 name: "myapex.key",
871 public_key: "testkey.avbpubkey",
872 private_key: "testkey.pem",
873 }
874
875 cc_library {
876 name: "mylib",
877 srcs: ["mylib.cpp"],
878 system_shared_libs: [],
879 stl: "none",
880 }
881 `)
882}
883
Jiyong Park16e91a02018-12-20 18:18:08 +0900884func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700885 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +0900886 apex {
887 name: "myapex",
888 key: "myapex.key",
889 native_shared_libs: ["mylib"],
890 }
891
892 apex_key {
893 name: "myapex.key",
894 public_key: "testkey.avbpubkey",
895 private_key: "testkey.pem",
896 }
897
898 cc_library {
899 name: "mylib",
900 srcs: ["mylib.cpp"],
901 system_shared_libs: [],
902 stl: "none",
903 stubs: {
904 versions: ["1", "2", "3"],
905 },
906 }
907
908 cc_binary {
909 name: "not_in_apex",
910 srcs: ["mylib.cpp"],
911 static_libs: ["mylib"],
912 static_executable: true,
913 system_shared_libs: [],
914 stl: "none",
915 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900916 `)
917
918 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
919
920 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800921 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900922}
Jiyong Park9335a262018-12-24 11:31:58 +0900923
924func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700925 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +0900926 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900927 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900928 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900929 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900930 native_shared_libs: ["mylib"],
931 }
932
933 cc_library {
934 name: "mylib",
935 srcs: ["mylib.cpp"],
936 system_shared_libs: [],
937 stl: "none",
938 }
939
940 apex_key {
941 name: "myapex.key",
942 public_key: "testkey.avbpubkey",
943 private_key: "testkey.pem",
944 }
945
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900946 android_app_certificate {
947 name: "myapex.certificate",
948 certificate: "testkey",
949 }
950
951 android_app_certificate {
952 name: "myapex.certificate.override",
953 certificate: "testkey.override",
954 }
955
Jiyong Park9335a262018-12-24 11:31:58 +0900956 `)
957
958 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900959 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900960
961 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
962 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
963 "vendor/foo/devkeys/testkey.avbpubkey")
964 }
965 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
966 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
967 "vendor/foo/devkeys/testkey.pem")
968 }
969
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900970 // check the APK certs. It should be overridden to myapex.certificate.override
971 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
972 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900973 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900974 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900975 }
976}
Jiyong Park58e364a2019-01-19 19:24:06 +0900977
978func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700979 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +0900980 apex {
981 name: "myapex",
982 key: "myapex.key",
983 native_shared_libs: ["mylib"],
984 }
985
986 apex {
987 name: "otherapex",
988 key: "myapex.key",
989 native_shared_libs: ["mylib"],
990 }
991
992 apex_key {
993 name: "myapex.key",
994 public_key: "testkey.avbpubkey",
995 private_key: "testkey.pem",
996 }
997
998 cc_library {
999 name: "mylib",
1000 srcs: ["mylib.cpp"],
1001 system_shared_libs: [],
1002 stl: "none",
1003 }
1004 `)
1005
1006 // non-APEX variant does not have __ANDROID__APEX__ defined
1007 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1008 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1009 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
1010
1011 // APEX variant has __ANDROID_APEX__=<apexname> defined
1012 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
1013 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1014 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
1015
1016 // APEX variant has __ANDROID_APEX__=<apexname> defined
1017 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
1018 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
1019 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
1020}
Jiyong Park7e636d02019-01-28 16:16:54 +09001021
1022func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001023 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001024 apex {
1025 name: "myapex",
1026 key: "myapex.key",
1027 native_shared_libs: ["mylib"],
1028 }
1029
1030 apex_key {
1031 name: "myapex.key",
1032 public_key: "testkey.avbpubkey",
1033 private_key: "testkey.pem",
1034 }
1035
1036 cc_library_headers {
1037 name: "mylib_headers",
1038 export_include_dirs: ["my_include"],
1039 system_shared_libs: [],
1040 stl: "none",
1041 }
1042
1043 cc_library {
1044 name: "mylib",
1045 srcs: ["mylib.cpp"],
1046 system_shared_libs: [],
1047 stl: "none",
1048 header_libs: ["mylib_headers"],
1049 export_header_lib_headers: ["mylib_headers"],
1050 stubs: {
1051 versions: ["1", "2", "3"],
1052 },
1053 }
1054
1055 cc_library {
1056 name: "otherlib",
1057 srcs: ["mylib.cpp"],
1058 system_shared_libs: [],
1059 stl: "none",
1060 shared_libs: ["mylib"],
1061 }
1062 `)
1063
1064 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1065
1066 // Ensure that the include path of the header lib is exported to 'otherlib'
1067 ensureContains(t, cFlags, "-Imy_include")
1068}
Alex Light9670d332019-01-29 18:07:33 -08001069
Jooyung Hane1633032019-08-01 17:41:43 +09001070func TestDependenciesInApexManifest(t *testing.T) {
1071 ctx, _ := testApex(t, `
1072 apex {
1073 name: "myapex_nodep",
1074 key: "myapex.key",
1075 native_shared_libs: ["lib_nodep"],
1076 compile_multilib: "both",
1077 file_contexts: "myapex",
1078 }
1079
1080 apex {
1081 name: "myapex_dep",
1082 key: "myapex.key",
1083 native_shared_libs: ["lib_dep"],
1084 compile_multilib: "both",
1085 file_contexts: "myapex",
1086 }
1087
1088 apex {
1089 name: "myapex_provider",
1090 key: "myapex.key",
1091 native_shared_libs: ["libfoo"],
1092 compile_multilib: "both",
1093 file_contexts: "myapex",
1094 }
1095
1096 apex {
1097 name: "myapex_selfcontained",
1098 key: "myapex.key",
1099 native_shared_libs: ["lib_dep", "libfoo"],
1100 compile_multilib: "both",
1101 file_contexts: "myapex",
1102 }
1103
1104 apex_key {
1105 name: "myapex.key",
1106 public_key: "testkey.avbpubkey",
1107 private_key: "testkey.pem",
1108 }
1109
1110 cc_library {
1111 name: "lib_nodep",
1112 srcs: ["mylib.cpp"],
1113 system_shared_libs: [],
1114 stl: "none",
1115 }
1116
1117 cc_library {
1118 name: "lib_dep",
1119 srcs: ["mylib.cpp"],
1120 shared_libs: ["libfoo"],
1121 system_shared_libs: [],
1122 stl: "none",
1123 }
1124
1125 cc_library {
1126 name: "libfoo",
1127 srcs: ["mytest.cpp"],
1128 stubs: {
1129 versions: ["1"],
1130 },
1131 system_shared_libs: [],
1132 stl: "none",
1133 }
1134 `)
1135
1136 names := func(s string) (ns []string) {
1137 for _, n := range strings.Split(s, " ") {
1138 if len(n) > 0 {
1139 ns = append(ns, n)
1140 }
1141 }
1142 return
1143 }
1144
1145 var injectRule android.TestingBuildParams
1146 var provideNativeLibs, requireNativeLibs []string
1147
1148 injectRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("injectApexDependency")
1149 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1150 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1151 ensureListEmpty(t, provideNativeLibs)
1152 ensureListEmpty(t, requireNativeLibs)
1153
1154 injectRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("injectApexDependency")
1155 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1156 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1157 ensureListEmpty(t, provideNativeLibs)
1158 ensureListContains(t, requireNativeLibs, "libfoo.so")
1159
1160 injectRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("injectApexDependency")
1161 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1162 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1163 ensureListContains(t, provideNativeLibs, "libfoo.so")
1164 ensureListEmpty(t, requireNativeLibs)
1165
1166 injectRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("injectApexDependency")
1167 provideNativeLibs = names(injectRule.Args["provideNativeLibs"])
1168 requireNativeLibs = names(injectRule.Args["requireNativeLibs"])
1169 ensureListContains(t, provideNativeLibs, "libfoo.so")
1170 ensureListEmpty(t, requireNativeLibs)
1171}
1172
Alex Light0851b882019-02-07 13:20:53 -08001173func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001174 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001175 apex {
1176 name: "myapex",
1177 key: "myapex.key",
1178 native_shared_libs: ["mylib_common"],
1179 }
1180
1181 apex_key {
1182 name: "myapex.key",
1183 public_key: "testkey.avbpubkey",
1184 private_key: "testkey.pem",
1185 }
1186
1187 cc_library {
1188 name: "mylib_common",
1189 srcs: ["mylib.cpp"],
1190 system_shared_libs: [],
1191 stl: "none",
1192 }
1193 `)
1194
1195 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1196 apexRule := module.Rule("apexRule")
1197 copyCmds := apexRule.Args["copy_commands"]
1198
1199 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1200 t.Log("Apex was a test apex!")
1201 t.Fail()
1202 }
1203 // Ensure that main rule creates an output
1204 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1205
1206 // Ensure that apex variant is created for the direct dep
1207 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1208
1209 // Ensure that both direct and indirect deps are copied into apex
1210 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1211
1212 // Ensure that the platform variant ends with _core_shared
1213 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1214
1215 if !android.InAnyApex("mylib_common") {
1216 t.Log("Found mylib_common not in any apex!")
1217 t.Fail()
1218 }
1219}
1220
1221func TestTestApex(t *testing.T) {
1222 if android.InAnyApex("mylib_common_test") {
1223 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!")
1224 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001225 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001226 apex_test {
1227 name: "myapex",
1228 key: "myapex.key",
1229 native_shared_libs: ["mylib_common_test"],
1230 }
1231
1232 apex_key {
1233 name: "myapex.key",
1234 public_key: "testkey.avbpubkey",
1235 private_key: "testkey.pem",
1236 }
1237
1238 cc_library {
1239 name: "mylib_common_test",
1240 srcs: ["mylib.cpp"],
1241 system_shared_libs: [],
1242 stl: "none",
1243 }
1244 `)
1245
1246 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1247 apexRule := module.Rule("apexRule")
1248 copyCmds := apexRule.Args["copy_commands"]
1249
1250 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1251 t.Log("Apex was not a test apex!")
1252 t.Fail()
1253 }
1254 // Ensure that main rule creates an output
1255 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1256
1257 // Ensure that apex variant is created for the direct dep
1258 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1259
1260 // Ensure that both direct and indirect deps are copied into apex
1261 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1262
1263 // Ensure that the platform variant ends with _core_shared
1264 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1265
1266 if android.InAnyApex("mylib_common_test") {
1267 t.Log("Found mylib_common_test in some apex!")
1268 t.Fail()
1269 }
1270}
1271
Alex Light9670d332019-01-29 18:07:33 -08001272func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001273 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08001274 apex {
1275 name: "myapex",
1276 key: "myapex.key",
1277 multilib: {
1278 first: {
1279 native_shared_libs: ["mylib_common"],
1280 }
1281 },
1282 target: {
1283 android: {
1284 multilib: {
1285 first: {
1286 native_shared_libs: ["mylib"],
1287 }
1288 }
1289 },
1290 host: {
1291 multilib: {
1292 first: {
1293 native_shared_libs: ["mylib2"],
1294 }
1295 }
1296 }
1297 }
1298 }
1299
1300 apex_key {
1301 name: "myapex.key",
1302 public_key: "testkey.avbpubkey",
1303 private_key: "testkey.pem",
1304 }
1305
1306 cc_library {
1307 name: "mylib",
1308 srcs: ["mylib.cpp"],
1309 system_shared_libs: [],
1310 stl: "none",
1311 }
1312
1313 cc_library {
1314 name: "mylib_common",
1315 srcs: ["mylib.cpp"],
1316 system_shared_libs: [],
1317 stl: "none",
1318 compile_multilib: "first",
1319 }
1320
1321 cc_library {
1322 name: "mylib2",
1323 srcs: ["mylib.cpp"],
1324 system_shared_libs: [],
1325 stl: "none",
1326 compile_multilib: "first",
1327 }
1328 `)
1329
1330 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1331 copyCmds := apexRule.Args["copy_commands"]
1332
1333 // Ensure that main rule creates an output
1334 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1335
1336 // Ensure that apex variant is created for the direct dep
1337 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1338 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1339 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1340
1341 // Ensure that both direct and indirect deps are copied into apex
1342 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1343 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1344 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1345
1346 // Ensure that the platform variant ends with _core_shared
1347 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1348 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1349 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1350}
Jiyong Park04480cf2019-02-06 00:16:29 +09001351
1352func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001353 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09001354 apex {
1355 name: "myapex",
1356 key: "myapex.key",
1357 binaries: ["myscript"],
1358 }
1359
1360 apex_key {
1361 name: "myapex.key",
1362 public_key: "testkey.avbpubkey",
1363 private_key: "testkey.pem",
1364 }
1365
1366 sh_binary {
1367 name: "myscript",
1368 src: "mylib.cpp",
1369 filename: "myscript.sh",
1370 sub_dir: "script",
1371 }
1372 `)
1373
1374 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1375 copyCmds := apexRule.Args["copy_commands"]
1376
1377 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1378}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001379
1380func TestApexInProductPartition(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001381 ctx, _ := testApex(t, `
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001382 apex {
1383 name: "myapex",
1384 key: "myapex.key",
1385 native_shared_libs: ["mylib"],
1386 product_specific: true,
1387 }
1388
1389 apex_key {
1390 name: "myapex.key",
1391 public_key: "testkey.avbpubkey",
1392 private_key: "testkey.pem",
1393 product_specific: true,
1394 }
1395
1396 cc_library {
1397 name: "mylib",
1398 srcs: ["mylib.cpp"],
1399 system_shared_libs: [],
1400 stl: "none",
1401 }
1402 `)
1403
1404 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1405 expected := "target/product/test_device/product/apex"
1406 actual := apex.installDir.RelPathString()
1407 if actual != expected {
1408 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1409 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001410}
Jiyong Park67882562019-03-21 01:11:21 +09001411
1412func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001413 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09001414 apex_key {
1415 name: "myapex.key",
1416 public_key: ":my.avbpubkey",
1417 private_key: ":my.pem",
1418 product_specific: true,
1419 }
1420
1421 filegroup {
1422 name: "my.avbpubkey",
1423 srcs: ["testkey2.avbpubkey"],
1424 }
1425
1426 filegroup {
1427 name: "my.pem",
1428 srcs: ["testkey2.pem"],
1429 }
1430 `)
1431
1432 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1433 expected_pubkey := "testkey2.avbpubkey"
1434 actual_pubkey := apex_key.public_key_file.String()
1435 if actual_pubkey != expected_pubkey {
1436 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1437 }
1438 expected_privkey := "testkey2.pem"
1439 actual_privkey := apex_key.private_key_file.String()
1440 if actual_privkey != expected_privkey {
1441 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1442 }
1443}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001444
1445func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001446 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001447 prebuilt_apex {
1448 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09001449 arch: {
1450 arm64: {
1451 src: "myapex-arm64.apex",
1452 },
1453 arm: {
1454 src: "myapex-arm.apex",
1455 },
1456 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001457 }
1458 `)
1459
1460 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1461
Jiyong Parkc95714e2019-03-29 14:23:10 +09001462 expectedInput := "myapex-arm64.apex"
1463 if prebuilt.inputApex.String() != expectedInput {
1464 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
1465 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001466}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001467
1468func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001469 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001470 prebuilt_apex {
1471 name: "myapex",
1472 src: "myapex-arm.apex",
1473 filename: "notmyapex.apex",
1474 }
1475 `)
1476
1477 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1478
1479 expected := "notmyapex.apex"
1480 if p.installFilename != expected {
1481 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
1482 }
1483}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07001484
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001485func TestPrebuiltOverrides(t *testing.T) {
1486 ctx, config := testApex(t, `
1487 prebuilt_apex {
1488 name: "myapex.prebuilt",
1489 src: "myapex-arm.apex",
1490 overrides: [
1491 "myapex",
1492 ],
1493 }
1494 `)
1495
1496 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
1497
1498 expected := []string{"myapex"}
1499 actual := android.AndroidMkEntriesForTest(t, config, "", p).EntryMap["LOCAL_OVERRIDES_PACKAGES"]
1500 if !reflect.DeepEqual(actual, expected) {
1501 t.Errorf("Incorrect LOCAL_OVERRIDES_PACKAGES value '%s', expected '%s'", actual, expected)
1502 }
1503}
1504
Roland Levillain630846d2019-06-26 12:48:34 +01001505func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01001506 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01001507 apex_test {
1508 name: "myapex",
1509 key: "myapex.key",
1510 tests: [
1511 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01001512 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01001513 ],
1514 }
1515
1516 apex_key {
1517 name: "myapex.key",
1518 public_key: "testkey.avbpubkey",
1519 private_key: "testkey.pem",
1520 }
1521
1522 cc_test {
1523 name: "mytest",
1524 gtest: false,
1525 srcs: ["mytest.cpp"],
1526 relative_install_path: "test",
1527 system_shared_libs: [],
1528 static_executable: true,
1529 stl: "none",
1530 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01001531
1532 cc_test {
1533 name: "mytests",
1534 gtest: false,
1535 srcs: [
1536 "mytest1.cpp",
1537 "mytest2.cpp",
1538 "mytest3.cpp",
1539 ],
1540 test_per_src: true,
1541 relative_install_path: "test",
1542 system_shared_libs: [],
1543 static_executable: true,
1544 stl: "none",
1545 }
Roland Levillain630846d2019-06-26 12:48:34 +01001546 `)
1547
1548 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1549 copyCmds := apexRule.Args["copy_commands"]
1550
1551 // Ensure that test dep is copied into apex.
1552 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01001553
1554 // Ensure that test deps built with `test_per_src` are copied into apex.
1555 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
1556 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
1557 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01001558
1559 // Ensure the module is correctly translated.
1560 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1561 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
1562 name := apexBundle.BaseModuleName()
1563 prefix := "TARGET_"
1564 var builder strings.Builder
1565 data.Custom(&builder, name, prefix, "", data)
1566 androidMk := builder.String()
1567 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest\n")
1568 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest1\n")
1569 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest2\n")
1570 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.mytest3\n")
1571 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.apex_manifest.json\n")
1572 ensureContains(t, androidMk, "LOCAL_MODULE := myapex.apex_pubkey\n")
1573 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01001574}
1575
Jooyung Han5c998b92019-06-27 11:30:33 +09001576func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001577 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09001578 apex {
1579 name: "myapex",
1580 key: "myapex.key",
1581 native_shared_libs: ["mylib"],
1582 uses: ["commonapex"],
1583 }
1584
1585 apex {
1586 name: "commonapex",
1587 key: "myapex.key",
1588 native_shared_libs: ["libcommon"],
1589 provide_cpp_shared_libs: true,
1590 }
1591
1592 apex_key {
1593 name: "myapex.key",
1594 public_key: "testkey.avbpubkey",
1595 private_key: "testkey.pem",
1596 }
1597
1598 cc_library {
1599 name: "mylib",
1600 srcs: ["mylib.cpp"],
1601 shared_libs: ["libcommon"],
1602 system_shared_libs: [],
1603 stl: "none",
1604 }
1605
1606 cc_library {
1607 name: "libcommon",
1608 srcs: ["mylib_common.cpp"],
1609 system_shared_libs: [],
1610 stl: "none",
1611 }
1612 `)
1613
1614 module1 := ctx.ModuleForTests("myapex", "android_common_myapex")
1615 apexRule1 := module1.Rule("apexRule")
1616 copyCmds1 := apexRule1.Args["copy_commands"]
1617
1618 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex")
1619 apexRule2 := module2.Rule("apexRule")
1620 copyCmds2 := apexRule2.Args["copy_commands"]
1621
1622 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1623 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_core_shared_commonapex")
1624 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
1625 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
1626 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
1627}
1628
1629func TestApexUsesFailsIfNotProvided(t *testing.T) {
1630 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
1631 apex {
1632 name: "myapex",
1633 key: "myapex.key",
1634 uses: ["commonapex"],
1635 }
1636
1637 apex {
1638 name: "commonapex",
1639 key: "myapex.key",
1640 }
1641
1642 apex_key {
1643 name: "myapex.key",
1644 public_key: "testkey.avbpubkey",
1645 private_key: "testkey.pem",
1646 }
1647 `)
1648 testApexError(t, `uses: "commonapex" is not a provider`, `
1649 apex {
1650 name: "myapex",
1651 key: "myapex.key",
1652 uses: ["commonapex"],
1653 }
1654
1655 cc_library {
1656 name: "commonapex",
1657 system_shared_libs: [],
1658 stl: "none",
1659 }
1660
1661 apex_key {
1662 name: "myapex.key",
1663 public_key: "testkey.avbpubkey",
1664 private_key: "testkey.pem",
1665 }
1666 `)
1667}
1668
1669func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
1670 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
1671 apex {
1672 name: "myapex",
1673 key: "myapex.key",
1674 use_vendor: true,
1675 uses: ["commonapex"],
1676 }
1677
1678 apex {
1679 name: "commonapex",
1680 key: "myapex.key",
1681 provide_cpp_shared_libs: true,
1682 }
1683
1684 apex_key {
1685 name: "myapex.key",
1686 public_key: "testkey.avbpubkey",
1687 private_key: "testkey.pem",
1688 }
1689 `)
1690}
1691
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07001692func TestMain(m *testing.M) {
1693 run := func() int {
1694 setUp()
1695 defer tearDown()
1696
1697 return m.Run()
1698 }
1699
1700 os.Exit(run())
1701}