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