blob: 9ef6aedfa7f7ea5a488631e8e6f24656e48112cf [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jiyong Parkaf8998c2020-02-28 16:51:07 +090090func withManifestPackageNameOverrides(specs []string) testCustomizer {
91 return func(fs map[string][]byte, config android.Config) {
92 config.TestProductVariables.ManifestPackageNameOverrides = specs
93 }
94}
95
Jooyung Han31c470b2019-10-18 16:26:59 +090096func withBinder32bit(fs map[string][]byte, config android.Config) {
97 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
98}
99
Jiyong Park7cd10e32020-01-14 09:22:18 +0900100func withUnbundledBuild(fs map[string][]byte, config android.Config) {
101 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
102}
103
Jooyung Han344d5432019-08-23 11:17:39 +0900104func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +0900105 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900106
107 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900108 filegroup {
109 name: "myapex-file_contexts",
110 srcs: [
111 "system/sepolicy/apex/myapex-file_contexts",
112 ],
113 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900114 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800115
Colin Crossf9aabd72020-02-15 11:29:50 -0800116 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
117
Dario Frenicde2a032019-10-27 00:29:22 +0100118 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900119
Jooyung Han344d5432019-08-23 11:17:39 +0900120 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900121 "a.java": nil,
122 "PrebuiltAppFoo.apk": nil,
123 "PrebuiltAppFooPriv.apk": nil,
124 "build/make/target/product/security": nil,
125 "apex_manifest.json": nil,
126 "AndroidManifest.xml": nil,
127 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900128 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900129 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900130 "system/sepolicy/apex/otherapex-file_contexts": nil,
131 "system/sepolicy/apex/commonapex-file_contexts": nil,
132 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800133 "mylib.cpp": nil,
134 "mylib_common.cpp": nil,
135 "mytest.cpp": nil,
136 "mytest1.cpp": nil,
137 "mytest2.cpp": nil,
138 "mytest3.cpp": nil,
139 "myprebuilt": nil,
140 "my_include": nil,
141 "foo/bar/MyClass.java": nil,
142 "prebuilt.jar": nil,
143 "vendor/foo/devkeys/test.x509.pem": nil,
144 "vendor/foo/devkeys/test.pk8": nil,
145 "testkey.x509.pem": nil,
146 "testkey.pk8": nil,
147 "testkey.override.x509.pem": nil,
148 "testkey.override.pk8": nil,
149 "vendor/foo/devkeys/testkey.avbpubkey": nil,
150 "vendor/foo/devkeys/testkey.pem": nil,
151 "NOTICE": nil,
152 "custom_notice": nil,
153 "testkey2.avbpubkey": nil,
154 "testkey2.pem": nil,
155 "myapex-arm64.apex": nil,
156 "myapex-arm.apex": nil,
157 "frameworks/base/api/current.txt": nil,
158 "framework/aidl/a.aidl": nil,
159 "build/make/core/proguard.flags": nil,
160 "build/make/core/proguard_basic_keeps.flags": nil,
161 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900162 }
163
Colin Crossf9aabd72020-02-15 11:29:50 -0800164 cc.GatherRequiredFilesForTest(fs)
165
Jooyung Han344d5432019-08-23 11:17:39 +0900166 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800167 // The fs now needs to be populated before creating the config, call handlers twice
168 // for now, once to get any fs changes, and later after the config was created to
169 // set product variables or targets.
170 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
171 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900172 }
173
Colin Cross98be1bb2019-12-13 20:41:13 -0800174 config := android.TestArchConfig(buildDir, nil, bp, fs)
175 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
176 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
177 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
178 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
179 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
180 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
181
182 for _, handler := range handlers {
183 // The fs now needs to be populated before creating the config, call handlers twice
184 // for now, earlier to get any fs changes, and now after the config was created to
185 // set product variables or targets.
186 tempFS := map[string][]byte{}
187 handler(tempFS, config)
188 }
189
190 ctx := android.NewTestArchContext()
191 ctx.RegisterModuleType("apex", BundleFactory)
192 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
193 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
194 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
195 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
196 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
197 ctx.RegisterModuleType("override_apex", overrideApexFactory)
198
Jooyung Hana57af4a2020-01-23 05:36:59 +0000199 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
200 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
201
Paul Duffin77980a82019-12-19 16:01:36 +0000202 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800203 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800204 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
205 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800206 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000207 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800208 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800209 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000210 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000211 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000212 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900213 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800214
Colin Cross98be1bb2019-12-13 20:41:13 -0800215 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800216 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800217
218 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900219
Jooyung Han5c998b92019-06-27 11:30:33 +0900220 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900221}
222
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700223func setUp() {
224 var err error
225 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900226 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700227 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900228 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229}
230
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700231func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900232 os.RemoveAll(buildDir)
233}
234
235// ensure that 'result' contains 'expected'
236func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900237 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238 if !strings.Contains(result, expected) {
239 t.Errorf("%q is not found in %q", expected, result)
240 }
241}
242
243// ensures that 'result' does not contain 'notExpected'
244func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900245 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900246 if strings.Contains(result, notExpected) {
247 t.Errorf("%q is found in %q", notExpected, result)
248 }
249}
250
251func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900252 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900253 if !android.InList(expected, result) {
254 t.Errorf("%q is not found in %v", expected, result)
255 }
256}
257
258func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900259 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900260 if android.InList(notExpected, result) {
261 t.Errorf("%q is found in %v", notExpected, result)
262 }
263}
264
Jooyung Hane1633032019-08-01 17:41:43 +0900265func ensureListEmpty(t *testing.T, result []string) {
266 t.Helper()
267 if len(result) > 0 {
268 t.Errorf("%q is expected to be empty", result)
269 }
270}
271
Jiyong Park25fc6a92018-11-18 18:02:45 +0900272// Minimal test
273func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700274 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900275 apex_defaults {
276 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900277 manifest: ":myapex.manifest",
278 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900279 key: "myapex.key",
280 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800281 multilib: {
282 both: {
283 binaries: ["foo",],
284 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900285 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900286 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 }
288
Jiyong Park30ca9372019-02-07 16:27:23 +0900289 apex {
290 name: "myapex",
291 defaults: ["myapex-defaults"],
292 }
293
Jiyong Park25fc6a92018-11-18 18:02:45 +0900294 apex_key {
295 name: "myapex.key",
296 public_key: "testkey.avbpubkey",
297 private_key: "testkey.pem",
298 }
299
Jiyong Park809bb722019-02-13 21:33:49 +0900300 filegroup {
301 name: "myapex.manifest",
302 srcs: ["apex_manifest.json"],
303 }
304
305 filegroup {
306 name: "myapex.androidmanifest",
307 srcs: ["AndroidManifest.xml"],
308 }
309
Jiyong Park25fc6a92018-11-18 18:02:45 +0900310 cc_library {
311 name: "mylib",
312 srcs: ["mylib.cpp"],
313 shared_libs: ["mylib2"],
314 system_shared_libs: [],
315 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000316 // TODO: remove //apex_available:platform
317 apex_available: [
318 "//apex_available:platform",
319 "myapex",
320 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 }
322
Alex Light3d673592019-01-18 14:37:31 -0800323 cc_binary {
324 name: "foo",
325 srcs: ["mylib.cpp"],
326 compile_multilib: "both",
327 multilib: {
328 lib32: {
329 suffix: "32",
330 },
331 lib64: {
332 suffix: "64",
333 },
334 },
335 symlinks: ["foo_link_"],
336 symlink_preferred_arch: true,
337 system_shared_libs: [],
338 static_executable: true,
339 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000340 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800341 }
342
Jiyong Park25fc6a92018-11-18 18:02:45 +0900343 cc_library {
344 name: "mylib2",
345 srcs: ["mylib.cpp"],
346 system_shared_libs: [],
347 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900348 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000349 // TODO: remove //apex_available:platform
350 apex_available: [
351 "//apex_available:platform",
352 "myapex",
353 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900354 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900355
356 java_library {
357 name: "myjar",
358 srcs: ["foo/bar/MyClass.java"],
359 sdk_version: "none",
360 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900361 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900362 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000363 // TODO: remove //apex_available:platform
364 apex_available: [
365 "//apex_available:platform",
366 "myapex",
367 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900368 }
369
370 java_library {
371 name: "myotherjar",
372 srcs: ["foo/bar/MyClass.java"],
373 sdk_version: "none",
374 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900375 // TODO: remove //apex_available:platform
376 apex_available: [
377 "//apex_available:platform",
378 "myapex",
379 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900380 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900381
382 java_library {
383 name: "mysharedjar",
384 srcs: ["foo/bar/MyClass.java"],
385 sdk_version: "none",
386 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900387 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900388 `)
389
Sundong Ahnabb64432019-10-22 13:58:29 +0900390 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900391
392 optFlags := apexRule.Args["opt_flags"]
393 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700394 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900395 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900396
Jiyong Park25fc6a92018-11-18 18:02:45 +0900397 copyCmds := apexRule.Args["copy_commands"]
398
399 // Ensure that main rule creates an output
400 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
401
402 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800403 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900404 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900405
406 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800407 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900408 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900409
410 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800411 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
412 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900413 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
414 // .. but not for java libs
415 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900416 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800417
Colin Cross7113d202019-11-20 16:39:12 -0800418 // Ensure that the platform variant ends with _shared or _common
419 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
420 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900421 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
422 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900423 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
424
425 // Ensure that dynamic dependency to java libs are not included
426 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800427
428 // Ensure that all symlinks are present.
429 found_foo_link_64 := false
430 found_foo := false
431 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900432 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800433 if strings.HasSuffix(cmd, "bin/foo") {
434 found_foo = true
435 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
436 found_foo_link_64 = true
437 }
438 }
439 }
440 good := found_foo && found_foo_link_64
441 if !good {
442 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
443 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900444
Sundong Ahnabb64432019-10-22 13:58:29 +0900445 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700446 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700447 if len(noticeInputs) != 2 {
448 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900449 }
450 ensureListContains(t, noticeInputs, "NOTICE")
451 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900452
453 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900454 ensureListContains(t, depsInfo, "myjar <- myapex")
455 ensureListContains(t, depsInfo, "mylib <- myapex")
456 ensureListContains(t, depsInfo, "mylib2 <- mylib")
457 ensureListContains(t, depsInfo, "myotherjar <- myjar")
458 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800459}
460
Jooyung Hanf21c7972019-12-16 22:32:06 +0900461func TestDefaults(t *testing.T) {
462 ctx, _ := testApex(t, `
463 apex_defaults {
464 name: "myapex-defaults",
465 key: "myapex.key",
466 prebuilts: ["myetc"],
467 native_shared_libs: ["mylib"],
468 java_libs: ["myjar"],
469 apps: ["AppFoo"],
470 }
471
472 prebuilt_etc {
473 name: "myetc",
474 src: "myprebuilt",
475 }
476
477 apex {
478 name: "myapex",
479 defaults: ["myapex-defaults"],
480 }
481
482 apex_key {
483 name: "myapex.key",
484 public_key: "testkey.avbpubkey",
485 private_key: "testkey.pem",
486 }
487
488 cc_library {
489 name: "mylib",
490 system_shared_libs: [],
491 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000492 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900493 }
494
495 java_library {
496 name: "myjar",
497 srcs: ["foo/bar/MyClass.java"],
498 sdk_version: "none",
499 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000500 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900501 }
502
503 android_app {
504 name: "AppFoo",
505 srcs: ["foo/bar/MyClass.java"],
506 sdk_version: "none",
507 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000508 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900509 }
510 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000511 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900512 "etc/myetc",
513 "javalib/myjar.jar",
514 "lib64/mylib.so",
515 "app/AppFoo/AppFoo.apk",
516 })
517}
518
Jooyung Han01a3ee22019-11-02 02:52:25 +0900519func TestApexManifest(t *testing.T) {
520 ctx, _ := testApex(t, `
521 apex {
522 name: "myapex",
523 key: "myapex.key",
524 }
525
526 apex_key {
527 name: "myapex.key",
528 public_key: "testkey.avbpubkey",
529 private_key: "testkey.pem",
530 }
531 `)
532
533 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900534 args := module.Rule("apexRule").Args
535 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
536 t.Error("manifest should be apex_manifest.pb, but " + manifest)
537 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900538}
539
Alex Light5098a612018-11-29 17:12:15 -0800540func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700541 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800542 apex {
543 name: "myapex",
544 key: "myapex.key",
545 payload_type: "zip",
546 native_shared_libs: ["mylib"],
547 }
548
549 apex_key {
550 name: "myapex.key",
551 public_key: "testkey.avbpubkey",
552 private_key: "testkey.pem",
553 }
554
555 cc_library {
556 name: "mylib",
557 srcs: ["mylib.cpp"],
558 shared_libs: ["mylib2"],
559 system_shared_libs: [],
560 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000561 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800562 }
563
564 cc_library {
565 name: "mylib2",
566 srcs: ["mylib.cpp"],
567 system_shared_libs: [],
568 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000569 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800570 }
571 `)
572
Sundong Ahnabb64432019-10-22 13:58:29 +0900573 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800574 copyCmds := zipApexRule.Args["copy_commands"]
575
576 // Ensure that main rule creates an output
577 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
578
579 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800580 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800581
582 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800583 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800584
585 // Ensure that both direct and indirect deps are copied into apex
586 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
587 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900588}
589
590func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700591 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900592 apex {
593 name: "myapex",
594 key: "myapex.key",
595 native_shared_libs: ["mylib", "mylib3"],
596 }
597
598 apex_key {
599 name: "myapex.key",
600 public_key: "testkey.avbpubkey",
601 private_key: "testkey.pem",
602 }
603
604 cc_library {
605 name: "mylib",
606 srcs: ["mylib.cpp"],
607 shared_libs: ["mylib2", "mylib3"],
608 system_shared_libs: [],
609 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000610 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900611 }
612
613 cc_library {
614 name: "mylib2",
615 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900616 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900617 system_shared_libs: [],
618 stl: "none",
619 stubs: {
620 versions: ["1", "2", "3"],
621 },
622 }
623
624 cc_library {
625 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900626 srcs: ["mylib.cpp"],
627 shared_libs: ["mylib4"],
628 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900629 stl: "none",
630 stubs: {
631 versions: ["10", "11", "12"],
632 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000633 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900634 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900635
636 cc_library {
637 name: "mylib4",
638 srcs: ["mylib.cpp"],
639 system_shared_libs: [],
640 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000641 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900642 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900643 `)
644
Sundong Ahnabb64432019-10-22 13:58:29 +0900645 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900646 copyCmds := apexRule.Args["copy_commands"]
647
648 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800649 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900650
651 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800652 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900653
654 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800655 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900656
Colin Cross7113d202019-11-20 16:39:12 -0800657 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900658
659 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900660 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900661 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900662 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900663
664 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Cross7113d202019-11-20 16:39:12 -0800665 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900666 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800667 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900668
669 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900670 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900671 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900672
673 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900674 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900675
Jooyung Hana57af4a2020-01-23 05:36:59 +0000676 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900677 "lib64/mylib.so",
678 "lib64/mylib3.so",
679 "lib64/mylib4.so",
680 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900681}
682
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900683func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700684 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900685 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900686 name: "myapex2",
687 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900688 native_shared_libs: ["mylib"],
689 }
690
691 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900692 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900693 public_key: "testkey.avbpubkey",
694 private_key: "testkey.pem",
695 }
696
697 cc_library {
698 name: "mylib",
699 srcs: ["mylib.cpp"],
700 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900701 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900702 system_shared_libs: [],
703 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000704 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900705 }
706
707 cc_library {
708 name: "libfoo",
709 srcs: ["mylib.cpp"],
710 shared_libs: ["libbar"],
711 system_shared_libs: [],
712 stl: "none",
713 stubs: {
714 versions: ["10", "20", "30"],
715 },
716 }
717
718 cc_library {
719 name: "libbar",
720 srcs: ["mylib.cpp"],
721 system_shared_libs: [],
722 stl: "none",
723 }
724
Jiyong Park678c8812020-02-07 17:25:49 +0900725 cc_library_static {
726 name: "libbaz",
727 srcs: ["mylib.cpp"],
728 system_shared_libs: [],
729 stl: "none",
730 apex_available: [ "myapex2" ],
731 }
732
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900733 `)
734
Jiyong Park83dc74b2020-01-14 18:38:44 +0900735 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900736 copyCmds := apexRule.Args["copy_commands"]
737
738 // Ensure that direct non-stubs dep is always included
739 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
740
741 // Ensure that indirect stubs dep is not included
742 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
743
744 // Ensure that dependency of stubs is not included
745 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
746
Jiyong Park83dc74b2020-01-14 18:38:44 +0900747 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900748
749 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900750 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900751 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900752 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900753
Jiyong Park3ff16992019-12-27 14:11:47 +0900754 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900755
756 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
757 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900758
759 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900760
761 ensureListContains(t, depsInfo, "mylib <- myapex2")
762 ensureListContains(t, depsInfo, "libbaz <- mylib")
763 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900764}
765
Jooyung Hand3639552019-08-09 12:57:43 +0900766func TestApexWithRuntimeLibsDependency(t *testing.T) {
767 /*
768 myapex
769 |
770 v (runtime_libs)
771 mylib ------+------> libfoo [provides stub]
772 |
773 `------> libbar
774 */
775 ctx, _ := testApex(t, `
776 apex {
777 name: "myapex",
778 key: "myapex.key",
779 native_shared_libs: ["mylib"],
780 }
781
782 apex_key {
783 name: "myapex.key",
784 public_key: "testkey.avbpubkey",
785 private_key: "testkey.pem",
786 }
787
788 cc_library {
789 name: "mylib",
790 srcs: ["mylib.cpp"],
791 runtime_libs: ["libfoo", "libbar"],
792 system_shared_libs: [],
793 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000794 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900795 }
796
797 cc_library {
798 name: "libfoo",
799 srcs: ["mylib.cpp"],
800 system_shared_libs: [],
801 stl: "none",
802 stubs: {
803 versions: ["10", "20", "30"],
804 },
805 }
806
807 cc_library {
808 name: "libbar",
809 srcs: ["mylib.cpp"],
810 system_shared_libs: [],
811 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000812 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900813 }
814
815 `)
816
Sundong Ahnabb64432019-10-22 13:58:29 +0900817 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900818 copyCmds := apexRule.Args["copy_commands"]
819
820 // Ensure that direct non-stubs dep is always included
821 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
822
823 // Ensure that indirect stubs dep is not included
824 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
825
826 // Ensure that runtime_libs dep in included
827 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
828
Sundong Ahnabb64432019-10-22 13:58:29 +0900829 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900830 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
831 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900832
833}
834
Jooyung Han9c80bae2019-08-20 17:30:57 +0900835func TestApexDependencyToLLNDK(t *testing.T) {
836 ctx, _ := testApex(t, `
837 apex {
838 name: "myapex",
839 key: "myapex.key",
840 use_vendor: true,
841 native_shared_libs: ["mylib"],
842 }
843
844 apex_key {
845 name: "myapex.key",
846 public_key: "testkey.avbpubkey",
847 private_key: "testkey.pem",
848 }
849
850 cc_library {
851 name: "mylib",
852 srcs: ["mylib.cpp"],
853 vendor_available: true,
854 shared_libs: ["libbar"],
855 system_shared_libs: [],
856 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000857 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900858 }
859
860 cc_library {
861 name: "libbar",
862 srcs: ["mylib.cpp"],
863 system_shared_libs: [],
864 stl: "none",
865 }
866
867 llndk_library {
868 name: "libbar",
869 symbol_file: "",
870 }
Jooyung Handc782442019-11-01 03:14:38 +0900871 `, func(fs map[string][]byte, config android.Config) {
872 setUseVendorWhitelistForTest(config, []string{"myapex"})
873 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900874
Sundong Ahnabb64432019-10-22 13:58:29 +0900875 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900876 copyCmds := apexRule.Args["copy_commands"]
877
878 // Ensure that LLNDK dep is not included
879 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
880
Sundong Ahnabb64432019-10-22 13:58:29 +0900881 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900882 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900883
884 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900885 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900886
887}
888
Jiyong Park25fc6a92018-11-18 18:02:45 +0900889func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700890 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900891 apex {
892 name: "myapex",
893 key: "myapex.key",
894 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
895 }
896
897 apex_key {
898 name: "myapex.key",
899 public_key: "testkey.avbpubkey",
900 private_key: "testkey.pem",
901 }
902
903 cc_library {
904 name: "mylib",
905 srcs: ["mylib.cpp"],
906 shared_libs: ["libdl#27"],
907 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000908 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900909 }
910
911 cc_library_shared {
912 name: "mylib_shared",
913 srcs: ["mylib.cpp"],
914 shared_libs: ["libdl#27"],
915 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000916 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900917 }
918
919 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900920 name: "libBootstrap",
921 srcs: ["mylib.cpp"],
922 stl: "none",
923 bootstrap: true,
924 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925 `)
926
Sundong Ahnabb64432019-10-22 13:58:29 +0900927 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928 copyCmds := apexRule.Args["copy_commands"]
929
930 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800931 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900932 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
933 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900934
935 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900936 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900937
Colin Cross7113d202019-11-20 16:39:12 -0800938 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
939 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
940 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900941
942 // For dependency to libc
943 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900944 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900945 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900946 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900947 // ... Cflags from stub is correctly exported to mylib
948 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
949 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
950
951 // For dependency to libm
952 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800953 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900954 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900955 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900956 // ... and is not compiling with the stub
957 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
958 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
959
960 // For dependency to libdl
961 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900962 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900963 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900964 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
965 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900966 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800967 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900968 // ... Cflags from stub is correctly exported to mylib
969 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
970 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900971
972 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800973 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
974 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
975 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
976 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900977}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900978
Jooyung Han0c4e0162020-02-26 22:45:42 +0900979func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
980 // there are three links between liba --> libz
981 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
982 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
983 // 3) (platform) -> liba -> libz : this should be non-stub link
984 ctx, _ := testApex(t, `
985 apex {
986 name: "myapex",
987 key: "myapex.key",
988 native_shared_libs: ["libx"],
989 min_sdk_version: "2",
990 }
991
992 apex {
993 name: "otherapex",
994 key: "myapex.key",
995 native_shared_libs: ["liby"],
996 min_sdk_version: "3",
997 }
998
999 apex_key {
1000 name: "myapex.key",
1001 public_key: "testkey.avbpubkey",
1002 private_key: "testkey.pem",
1003 }
1004
1005 cc_library {
1006 name: "libx",
1007 shared_libs: ["liba"],
1008 system_shared_libs: [],
1009 stl: "none",
1010 apex_available: [ "myapex" ],
1011 }
1012
1013 cc_library {
1014 name: "liby",
1015 shared_libs: ["liba"],
1016 system_shared_libs: [],
1017 stl: "none",
1018 apex_available: [ "otherapex" ],
1019 }
1020
1021 cc_library {
1022 name: "liba",
1023 shared_libs: ["libz"],
1024 system_shared_libs: [],
1025 stl: "none",
1026 apex_available: [
1027 "//apex_available:anyapex",
1028 "//apex_available:platform",
1029 ],
1030 }
1031
1032 cc_library {
1033 name: "libz",
1034 system_shared_libs: [],
1035 stl: "none",
1036 stubs: {
1037 versions: ["1", "3"],
1038 },
1039 }
1040 `, withUnbundledBuild)
1041
1042 expectLink := func(from, from_variant, to, to_variant string) {
1043 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1044 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1045 }
1046 expectNoLink := func(from, from_variant, to, to_variant string) {
1047 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1048 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1049 }
1050 // platform liba is linked to non-stub version
1051 expectLink("liba", "shared", "libz", "shared")
1052 // liba in myapex is linked to #1
1053 expectLink("liba", "shared_myapex", "libz", "shared_1")
1054 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1055 expectNoLink("liba", "shared_myapex", "libz", "shared")
1056 // liba in otherapex is linked to #3
1057 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1058 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1059 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1060}
1061
1062func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1063 ctx, _ := testApex(t, `
1064 apex {
1065 name: "myapex",
1066 key: "myapex.key",
1067 native_shared_libs: ["libx"],
1068 }
1069
1070 apex_key {
1071 name: "myapex.key",
1072 public_key: "testkey.avbpubkey",
1073 private_key: "testkey.pem",
1074 }
1075
1076 cc_library {
1077 name: "libx",
1078 shared_libs: ["libz"],
1079 system_shared_libs: [],
1080 stl: "none",
1081 apex_available: [ "myapex" ],
1082 }
1083
1084 cc_library {
1085 name: "libz",
1086 system_shared_libs: [],
1087 stl: "none",
1088 stubs: {
1089 versions: ["1", "2"],
1090 },
1091 }
1092 `)
1093
1094 expectLink := func(from, from_variant, to, to_variant string) {
1095 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1096 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1097 }
1098 expectNoLink := func(from, from_variant, to, to_variant string) {
1099 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1100 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1101 }
1102 expectLink("libx", "shared_myapex", "libz", "shared_2")
1103 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1104 expectNoLink("libx", "shared_myapex", "libz", "shared")
1105}
1106
1107func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1108 ctx, _ := testApex(t, `
1109 apex {
1110 name: "myapex",
1111 key: "myapex.key",
1112 native_shared_libs: ["libx"],
1113 }
1114
1115 apex_key {
1116 name: "myapex.key",
1117 public_key: "testkey.avbpubkey",
1118 private_key: "testkey.pem",
1119 }
1120
1121 cc_library {
1122 name: "libx",
1123 system_shared_libs: [],
1124 stl: "none",
1125 apex_available: [ "myapex" ],
1126 stubs: {
1127 versions: ["1", "2"],
1128 },
1129 }
1130
1131 cc_library {
1132 name: "libz",
1133 shared_libs: ["libx"],
1134 system_shared_libs: [],
1135 stl: "none",
1136 }
1137 `)
1138
1139 expectLink := func(from, from_variant, to, to_variant string) {
1140 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1141 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1142 }
1143 expectNoLink := func(from, from_variant, to, to_variant string) {
1144 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1145 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1146 }
1147 expectLink("libz", "shared", "libx", "shared_2")
1148 expectNoLink("libz", "shared", "libz", "shared_1")
1149 expectNoLink("libz", "shared", "libz", "shared")
1150}
1151
1152func TestQApexesUseLatestStubsInBundledBuilds(t *testing.T) {
1153 ctx, _ := testApex(t, `
1154 apex {
1155 name: "myapex",
1156 key: "myapex.key",
1157 native_shared_libs: ["libx"],
1158 min_sdk_version: "29",
1159 }
1160
1161 apex_key {
1162 name: "myapex.key",
1163 public_key: "testkey.avbpubkey",
1164 private_key: "testkey.pem",
1165 }
1166
1167 cc_library {
1168 name: "libx",
1169 shared_libs: ["libbar"],
1170 apex_available: [ "myapex" ],
1171 }
1172
1173 cc_library {
1174 name: "libbar",
1175 stubs: {
1176 versions: ["29", "30"],
1177 },
1178 }
1179 `)
1180 expectLink := func(from, from_variant, to, to_variant string) {
1181 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1182 libFlags := ld.Args["libFlags"]
1183 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1184 }
1185 expectLink("libx", "shared_myapex", "libbar", "shared_30")
1186}
1187
1188func TestQTargetApexUseStaticUnwinder(t *testing.T) {
1189 ctx, _ := testApex(t, `
1190 apex {
1191 name: "myapex",
1192 key: "myapex.key",
1193 native_shared_libs: ["libx"],
1194 min_sdk_version: "29",
1195 }
1196
1197 apex_key {
1198 name: "myapex.key",
1199 public_key: "testkey.avbpubkey",
1200 private_key: "testkey.pem",
1201 }
1202
1203 cc_library {
1204 name: "libx",
1205 apex_available: [ "myapex" ],
1206 }
1207
1208 `, withUnbundledBuild)
1209
1210 // ensure apex variant of c++ is linked with static unwinder
1211 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1212 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1213 // note that platform variant is not.
1214 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1215 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1216
1217 libFlags := ctx.ModuleForTests("libx", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1218 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_myapex/libc++.so")
1219 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_29/libc.so") // min_sdk_version applied
1220}
1221
1222func TestInvalidMinSdkVersion(t *testing.T) {
1223 testApexError(t, `"libz" .*: min_sdk_version is set 29.*`, `
1224 apex {
1225 name: "myapex",
1226 key: "myapex.key",
1227 native_shared_libs: ["libx"],
1228 min_sdk_version: "29",
1229 }
1230
1231 apex_key {
1232 name: "myapex.key",
1233 public_key: "testkey.avbpubkey",
1234 private_key: "testkey.pem",
1235 }
1236
1237 cc_library {
1238 name: "libx",
1239 shared_libs: ["libz"],
1240 system_shared_libs: [],
1241 stl: "none",
1242 apex_available: [ "myapex" ],
1243 }
1244
1245 cc_library {
1246 name: "libz",
1247 system_shared_libs: [],
1248 stl: "none",
1249 stubs: {
1250 versions: ["30"],
1251 },
1252 }
1253 `, withUnbundledBuild)
1254
1255 testApexError(t, `"myapex" .*: min_sdk_version: should be .*`, `
1256 apex {
1257 name: "myapex",
1258 key: "myapex.key",
1259 min_sdk_version: "R",
1260 }
1261
1262 apex_key {
1263 name: "myapex.key",
1264 public_key: "testkey.avbpubkey",
1265 private_key: "testkey.pem",
1266 }
1267 `)
1268}
1269
Jiyong Park7c2ee712018-12-07 00:42:25 +09001270func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001271 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001272 apex {
1273 name: "myapex",
1274 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001275 native_shared_libs: ["mylib"],
1276 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001277 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001278 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001279 }
1280
1281 apex_key {
1282 name: "myapex.key",
1283 public_key: "testkey.avbpubkey",
1284 private_key: "testkey.pem",
1285 }
1286
1287 prebuilt_etc {
1288 name: "myetc",
1289 src: "myprebuilt",
1290 sub_dir: "foo/bar",
1291 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001292
1293 cc_library {
1294 name: "mylib",
1295 srcs: ["mylib.cpp"],
1296 relative_install_path: "foo/bar",
1297 system_shared_libs: [],
1298 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001299 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001300 }
1301
1302 cc_binary {
1303 name: "mybin",
1304 srcs: ["mylib.cpp"],
1305 relative_install_path: "foo/bar",
1306 system_shared_libs: [],
1307 static_executable: true,
1308 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001309 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001310 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001311 `)
1312
Sundong Ahnabb64432019-10-22 13:58:29 +09001313 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001314 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1315
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001316 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001317 ensureListContains(t, dirs, "etc")
1318 ensureListContains(t, dirs, "etc/foo")
1319 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001320 ensureListContains(t, dirs, "lib64")
1321 ensureListContains(t, dirs, "lib64/foo")
1322 ensureListContains(t, dirs, "lib64/foo/bar")
1323 ensureListContains(t, dirs, "lib")
1324 ensureListContains(t, dirs, "lib/foo")
1325 ensureListContains(t, dirs, "lib/foo/bar")
1326
Jiyong Parkbd13e442019-03-15 18:10:35 +09001327 ensureListContains(t, dirs, "bin")
1328 ensureListContains(t, dirs, "bin/foo")
1329 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001330}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001331
1332func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001333 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001334 apex {
1335 name: "myapex",
1336 key: "myapex.key",
1337 native_shared_libs: ["mylib"],
1338 use_vendor: true,
1339 }
1340
1341 apex_key {
1342 name: "myapex.key",
1343 public_key: "testkey.avbpubkey",
1344 private_key: "testkey.pem",
1345 }
1346
1347 cc_library {
1348 name: "mylib",
1349 srcs: ["mylib.cpp"],
1350 shared_libs: ["mylib2"],
1351 system_shared_libs: [],
1352 vendor_available: true,
1353 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001354 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001355 }
1356
1357 cc_library {
1358 name: "mylib2",
1359 srcs: ["mylib.cpp"],
1360 system_shared_libs: [],
1361 vendor_available: true,
1362 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001363 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001364 }
Jooyung Handc782442019-11-01 03:14:38 +09001365 `, func(fs map[string][]byte, config android.Config) {
1366 setUseVendorWhitelistForTest(config, []string{"myapex"})
1367 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001368
1369 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001370 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001371 for _, implicit := range i.Implicits {
1372 inputsList = append(inputsList, implicit.String())
1373 }
1374 }
1375 inputsString := strings.Join(inputsList, " ")
1376
1377 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001378 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1379 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001380
1381 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001382 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1383 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001384}
Jiyong Park16e91a02018-12-20 18:18:08 +09001385
Jooyung Handc782442019-11-01 03:14:38 +09001386func TestUseVendorRestriction(t *testing.T) {
1387 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1388 apex {
1389 name: "myapex",
1390 key: "myapex.key",
1391 use_vendor: true,
1392 }
1393 apex_key {
1394 name: "myapex.key",
1395 public_key: "testkey.avbpubkey",
1396 private_key: "testkey.pem",
1397 }
1398 `, func(fs map[string][]byte, config android.Config) {
1399 setUseVendorWhitelistForTest(config, []string{""})
1400 })
1401 // no error with whitelist
1402 testApex(t, `
1403 apex {
1404 name: "myapex",
1405 key: "myapex.key",
1406 use_vendor: true,
1407 }
1408 apex_key {
1409 name: "myapex.key",
1410 public_key: "testkey.avbpubkey",
1411 private_key: "testkey.pem",
1412 }
1413 `, func(fs map[string][]byte, config android.Config) {
1414 setUseVendorWhitelistForTest(config, []string{"myapex"})
1415 })
1416}
1417
Jooyung Han5c998b92019-06-27 11:30:33 +09001418func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1419 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1420 apex {
1421 name: "myapex",
1422 key: "myapex.key",
1423 native_shared_libs: ["mylib"],
1424 use_vendor: true,
1425 }
1426
1427 apex_key {
1428 name: "myapex.key",
1429 public_key: "testkey.avbpubkey",
1430 private_key: "testkey.pem",
1431 }
1432
1433 cc_library {
1434 name: "mylib",
1435 srcs: ["mylib.cpp"],
1436 system_shared_libs: [],
1437 stl: "none",
1438 }
1439 `)
1440}
1441
Jiyong Park16e91a02018-12-20 18:18:08 +09001442func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001443 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001444 apex {
1445 name: "myapex",
1446 key: "myapex.key",
1447 native_shared_libs: ["mylib"],
1448 }
1449
1450 apex_key {
1451 name: "myapex.key",
1452 public_key: "testkey.avbpubkey",
1453 private_key: "testkey.pem",
1454 }
1455
1456 cc_library {
1457 name: "mylib",
1458 srcs: ["mylib.cpp"],
1459 system_shared_libs: [],
1460 stl: "none",
1461 stubs: {
1462 versions: ["1", "2", "3"],
1463 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001464 apex_available: [
1465 "//apex_available:platform",
1466 "myapex",
1467 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001468 }
1469
1470 cc_binary {
1471 name: "not_in_apex",
1472 srcs: ["mylib.cpp"],
1473 static_libs: ["mylib"],
1474 static_executable: true,
1475 system_shared_libs: [],
1476 stl: "none",
1477 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001478 `)
1479
Colin Cross7113d202019-11-20 16:39:12 -08001480 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001481
1482 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001483 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001484}
Jiyong Park9335a262018-12-24 11:31:58 +09001485
1486func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001487 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001488 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001489 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001490 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001491 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001492 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001493 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001494 }
1495
1496 cc_library {
1497 name: "mylib",
1498 srcs: ["mylib.cpp"],
1499 system_shared_libs: [],
1500 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001501 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001502 }
1503
1504 apex_key {
1505 name: "myapex.key",
1506 public_key: "testkey.avbpubkey",
1507 private_key: "testkey.pem",
1508 }
1509
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001510 android_app_certificate {
1511 name: "myapex.certificate",
1512 certificate: "testkey",
1513 }
1514
1515 android_app_certificate {
1516 name: "myapex.certificate.override",
1517 certificate: "testkey.override",
1518 }
1519
Jiyong Park9335a262018-12-24 11:31:58 +09001520 `)
1521
1522 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001523 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001524
1525 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1526 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1527 "vendor/foo/devkeys/testkey.avbpubkey")
1528 }
1529 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1530 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1531 "vendor/foo/devkeys/testkey.pem")
1532 }
1533
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001534 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001535 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001536 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001537 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001538 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001539 }
1540}
Jiyong Park58e364a2019-01-19 19:24:06 +09001541
Jooyung Hanf121a652019-12-17 14:30:11 +09001542func TestCertificate(t *testing.T) {
1543 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1544 ctx, _ := testApex(t, `
1545 apex {
1546 name: "myapex",
1547 key: "myapex.key",
1548 }
1549 apex_key {
1550 name: "myapex.key",
1551 public_key: "testkey.avbpubkey",
1552 private_key: "testkey.pem",
1553 }`)
1554 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1555 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1556 if actual := rule.Args["certificates"]; actual != expected {
1557 t.Errorf("certificates should be %q, not %q", expected, actual)
1558 }
1559 })
1560 t.Run("override when unspecified", func(t *testing.T) {
1561 ctx, _ := testApex(t, `
1562 apex {
1563 name: "myapex_keytest",
1564 key: "myapex.key",
1565 file_contexts: ":myapex-file_contexts",
1566 }
1567 apex_key {
1568 name: "myapex.key",
1569 public_key: "testkey.avbpubkey",
1570 private_key: "testkey.pem",
1571 }
1572 android_app_certificate {
1573 name: "myapex.certificate.override",
1574 certificate: "testkey.override",
1575 }`)
1576 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1577 expected := "testkey.override.x509.pem testkey.override.pk8"
1578 if actual := rule.Args["certificates"]; actual != expected {
1579 t.Errorf("certificates should be %q, not %q", expected, actual)
1580 }
1581 })
1582 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1583 ctx, _ := testApex(t, `
1584 apex {
1585 name: "myapex",
1586 key: "myapex.key",
1587 certificate: ":myapex.certificate",
1588 }
1589 apex_key {
1590 name: "myapex.key",
1591 public_key: "testkey.avbpubkey",
1592 private_key: "testkey.pem",
1593 }
1594 android_app_certificate {
1595 name: "myapex.certificate",
1596 certificate: "testkey",
1597 }`)
1598 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1599 expected := "testkey.x509.pem testkey.pk8"
1600 if actual := rule.Args["certificates"]; actual != expected {
1601 t.Errorf("certificates should be %q, not %q", expected, actual)
1602 }
1603 })
1604 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1605 ctx, _ := testApex(t, `
1606 apex {
1607 name: "myapex_keytest",
1608 key: "myapex.key",
1609 file_contexts: ":myapex-file_contexts",
1610 certificate: ":myapex.certificate",
1611 }
1612 apex_key {
1613 name: "myapex.key",
1614 public_key: "testkey.avbpubkey",
1615 private_key: "testkey.pem",
1616 }
1617 android_app_certificate {
1618 name: "myapex.certificate.override",
1619 certificate: "testkey.override",
1620 }`)
1621 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1622 expected := "testkey.override.x509.pem testkey.override.pk8"
1623 if actual := rule.Args["certificates"]; actual != expected {
1624 t.Errorf("certificates should be %q, not %q", expected, actual)
1625 }
1626 })
1627 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1628 ctx, _ := testApex(t, `
1629 apex {
1630 name: "myapex",
1631 key: "myapex.key",
1632 certificate: "testkey",
1633 }
1634 apex_key {
1635 name: "myapex.key",
1636 public_key: "testkey.avbpubkey",
1637 private_key: "testkey.pem",
1638 }`)
1639 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1640 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1641 if actual := rule.Args["certificates"]; actual != expected {
1642 t.Errorf("certificates should be %q, not %q", expected, actual)
1643 }
1644 })
1645 t.Run("override when specified as <name>", func(t *testing.T) {
1646 ctx, _ := testApex(t, `
1647 apex {
1648 name: "myapex_keytest",
1649 key: "myapex.key",
1650 file_contexts: ":myapex-file_contexts",
1651 certificate: "testkey",
1652 }
1653 apex_key {
1654 name: "myapex.key",
1655 public_key: "testkey.avbpubkey",
1656 private_key: "testkey.pem",
1657 }
1658 android_app_certificate {
1659 name: "myapex.certificate.override",
1660 certificate: "testkey.override",
1661 }`)
1662 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1663 expected := "testkey.override.x509.pem testkey.override.pk8"
1664 if actual := rule.Args["certificates"]; actual != expected {
1665 t.Errorf("certificates should be %q, not %q", expected, actual)
1666 }
1667 })
1668}
1669
Jiyong Park58e364a2019-01-19 19:24:06 +09001670func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001671 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001672 apex {
1673 name: "myapex",
1674 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001675 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001676 }
1677
1678 apex {
1679 name: "otherapex",
1680 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001681 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001682 }
1683
1684 apex_key {
1685 name: "myapex.key",
1686 public_key: "testkey.avbpubkey",
1687 private_key: "testkey.pem",
1688 }
1689
1690 cc_library {
1691 name: "mylib",
1692 srcs: ["mylib.cpp"],
1693 system_shared_libs: [],
1694 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001695 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001696 "myapex",
1697 "otherapex",
1698 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001699 }
Jooyung Han68e511e2020-03-02 17:44:33 +09001700 cc_library {
1701 name: "mylib2",
1702 srcs: ["mylib.cpp"],
1703 system_shared_libs: [],
1704 stl: "none",
1705 apex_available: [
1706 "myapex",
1707 "otherapex",
1708 ],
1709 use_apex_name_macro: true,
1710 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001711 `)
1712
Jooyung Han68e511e2020-03-02 17:44:33 +09001713 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001714 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001715 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han68e511e2020-03-02 17:44:33 +09001716
1717 // APEX variant has __ANDROID_APEX__ defined
1718 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1719 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001720 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han68e511e2020-03-02 17:44:33 +09001721
1722 // APEX variant has __ANDROID_APEX__ defined
1723 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1724 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001725 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001726
Jooyung Han68e511e2020-03-02 17:44:33 +09001727 // When cc_library sets use_apex_name_macro: true
1728 // apex variants define additional macro to distinguish which apex variant it is built for
1729
1730 // non-APEX variant does not have __ANDROID_APEX__ defined
1731 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1732 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1733
1734 // APEX variant has __ANDROID_APEX__ defined
1735 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001736 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001737 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1738 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001739
Jooyung Han68e511e2020-03-02 17:44:33 +09001740 // APEX variant has __ANDROID_APEX__ defined
1741 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001742 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001743 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1744 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001745}
Jiyong Park7e636d02019-01-28 16:16:54 +09001746
1747func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001748 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001749 apex {
1750 name: "myapex",
1751 key: "myapex.key",
1752 native_shared_libs: ["mylib"],
1753 }
1754
1755 apex_key {
1756 name: "myapex.key",
1757 public_key: "testkey.avbpubkey",
1758 private_key: "testkey.pem",
1759 }
1760
1761 cc_library_headers {
1762 name: "mylib_headers",
1763 export_include_dirs: ["my_include"],
1764 system_shared_libs: [],
1765 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001766 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001767 }
1768
1769 cc_library {
1770 name: "mylib",
1771 srcs: ["mylib.cpp"],
1772 system_shared_libs: [],
1773 stl: "none",
1774 header_libs: ["mylib_headers"],
1775 export_header_lib_headers: ["mylib_headers"],
1776 stubs: {
1777 versions: ["1", "2", "3"],
1778 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001779 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001780 }
1781
1782 cc_library {
1783 name: "otherlib",
1784 srcs: ["mylib.cpp"],
1785 system_shared_libs: [],
1786 stl: "none",
1787 shared_libs: ["mylib"],
1788 }
1789 `)
1790
Colin Cross7113d202019-11-20 16:39:12 -08001791 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001792
1793 // Ensure that the include path of the header lib is exported to 'otherlib'
1794 ensureContains(t, cFlags, "-Imy_include")
1795}
Alex Light9670d332019-01-29 18:07:33 -08001796
Jiyong Park7cd10e32020-01-14 09:22:18 +09001797type fileInApex struct {
1798 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001799 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001800 isLink bool
1801}
1802
Jooyung Hana57af4a2020-01-23 05:36:59 +00001803func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001804 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001805 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001806 copyCmds := apexRule.Args["copy_commands"]
1807 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001808 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001809 for _, cmd := range strings.Split(copyCmds, "&&") {
1810 cmd = strings.TrimSpace(cmd)
1811 if cmd == "" {
1812 continue
1813 }
1814 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001815 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001816 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001817 switch terms[0] {
1818 case "mkdir":
1819 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001820 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001821 t.Fatal("copyCmds contains invalid cp command", cmd)
1822 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001823 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001824 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001825 isLink = false
1826 case "ln":
1827 if len(terms) != 3 && len(terms) != 4 {
1828 // ln LINK TARGET or ln -s LINK TARGET
1829 t.Fatal("copyCmds contains invalid ln command", cmd)
1830 }
1831 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001832 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001833 isLink = true
1834 default:
1835 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1836 }
1837 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001838 index := strings.Index(dst, imageApexDir)
1839 if index == -1 {
1840 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1841 }
1842 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001843 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001844 }
1845 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001846 return ret
1847}
1848
Jooyung Hana57af4a2020-01-23 05:36:59 +00001849func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1850 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001851 var failed bool
1852 var surplus []string
1853 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001854 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09001855 for _, expected := range files {
1856 if matched, _ := path.Match(expected, file.path); matched {
1857 filesMatched[expected] = true
1858 return
1859 }
1860 }
1861 surplus = append(surplus, file.path)
1862 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001863
Jooyung Han31c470b2019-10-18 16:26:59 +09001864 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001865 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001866 t.Log("surplus files", surplus)
1867 failed = true
1868 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001869
1870 if len(files) > len(filesMatched) {
1871 var missing []string
1872 for _, expected := range files {
1873 if !filesMatched[expected] {
1874 missing = append(missing, expected)
1875 }
1876 }
1877 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001878 t.Log("missing files", missing)
1879 failed = true
1880 }
1881 if failed {
1882 t.Fail()
1883 }
1884}
1885
Jooyung Han344d5432019-08-23 11:17:39 +09001886func TestVndkApexCurrent(t *testing.T) {
1887 ctx, _ := testApex(t, `
1888 apex_vndk {
1889 name: "myapex",
1890 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001891 }
1892
1893 apex_key {
1894 name: "myapex.key",
1895 public_key: "testkey.avbpubkey",
1896 private_key: "testkey.pem",
1897 }
1898
1899 cc_library {
1900 name: "libvndk",
1901 srcs: ["mylib.cpp"],
1902 vendor_available: true,
1903 vndk: {
1904 enabled: true,
1905 },
1906 system_shared_libs: [],
1907 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001908 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001909 }
1910
1911 cc_library {
1912 name: "libvndksp",
1913 srcs: ["mylib.cpp"],
1914 vendor_available: true,
1915 vndk: {
1916 enabled: true,
1917 support_system_process: true,
1918 },
1919 system_shared_libs: [],
1920 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001921 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001922 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001923 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001924
Jooyung Hana57af4a2020-01-23 05:36:59 +00001925 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001926 "lib/libvndk.so",
1927 "lib/libvndksp.so",
1928 "lib64/libvndk.so",
1929 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001930 "etc/llndk.libraries.VER.txt",
1931 "etc/vndkcore.libraries.VER.txt",
1932 "etc/vndksp.libraries.VER.txt",
1933 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001934 })
Jooyung Han344d5432019-08-23 11:17:39 +09001935}
1936
1937func TestVndkApexWithPrebuilt(t *testing.T) {
1938 ctx, _ := testApex(t, `
1939 apex_vndk {
1940 name: "myapex",
1941 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001942 }
1943
1944 apex_key {
1945 name: "myapex.key",
1946 public_key: "testkey.avbpubkey",
1947 private_key: "testkey.pem",
1948 }
1949
1950 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001951 name: "libvndk",
1952 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001953 vendor_available: true,
1954 vndk: {
1955 enabled: true,
1956 },
1957 system_shared_libs: [],
1958 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001959 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001960 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001961
1962 cc_prebuilt_library_shared {
1963 name: "libvndk.arm",
1964 srcs: ["libvndk.arm.so"],
1965 vendor_available: true,
1966 vndk: {
1967 enabled: true,
1968 },
1969 enabled: false,
1970 arch: {
1971 arm: {
1972 enabled: true,
1973 },
1974 },
1975 system_shared_libs: [],
1976 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001977 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001978 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001979 `+vndkLibrariesTxtFiles("current"),
1980 withFiles(map[string][]byte{
1981 "libvndk.so": nil,
1982 "libvndk.arm.so": nil,
1983 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001984
Jooyung Hana57af4a2020-01-23 05:36:59 +00001985 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001986 "lib/libvndk.so",
1987 "lib/libvndk.arm.so",
1988 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001989 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001990 })
Jooyung Han344d5432019-08-23 11:17:39 +09001991}
1992
Jooyung Han39edb6c2019-11-06 16:53:07 +09001993func vndkLibrariesTxtFiles(vers ...string) (result string) {
1994 for _, v := range vers {
1995 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001996 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001997 result += `
1998 vndk_libraries_txt {
1999 name: "` + txt + `.libraries.txt",
2000 }
2001 `
2002 }
2003 } else {
2004 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2005 result += `
2006 prebuilt_etc {
2007 name: "` + txt + `.libraries.` + v + `.txt",
2008 src: "dummy.txt",
2009 }
2010 `
2011 }
2012 }
2013 }
2014 return
2015}
2016
Jooyung Han344d5432019-08-23 11:17:39 +09002017func TestVndkApexVersion(t *testing.T) {
2018 ctx, _ := testApex(t, `
2019 apex_vndk {
2020 name: "myapex_v27",
2021 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002022 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002023 vndk_version: "27",
2024 }
2025
2026 apex_key {
2027 name: "myapex.key",
2028 public_key: "testkey.avbpubkey",
2029 private_key: "testkey.pem",
2030 }
2031
Jooyung Han31c470b2019-10-18 16:26:59 +09002032 vndk_prebuilt_shared {
2033 name: "libvndk27",
2034 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002035 vendor_available: true,
2036 vndk: {
2037 enabled: true,
2038 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002039 target_arch: "arm64",
2040 arch: {
2041 arm: {
2042 srcs: ["libvndk27_arm.so"],
2043 },
2044 arm64: {
2045 srcs: ["libvndk27_arm64.so"],
2046 },
2047 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002048 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002049 }
2050
2051 vndk_prebuilt_shared {
2052 name: "libvndk27",
2053 version: "27",
2054 vendor_available: true,
2055 vndk: {
2056 enabled: true,
2057 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002058 target_arch: "x86_64",
2059 arch: {
2060 x86: {
2061 srcs: ["libvndk27_x86.so"],
2062 },
2063 x86_64: {
2064 srcs: ["libvndk27_x86_64.so"],
2065 },
2066 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002067 }
2068 `+vndkLibrariesTxtFiles("27"),
2069 withFiles(map[string][]byte{
2070 "libvndk27_arm.so": nil,
2071 "libvndk27_arm64.so": nil,
2072 "libvndk27_x86.so": nil,
2073 "libvndk27_x86_64.so": nil,
2074 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002075
Jooyung Hana57af4a2020-01-23 05:36:59 +00002076 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002077 "lib/libvndk27_arm.so",
2078 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002079 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002080 })
Jooyung Han344d5432019-08-23 11:17:39 +09002081}
2082
2083func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2084 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2085 apex_vndk {
2086 name: "myapex_v27",
2087 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002088 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002089 vndk_version: "27",
2090 }
2091 apex_vndk {
2092 name: "myapex_v27_other",
2093 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002094 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002095 vndk_version: "27",
2096 }
2097
2098 apex_key {
2099 name: "myapex.key",
2100 public_key: "testkey.avbpubkey",
2101 private_key: "testkey.pem",
2102 }
2103
2104 cc_library {
2105 name: "libvndk",
2106 srcs: ["mylib.cpp"],
2107 vendor_available: true,
2108 vndk: {
2109 enabled: true,
2110 },
2111 system_shared_libs: [],
2112 stl: "none",
2113 }
2114
2115 vndk_prebuilt_shared {
2116 name: "libvndk",
2117 version: "27",
2118 vendor_available: true,
2119 vndk: {
2120 enabled: true,
2121 },
2122 srcs: ["libvndk.so"],
2123 }
2124 `, withFiles(map[string][]byte{
2125 "libvndk.so": nil,
2126 }))
2127}
2128
Jooyung Han90eee022019-10-01 20:02:42 +09002129func TestVndkApexNameRule(t *testing.T) {
2130 ctx, _ := testApex(t, `
2131 apex_vndk {
2132 name: "myapex",
2133 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002134 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002135 }
2136 apex_vndk {
2137 name: "myapex_v28",
2138 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002139 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002140 vndk_version: "28",
2141 }
2142 apex_key {
2143 name: "myapex.key",
2144 public_key: "testkey.avbpubkey",
2145 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002146 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002147
2148 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002149 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002150 actual := proptools.String(bundle.properties.Apex_name)
2151 if !reflect.DeepEqual(actual, expected) {
2152 t.Errorf("Got '%v', expected '%v'", actual, expected)
2153 }
2154 }
2155
2156 assertApexName("com.android.vndk.vVER", "myapex")
2157 assertApexName("com.android.vndk.v28", "myapex_v28")
2158}
2159
Jooyung Han344d5432019-08-23 11:17:39 +09002160func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2161 ctx, _ := testApex(t, `
2162 apex_vndk {
2163 name: "myapex",
2164 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002165 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002166 }
2167
2168 apex_key {
2169 name: "myapex.key",
2170 public_key: "testkey.avbpubkey",
2171 private_key: "testkey.pem",
2172 }
2173
2174 cc_library {
2175 name: "libvndk",
2176 srcs: ["mylib.cpp"],
2177 vendor_available: true,
2178 native_bridge_supported: true,
2179 host_supported: true,
2180 vndk: {
2181 enabled: true,
2182 },
2183 system_shared_libs: [],
2184 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002185 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002186 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002187 `+vndkLibrariesTxtFiles("current"),
2188 withTargets(map[android.OsType][]android.Target{
2189 android.Android: []android.Target{
2190 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2191 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2192 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2193 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2194 },
2195 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002196
Jooyung Hana57af4a2020-01-23 05:36:59 +00002197 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002198 "lib/libvndk.so",
2199 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002200 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002201 })
Jooyung Han344d5432019-08-23 11:17:39 +09002202}
2203
2204func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2205 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2206 apex_vndk {
2207 name: "myapex",
2208 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002209 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002210 native_bridge_supported: true,
2211 }
2212
2213 apex_key {
2214 name: "myapex.key",
2215 public_key: "testkey.avbpubkey",
2216 private_key: "testkey.pem",
2217 }
2218
2219 cc_library {
2220 name: "libvndk",
2221 srcs: ["mylib.cpp"],
2222 vendor_available: true,
2223 native_bridge_supported: true,
2224 host_supported: true,
2225 vndk: {
2226 enabled: true,
2227 },
2228 system_shared_libs: [],
2229 stl: "none",
2230 }
2231 `)
2232}
2233
Jooyung Han31c470b2019-10-18 16:26:59 +09002234func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002235 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002236 apex_vndk {
2237 name: "myapex_v27",
2238 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002239 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002240 vndk_version: "27",
2241 }
2242
2243 apex_key {
2244 name: "myapex.key",
2245 public_key: "testkey.avbpubkey",
2246 private_key: "testkey.pem",
2247 }
2248
2249 vndk_prebuilt_shared {
2250 name: "libvndk27",
2251 version: "27",
2252 target_arch: "arm",
2253 vendor_available: true,
2254 vndk: {
2255 enabled: true,
2256 },
2257 arch: {
2258 arm: {
2259 srcs: ["libvndk27.so"],
2260 }
2261 },
2262 }
2263
2264 vndk_prebuilt_shared {
2265 name: "libvndk27",
2266 version: "27",
2267 target_arch: "arm",
2268 binder32bit: true,
2269 vendor_available: true,
2270 vndk: {
2271 enabled: true,
2272 },
2273 arch: {
2274 arm: {
2275 srcs: ["libvndk27binder32.so"],
2276 }
2277 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002278 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002279 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002280 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002281 withFiles(map[string][]byte{
2282 "libvndk27.so": nil,
2283 "libvndk27binder32.so": nil,
2284 }),
2285 withBinder32bit,
2286 withTargets(map[android.OsType][]android.Target{
2287 android.Android: []android.Target{
2288 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2289 },
2290 }),
2291 )
2292
Jooyung Hana57af4a2020-01-23 05:36:59 +00002293 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002294 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002295 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002296 })
2297}
2298
Jooyung Hane1633032019-08-01 17:41:43 +09002299func TestDependenciesInApexManifest(t *testing.T) {
2300 ctx, _ := testApex(t, `
2301 apex {
2302 name: "myapex_nodep",
2303 key: "myapex.key",
2304 native_shared_libs: ["lib_nodep"],
2305 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002306 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002307 }
2308
2309 apex {
2310 name: "myapex_dep",
2311 key: "myapex.key",
2312 native_shared_libs: ["lib_dep"],
2313 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002314 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002315 }
2316
2317 apex {
2318 name: "myapex_provider",
2319 key: "myapex.key",
2320 native_shared_libs: ["libfoo"],
2321 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002322 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002323 }
2324
2325 apex {
2326 name: "myapex_selfcontained",
2327 key: "myapex.key",
2328 native_shared_libs: ["lib_dep", "libfoo"],
2329 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002330 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002331 }
2332
2333 apex_key {
2334 name: "myapex.key",
2335 public_key: "testkey.avbpubkey",
2336 private_key: "testkey.pem",
2337 }
2338
2339 cc_library {
2340 name: "lib_nodep",
2341 srcs: ["mylib.cpp"],
2342 system_shared_libs: [],
2343 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002344 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002345 }
2346
2347 cc_library {
2348 name: "lib_dep",
2349 srcs: ["mylib.cpp"],
2350 shared_libs: ["libfoo"],
2351 system_shared_libs: [],
2352 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002353 apex_available: [
2354 "myapex_dep",
2355 "myapex_provider",
2356 "myapex_selfcontained",
2357 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002358 }
2359
2360 cc_library {
2361 name: "libfoo",
2362 srcs: ["mytest.cpp"],
2363 stubs: {
2364 versions: ["1"],
2365 },
2366 system_shared_libs: [],
2367 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002368 apex_available: [
2369 "myapex_provider",
2370 "myapex_selfcontained",
2371 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002372 }
2373 `)
2374
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002375 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002376 var provideNativeLibs, requireNativeLibs []string
2377
Sundong Ahnabb64432019-10-22 13:58:29 +09002378 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002379 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2380 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002381 ensureListEmpty(t, provideNativeLibs)
2382 ensureListEmpty(t, requireNativeLibs)
2383
Sundong Ahnabb64432019-10-22 13:58:29 +09002384 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002385 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2386 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002387 ensureListEmpty(t, provideNativeLibs)
2388 ensureListContains(t, requireNativeLibs, "libfoo.so")
2389
Sundong Ahnabb64432019-10-22 13:58:29 +09002390 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002391 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2392 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002393 ensureListContains(t, provideNativeLibs, "libfoo.so")
2394 ensureListEmpty(t, requireNativeLibs)
2395
Sundong Ahnabb64432019-10-22 13:58:29 +09002396 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002397 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2398 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002399 ensureListContains(t, provideNativeLibs, "libfoo.so")
2400 ensureListEmpty(t, requireNativeLibs)
2401}
2402
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002403func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002404 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002405 apex {
2406 name: "myapex",
2407 key: "myapex.key",
2408 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002409 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002410 }
2411
2412 apex_key {
2413 name: "myapex.key",
2414 public_key: "testkey.avbpubkey",
2415 private_key: "testkey.pem",
2416 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002417
2418 cc_library {
2419 name: "mylib",
2420 srcs: ["mylib.cpp"],
2421 system_shared_libs: [],
2422 stl: "none",
2423 apex_available: [
2424 "//apex_available:platform",
2425 "myapex",
2426 ],
2427 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002428 `)
2429
Sundong Ahnabb64432019-10-22 13:58:29 +09002430 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002431 apexManifestRule := module.Rule("apexManifestRule")
2432 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2433 apexRule := module.Rule("apexRule")
2434 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002435
2436 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2437 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2438 name := apexBundle.BaseModuleName()
2439 prefix := "TARGET_"
2440 var builder strings.Builder
2441 data.Custom(&builder, name, prefix, "", data)
2442 androidMk := builder.String()
2443 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2444 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002445}
2446
Alex Light0851b882019-02-07 13:20:53 -08002447func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002448 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002449 apex {
2450 name: "myapex",
2451 key: "myapex.key",
2452 native_shared_libs: ["mylib_common"],
2453 }
2454
2455 apex_key {
2456 name: "myapex.key",
2457 public_key: "testkey.avbpubkey",
2458 private_key: "testkey.pem",
2459 }
2460
2461 cc_library {
2462 name: "mylib_common",
2463 srcs: ["mylib.cpp"],
2464 system_shared_libs: [],
2465 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002466 apex_available: [
2467 "//apex_available:platform",
2468 "myapex",
2469 ],
Alex Light0851b882019-02-07 13:20:53 -08002470 }
2471 `)
2472
Sundong Ahnabb64432019-10-22 13:58:29 +09002473 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002474 apexRule := module.Rule("apexRule")
2475 copyCmds := apexRule.Args["copy_commands"]
2476
2477 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2478 t.Log("Apex was a test apex!")
2479 t.Fail()
2480 }
2481 // Ensure that main rule creates an output
2482 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2483
2484 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002485 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002486
2487 // Ensure that both direct and indirect deps are copied into apex
2488 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2489
Colin Cross7113d202019-11-20 16:39:12 -08002490 // Ensure that the platform variant ends with _shared
2491 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002492
2493 if !android.InAnyApex("mylib_common") {
2494 t.Log("Found mylib_common not in any apex!")
2495 t.Fail()
2496 }
2497}
2498
2499func TestTestApex(t *testing.T) {
2500 if android.InAnyApex("mylib_common_test") {
2501 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!")
2502 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002503 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002504 apex_test {
2505 name: "myapex",
2506 key: "myapex.key",
2507 native_shared_libs: ["mylib_common_test"],
2508 }
2509
2510 apex_key {
2511 name: "myapex.key",
2512 public_key: "testkey.avbpubkey",
2513 private_key: "testkey.pem",
2514 }
2515
2516 cc_library {
2517 name: "mylib_common_test",
2518 srcs: ["mylib.cpp"],
2519 system_shared_libs: [],
2520 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002521 // TODO: remove //apex_available:platform
2522 apex_available: [
2523 "//apex_available:platform",
2524 "myapex",
2525 ],
Alex Light0851b882019-02-07 13:20:53 -08002526 }
2527 `)
2528
Sundong Ahnabb64432019-10-22 13:58:29 +09002529 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002530 apexRule := module.Rule("apexRule")
2531 copyCmds := apexRule.Args["copy_commands"]
2532
2533 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2534 t.Log("Apex was not a test apex!")
2535 t.Fail()
2536 }
2537 // Ensure that main rule creates an output
2538 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2539
2540 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002541 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002542
2543 // Ensure that both direct and indirect deps are copied into apex
2544 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2545
Colin Cross7113d202019-11-20 16:39:12 -08002546 // Ensure that the platform variant ends with _shared
2547 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002548}
2549
Alex Light9670d332019-01-29 18:07:33 -08002550func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002551 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002552 apex {
2553 name: "myapex",
2554 key: "myapex.key",
2555 multilib: {
2556 first: {
2557 native_shared_libs: ["mylib_common"],
2558 }
2559 },
2560 target: {
2561 android: {
2562 multilib: {
2563 first: {
2564 native_shared_libs: ["mylib"],
2565 }
2566 }
2567 },
2568 host: {
2569 multilib: {
2570 first: {
2571 native_shared_libs: ["mylib2"],
2572 }
2573 }
2574 }
2575 }
2576 }
2577
2578 apex_key {
2579 name: "myapex.key",
2580 public_key: "testkey.avbpubkey",
2581 private_key: "testkey.pem",
2582 }
2583
2584 cc_library {
2585 name: "mylib",
2586 srcs: ["mylib.cpp"],
2587 system_shared_libs: [],
2588 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002589 // TODO: remove //apex_available:platform
2590 apex_available: [
2591 "//apex_available:platform",
2592 "myapex",
2593 ],
Alex Light9670d332019-01-29 18:07:33 -08002594 }
2595
2596 cc_library {
2597 name: "mylib_common",
2598 srcs: ["mylib.cpp"],
2599 system_shared_libs: [],
2600 stl: "none",
2601 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002602 // TODO: remove //apex_available:platform
2603 apex_available: [
2604 "//apex_available:platform",
2605 "myapex",
2606 ],
Alex Light9670d332019-01-29 18:07:33 -08002607 }
2608
2609 cc_library {
2610 name: "mylib2",
2611 srcs: ["mylib.cpp"],
2612 system_shared_libs: [],
2613 stl: "none",
2614 compile_multilib: "first",
2615 }
2616 `)
2617
Sundong Ahnabb64432019-10-22 13:58:29 +09002618 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002619 copyCmds := apexRule.Args["copy_commands"]
2620
2621 // Ensure that main rule creates an output
2622 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2623
2624 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002625 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2626 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2627 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002628
2629 // Ensure that both direct and indirect deps are copied into apex
2630 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2631 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2632 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2633
Colin Cross7113d202019-11-20 16:39:12 -08002634 // Ensure that the platform variant ends with _shared
2635 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2636 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2637 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002638}
Jiyong Park04480cf2019-02-06 00:16:29 +09002639
2640func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002641 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002642 apex {
2643 name: "myapex",
2644 key: "myapex.key",
2645 binaries: ["myscript"],
2646 }
2647
2648 apex_key {
2649 name: "myapex.key",
2650 public_key: "testkey.avbpubkey",
2651 private_key: "testkey.pem",
2652 }
2653
2654 sh_binary {
2655 name: "myscript",
2656 src: "mylib.cpp",
2657 filename: "myscript.sh",
2658 sub_dir: "script",
2659 }
2660 `)
2661
Sundong Ahnabb64432019-10-22 13:58:29 +09002662 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002663 copyCmds := apexRule.Args["copy_commands"]
2664
2665 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2666}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002667
Jooyung Han91df2082019-11-20 01:49:42 +09002668func TestApexInVariousPartition(t *testing.T) {
2669 testcases := []struct {
2670 propName, parition, flattenedPartition string
2671 }{
2672 {"", "system", "system_ext"},
2673 {"product_specific: true", "product", "product"},
2674 {"soc_specific: true", "vendor", "vendor"},
2675 {"proprietary: true", "vendor", "vendor"},
2676 {"vendor: true", "vendor", "vendor"},
2677 {"system_ext_specific: true", "system_ext", "system_ext"},
2678 }
2679 for _, tc := range testcases {
2680 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2681 ctx, _ := testApex(t, `
2682 apex {
2683 name: "myapex",
2684 key: "myapex.key",
2685 `+tc.propName+`
2686 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002687
Jooyung Han91df2082019-11-20 01:49:42 +09002688 apex_key {
2689 name: "myapex.key",
2690 public_key: "testkey.avbpubkey",
2691 private_key: "testkey.pem",
2692 }
2693 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002694
Jooyung Han91df2082019-11-20 01:49:42 +09002695 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2696 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2697 actual := apex.installDir.String()
2698 if actual != expected {
2699 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2700 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002701
Jooyung Han91df2082019-11-20 01:49:42 +09002702 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2703 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2704 actual = flattened.installDir.String()
2705 if actual != expected {
2706 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2707 }
2708 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002709 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002710}
Jiyong Park67882562019-03-21 01:11:21 +09002711
Jooyung Han54aca7b2019-11-20 02:26:02 +09002712func TestFileContexts(t *testing.T) {
2713 ctx, _ := testApex(t, `
2714 apex {
2715 name: "myapex",
2716 key: "myapex.key",
2717 }
2718
2719 apex_key {
2720 name: "myapex.key",
2721 public_key: "testkey.avbpubkey",
2722 private_key: "testkey.pem",
2723 }
2724 `)
2725 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2726 apexRule := module.Rule("apexRule")
2727 actual := apexRule.Args["file_contexts"]
2728 expected := "system/sepolicy/apex/myapex-file_contexts"
2729 if actual != expected {
2730 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2731 }
2732
2733 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2734 apex {
2735 name: "myapex",
2736 key: "myapex.key",
2737 file_contexts: "my_own_file_contexts",
2738 }
2739
2740 apex_key {
2741 name: "myapex.key",
2742 public_key: "testkey.avbpubkey",
2743 private_key: "testkey.pem",
2744 }
2745 `, withFiles(map[string][]byte{
2746 "my_own_file_contexts": nil,
2747 }))
2748
2749 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2750 apex {
2751 name: "myapex",
2752 key: "myapex.key",
2753 product_specific: true,
2754 file_contexts: "product_specific_file_contexts",
2755 }
2756
2757 apex_key {
2758 name: "myapex.key",
2759 public_key: "testkey.avbpubkey",
2760 private_key: "testkey.pem",
2761 }
2762 `)
2763
2764 ctx, _ = testApex(t, `
2765 apex {
2766 name: "myapex",
2767 key: "myapex.key",
2768 product_specific: true,
2769 file_contexts: "product_specific_file_contexts",
2770 }
2771
2772 apex_key {
2773 name: "myapex.key",
2774 public_key: "testkey.avbpubkey",
2775 private_key: "testkey.pem",
2776 }
2777 `, withFiles(map[string][]byte{
2778 "product_specific_file_contexts": nil,
2779 }))
2780 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2781 apexRule = module.Rule("apexRule")
2782 actual = apexRule.Args["file_contexts"]
2783 expected = "product_specific_file_contexts"
2784 if actual != expected {
2785 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2786 }
2787
2788 ctx, _ = testApex(t, `
2789 apex {
2790 name: "myapex",
2791 key: "myapex.key",
2792 product_specific: true,
2793 file_contexts: ":my-file-contexts",
2794 }
2795
2796 apex_key {
2797 name: "myapex.key",
2798 public_key: "testkey.avbpubkey",
2799 private_key: "testkey.pem",
2800 }
2801
2802 filegroup {
2803 name: "my-file-contexts",
2804 srcs: ["product_specific_file_contexts"],
2805 }
2806 `, withFiles(map[string][]byte{
2807 "product_specific_file_contexts": nil,
2808 }))
2809 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2810 apexRule = module.Rule("apexRule")
2811 actual = apexRule.Args["file_contexts"]
2812 expected = "product_specific_file_contexts"
2813 if actual != expected {
2814 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2815 }
2816}
2817
Jiyong Park67882562019-03-21 01:11:21 +09002818func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002819 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002820 apex_key {
2821 name: "myapex.key",
2822 public_key: ":my.avbpubkey",
2823 private_key: ":my.pem",
2824 product_specific: true,
2825 }
2826
2827 filegroup {
2828 name: "my.avbpubkey",
2829 srcs: ["testkey2.avbpubkey"],
2830 }
2831
2832 filegroup {
2833 name: "my.pem",
2834 srcs: ["testkey2.pem"],
2835 }
2836 `)
2837
2838 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2839 expected_pubkey := "testkey2.avbpubkey"
2840 actual_pubkey := apex_key.public_key_file.String()
2841 if actual_pubkey != expected_pubkey {
2842 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2843 }
2844 expected_privkey := "testkey2.pem"
2845 actual_privkey := apex_key.private_key_file.String()
2846 if actual_privkey != expected_privkey {
2847 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2848 }
2849}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002850
2851func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002852 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002853 prebuilt_apex {
2854 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002855 arch: {
2856 arm64: {
2857 src: "myapex-arm64.apex",
2858 },
2859 arm: {
2860 src: "myapex-arm.apex",
2861 },
2862 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002863 }
2864 `)
2865
2866 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2867
Jiyong Parkc95714e2019-03-29 14:23:10 +09002868 expectedInput := "myapex-arm64.apex"
2869 if prebuilt.inputApex.String() != expectedInput {
2870 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2871 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002872}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002873
2874func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002875 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002876 prebuilt_apex {
2877 name: "myapex",
2878 src: "myapex-arm.apex",
2879 filename: "notmyapex.apex",
2880 }
2881 `)
2882
2883 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2884
2885 expected := "notmyapex.apex"
2886 if p.installFilename != expected {
2887 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2888 }
2889}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002890
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002891func TestPrebuiltOverrides(t *testing.T) {
2892 ctx, config := testApex(t, `
2893 prebuilt_apex {
2894 name: "myapex.prebuilt",
2895 src: "myapex-arm.apex",
2896 overrides: [
2897 "myapex",
2898 ],
2899 }
2900 `)
2901
2902 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2903
2904 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002905 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002906 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002907 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002908 }
2909}
2910
Roland Levillain630846d2019-06-26 12:48:34 +01002911func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002912 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002913 apex_test {
2914 name: "myapex",
2915 key: "myapex.key",
2916 tests: [
2917 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002918 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002919 ],
2920 }
2921
2922 apex_key {
2923 name: "myapex.key",
2924 public_key: "testkey.avbpubkey",
2925 private_key: "testkey.pem",
2926 }
2927
2928 cc_test {
2929 name: "mytest",
2930 gtest: false,
2931 srcs: ["mytest.cpp"],
2932 relative_install_path: "test",
2933 system_shared_libs: [],
2934 static_executable: true,
2935 stl: "none",
2936 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002937
2938 cc_test {
2939 name: "mytests",
2940 gtest: false,
2941 srcs: [
2942 "mytest1.cpp",
2943 "mytest2.cpp",
2944 "mytest3.cpp",
2945 ],
2946 test_per_src: true,
2947 relative_install_path: "test",
2948 system_shared_libs: [],
2949 static_executable: true,
2950 stl: "none",
2951 }
Roland Levillain630846d2019-06-26 12:48:34 +01002952 `)
2953
Sundong Ahnabb64432019-10-22 13:58:29 +09002954 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002955 copyCmds := apexRule.Args["copy_commands"]
2956
2957 // Ensure that test dep is copied into apex.
2958 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002959
2960 // Ensure that test deps built with `test_per_src` are copied into apex.
2961 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2962 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2963 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002964
2965 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002966 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002967 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2968 name := apexBundle.BaseModuleName()
2969 prefix := "TARGET_"
2970 var builder strings.Builder
2971 data.Custom(&builder, name, prefix, "", data)
2972 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002973 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2974 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2975 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2976 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002977 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002978 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002979 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002980}
2981
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002982func TestInstallExtraFlattenedApexes(t *testing.T) {
2983 ctx, config := testApex(t, `
2984 apex {
2985 name: "myapex",
2986 key: "myapex.key",
2987 }
2988 apex_key {
2989 name: "myapex.key",
2990 public_key: "testkey.avbpubkey",
2991 private_key: "testkey.pem",
2992 }
2993 `, func(fs map[string][]byte, config android.Config) {
2994 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2995 })
2996 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002997 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002998 mk := android.AndroidMkDataForTest(t, config, "", ab)
2999 var builder strings.Builder
3000 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3001 androidMk := builder.String()
3002 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3003}
3004
Jooyung Han5c998b92019-06-27 11:30:33 +09003005func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003006 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003007 apex {
3008 name: "myapex",
3009 key: "myapex.key",
3010 native_shared_libs: ["mylib"],
3011 uses: ["commonapex"],
3012 }
3013
3014 apex {
3015 name: "commonapex",
3016 key: "myapex.key",
3017 native_shared_libs: ["libcommon"],
3018 provide_cpp_shared_libs: true,
3019 }
3020
3021 apex_key {
3022 name: "myapex.key",
3023 public_key: "testkey.avbpubkey",
3024 private_key: "testkey.pem",
3025 }
3026
3027 cc_library {
3028 name: "mylib",
3029 srcs: ["mylib.cpp"],
3030 shared_libs: ["libcommon"],
3031 system_shared_libs: [],
3032 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003033 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003034 }
3035
3036 cc_library {
3037 name: "libcommon",
3038 srcs: ["mylib_common.cpp"],
3039 system_shared_libs: [],
3040 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003041 // TODO: remove //apex_available:platform
3042 apex_available: [
3043 "//apex_available:platform",
3044 "commonapex",
3045 "myapex",
3046 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003047 }
3048 `)
3049
Sundong Ahnabb64432019-10-22 13:58:29 +09003050 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003051 apexRule1 := module1.Rule("apexRule")
3052 copyCmds1 := apexRule1.Args["copy_commands"]
3053
Sundong Ahnabb64432019-10-22 13:58:29 +09003054 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003055 apexRule2 := module2.Rule("apexRule")
3056 copyCmds2 := apexRule2.Args["copy_commands"]
3057
Colin Cross7113d202019-11-20 16:39:12 -08003058 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3059 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003060 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3061 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3062 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3063}
3064
3065func TestApexUsesFailsIfNotProvided(t *testing.T) {
3066 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3067 apex {
3068 name: "myapex",
3069 key: "myapex.key",
3070 uses: ["commonapex"],
3071 }
3072
3073 apex {
3074 name: "commonapex",
3075 key: "myapex.key",
3076 }
3077
3078 apex_key {
3079 name: "myapex.key",
3080 public_key: "testkey.avbpubkey",
3081 private_key: "testkey.pem",
3082 }
3083 `)
3084 testApexError(t, `uses: "commonapex" is not a provider`, `
3085 apex {
3086 name: "myapex",
3087 key: "myapex.key",
3088 uses: ["commonapex"],
3089 }
3090
3091 cc_library {
3092 name: "commonapex",
3093 system_shared_libs: [],
3094 stl: "none",
3095 }
3096
3097 apex_key {
3098 name: "myapex.key",
3099 public_key: "testkey.avbpubkey",
3100 private_key: "testkey.pem",
3101 }
3102 `)
3103}
3104
3105func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3106 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3107 apex {
3108 name: "myapex",
3109 key: "myapex.key",
3110 use_vendor: true,
3111 uses: ["commonapex"],
3112 }
3113
3114 apex {
3115 name: "commonapex",
3116 key: "myapex.key",
3117 provide_cpp_shared_libs: true,
3118 }
3119
3120 apex_key {
3121 name: "myapex.key",
3122 public_key: "testkey.avbpubkey",
3123 private_key: "testkey.pem",
3124 }
Jooyung Handc782442019-11-01 03:14:38 +09003125 `, func(fs map[string][]byte, config android.Config) {
3126 setUseVendorWhitelistForTest(config, []string{"myapex"})
3127 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003128}
3129
Jooyung Hand48f3c32019-08-23 11:18:57 +09003130func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3131 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3132 apex {
3133 name: "myapex",
3134 key: "myapex.key",
3135 native_shared_libs: ["libfoo"],
3136 }
3137
3138 apex_key {
3139 name: "myapex.key",
3140 public_key: "testkey.avbpubkey",
3141 private_key: "testkey.pem",
3142 }
3143
3144 cc_library {
3145 name: "libfoo",
3146 stl: "none",
3147 system_shared_libs: [],
3148 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003149 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003150 }
3151 `)
3152 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3153 apex {
3154 name: "myapex",
3155 key: "myapex.key",
3156 java_libs: ["myjar"],
3157 }
3158
3159 apex_key {
3160 name: "myapex.key",
3161 public_key: "testkey.avbpubkey",
3162 private_key: "testkey.pem",
3163 }
3164
3165 java_library {
3166 name: "myjar",
3167 srcs: ["foo/bar/MyClass.java"],
3168 sdk_version: "none",
3169 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003170 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003171 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003172 }
3173 `)
3174}
3175
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003176func TestApexWithApps(t *testing.T) {
3177 ctx, _ := testApex(t, `
3178 apex {
3179 name: "myapex",
3180 key: "myapex.key",
3181 apps: [
3182 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003183 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003184 ],
3185 }
3186
3187 apex_key {
3188 name: "myapex.key",
3189 public_key: "testkey.avbpubkey",
3190 private_key: "testkey.pem",
3191 }
3192
3193 android_app {
3194 name: "AppFoo",
3195 srcs: ["foo/bar/MyClass.java"],
3196 sdk_version: "none",
3197 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003198 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003199 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003200 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003201
3202 android_app {
3203 name: "AppFooPriv",
3204 srcs: ["foo/bar/MyClass.java"],
3205 sdk_version: "none",
3206 system_modules: "none",
3207 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003208 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003209 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003210
3211 cc_library_shared {
3212 name: "libjni",
3213 srcs: ["mylib.cpp"],
Jooyung Han65041792020-02-25 16:59:29 +09003214 shared_libs: ["libfoo"],
Jiyong Park8be103b2019-11-08 15:53:48 +09003215 stl: "none",
3216 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003217 apex_available: [ "myapex" ],
Jooyung Han65041792020-02-25 16:59:29 +09003218 sdk_version: "current",
3219 }
3220
3221 cc_library_shared {
3222 name: "libfoo",
3223 stl: "none",
3224 system_shared_libs: [],
3225 apex_available: [ "myapex" ],
3226 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003227 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003228 `)
3229
Sundong Ahnabb64432019-10-22 13:58:29 +09003230 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003231 apexRule := module.Rule("apexRule")
3232 copyCmds := apexRule.Args["copy_commands"]
3233
3234 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003235 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003236
Jooyung Han65041792020-02-25 16:59:29 +09003237 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3238 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003239 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Han65041792020-02-25 16:59:29 +09003240 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003241 }
Jooyung Han65041792020-02-25 16:59:29 +09003242 // JNI libraries including transitive deps are
3243 for _, jni := range []string{"libjni", "libfoo"} {
3244 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
3245 // ... embedded inside APK (jnilibs.zip)
3246 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3247 // ... and not directly inside the APEX
3248 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3249 }
Dario Frenicde2a032019-10-27 00:29:22 +01003250}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003251
Dario Frenicde2a032019-10-27 00:29:22 +01003252func TestApexWithAppImports(t *testing.T) {
3253 ctx, _ := testApex(t, `
3254 apex {
3255 name: "myapex",
3256 key: "myapex.key",
3257 apps: [
3258 "AppFooPrebuilt",
3259 "AppFooPrivPrebuilt",
3260 ],
3261 }
3262
3263 apex_key {
3264 name: "myapex.key",
3265 public_key: "testkey.avbpubkey",
3266 private_key: "testkey.pem",
3267 }
3268
3269 android_app_import {
3270 name: "AppFooPrebuilt",
3271 apk: "PrebuiltAppFoo.apk",
3272 presigned: true,
3273 dex_preopt: {
3274 enabled: false,
3275 },
3276 }
3277
3278 android_app_import {
3279 name: "AppFooPrivPrebuilt",
3280 apk: "PrebuiltAppFooPriv.apk",
3281 privileged: true,
3282 presigned: true,
3283 dex_preopt: {
3284 enabled: false,
3285 },
3286 }
3287 `)
3288
Sundong Ahnabb64432019-10-22 13:58:29 +09003289 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003290 apexRule := module.Rule("apexRule")
3291 copyCmds := apexRule.Args["copy_commands"]
3292
3293 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3294 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003295}
3296
Dario Freni6f3937c2019-12-20 22:58:03 +00003297func TestApexWithTestHelperApp(t *testing.T) {
3298 ctx, _ := testApex(t, `
3299 apex {
3300 name: "myapex",
3301 key: "myapex.key",
3302 apps: [
3303 "TesterHelpAppFoo",
3304 ],
3305 }
3306
3307 apex_key {
3308 name: "myapex.key",
3309 public_key: "testkey.avbpubkey",
3310 private_key: "testkey.pem",
3311 }
3312
3313 android_test_helper_app {
3314 name: "TesterHelpAppFoo",
3315 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003316 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003317 }
3318
3319 `)
3320
3321 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3322 apexRule := module.Rule("apexRule")
3323 copyCmds := apexRule.Args["copy_commands"]
3324
3325 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3326}
3327
Jooyung Han18020ea2019-11-13 10:50:48 +09003328func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3329 // libfoo's apex_available comes from cc_defaults
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003330 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003331 apex {
3332 name: "myapex",
3333 key: "myapex.key",
3334 native_shared_libs: ["libfoo"],
3335 }
3336
3337 apex_key {
3338 name: "myapex.key",
3339 public_key: "testkey.avbpubkey",
3340 private_key: "testkey.pem",
3341 }
3342
3343 apex {
3344 name: "otherapex",
3345 key: "myapex.key",
3346 native_shared_libs: ["libfoo"],
3347 }
3348
3349 cc_defaults {
3350 name: "libfoo-defaults",
3351 apex_available: ["otherapex"],
3352 }
3353
3354 cc_library {
3355 name: "libfoo",
3356 defaults: ["libfoo-defaults"],
3357 stl: "none",
3358 system_shared_libs: [],
3359 }`)
3360}
3361
Jiyong Park127b40b2019-09-30 16:04:35 +09003362func TestApexAvailable(t *testing.T) {
3363 // libfoo is not available to myapex, but only to otherapex
3364 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3365 apex {
3366 name: "myapex",
3367 key: "myapex.key",
3368 native_shared_libs: ["libfoo"],
3369 }
3370
3371 apex_key {
3372 name: "myapex.key",
3373 public_key: "testkey.avbpubkey",
3374 private_key: "testkey.pem",
3375 }
3376
3377 apex {
3378 name: "otherapex",
3379 key: "otherapex.key",
3380 native_shared_libs: ["libfoo"],
3381 }
3382
3383 apex_key {
3384 name: "otherapex.key",
3385 public_key: "testkey.avbpubkey",
3386 private_key: "testkey.pem",
3387 }
3388
3389 cc_library {
3390 name: "libfoo",
3391 stl: "none",
3392 system_shared_libs: [],
3393 apex_available: ["otherapex"],
3394 }`)
3395
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003396 // libbbaz is an indirect dep
3397 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003398 apex {
3399 name: "myapex",
3400 key: "myapex.key",
3401 native_shared_libs: ["libfoo"],
3402 }
3403
3404 apex_key {
3405 name: "myapex.key",
3406 public_key: "testkey.avbpubkey",
3407 private_key: "testkey.pem",
3408 }
3409
Jiyong Park127b40b2019-09-30 16:04:35 +09003410 cc_library {
3411 name: "libfoo",
3412 stl: "none",
3413 shared_libs: ["libbar"],
3414 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003415 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003416 }
3417
3418 cc_library {
3419 name: "libbar",
3420 stl: "none",
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003421 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003422 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003423 apex_available: ["myapex"],
3424 }
3425
3426 cc_library {
3427 name: "libbaz",
3428 stl: "none",
3429 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003430 }`)
3431
3432 testApexError(t, "\"otherapex\" is not a valid module name", `
3433 apex {
3434 name: "myapex",
3435 key: "myapex.key",
3436 native_shared_libs: ["libfoo"],
3437 }
3438
3439 apex_key {
3440 name: "myapex.key",
3441 public_key: "testkey.avbpubkey",
3442 private_key: "testkey.pem",
3443 }
3444
3445 cc_library {
3446 name: "libfoo",
3447 stl: "none",
3448 system_shared_libs: [],
3449 apex_available: ["otherapex"],
3450 }`)
3451
3452 ctx, _ := testApex(t, `
3453 apex {
3454 name: "myapex",
3455 key: "myapex.key",
3456 native_shared_libs: ["libfoo", "libbar"],
3457 }
3458
3459 apex_key {
3460 name: "myapex.key",
3461 public_key: "testkey.avbpubkey",
3462 private_key: "testkey.pem",
3463 }
3464
3465 cc_library {
3466 name: "libfoo",
3467 stl: "none",
3468 system_shared_libs: [],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003469 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003470 apex_available: ["myapex"],
3471 }
3472
3473 cc_library {
3474 name: "libbar",
3475 stl: "none",
3476 system_shared_libs: [],
3477 apex_available: ["//apex_available:anyapex"],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003478 }
3479
3480 cc_library {
3481 name: "libbaz",
3482 stl: "none",
3483 system_shared_libs: [],
3484 stubs: {
3485 versions: ["10", "20", "30"],
3486 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003487 }`)
3488
3489 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003490 // TODO(jiyong) the checks for the platform variant are removed because we now create
3491 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3492 // the platform variants are not used from other platform modules. When that is done,
3493 // these checks will be replaced by expecting a specific error message that will be
3494 // emitted when the platform variant is used.
3495 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3496 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3497 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3498 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003499
3500 ctx, _ = testApex(t, `
3501 apex {
3502 name: "myapex",
3503 key: "myapex.key",
3504 }
3505
3506 apex_key {
3507 name: "myapex.key",
3508 public_key: "testkey.avbpubkey",
3509 private_key: "testkey.pem",
3510 }
3511
3512 cc_library {
3513 name: "libfoo",
3514 stl: "none",
3515 system_shared_libs: [],
3516 apex_available: ["//apex_available:platform"],
3517 }`)
3518
3519 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003520 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3521 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003522
3523 ctx, _ = testApex(t, `
3524 apex {
3525 name: "myapex",
3526 key: "myapex.key",
3527 native_shared_libs: ["libfoo"],
3528 }
3529
3530 apex_key {
3531 name: "myapex.key",
3532 public_key: "testkey.avbpubkey",
3533 private_key: "testkey.pem",
3534 }
3535
3536 cc_library {
3537 name: "libfoo",
3538 stl: "none",
3539 system_shared_libs: [],
3540 apex_available: ["myapex"],
3541 static: {
3542 apex_available: ["//apex_available:platform"],
3543 },
3544 }`)
3545
3546 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003547 // TODO(jiyong) the checks for the platform variant are removed because we now create
3548 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3549 // the platform variants are not used from other platform modules. When that is done,
3550 // these checks will be replaced by expecting a specific error message that will be
3551 // emitted when the platform variant is used.
3552 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3553 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3554 // // but the static variant is available to both myapex and the platform
3555 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3556 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003557}
3558
Jiyong Park5d790c32019-11-15 18:40:32 +09003559func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003560 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003561 apex {
3562 name: "myapex",
3563 key: "myapex.key",
3564 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003565 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003566 }
3567
3568 override_apex {
3569 name: "override_myapex",
3570 base: "myapex",
3571 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003572 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003573 logging_parent: "com.foo.bar",
Jiyong Park5d790c32019-11-15 18:40:32 +09003574 }
3575
3576 apex_key {
3577 name: "myapex.key",
3578 public_key: "testkey.avbpubkey",
3579 private_key: "testkey.pem",
3580 }
3581
3582 android_app {
3583 name: "app",
3584 srcs: ["foo/bar/MyClass.java"],
3585 package_name: "foo",
3586 sdk_version: "none",
3587 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003588 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003589 }
3590
3591 override_android_app {
3592 name: "override_app",
3593 base: "app",
3594 package_name: "bar",
3595 }
Jiyong Parka519c542020-03-03 11:45:41 +09003596 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003597
Jiyong Park317645e2019-12-05 13:20:58 +09003598 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3599 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3600 if originalVariant.GetOverriddenBy() != "" {
3601 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3602 }
3603 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3604 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3605 }
3606
Jiyong Park5d790c32019-11-15 18:40:32 +09003607 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3608 apexRule := module.Rule("apexRule")
3609 copyCmds := apexRule.Args["copy_commands"]
3610
3611 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3612 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003613
3614 apexBundle := module.Module().(*apexBundle)
3615 name := apexBundle.Name()
3616 if name != "override_myapex" {
3617 t.Errorf("name should be \"override_myapex\", but was %q", name)
3618 }
3619
Baligh Uddin004d7172020-02-19 21:29:28 -08003620 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3621 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3622 }
3623
Jiyong Parka519c542020-03-03 11:45:41 +09003624 optFlags := apexRule.Args["opt_flags"]
3625 ensureContains(t, optFlags, "--override_apk_package_name com.android.myapex")
3626
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003627 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3628 var builder strings.Builder
3629 data.Custom(&builder, name, "TARGET_", "", data)
3630 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003631 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003632 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3633 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003634 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003635 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003636 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003637 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3638 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003639}
3640
Jooyung Han214bf372019-11-12 13:03:50 +09003641func TestLegacyAndroid10Support(t *testing.T) {
3642 ctx, _ := testApex(t, `
3643 apex {
3644 name: "myapex",
3645 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003646 native_shared_libs: ["mylib"],
Jooyung Han214bf372019-11-12 13:03:50 +09003647 legacy_android10_support: true,
3648 }
3649
3650 apex_key {
3651 name: "myapex.key",
3652 public_key: "testkey.avbpubkey",
3653 private_key: "testkey.pem",
3654 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003655
3656 cc_library {
3657 name: "mylib",
3658 srcs: ["mylib.cpp"],
3659 stl: "libc++",
3660 system_shared_libs: [],
3661 apex_available: [ "myapex" ],
3662 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003663 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003664
3665 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3666 args := module.Rule("apexRule").Args
3667 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003668 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003669
3670 // The copies of the libraries in the apex should have one more dependency than
3671 // the ones outside the apex, namely the unwinder. Ideally we should check
3672 // the dependency names directly here but for some reason the names are blank in
3673 // this test.
3674 for _, lib := range []string{"libc++", "mylib"} {
3675 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3676 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3677 if len(apexImplicits) != len(nonApexImplicits)+1 {
3678 t.Errorf("%q missing unwinder dep", lib)
3679 }
3680 }
Jooyung Han214bf372019-11-12 13:03:50 +09003681}
3682
Jooyung Han58f26ab2019-12-18 15:34:32 +09003683func TestJavaSDKLibrary(t *testing.T) {
3684 ctx, _ := testApex(t, `
3685 apex {
3686 name: "myapex",
3687 key: "myapex.key",
3688 java_libs: ["foo"],
3689 }
3690
3691 apex_key {
3692 name: "myapex.key",
3693 public_key: "testkey.avbpubkey",
3694 private_key: "testkey.pem",
3695 }
3696
3697 java_sdk_library {
3698 name: "foo",
3699 srcs: ["a.java"],
3700 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003701 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003702 }
3703 `, withFiles(map[string][]byte{
3704 "api/current.txt": nil,
3705 "api/removed.txt": nil,
3706 "api/system-current.txt": nil,
3707 "api/system-removed.txt": nil,
3708 "api/test-current.txt": nil,
3709 "api/test-removed.txt": nil,
3710 }))
3711
3712 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003713 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003714 "javalib/foo.jar",
3715 "etc/permissions/foo.xml",
3716 })
3717 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003718 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3719 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003720}
3721
atrost6e126252020-01-27 17:01:16 +00003722func TestCompatConfig(t *testing.T) {
3723 ctx, _ := testApex(t, `
3724 apex {
3725 name: "myapex",
3726 key: "myapex.key",
3727 prebuilts: ["myjar-platform-compat-config"],
3728 java_libs: ["myjar"],
3729 }
3730
3731 apex_key {
3732 name: "myapex.key",
3733 public_key: "testkey.avbpubkey",
3734 private_key: "testkey.pem",
3735 }
3736
3737 platform_compat_config {
3738 name: "myjar-platform-compat-config",
3739 src: ":myjar",
3740 }
3741
3742 java_library {
3743 name: "myjar",
3744 srcs: ["foo/bar/MyClass.java"],
3745 sdk_version: "none",
3746 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003747 apex_available: [ "myapex" ],
3748 }
3749 `)
3750 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3751 "etc/compatconfig/myjar-platform-compat-config.xml",
3752 "javalib/myjar.jar",
3753 })
3754}
3755
Jiyong Park479321d2019-12-16 11:47:12 +09003756func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3757 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3758 apex {
3759 name: "myapex",
3760 key: "myapex.key",
3761 java_libs: ["myjar"],
3762 }
3763
3764 apex_key {
3765 name: "myapex.key",
3766 public_key: "testkey.avbpubkey",
3767 private_key: "testkey.pem",
3768 }
3769
3770 java_library {
3771 name: "myjar",
3772 srcs: ["foo/bar/MyClass.java"],
3773 sdk_version: "none",
3774 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003775 compile_dex: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003776 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003777 }
3778 `)
3779}
3780
Jiyong Park7afd1072019-12-30 16:56:33 +09003781func TestCarryRequiredModuleNames(t *testing.T) {
3782 ctx, config := testApex(t, `
3783 apex {
3784 name: "myapex",
3785 key: "myapex.key",
3786 native_shared_libs: ["mylib"],
3787 }
3788
3789 apex_key {
3790 name: "myapex.key",
3791 public_key: "testkey.avbpubkey",
3792 private_key: "testkey.pem",
3793 }
3794
3795 cc_library {
3796 name: "mylib",
3797 srcs: ["mylib.cpp"],
3798 system_shared_libs: [],
3799 stl: "none",
3800 required: ["a", "b"],
3801 host_required: ["c", "d"],
3802 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003803 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003804 }
3805 `)
3806
3807 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3808 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3809 name := apexBundle.BaseModuleName()
3810 prefix := "TARGET_"
3811 var builder strings.Builder
3812 data.Custom(&builder, name, prefix, "", data)
3813 androidMk := builder.String()
3814 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3815 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3816 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3817}
3818
Jiyong Park7cd10e32020-01-14 09:22:18 +09003819func TestSymlinksFromApexToSystem(t *testing.T) {
3820 bp := `
3821 apex {
3822 name: "myapex",
3823 key: "myapex.key",
3824 native_shared_libs: ["mylib"],
3825 java_libs: ["myjar"],
3826 }
3827
Jiyong Park9d677202020-02-19 16:29:35 +09003828 apex {
3829 name: "myapex.updatable",
3830 key: "myapex.key",
3831 native_shared_libs: ["mylib"],
3832 java_libs: ["myjar"],
3833 updatable: true,
3834 }
3835
Jiyong Park7cd10e32020-01-14 09:22:18 +09003836 apex_key {
3837 name: "myapex.key",
3838 public_key: "testkey.avbpubkey",
3839 private_key: "testkey.pem",
3840 }
3841
3842 cc_library {
3843 name: "mylib",
3844 srcs: ["mylib.cpp"],
3845 shared_libs: ["myotherlib"],
3846 system_shared_libs: [],
3847 stl: "none",
3848 apex_available: [
3849 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003850 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003851 "//apex_available:platform",
3852 ],
3853 }
3854
3855 cc_library {
3856 name: "myotherlib",
3857 srcs: ["mylib.cpp"],
3858 system_shared_libs: [],
3859 stl: "none",
3860 apex_available: [
3861 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003862 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003863 "//apex_available:platform",
3864 ],
3865 }
3866
3867 java_library {
3868 name: "myjar",
3869 srcs: ["foo/bar/MyClass.java"],
3870 sdk_version: "none",
3871 system_modules: "none",
3872 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003873 apex_available: [
3874 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003875 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003876 "//apex_available:platform",
3877 ],
3878 }
3879
3880 java_library {
3881 name: "myotherjar",
3882 srcs: ["foo/bar/MyClass.java"],
3883 sdk_version: "none",
3884 system_modules: "none",
3885 apex_available: [
3886 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003887 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003888 "//apex_available:platform",
3889 ],
3890 }
3891 `
3892
3893 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3894 for _, f := range files {
3895 if f.path == file {
3896 if f.isLink {
3897 t.Errorf("%q is not a real file", file)
3898 }
3899 return
3900 }
3901 }
3902 t.Errorf("%q is not found", file)
3903 }
3904
3905 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3906 for _, f := range files {
3907 if f.path == file {
3908 if !f.isLink {
3909 t.Errorf("%q is not a symlink", file)
3910 }
3911 return
3912 }
3913 }
3914 t.Errorf("%q is not found", file)
3915 }
3916
Jiyong Park9d677202020-02-19 16:29:35 +09003917 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3918 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003919 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003920 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003921 ensureRealfileExists(t, files, "javalib/myjar.jar")
3922 ensureRealfileExists(t, files, "lib64/mylib.so")
3923 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3924
Jiyong Park9d677202020-02-19 16:29:35 +09003925 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3926 ensureRealfileExists(t, files, "javalib/myjar.jar")
3927 ensureRealfileExists(t, files, "lib64/mylib.so")
3928 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3929
3930 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003931 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003932 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003933 ensureRealfileExists(t, files, "javalib/myjar.jar")
3934 ensureRealfileExists(t, files, "lib64/mylib.so")
3935 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09003936
3937 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3938 ensureRealfileExists(t, files, "javalib/myjar.jar")
3939 ensureRealfileExists(t, files, "lib64/mylib.so")
3940 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09003941}
3942
Jiyong Parkd93e1b12020-02-28 15:22:21 +09003943func TestAppBundle(t *testing.T) {
3944 ctx, _ := testApex(t, `
3945 apex {
3946 name: "myapex",
3947 key: "myapex.key",
3948 apps: ["AppFoo"],
3949 }
3950
3951 apex_key {
3952 name: "myapex.key",
3953 public_key: "testkey.avbpubkey",
3954 private_key: "testkey.pem",
3955 }
3956
3957 android_app {
3958 name: "AppFoo",
3959 srcs: ["foo/bar/MyClass.java"],
3960 sdk_version: "none",
3961 system_modules: "none",
3962 apex_available: [ "myapex" ],
3963 }
Jiyong Parkaf8998c2020-02-28 16:51:07 +09003964 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkd93e1b12020-02-28 15:22:21 +09003965
3966 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
3967 content := bundleConfigRule.Args["content"]
3968
3969 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkaf8998c2020-02-28 16:51:07 +09003970 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo/AppFoo.apk"}]}`)
Jiyong Parkd93e1b12020-02-28 15:22:21 +09003971}
3972
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003973func TestMain(m *testing.M) {
3974 run := func() int {
3975 setUp()
3976 defer tearDown()
3977
3978 return m.Run()
3979 }
3980
3981 os.Exit(run())
3982}