blob: 2d9cca6ff11ece9cdbaa0d337f40f91b3409cb80 [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,
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700174 "myapex.apex": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900175 })
176 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
177 android.FailIfErrored(t, errs)
178 _, errs = ctx.PrepareBuildActions(config)
179 android.FailIfErrored(t, errs)
180
181 return ctx
182}
183
184func setup(t *testing.T) (config android.Config, buildDir string) {
185 buildDir, err := ioutil.TempDir("", "soong_apex_test")
186 if err != nil {
187 t.Fatal(err)
188 }
189
190 config = android.TestArchConfig(buildDir, nil)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900191 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
Jiyong Park9335a262018-12-24 11:31:58 +0900192 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900193 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
Jiyong Park25fc6a92018-11-18 18:02:45 +0900194 return
195}
196
197func teardown(buildDir string) {
198 os.RemoveAll(buildDir)
199}
200
201// ensure that 'result' contains 'expected'
202func ensureContains(t *testing.T, result string, expected string) {
203 if !strings.Contains(result, expected) {
204 t.Errorf("%q is not found in %q", expected, result)
205 }
206}
207
208// ensures that 'result' does not contain 'notExpected'
209func ensureNotContains(t *testing.T, result string, notExpected string) {
210 if strings.Contains(result, notExpected) {
211 t.Errorf("%q is found in %q", notExpected, result)
212 }
213}
214
215func ensureListContains(t *testing.T, result []string, expected string) {
216 if !android.InList(expected, result) {
217 t.Errorf("%q is not found in %v", expected, result)
218 }
219}
220
221func ensureListNotContains(t *testing.T, result []string, notExpected string) {
222 if android.InList(notExpected, result) {
223 t.Errorf("%q is found in %v", notExpected, result)
224 }
225}
226
227// Minimal test
228func TestBasicApex(t *testing.T) {
229 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900230 apex_defaults {
231 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900232 manifest: ":myapex.manifest",
233 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900234 key: "myapex.key",
235 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800236 multilib: {
237 both: {
238 binaries: ["foo",],
239 }
240 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900241 }
242
Jiyong Park30ca9372019-02-07 16:27:23 +0900243 apex {
244 name: "myapex",
245 defaults: ["myapex-defaults"],
246 }
247
Jiyong Park25fc6a92018-11-18 18:02:45 +0900248 apex_key {
249 name: "myapex.key",
250 public_key: "testkey.avbpubkey",
251 private_key: "testkey.pem",
252 }
253
Jiyong Park809bb722019-02-13 21:33:49 +0900254 filegroup {
255 name: "myapex.manifest",
256 srcs: ["apex_manifest.json"],
257 }
258
259 filegroup {
260 name: "myapex.androidmanifest",
261 srcs: ["AndroidManifest.xml"],
262 }
263
Jiyong Park25fc6a92018-11-18 18:02:45 +0900264 cc_library {
265 name: "mylib",
266 srcs: ["mylib.cpp"],
267 shared_libs: ["mylib2"],
268 system_shared_libs: [],
269 stl: "none",
270 }
271
Alex Light3d673592019-01-18 14:37:31 -0800272 cc_binary {
273 name: "foo",
274 srcs: ["mylib.cpp"],
275 compile_multilib: "both",
276 multilib: {
277 lib32: {
278 suffix: "32",
279 },
280 lib64: {
281 suffix: "64",
282 },
283 },
284 symlinks: ["foo_link_"],
285 symlink_preferred_arch: true,
286 system_shared_libs: [],
287 static_executable: true,
288 stl: "none",
289 }
290
Jiyong Park25fc6a92018-11-18 18:02:45 +0900291 cc_library {
292 name: "mylib2",
293 srcs: ["mylib.cpp"],
294 system_shared_libs: [],
295 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900296 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900297 }
298 `)
299
300 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
301 copyCmds := apexRule.Args["copy_commands"]
302
303 // Ensure that main rule creates an output
304 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
305
306 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900307 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308
309 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900310 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900311
312 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800313 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
314 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800315
316 // Ensure that the platform variant ends with _core_shared
317 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
318 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800319
320 // Ensure that all symlinks are present.
321 found_foo_link_64 := false
322 found_foo := false
323 for _, cmd := range strings.Split(copyCmds, " && ") {
324 if strings.HasPrefix(cmd, "ln -s foo64") {
325 if strings.HasSuffix(cmd, "bin/foo") {
326 found_foo = true
327 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
328 found_foo_link_64 = true
329 }
330 }
331 }
332 good := found_foo && found_foo_link_64
333 if !good {
334 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
335 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900336
337 apexMergeNoticeRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexMergeNoticeRule")
338 noticeInputs := strings.Split(apexMergeNoticeRule.Args["inputs"], " ")
339 if len(noticeInputs) != 3 {
340 t.Errorf("number of input notice files: expected = 3, actual = %d", len(noticeInputs))
341 }
342 ensureListContains(t, noticeInputs, "NOTICE")
343 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800344}
345
346func TestBasicZipApex(t *testing.T) {
347 ctx := testApex(t, `
348 apex {
349 name: "myapex",
350 key: "myapex.key",
351 payload_type: "zip",
352 native_shared_libs: ["mylib"],
353 }
354
355 apex_key {
356 name: "myapex.key",
357 public_key: "testkey.avbpubkey",
358 private_key: "testkey.pem",
359 }
360
361 cc_library {
362 name: "mylib",
363 srcs: ["mylib.cpp"],
364 shared_libs: ["mylib2"],
365 system_shared_libs: [],
366 stl: "none",
367 }
368
369 cc_library {
370 name: "mylib2",
371 srcs: ["mylib.cpp"],
372 system_shared_libs: [],
373 stl: "none",
374 }
375 `)
376
377 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
378 copyCmds := zipApexRule.Args["copy_commands"]
379
380 // Ensure that main rule creates an output
381 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.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")
Alex Light5098a612018-11-29 17:12:15 -0800385
386 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900387 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800388
389 // Ensure that both direct and indirect deps are copied into apex
390 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
391 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900392}
393
394func TestApexWithStubs(t *testing.T) {
395 ctx := testApex(t, `
396 apex {
397 name: "myapex",
398 key: "myapex.key",
399 native_shared_libs: ["mylib", "mylib3"],
400 }
401
402 apex_key {
403 name: "myapex.key",
404 public_key: "testkey.avbpubkey",
405 private_key: "testkey.pem",
406 }
407
408 cc_library {
409 name: "mylib",
410 srcs: ["mylib.cpp"],
411 shared_libs: ["mylib2", "mylib3"],
412 system_shared_libs: [],
413 stl: "none",
414 }
415
416 cc_library {
417 name: "mylib2",
418 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900419 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900420 system_shared_libs: [],
421 stl: "none",
422 stubs: {
423 versions: ["1", "2", "3"],
424 },
425 }
426
427 cc_library {
428 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900429 srcs: ["mylib.cpp"],
430 shared_libs: ["mylib4"],
431 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900432 stl: "none",
433 stubs: {
434 versions: ["10", "11", "12"],
435 },
436 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900437
438 cc_library {
439 name: "mylib4",
440 srcs: ["mylib.cpp"],
441 system_shared_libs: [],
442 stl: "none",
443 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900444 `)
445
446 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
447 copyCmds := apexRule.Args["copy_commands"]
448
449 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800450 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900451
452 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800453 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900454
455 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800456 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900457
Jiyong Parkda6eb592018-12-19 17:12:36 +0900458 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900459
460 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900461 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900462 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900463 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900464
465 // 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 +0900466 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900467 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900468 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900469
470 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900471 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900472 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900473
474 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900475 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 +0900476}
477
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900478func TestApexWithExplicitStubsDependency(t *testing.T) {
479 ctx := testApex(t, `
480 apex {
481 name: "myapex",
482 key: "myapex.key",
483 native_shared_libs: ["mylib"],
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: ["libfoo#10"],
496 system_shared_libs: [],
497 stl: "none",
498 }
499
500 cc_library {
501 name: "libfoo",
502 srcs: ["mylib.cpp"],
503 shared_libs: ["libbar"],
504 system_shared_libs: [],
505 stl: "none",
506 stubs: {
507 versions: ["10", "20", "30"],
508 },
509 }
510
511 cc_library {
512 name: "libbar",
513 srcs: ["mylib.cpp"],
514 system_shared_libs: [],
515 stl: "none",
516 }
517
518 `)
519
520 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
521 copyCmds := apexRule.Args["copy_commands"]
522
523 // Ensure that direct non-stubs dep is always included
524 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
525
526 // Ensure that indirect stubs dep is not included
527 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
528
529 // Ensure that dependency of stubs is not included
530 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
531
Jiyong Parkda6eb592018-12-19 17:12:36 +0900532 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900533
534 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900535 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900536 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900537 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900538
Jiyong Parkda6eb592018-12-19 17:12:36 +0900539 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900540
541 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
542 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
543}
544
Jiyong Park25fc6a92018-11-18 18:02:45 +0900545func TestApexWithSystemLibsStubs(t *testing.T) {
546 ctx := testApex(t, `
547 apex {
548 name: "myapex",
549 key: "myapex.key",
550 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
551 }
552
553 apex_key {
554 name: "myapex.key",
555 public_key: "testkey.avbpubkey",
556 private_key: "testkey.pem",
557 }
558
559 cc_library {
560 name: "mylib",
561 srcs: ["mylib.cpp"],
562 shared_libs: ["libdl#27"],
563 stl: "none",
564 }
565
566 cc_library_shared {
567 name: "mylib_shared",
568 srcs: ["mylib.cpp"],
569 shared_libs: ["libdl#27"],
570 stl: "none",
571 }
572
573 cc_library {
574 name: "libc",
575 no_libgcc: true,
576 nocrt: true,
577 system_shared_libs: [],
578 stl: "none",
579 stubs: {
580 versions: ["27", "28", "29"],
581 },
582 }
583
584 cc_library {
585 name: "libm",
586 no_libgcc: true,
587 nocrt: true,
588 system_shared_libs: [],
589 stl: "none",
590 stubs: {
591 versions: ["27", "28", "29"],
592 },
593 }
594
595 cc_library {
596 name: "libdl",
597 no_libgcc: true,
598 nocrt: true,
599 system_shared_libs: [],
600 stl: "none",
601 stubs: {
602 versions: ["27", "28", "29"],
603 },
604 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900605
606 cc_library {
607 name: "libBootstrap",
608 srcs: ["mylib.cpp"],
609 stl: "none",
610 bootstrap: true,
611 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900612 `)
613
614 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
615 copyCmds := apexRule.Args["copy_commands"]
616
617 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800618 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900619 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
620 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900621
622 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900623 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900624
Jiyong Parkda6eb592018-12-19 17:12:36 +0900625 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
626 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
627 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900628
629 // For dependency to libc
630 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900631 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900632 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900633 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900634 // ... Cflags from stub is correctly exported to mylib
635 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
636 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
637
638 // For dependency to libm
639 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900640 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900641 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900642 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900643 // ... and is not compiling with the stub
644 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
645 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
646
647 // For dependency to libdl
648 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900649 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900650 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900651 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
652 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900653 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900654 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900655 // ... Cflags from stub is correctly exported to mylib
656 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
657 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900658
659 // Ensure that libBootstrap is depending on the platform variant of bionic libs
660 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
661 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
662 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
663 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900664}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900665
666func TestFilesInSubDir(t *testing.T) {
667 ctx := testApex(t, `
668 apex {
669 name: "myapex",
670 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900671 native_shared_libs: ["mylib"],
672 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900673 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900674 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900675 }
676
677 apex_key {
678 name: "myapex.key",
679 public_key: "testkey.avbpubkey",
680 private_key: "testkey.pem",
681 }
682
683 prebuilt_etc {
684 name: "myetc",
685 src: "myprebuilt",
686 sub_dir: "foo/bar",
687 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900688
689 cc_library {
690 name: "mylib",
691 srcs: ["mylib.cpp"],
692 relative_install_path: "foo/bar",
693 system_shared_libs: [],
694 stl: "none",
695 }
696
697 cc_binary {
698 name: "mybin",
699 srcs: ["mylib.cpp"],
700 relative_install_path: "foo/bar",
701 system_shared_libs: [],
702 static_executable: true,
703 stl: "none",
704 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900705 `)
706
707 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
708 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
709
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900710 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900711 ensureListContains(t, dirs, "etc")
712 ensureListContains(t, dirs, "etc/foo")
713 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900714 ensureListContains(t, dirs, "lib64")
715 ensureListContains(t, dirs, "lib64/foo")
716 ensureListContains(t, dirs, "lib64/foo/bar")
717 ensureListContains(t, dirs, "lib")
718 ensureListContains(t, dirs, "lib/foo")
719 ensureListContains(t, dirs, "lib/foo/bar")
720
Jiyong Parkbd13e442019-03-15 18:10:35 +0900721 ensureListContains(t, dirs, "bin")
722 ensureListContains(t, dirs, "bin/foo")
723 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900724}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900725
726func TestUseVendor(t *testing.T) {
727 ctx := testApex(t, `
728 apex {
729 name: "myapex",
730 key: "myapex.key",
731 native_shared_libs: ["mylib"],
732 use_vendor: true,
733 }
734
735 apex_key {
736 name: "myapex.key",
737 public_key: "testkey.avbpubkey",
738 private_key: "testkey.pem",
739 }
740
741 cc_library {
742 name: "mylib",
743 srcs: ["mylib.cpp"],
744 shared_libs: ["mylib2"],
745 system_shared_libs: [],
746 vendor_available: true,
747 stl: "none",
748 }
749
750 cc_library {
751 name: "mylib2",
752 srcs: ["mylib.cpp"],
753 system_shared_libs: [],
754 vendor_available: true,
755 stl: "none",
756 }
757 `)
758
759 inputsList := []string{}
760 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
761 for _, implicit := range i.Implicits {
762 inputsList = append(inputsList, implicit.String())
763 }
764 }
765 inputsString := strings.Join(inputsList, " ")
766
767 // ensure that the apex includes vendor variants of the direct and indirect deps
768 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
769 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
770
771 // ensure that the apex does not include core variants
772 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
773 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
774}
Jiyong Park16e91a02018-12-20 18:18:08 +0900775
776func TestStaticLinking(t *testing.T) {
777 ctx := testApex(t, `
778 apex {
779 name: "myapex",
780 key: "myapex.key",
781 native_shared_libs: ["mylib"],
782 }
783
784 apex_key {
785 name: "myapex.key",
786 public_key: "testkey.avbpubkey",
787 private_key: "testkey.pem",
788 }
789
790 cc_library {
791 name: "mylib",
792 srcs: ["mylib.cpp"],
793 system_shared_libs: [],
794 stl: "none",
795 stubs: {
796 versions: ["1", "2", "3"],
797 },
798 }
799
800 cc_binary {
801 name: "not_in_apex",
802 srcs: ["mylib.cpp"],
803 static_libs: ["mylib"],
804 static_executable: true,
805 system_shared_libs: [],
806 stl: "none",
807 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900808 `)
809
810 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
811
812 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800813 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900814}
Jiyong Park9335a262018-12-24 11:31:58 +0900815
816func TestKeys(t *testing.T) {
817 ctx := testApex(t, `
818 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900819 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900820 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900821 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900822 native_shared_libs: ["mylib"],
823 }
824
825 cc_library {
826 name: "mylib",
827 srcs: ["mylib.cpp"],
828 system_shared_libs: [],
829 stl: "none",
830 }
831
832 apex_key {
833 name: "myapex.key",
834 public_key: "testkey.avbpubkey",
835 private_key: "testkey.pem",
836 }
837
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900838 android_app_certificate {
839 name: "myapex.certificate",
840 certificate: "testkey",
841 }
842
843 android_app_certificate {
844 name: "myapex.certificate.override",
845 certificate: "testkey.override",
846 }
847
Jiyong Park9335a262018-12-24 11:31:58 +0900848 `)
849
850 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900851 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900852
853 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
854 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
855 "vendor/foo/devkeys/testkey.avbpubkey")
856 }
857 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
858 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
859 "vendor/foo/devkeys/testkey.pem")
860 }
861
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900862 // check the APK certs. It should be overridden to myapex.certificate.override
863 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
864 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900865 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900866 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900867 }
868}
Jiyong Park58e364a2019-01-19 19:24:06 +0900869
870func TestMacro(t *testing.T) {
871 ctx := testApex(t, `
872 apex {
873 name: "myapex",
874 key: "myapex.key",
875 native_shared_libs: ["mylib"],
876 }
877
878 apex {
879 name: "otherapex",
880 key: "myapex.key",
881 native_shared_libs: ["mylib"],
882 }
883
884 apex_key {
885 name: "myapex.key",
886 public_key: "testkey.avbpubkey",
887 private_key: "testkey.pem",
888 }
889
890 cc_library {
891 name: "mylib",
892 srcs: ["mylib.cpp"],
893 system_shared_libs: [],
894 stl: "none",
895 }
896 `)
897
898 // non-APEX variant does not have __ANDROID__APEX__ defined
899 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
900 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
901 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
902
903 // APEX variant has __ANDROID_APEX__=<apexname> defined
904 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
905 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
906 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
907
908 // APEX variant has __ANDROID_APEX__=<apexname> defined
909 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
910 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
911 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
912}
Jiyong Park7e636d02019-01-28 16:16:54 +0900913
914func TestHeaderLibsDependency(t *testing.T) {
915 ctx := testApex(t, `
916 apex {
917 name: "myapex",
918 key: "myapex.key",
919 native_shared_libs: ["mylib"],
920 }
921
922 apex_key {
923 name: "myapex.key",
924 public_key: "testkey.avbpubkey",
925 private_key: "testkey.pem",
926 }
927
928 cc_library_headers {
929 name: "mylib_headers",
930 export_include_dirs: ["my_include"],
931 system_shared_libs: [],
932 stl: "none",
933 }
934
935 cc_library {
936 name: "mylib",
937 srcs: ["mylib.cpp"],
938 system_shared_libs: [],
939 stl: "none",
940 header_libs: ["mylib_headers"],
941 export_header_lib_headers: ["mylib_headers"],
942 stubs: {
943 versions: ["1", "2", "3"],
944 },
945 }
946
947 cc_library {
948 name: "otherlib",
949 srcs: ["mylib.cpp"],
950 system_shared_libs: [],
951 stl: "none",
952 shared_libs: ["mylib"],
953 }
954 `)
955
956 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
957
958 // Ensure that the include path of the header lib is exported to 'otherlib'
959 ensureContains(t, cFlags, "-Imy_include")
960}
Alex Light9670d332019-01-29 18:07:33 -0800961
Alex Light0851b882019-02-07 13:20:53 -0800962func TestNonTestApex(t *testing.T) {
963 ctx := testApex(t, `
964 apex {
965 name: "myapex",
966 key: "myapex.key",
967 native_shared_libs: ["mylib_common"],
968 }
969
970 apex_key {
971 name: "myapex.key",
972 public_key: "testkey.avbpubkey",
973 private_key: "testkey.pem",
974 }
975
976 cc_library {
977 name: "mylib_common",
978 srcs: ["mylib.cpp"],
979 system_shared_libs: [],
980 stl: "none",
981 }
982 `)
983
984 module := ctx.ModuleForTests("myapex", "android_common_myapex")
985 apexRule := module.Rule("apexRule")
986 copyCmds := apexRule.Args["copy_commands"]
987
988 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
989 t.Log("Apex was a test apex!")
990 t.Fail()
991 }
992 // Ensure that main rule creates an output
993 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
994
995 // Ensure that apex variant is created for the direct dep
996 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
997
998 // Ensure that both direct and indirect deps are copied into apex
999 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1000
1001 // Ensure that the platform variant ends with _core_shared
1002 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1003
1004 if !android.InAnyApex("mylib_common") {
1005 t.Log("Found mylib_common not in any apex!")
1006 t.Fail()
1007 }
1008}
1009
1010func TestTestApex(t *testing.T) {
1011 if android.InAnyApex("mylib_common_test") {
1012 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!")
1013 }
1014 ctx := testApex(t, `
1015 apex_test {
1016 name: "myapex",
1017 key: "myapex.key",
1018 native_shared_libs: ["mylib_common_test"],
1019 }
1020
1021 apex_key {
1022 name: "myapex.key",
1023 public_key: "testkey.avbpubkey",
1024 private_key: "testkey.pem",
1025 }
1026
1027 cc_library {
1028 name: "mylib_common_test",
1029 srcs: ["mylib.cpp"],
1030 system_shared_libs: [],
1031 stl: "none",
1032 }
1033 `)
1034
1035 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1036 apexRule := module.Rule("apexRule")
1037 copyCmds := apexRule.Args["copy_commands"]
1038
1039 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1040 t.Log("Apex was not a test apex!")
1041 t.Fail()
1042 }
1043 // Ensure that main rule creates an output
1044 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1045
1046 // Ensure that apex variant is created for the direct dep
1047 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1048
1049 // Ensure that both direct and indirect deps are copied into apex
1050 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1051
1052 // Ensure that the platform variant ends with _core_shared
1053 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1054
1055 if android.InAnyApex("mylib_common_test") {
1056 t.Log("Found mylib_common_test in some apex!")
1057 t.Fail()
1058 }
1059}
1060
Alex Light9670d332019-01-29 18:07:33 -08001061func TestApexWithTarget(t *testing.T) {
1062 ctx := testApex(t, `
1063 apex {
1064 name: "myapex",
1065 key: "myapex.key",
1066 multilib: {
1067 first: {
1068 native_shared_libs: ["mylib_common"],
1069 }
1070 },
1071 target: {
1072 android: {
1073 multilib: {
1074 first: {
1075 native_shared_libs: ["mylib"],
1076 }
1077 }
1078 },
1079 host: {
1080 multilib: {
1081 first: {
1082 native_shared_libs: ["mylib2"],
1083 }
1084 }
1085 }
1086 }
1087 }
1088
1089 apex_key {
1090 name: "myapex.key",
1091 public_key: "testkey.avbpubkey",
1092 private_key: "testkey.pem",
1093 }
1094
1095 cc_library {
1096 name: "mylib",
1097 srcs: ["mylib.cpp"],
1098 system_shared_libs: [],
1099 stl: "none",
1100 }
1101
1102 cc_library {
1103 name: "mylib_common",
1104 srcs: ["mylib.cpp"],
1105 system_shared_libs: [],
1106 stl: "none",
1107 compile_multilib: "first",
1108 }
1109
1110 cc_library {
1111 name: "mylib2",
1112 srcs: ["mylib.cpp"],
1113 system_shared_libs: [],
1114 stl: "none",
1115 compile_multilib: "first",
1116 }
1117 `)
1118
1119 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1120 copyCmds := apexRule.Args["copy_commands"]
1121
1122 // Ensure that main rule creates an output
1123 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1124
1125 // Ensure that apex variant is created for the direct dep
1126 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1127 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1128 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1129
1130 // Ensure that both direct and indirect deps are copied into apex
1131 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1132 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1133 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1134
1135 // Ensure that the platform variant ends with _core_shared
1136 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1137 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1138 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1139}
Jiyong Park04480cf2019-02-06 00:16:29 +09001140
1141func TestApexWithShBinary(t *testing.T) {
1142 ctx := testApex(t, `
1143 apex {
1144 name: "myapex",
1145 key: "myapex.key",
1146 binaries: ["myscript"],
1147 }
1148
1149 apex_key {
1150 name: "myapex.key",
1151 public_key: "testkey.avbpubkey",
1152 private_key: "testkey.pem",
1153 }
1154
1155 sh_binary {
1156 name: "myscript",
1157 src: "mylib.cpp",
1158 filename: "myscript.sh",
1159 sub_dir: "script",
1160 }
1161 `)
1162
1163 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1164 copyCmds := apexRule.Args["copy_commands"]
1165
1166 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1167}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001168
1169func TestApexInProductPartition(t *testing.T) {
1170 ctx := testApex(t, `
1171 apex {
1172 name: "myapex",
1173 key: "myapex.key",
1174 native_shared_libs: ["mylib"],
1175 product_specific: true,
1176 }
1177
1178 apex_key {
1179 name: "myapex.key",
1180 public_key: "testkey.avbpubkey",
1181 private_key: "testkey.pem",
1182 product_specific: true,
1183 }
1184
1185 cc_library {
1186 name: "mylib",
1187 srcs: ["mylib.cpp"],
1188 system_shared_libs: [],
1189 stl: "none",
1190 }
1191 `)
1192
1193 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1194 expected := "target/product/test_device/product/apex"
1195 actual := apex.installDir.RelPathString()
1196 if actual != expected {
1197 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1198 }
1199
1200 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1201 expected = "target/product/test_device/product/etc/security/apex"
1202 actual = apex_key.installDir.RelPathString()
1203 if actual != expected {
1204 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1205 }
1206
1207}
Jiyong Park67882562019-03-21 01:11:21 +09001208
1209func TestApexKeyFromOtherModule(t *testing.T) {
1210 ctx := testApex(t, `
1211 apex_key {
1212 name: "myapex.key",
1213 public_key: ":my.avbpubkey",
1214 private_key: ":my.pem",
1215 product_specific: true,
1216 }
1217
1218 filegroup {
1219 name: "my.avbpubkey",
1220 srcs: ["testkey2.avbpubkey"],
1221 }
1222
1223 filegroup {
1224 name: "my.pem",
1225 srcs: ["testkey2.pem"],
1226 }
1227 `)
1228
1229 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1230 expected_pubkey := "testkey2.avbpubkey"
1231 actual_pubkey := apex_key.public_key_file.String()
1232 if actual_pubkey != expected_pubkey {
1233 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1234 }
1235 expected_privkey := "testkey2.pem"
1236 actual_privkey := apex_key.private_key_file.String()
1237 if actual_privkey != expected_privkey {
1238 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1239 }
1240}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001241
1242func TestPrebuilt(t *testing.T) {
1243 ctx := testApex(t, `
1244 prebuilt_apex {
1245 name: "myapex",
1246 src: "myapex.apex",
1247 key: "myapex.key"
1248 }
1249
1250 apex_key {
1251 name: "myapex.key",
1252 public_key: "testkey.avbpubkey",
1253 private_key: "testkey.pem",
1254 product_specific: true,
1255 }
1256 `)
1257
1258 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1259
1260 // Check if the key module is added as a required module.
1261 buf := &bytes.Buffer{}
1262 prebuilt.AndroidMk().Extra[0](buf, nil)
1263 found := false
1264 scanner := bufio.NewScanner(bytes.NewReader(buf.Bytes()))
1265 expected := "myapex.key"
1266 for scanner.Scan() {
1267 line := scanner.Text()
1268 tok := strings.Split(line, " := ")
1269 if tok[0] == "LOCAL_REQUIRED_MODULES" {
1270 found = true
1271 if tok[1] != "myapex.key" {
1272 t.Errorf("Unexpected LOCAL_REQUIRED_MODULES '%s', expected '%s'", tok[1], expected)
1273 }
1274 }
1275 }
1276 if !found {
1277 t.Errorf("Couldn't find a LOCAL_REQUIRED_MODULES entry")
1278 }
1279}