blob: 80d8153b4395799e42da6cfcae967fa021ee4ca0 [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,
Jiyong Park162e8442020-03-17 19:16:40 +0900153 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800154 "testkey2.avbpubkey": nil,
155 "testkey2.pem": nil,
156 "myapex-arm64.apex": nil,
157 "myapex-arm.apex": nil,
158 "frameworks/base/api/current.txt": nil,
159 "framework/aidl/a.aidl": nil,
160 "build/make/core/proguard.flags": nil,
161 "build/make/core/proguard_basic_keeps.flags": nil,
162 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900163 }
164
Colin Crossf9aabd72020-02-15 11:29:50 -0800165 cc.GatherRequiredFilesForTest(fs)
166
Jooyung Han344d5432019-08-23 11:17:39 +0900167 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800168 // The fs now needs to be populated before creating the config, call handlers twice
169 // for now, once to get any fs changes, and later after the config was created to
170 // set product variables or targets.
171 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
172 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900173 }
174
Colin Cross98be1bb2019-12-13 20:41:13 -0800175 config := android.TestArchConfig(buildDir, nil, bp, fs)
176 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
177 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
178 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
179 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
180 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
181 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
182
183 for _, handler := range handlers {
184 // The fs now needs to be populated before creating the config, call handlers twice
185 // for now, earlier to get any fs changes, and now after the config was created to
186 // set product variables or targets.
187 tempFS := map[string][]byte{}
188 handler(tempFS, config)
189 }
190
191 ctx := android.NewTestArchContext()
192 ctx.RegisterModuleType("apex", BundleFactory)
193 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
194 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
195 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
196 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
197 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
198 ctx.RegisterModuleType("override_apex", overrideApexFactory)
199
Jooyung Hana57af4a2020-01-23 05:36:59 +0000200 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
201 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
202
Paul Duffin77980a82019-12-19 16:01:36 +0000203 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800204 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800205 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
206 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800207 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000208 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800209 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800210 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000211 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000212 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000213 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900214 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800215
Colin Cross98be1bb2019-12-13 20:41:13 -0800216 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800217 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800218
219 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900220
Jooyung Han5c998b92019-06-27 11:30:33 +0900221 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900222}
223
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700224func setUp() {
225 var err error
226 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900227 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700228 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900230}
231
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700232func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900233 os.RemoveAll(buildDir)
234}
235
236// ensure that 'result' contains 'expected'
237func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900238 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900239 if !strings.Contains(result, expected) {
240 t.Errorf("%q is not found in %q", expected, result)
241 }
242}
243
244// ensures that 'result' does not contain 'notExpected'
245func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900246 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 if strings.Contains(result, notExpected) {
248 t.Errorf("%q is found in %q", notExpected, result)
249 }
250}
251
252func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900253 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900254 if !android.InList(expected, result) {
255 t.Errorf("%q is not found in %v", expected, result)
256 }
257}
258
259func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900260 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261 if android.InList(notExpected, result) {
262 t.Errorf("%q is found in %v", notExpected, result)
263 }
264}
265
Jooyung Hane1633032019-08-01 17:41:43 +0900266func ensureListEmpty(t *testing.T, result []string) {
267 t.Helper()
268 if len(result) > 0 {
269 t.Errorf("%q is expected to be empty", result)
270 }
271}
272
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273// Minimal test
274func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700275 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900276 apex_defaults {
277 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900278 manifest: ":myapex.manifest",
279 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 key: "myapex.key",
281 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800282 multilib: {
283 both: {
284 binaries: ["foo",],
285 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900286 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900287 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900288 }
289
Jiyong Park30ca9372019-02-07 16:27:23 +0900290 apex {
291 name: "myapex",
292 defaults: ["myapex-defaults"],
293 }
294
Jiyong Park25fc6a92018-11-18 18:02:45 +0900295 apex_key {
296 name: "myapex.key",
297 public_key: "testkey.avbpubkey",
298 private_key: "testkey.pem",
299 }
300
Jiyong Park809bb722019-02-13 21:33:49 +0900301 filegroup {
302 name: "myapex.manifest",
303 srcs: ["apex_manifest.json"],
304 }
305
306 filegroup {
307 name: "myapex.androidmanifest",
308 srcs: ["AndroidManifest.xml"],
309 }
310
Jiyong Park25fc6a92018-11-18 18:02:45 +0900311 cc_library {
312 name: "mylib",
313 srcs: ["mylib.cpp"],
314 shared_libs: ["mylib2"],
315 system_shared_libs: [],
316 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000317 // TODO: remove //apex_available:platform
318 apex_available: [
319 "//apex_available:platform",
320 "myapex",
321 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322 }
323
Alex Light3d673592019-01-18 14:37:31 -0800324 cc_binary {
325 name: "foo",
326 srcs: ["mylib.cpp"],
327 compile_multilib: "both",
328 multilib: {
329 lib32: {
330 suffix: "32",
331 },
332 lib64: {
333 suffix: "64",
334 },
335 },
336 symlinks: ["foo_link_"],
337 symlink_preferred_arch: true,
338 system_shared_libs: [],
339 static_executable: true,
340 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000341 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800342 }
343
Jiyong Park25fc6a92018-11-18 18:02:45 +0900344 cc_library {
345 name: "mylib2",
346 srcs: ["mylib.cpp"],
347 system_shared_libs: [],
348 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900349 notice: "custom_notice",
Jiyong Park162e8442020-03-17 19:16:40 +0900350 static_libs: ["libstatic"],
351 // TODO: remove //apex_available:platform
352 apex_available: [
353 "//apex_available:platform",
354 "myapex",
355 ],
356 }
357
358 cc_library_static {
359 name: "libstatic",
360 srcs: ["mylib.cpp"],
361 system_shared_libs: [],
362 stl: "none",
363 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000364 // TODO: remove //apex_available:platform
365 apex_available: [
366 "//apex_available:platform",
367 "myapex",
368 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900370
371 java_library {
372 name: "myjar",
373 srcs: ["foo/bar/MyClass.java"],
374 sdk_version: "none",
375 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900376 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900377 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000378 // TODO: remove //apex_available:platform
379 apex_available: [
380 "//apex_available:platform",
381 "myapex",
382 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900383 }
384
385 java_library {
386 name: "myotherjar",
387 srcs: ["foo/bar/MyClass.java"],
388 sdk_version: "none",
389 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900390 // TODO: remove //apex_available:platform
391 apex_available: [
392 "//apex_available:platform",
393 "myapex",
394 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900395 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900396
397 java_library {
398 name: "mysharedjar",
399 srcs: ["foo/bar/MyClass.java"],
400 sdk_version: "none",
401 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900402 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900403 `)
404
Sundong Ahnabb64432019-10-22 13:58:29 +0900405 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900406
407 optFlags := apexRule.Args["opt_flags"]
408 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700409 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900410 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900411
Jiyong Park25fc6a92018-11-18 18:02:45 +0900412 copyCmds := apexRule.Args["copy_commands"]
413
414 // Ensure that main rule creates an output
415 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
416
417 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800418 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900419 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900420
421 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800422 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900423 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900424
425 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800426 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
427 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900428 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
429 // .. but not for java libs
430 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900431 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800432
Colin Cross7113d202019-11-20 16:39:12 -0800433 // Ensure that the platform variant ends with _shared or _common
434 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
435 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900436 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
437 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900438 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
439
440 // Ensure that dynamic dependency to java libs are not included
441 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800442
443 // Ensure that all symlinks are present.
444 found_foo_link_64 := false
445 found_foo := false
446 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900447 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800448 if strings.HasSuffix(cmd, "bin/foo") {
449 found_foo = true
450 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
451 found_foo_link_64 = true
452 }
453 }
454 }
455 good := found_foo && found_foo_link_64
456 if !good {
457 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
458 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900459
Sundong Ahnabb64432019-10-22 13:58:29 +0900460 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700461 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park162e8442020-03-17 19:16:40 +0900462 if len(noticeInputs) != 3 {
463 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900464 }
465 ensureListContains(t, noticeInputs, "NOTICE")
466 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park162e8442020-03-17 19:16:40 +0900467 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900468
469 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 +0900470 ensureListContains(t, depsInfo, "myjar <- myapex")
471 ensureListContains(t, depsInfo, "mylib <- myapex")
472 ensureListContains(t, depsInfo, "mylib2 <- mylib")
473 ensureListContains(t, depsInfo, "myotherjar <- myjar")
474 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800475}
476
Jooyung Hanf21c7972019-12-16 22:32:06 +0900477func TestDefaults(t *testing.T) {
478 ctx, _ := testApex(t, `
479 apex_defaults {
480 name: "myapex-defaults",
481 key: "myapex.key",
482 prebuilts: ["myetc"],
483 native_shared_libs: ["mylib"],
484 java_libs: ["myjar"],
485 apps: ["AppFoo"],
486 }
487
488 prebuilt_etc {
489 name: "myetc",
490 src: "myprebuilt",
491 }
492
493 apex {
494 name: "myapex",
495 defaults: ["myapex-defaults"],
496 }
497
498 apex_key {
499 name: "myapex.key",
500 public_key: "testkey.avbpubkey",
501 private_key: "testkey.pem",
502 }
503
504 cc_library {
505 name: "mylib",
506 system_shared_libs: [],
507 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000508 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900509 }
510
511 java_library {
512 name: "myjar",
513 srcs: ["foo/bar/MyClass.java"],
514 sdk_version: "none",
515 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000516 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900517 }
518
519 android_app {
520 name: "AppFoo",
521 srcs: ["foo/bar/MyClass.java"],
522 sdk_version: "none",
523 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000524 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900525 }
526 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000527 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900528 "etc/myetc",
529 "javalib/myjar.jar",
530 "lib64/mylib.so",
531 "app/AppFoo/AppFoo.apk",
532 })
533}
534
Jooyung Han01a3ee22019-11-02 02:52:25 +0900535func TestApexManifest(t *testing.T) {
536 ctx, _ := testApex(t, `
537 apex {
538 name: "myapex",
539 key: "myapex.key",
540 }
541
542 apex_key {
543 name: "myapex.key",
544 public_key: "testkey.avbpubkey",
545 private_key: "testkey.pem",
546 }
547 `)
548
549 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900550 args := module.Rule("apexRule").Args
551 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
552 t.Error("manifest should be apex_manifest.pb, but " + manifest)
553 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900554}
555
Alex Light5098a612018-11-29 17:12:15 -0800556func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700557 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800558 apex {
559 name: "myapex",
560 key: "myapex.key",
561 payload_type: "zip",
562 native_shared_libs: ["mylib"],
563 }
564
565 apex_key {
566 name: "myapex.key",
567 public_key: "testkey.avbpubkey",
568 private_key: "testkey.pem",
569 }
570
571 cc_library {
572 name: "mylib",
573 srcs: ["mylib.cpp"],
574 shared_libs: ["mylib2"],
575 system_shared_libs: [],
576 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000577 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800578 }
579
580 cc_library {
581 name: "mylib2",
582 srcs: ["mylib.cpp"],
583 system_shared_libs: [],
584 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000585 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800586 }
587 `)
588
Sundong Ahnabb64432019-10-22 13:58:29 +0900589 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800590 copyCmds := zipApexRule.Args["copy_commands"]
591
592 // Ensure that main rule creates an output
593 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
594
595 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800596 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800597
598 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800599 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800600
601 // Ensure that both direct and indirect deps are copied into apex
602 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
603 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900604}
605
606func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700607 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900608 apex {
609 name: "myapex",
610 key: "myapex.key",
611 native_shared_libs: ["mylib", "mylib3"],
612 }
613
614 apex_key {
615 name: "myapex.key",
616 public_key: "testkey.avbpubkey",
617 private_key: "testkey.pem",
618 }
619
620 cc_library {
621 name: "mylib",
622 srcs: ["mylib.cpp"],
623 shared_libs: ["mylib2", "mylib3"],
624 system_shared_libs: [],
625 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000626 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900627 }
628
629 cc_library {
630 name: "mylib2",
631 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900632 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900633 system_shared_libs: [],
634 stl: "none",
635 stubs: {
636 versions: ["1", "2", "3"],
637 },
638 }
639
640 cc_library {
641 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900642 srcs: ["mylib.cpp"],
643 shared_libs: ["mylib4"],
644 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900645 stl: "none",
646 stubs: {
647 versions: ["10", "11", "12"],
648 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900650 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900651
652 cc_library {
653 name: "mylib4",
654 srcs: ["mylib.cpp"],
655 system_shared_libs: [],
656 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000657 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900658 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900659 `)
660
Sundong Ahnabb64432019-10-22 13:58:29 +0900661 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900662 copyCmds := apexRule.Args["copy_commands"]
663
664 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800665 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900666
667 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800668 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669
670 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800671 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900672
Colin Cross7113d202019-11-20 16:39:12 -0800673 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900674
675 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900676 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900677 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900678 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900679
680 // 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 -0800681 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900682 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800683 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900684
685 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900686 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900687 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900688
689 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900690 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900691
Jooyung Hana57af4a2020-01-23 05:36:59 +0000692 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900693 "lib64/mylib.so",
694 "lib64/mylib3.so",
695 "lib64/mylib4.so",
696 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900697}
698
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900699func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700700 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900701 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900702 name: "myapex2",
703 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900704 native_shared_libs: ["mylib"],
705 }
706
707 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900708 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900709 public_key: "testkey.avbpubkey",
710 private_key: "testkey.pem",
711 }
712
713 cc_library {
714 name: "mylib",
715 srcs: ["mylib.cpp"],
716 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900717 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900718 system_shared_libs: [],
719 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000720 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900721 }
722
723 cc_library {
724 name: "libfoo",
725 srcs: ["mylib.cpp"],
726 shared_libs: ["libbar"],
727 system_shared_libs: [],
728 stl: "none",
729 stubs: {
730 versions: ["10", "20", "30"],
731 },
732 }
733
734 cc_library {
735 name: "libbar",
736 srcs: ["mylib.cpp"],
737 system_shared_libs: [],
738 stl: "none",
739 }
740
Jiyong Park678c8812020-02-07 17:25:49 +0900741 cc_library_static {
742 name: "libbaz",
743 srcs: ["mylib.cpp"],
744 system_shared_libs: [],
745 stl: "none",
746 apex_available: [ "myapex2" ],
747 }
748
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900749 `)
750
Jiyong Park83dc74b2020-01-14 18:38:44 +0900751 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900752 copyCmds := apexRule.Args["copy_commands"]
753
754 // Ensure that direct non-stubs dep is always included
755 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
756
757 // Ensure that indirect stubs dep is not included
758 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
759
760 // Ensure that dependency of stubs is not included
761 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
762
Jiyong Park83dc74b2020-01-14 18:38:44 +0900763 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900764
765 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900766 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900767 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900768 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900769
Jiyong Park3ff16992019-12-27 14:11:47 +0900770 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900771
772 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
773 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900774
775 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 +0900776
777 ensureListContains(t, depsInfo, "mylib <- myapex2")
778 ensureListContains(t, depsInfo, "libbaz <- mylib")
779 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900780}
781
Jooyung Hand3639552019-08-09 12:57:43 +0900782func TestApexWithRuntimeLibsDependency(t *testing.T) {
783 /*
784 myapex
785 |
786 v (runtime_libs)
787 mylib ------+------> libfoo [provides stub]
788 |
789 `------> libbar
790 */
791 ctx, _ := testApex(t, `
792 apex {
793 name: "myapex",
794 key: "myapex.key",
795 native_shared_libs: ["mylib"],
796 }
797
798 apex_key {
799 name: "myapex.key",
800 public_key: "testkey.avbpubkey",
801 private_key: "testkey.pem",
802 }
803
804 cc_library {
805 name: "mylib",
806 srcs: ["mylib.cpp"],
807 runtime_libs: ["libfoo", "libbar"],
808 system_shared_libs: [],
809 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000810 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900811 }
812
813 cc_library {
814 name: "libfoo",
815 srcs: ["mylib.cpp"],
816 system_shared_libs: [],
817 stl: "none",
818 stubs: {
819 versions: ["10", "20", "30"],
820 },
821 }
822
823 cc_library {
824 name: "libbar",
825 srcs: ["mylib.cpp"],
826 system_shared_libs: [],
827 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000828 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900829 }
830
831 `)
832
Sundong Ahnabb64432019-10-22 13:58:29 +0900833 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900834 copyCmds := apexRule.Args["copy_commands"]
835
836 // Ensure that direct non-stubs dep is always included
837 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
838
839 // Ensure that indirect stubs dep is not included
840 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
841
842 // Ensure that runtime_libs dep in included
843 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
844
Sundong Ahnabb64432019-10-22 13:58:29 +0900845 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900846 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
847 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900848
849}
850
Jooyung Han67a96cd2020-03-13 15:23:36 +0900851func TestApexDependsOnLLNDKTransitively(t *testing.T) {
852 testcases := []struct {
853 name string
854 minSdkVersion string
855 shouldLink string
856 shouldNotLink []string
857 }{
858 {
Jooyung Han74066602020-03-20 04:29:24 +0900859 name: "should link to the latest",
Jooyung Han67a96cd2020-03-13 15:23:36 +0900860 minSdkVersion: "current",
861 shouldLink: "30",
862 shouldNotLink: []string{"29"},
863 },
864 {
865 name: "should link to llndk#29",
866 minSdkVersion: "29",
867 shouldLink: "29",
868 shouldNotLink: []string{"30"},
869 },
870 }
871 for _, tc := range testcases {
872 t.Run(tc.name, func(t *testing.T) {
873 ctx, _ := testApex(t, `
874 apex {
875 name: "myapex",
876 key: "myapex.key",
877 use_vendor: true,
878 native_shared_libs: ["mylib"],
879 min_sdk_version: "`+tc.minSdkVersion+`",
880 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900881
Jooyung Han67a96cd2020-03-13 15:23:36 +0900882 apex_key {
883 name: "myapex.key",
884 public_key: "testkey.avbpubkey",
885 private_key: "testkey.pem",
886 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900887
Jooyung Han67a96cd2020-03-13 15:23:36 +0900888 cc_library {
889 name: "mylib",
890 srcs: ["mylib.cpp"],
891 vendor_available: true,
892 shared_libs: ["libbar"],
893 system_shared_libs: [],
894 stl: "none",
895 apex_available: [ "myapex" ],
896 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900897
Jooyung Han67a96cd2020-03-13 15:23:36 +0900898 cc_library {
899 name: "libbar",
900 srcs: ["mylib.cpp"],
901 system_shared_libs: [],
902 stl: "none",
903 stubs: { versions: ["29","30"] },
904 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900905
Jooyung Han67a96cd2020-03-13 15:23:36 +0900906 llndk_library {
907 name: "libbar",
908 symbol_file: "",
909 }
910 `, func(fs map[string][]byte, config android.Config) {
911 setUseVendorWhitelistForTest(config, []string{"myapex"})
912 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +0900913
Jooyung Han67a96cd2020-03-13 15:23:36 +0900914 // Ensure that LLNDK dep is not included
915 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
916 "lib64/mylib.so",
917 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900918
Jooyung Han67a96cd2020-03-13 15:23:36 +0900919 // Ensure that LLNDK dep is required
920 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
921 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
922 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900923
Jooyung Han67a96cd2020-03-13 15:23:36 +0900924 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
925 ensureContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
926 for _, ver := range tc.shouldNotLink {
927 ensureNotContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
928 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900929
Jooyung Han67a96cd2020-03-13 15:23:36 +0900930 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
931 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
932 })
933 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900934}
935
Jiyong Park25fc6a92018-11-18 18:02:45 +0900936func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700937 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900938 apex {
939 name: "myapex",
940 key: "myapex.key",
941 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
942 }
943
944 apex_key {
945 name: "myapex.key",
946 public_key: "testkey.avbpubkey",
947 private_key: "testkey.pem",
948 }
949
950 cc_library {
951 name: "mylib",
952 srcs: ["mylib.cpp"],
953 shared_libs: ["libdl#27"],
954 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000955 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900956 }
957
958 cc_library_shared {
959 name: "mylib_shared",
960 srcs: ["mylib.cpp"],
961 shared_libs: ["libdl#27"],
962 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000963 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964 }
965
966 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900967 name: "libBootstrap",
968 srcs: ["mylib.cpp"],
969 stl: "none",
970 bootstrap: true,
971 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972 `)
973
Sundong Ahnabb64432019-10-22 13:58:29 +0900974 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900975 copyCmds := apexRule.Args["copy_commands"]
976
977 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800978 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900979 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
980 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900981
982 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900983 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900984
Colin Cross7113d202019-11-20 16:39:12 -0800985 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
986 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
987 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900988
989 // For dependency to libc
990 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900991 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900993 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900994 // ... Cflags from stub is correctly exported to mylib
995 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
996 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
997
998 // For dependency to libm
999 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001000 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001001 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001002 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001003 // ... and is not compiling with the stub
1004 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1005 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1006
1007 // For dependency to libdl
1008 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001009 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001011 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1012 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001013 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001014 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001015 // ... Cflags from stub is correctly exported to mylib
1016 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1017 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001018
1019 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001020 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1021 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1022 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1023 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001024}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001025
Jooyung Han0c4e0162020-02-26 22:45:42 +09001026func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
1027 // there are three links between liba --> libz
1028 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
1029 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
1030 // 3) (platform) -> liba -> libz : this should be non-stub link
1031 ctx, _ := testApex(t, `
1032 apex {
1033 name: "myapex",
1034 key: "myapex.key",
1035 native_shared_libs: ["libx"],
1036 min_sdk_version: "2",
1037 }
1038
1039 apex {
1040 name: "otherapex",
1041 key: "myapex.key",
1042 native_shared_libs: ["liby"],
1043 min_sdk_version: "3",
1044 }
1045
1046 apex_key {
1047 name: "myapex.key",
1048 public_key: "testkey.avbpubkey",
1049 private_key: "testkey.pem",
1050 }
1051
1052 cc_library {
1053 name: "libx",
1054 shared_libs: ["liba"],
1055 system_shared_libs: [],
1056 stl: "none",
1057 apex_available: [ "myapex" ],
1058 }
1059
1060 cc_library {
1061 name: "liby",
1062 shared_libs: ["liba"],
1063 system_shared_libs: [],
1064 stl: "none",
1065 apex_available: [ "otherapex" ],
1066 }
1067
1068 cc_library {
1069 name: "liba",
1070 shared_libs: ["libz"],
1071 system_shared_libs: [],
1072 stl: "none",
1073 apex_available: [
1074 "//apex_available:anyapex",
1075 "//apex_available:platform",
1076 ],
1077 }
1078
1079 cc_library {
1080 name: "libz",
1081 system_shared_libs: [],
1082 stl: "none",
1083 stubs: {
1084 versions: ["1", "3"],
1085 },
1086 }
1087 `, withUnbundledBuild)
1088
1089 expectLink := func(from, from_variant, to, to_variant string) {
1090 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1091 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1092 }
1093 expectNoLink := func(from, from_variant, to, to_variant string) {
1094 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1095 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1096 }
1097 // platform liba is linked to non-stub version
1098 expectLink("liba", "shared", "libz", "shared")
1099 // liba in myapex is linked to #1
1100 expectLink("liba", "shared_myapex", "libz", "shared_1")
1101 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1102 expectNoLink("liba", "shared_myapex", "libz", "shared")
1103 // liba in otherapex is linked to #3
1104 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1105 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1106 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1107}
1108
Jooyung Han29e91d22020-04-02 01:41:41 +09001109func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
1110 ctx, _ := testApex(t, `
1111 apex {
1112 name: "myapex",
1113 key: "myapex.key",
1114 native_shared_libs: ["libx"],
1115 min_sdk_version: "R",
1116 }
1117
1118 apex_key {
1119 name: "myapex.key",
1120 public_key: "testkey.avbpubkey",
1121 private_key: "testkey.pem",
1122 }
1123
1124 cc_library {
1125 name: "libx",
1126 shared_libs: ["libz"],
1127 system_shared_libs: [],
1128 stl: "none",
1129 apex_available: [ "myapex" ],
1130 }
1131
1132 cc_library {
1133 name: "libz",
1134 system_shared_libs: [],
1135 stl: "none",
1136 stubs: {
1137 versions: ["29", "R"],
1138 },
1139 }
1140 `, func(fs map[string][]byte, config android.Config) {
1141 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
1142 })
1143
1144 expectLink := func(from, from_variant, to, to_variant string) {
1145 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1146 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1147 }
1148 expectNoLink := func(from, from_variant, to, to_variant string) {
1149 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1150 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1151 }
1152 // 9000 is quite a magic number.
1153 // Finalized SDK codenames are mapped as P(28), Q(29), ...
1154 // And, codenames which are not finalized yet(active_codenames + future_codenames) are numbered from 9000, 9001, ...
1155 // to distinguish them from finalized and future_api(10000)
1156 // In this test, "R" is assumed not finalized yet( listed in Platform_version_active_codenames) and translated into 9000
1157 // (refer android/api_levels.go)
1158 expectLink("libx", "shared_myapex", "libz", "shared_9000")
1159 expectNoLink("libx", "shared_myapex", "libz", "shared_29")
1160 expectNoLink("libx", "shared_myapex", "libz", "shared")
1161}
1162
Jooyung Han0c4e0162020-02-26 22:45:42 +09001163func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1164 ctx, _ := testApex(t, `
1165 apex {
1166 name: "myapex",
1167 key: "myapex.key",
1168 native_shared_libs: ["libx"],
1169 }
1170
1171 apex_key {
1172 name: "myapex.key",
1173 public_key: "testkey.avbpubkey",
1174 private_key: "testkey.pem",
1175 }
1176
1177 cc_library {
1178 name: "libx",
1179 shared_libs: ["libz"],
1180 system_shared_libs: [],
1181 stl: "none",
1182 apex_available: [ "myapex" ],
1183 }
1184
1185 cc_library {
1186 name: "libz",
1187 system_shared_libs: [],
1188 stl: "none",
1189 stubs: {
1190 versions: ["1", "2"],
1191 },
1192 }
1193 `)
1194
1195 expectLink := func(from, from_variant, to, to_variant string) {
1196 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1197 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1198 }
1199 expectNoLink := func(from, from_variant, to, to_variant string) {
1200 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1201 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1202 }
1203 expectLink("libx", "shared_myapex", "libz", "shared_2")
1204 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1205 expectNoLink("libx", "shared_myapex", "libz", "shared")
1206}
1207
1208func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1209 ctx, _ := testApex(t, `
1210 apex {
1211 name: "myapex",
1212 key: "myapex.key",
1213 native_shared_libs: ["libx"],
1214 }
1215
1216 apex_key {
1217 name: "myapex.key",
1218 public_key: "testkey.avbpubkey",
1219 private_key: "testkey.pem",
1220 }
1221
1222 cc_library {
1223 name: "libx",
1224 system_shared_libs: [],
1225 stl: "none",
1226 apex_available: [ "myapex" ],
1227 stubs: {
1228 versions: ["1", "2"],
1229 },
1230 }
1231
1232 cc_library {
1233 name: "libz",
1234 shared_libs: ["libx"],
1235 system_shared_libs: [],
1236 stl: "none",
1237 }
1238 `)
1239
1240 expectLink := func(from, from_variant, to, to_variant string) {
1241 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1242 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1243 }
1244 expectNoLink := func(from, from_variant, to, to_variant string) {
1245 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1246 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1247 }
1248 expectLink("libz", "shared", "libx", "shared_2")
1249 expectNoLink("libz", "shared", "libz", "shared_1")
1250 expectNoLink("libz", "shared", "libz", "shared")
1251}
1252
Jooyung Han74066602020-03-20 04:29:24 +09001253func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Jooyung Han0c4e0162020-02-26 22:45:42 +09001254 ctx, _ := testApex(t, `
1255 apex {
1256 name: "myapex",
1257 key: "myapex.key",
1258 native_shared_libs: ["libx"],
1259 min_sdk_version: "29",
1260 }
1261
1262 apex_key {
1263 name: "myapex.key",
1264 public_key: "testkey.avbpubkey",
1265 private_key: "testkey.pem",
1266 }
1267
1268 cc_library {
1269 name: "libx",
1270 shared_libs: ["libbar"],
1271 apex_available: [ "myapex" ],
1272 }
1273
1274 cc_library {
1275 name: "libbar",
1276 stubs: {
1277 versions: ["29", "30"],
1278 },
1279 }
Jooyung Han74066602020-03-20 04:29:24 +09001280 `, func(fs map[string][]byte, config android.Config) {
1281 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1282 })
Jooyung Han0c4e0162020-02-26 22:45:42 +09001283 expectLink := func(from, from_variant, to, to_variant string) {
1284 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1285 libFlags := ld.Args["libFlags"]
1286 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1287 }
Jooyung Han74066602020-03-20 04:29:24 +09001288 expectLink("libx", "shared_hwasan_myapex", "libbar", "shared_30")
Jooyung Han0c4e0162020-02-26 22:45:42 +09001289}
1290
Jooyung Han74066602020-03-20 04:29:24 +09001291func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Jooyung Han0c4e0162020-02-26 22:45:42 +09001292 ctx, _ := testApex(t, `
1293 apex {
1294 name: "myapex",
1295 key: "myapex.key",
1296 native_shared_libs: ["libx"],
1297 min_sdk_version: "29",
1298 }
1299
1300 apex_key {
1301 name: "myapex.key",
1302 public_key: "testkey.avbpubkey",
1303 private_key: "testkey.pem",
1304 }
1305
1306 cc_library {
1307 name: "libx",
1308 apex_available: [ "myapex" ],
1309 }
Jooyung Han74066602020-03-20 04:29:24 +09001310 `)
Jooyung Han0c4e0162020-02-26 22:45:42 +09001311
1312 // ensure apex variant of c++ is linked with static unwinder
1313 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1314 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1315 // note that platform variant is not.
1316 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1317 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Han0c4e0162020-02-26 22:45:42 +09001318}
1319
1320func TestInvalidMinSdkVersion(t *testing.T) {
Jooyung Han74066602020-03-20 04:29:24 +09001321 testApexError(t, `"libz" .*: not found a version\(<=29\)`, `
Jooyung Han0c4e0162020-02-26 22:45:42 +09001322 apex {
1323 name: "myapex",
1324 key: "myapex.key",
1325 native_shared_libs: ["libx"],
1326 min_sdk_version: "29",
1327 }
1328
1329 apex_key {
1330 name: "myapex.key",
1331 public_key: "testkey.avbpubkey",
1332 private_key: "testkey.pem",
1333 }
1334
1335 cc_library {
1336 name: "libx",
1337 shared_libs: ["libz"],
1338 system_shared_libs: [],
1339 stl: "none",
1340 apex_available: [ "myapex" ],
1341 }
1342
1343 cc_library {
1344 name: "libz",
1345 system_shared_libs: [],
1346 stl: "none",
1347 stubs: {
1348 versions: ["30"],
1349 },
1350 }
Jooyung Han74066602020-03-20 04:29:24 +09001351 `)
Jooyung Han0c4e0162020-02-26 22:45:42 +09001352
Jooyung Han29e91d22020-04-02 01:41:41 +09001353 testApexError(t, `"myapex" .*: min_sdk_version: SDK version should be .*`, `
Jooyung Han0c4e0162020-02-26 22:45:42 +09001354 apex {
1355 name: "myapex",
1356 key: "myapex.key",
Jooyung Han29e91d22020-04-02 01:41:41 +09001357 min_sdk_version: "abc",
Jooyung Han0c4e0162020-02-26 22:45:42 +09001358 }
1359
1360 apex_key {
1361 name: "myapex.key",
1362 public_key: "testkey.avbpubkey",
1363 private_key: "testkey.pem",
1364 }
1365 `)
1366}
1367
Jiyong Park7c2ee712018-12-07 00:42:25 +09001368func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001369 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001370 apex {
1371 name: "myapex",
1372 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001373 native_shared_libs: ["mylib"],
1374 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001375 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001376 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001377 }
1378
1379 apex_key {
1380 name: "myapex.key",
1381 public_key: "testkey.avbpubkey",
1382 private_key: "testkey.pem",
1383 }
1384
1385 prebuilt_etc {
1386 name: "myetc",
1387 src: "myprebuilt",
1388 sub_dir: "foo/bar",
1389 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001390
1391 cc_library {
1392 name: "mylib",
1393 srcs: ["mylib.cpp"],
1394 relative_install_path: "foo/bar",
1395 system_shared_libs: [],
1396 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001397 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001398 }
1399
1400 cc_binary {
1401 name: "mybin",
1402 srcs: ["mylib.cpp"],
1403 relative_install_path: "foo/bar",
1404 system_shared_libs: [],
1405 static_executable: true,
1406 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001407 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001408 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001409 `)
1410
Sundong Ahnabb64432019-10-22 13:58:29 +09001411 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001412 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1413
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001414 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001415 ensureListContains(t, dirs, "etc")
1416 ensureListContains(t, dirs, "etc/foo")
1417 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001418 ensureListContains(t, dirs, "lib64")
1419 ensureListContains(t, dirs, "lib64/foo")
1420 ensureListContains(t, dirs, "lib64/foo/bar")
1421 ensureListContains(t, dirs, "lib")
1422 ensureListContains(t, dirs, "lib/foo")
1423 ensureListContains(t, dirs, "lib/foo/bar")
1424
Jiyong Parkbd13e442019-03-15 18:10:35 +09001425 ensureListContains(t, dirs, "bin")
1426 ensureListContains(t, dirs, "bin/foo")
1427 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001428}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001429
1430func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001431 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001432 apex {
1433 name: "myapex",
1434 key: "myapex.key",
1435 native_shared_libs: ["mylib"],
1436 use_vendor: true,
1437 }
1438
1439 apex_key {
1440 name: "myapex.key",
1441 public_key: "testkey.avbpubkey",
1442 private_key: "testkey.pem",
1443 }
1444
1445 cc_library {
1446 name: "mylib",
1447 srcs: ["mylib.cpp"],
1448 shared_libs: ["mylib2"],
1449 system_shared_libs: [],
1450 vendor_available: true,
1451 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001452 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001453 }
1454
1455 cc_library {
1456 name: "mylib2",
1457 srcs: ["mylib.cpp"],
1458 system_shared_libs: [],
1459 vendor_available: true,
1460 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001461 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001462 }
Jooyung Handc782442019-11-01 03:14:38 +09001463 `, func(fs map[string][]byte, config android.Config) {
1464 setUseVendorWhitelistForTest(config, []string{"myapex"})
1465 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001466
1467 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001468 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001469 for _, implicit := range i.Implicits {
1470 inputsList = append(inputsList, implicit.String())
1471 }
1472 }
1473 inputsString := strings.Join(inputsList, " ")
1474
1475 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001476 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1477 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001478
1479 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001480 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1481 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001482}
Jiyong Park16e91a02018-12-20 18:18:08 +09001483
Jooyung Handc782442019-11-01 03:14:38 +09001484func TestUseVendorRestriction(t *testing.T) {
1485 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1486 apex {
1487 name: "myapex",
1488 key: "myapex.key",
1489 use_vendor: true,
1490 }
1491 apex_key {
1492 name: "myapex.key",
1493 public_key: "testkey.avbpubkey",
1494 private_key: "testkey.pem",
1495 }
1496 `, func(fs map[string][]byte, config android.Config) {
1497 setUseVendorWhitelistForTest(config, []string{""})
1498 })
1499 // no error with whitelist
1500 testApex(t, `
1501 apex {
1502 name: "myapex",
1503 key: "myapex.key",
1504 use_vendor: true,
1505 }
1506 apex_key {
1507 name: "myapex.key",
1508 public_key: "testkey.avbpubkey",
1509 private_key: "testkey.pem",
1510 }
1511 `, func(fs map[string][]byte, config android.Config) {
1512 setUseVendorWhitelistForTest(config, []string{"myapex"})
1513 })
1514}
1515
Jooyung Han5c998b92019-06-27 11:30:33 +09001516func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1517 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1518 apex {
1519 name: "myapex",
1520 key: "myapex.key",
1521 native_shared_libs: ["mylib"],
1522 use_vendor: true,
1523 }
1524
1525 apex_key {
1526 name: "myapex.key",
1527 public_key: "testkey.avbpubkey",
1528 private_key: "testkey.pem",
1529 }
1530
1531 cc_library {
1532 name: "mylib",
1533 srcs: ["mylib.cpp"],
1534 system_shared_libs: [],
1535 stl: "none",
1536 }
1537 `)
1538}
1539
Jiyong Park16e91a02018-12-20 18:18:08 +09001540func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001541 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001542 apex {
1543 name: "myapex",
1544 key: "myapex.key",
1545 native_shared_libs: ["mylib"],
1546 }
1547
1548 apex_key {
1549 name: "myapex.key",
1550 public_key: "testkey.avbpubkey",
1551 private_key: "testkey.pem",
1552 }
1553
1554 cc_library {
1555 name: "mylib",
1556 srcs: ["mylib.cpp"],
1557 system_shared_libs: [],
1558 stl: "none",
1559 stubs: {
1560 versions: ["1", "2", "3"],
1561 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001562 apex_available: [
1563 "//apex_available:platform",
1564 "myapex",
1565 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001566 }
1567
1568 cc_binary {
1569 name: "not_in_apex",
1570 srcs: ["mylib.cpp"],
1571 static_libs: ["mylib"],
1572 static_executable: true,
1573 system_shared_libs: [],
1574 stl: "none",
1575 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001576 `)
1577
Colin Cross7113d202019-11-20 16:39:12 -08001578 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001579
1580 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001581 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001582}
Jiyong Park9335a262018-12-24 11:31:58 +09001583
1584func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001585 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001586 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001587 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001588 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001589 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001590 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001591 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001592 }
1593
1594 cc_library {
1595 name: "mylib",
1596 srcs: ["mylib.cpp"],
1597 system_shared_libs: [],
1598 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001599 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001600 }
1601
1602 apex_key {
1603 name: "myapex.key",
1604 public_key: "testkey.avbpubkey",
1605 private_key: "testkey.pem",
1606 }
1607
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001608 android_app_certificate {
1609 name: "myapex.certificate",
1610 certificate: "testkey",
1611 }
1612
1613 android_app_certificate {
1614 name: "myapex.certificate.override",
1615 certificate: "testkey.override",
1616 }
1617
Jiyong Park9335a262018-12-24 11:31:58 +09001618 `)
1619
1620 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001621 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001622
1623 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1624 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1625 "vendor/foo/devkeys/testkey.avbpubkey")
1626 }
1627 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1628 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1629 "vendor/foo/devkeys/testkey.pem")
1630 }
1631
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001632 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001633 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001634 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001635 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001636 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001637 }
1638}
Jiyong Park58e364a2019-01-19 19:24:06 +09001639
Jooyung Hanf121a652019-12-17 14:30:11 +09001640func TestCertificate(t *testing.T) {
1641 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1642 ctx, _ := testApex(t, `
1643 apex {
1644 name: "myapex",
1645 key: "myapex.key",
1646 }
1647 apex_key {
1648 name: "myapex.key",
1649 public_key: "testkey.avbpubkey",
1650 private_key: "testkey.pem",
1651 }`)
1652 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1653 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1654 if actual := rule.Args["certificates"]; actual != expected {
1655 t.Errorf("certificates should be %q, not %q", expected, actual)
1656 }
1657 })
1658 t.Run("override when unspecified", func(t *testing.T) {
1659 ctx, _ := testApex(t, `
1660 apex {
1661 name: "myapex_keytest",
1662 key: "myapex.key",
1663 file_contexts: ":myapex-file_contexts",
1664 }
1665 apex_key {
1666 name: "myapex.key",
1667 public_key: "testkey.avbpubkey",
1668 private_key: "testkey.pem",
1669 }
1670 android_app_certificate {
1671 name: "myapex.certificate.override",
1672 certificate: "testkey.override",
1673 }`)
1674 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1675 expected := "testkey.override.x509.pem testkey.override.pk8"
1676 if actual := rule.Args["certificates"]; actual != expected {
1677 t.Errorf("certificates should be %q, not %q", expected, actual)
1678 }
1679 })
1680 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1681 ctx, _ := testApex(t, `
1682 apex {
1683 name: "myapex",
1684 key: "myapex.key",
1685 certificate: ":myapex.certificate",
1686 }
1687 apex_key {
1688 name: "myapex.key",
1689 public_key: "testkey.avbpubkey",
1690 private_key: "testkey.pem",
1691 }
1692 android_app_certificate {
1693 name: "myapex.certificate",
1694 certificate: "testkey",
1695 }`)
1696 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1697 expected := "testkey.x509.pem testkey.pk8"
1698 if actual := rule.Args["certificates"]; actual != expected {
1699 t.Errorf("certificates should be %q, not %q", expected, actual)
1700 }
1701 })
1702 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1703 ctx, _ := testApex(t, `
1704 apex {
1705 name: "myapex_keytest",
1706 key: "myapex.key",
1707 file_contexts: ":myapex-file_contexts",
1708 certificate: ":myapex.certificate",
1709 }
1710 apex_key {
1711 name: "myapex.key",
1712 public_key: "testkey.avbpubkey",
1713 private_key: "testkey.pem",
1714 }
1715 android_app_certificate {
1716 name: "myapex.certificate.override",
1717 certificate: "testkey.override",
1718 }`)
1719 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1720 expected := "testkey.override.x509.pem testkey.override.pk8"
1721 if actual := rule.Args["certificates"]; actual != expected {
1722 t.Errorf("certificates should be %q, not %q", expected, actual)
1723 }
1724 })
1725 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1726 ctx, _ := testApex(t, `
1727 apex {
1728 name: "myapex",
1729 key: "myapex.key",
1730 certificate: "testkey",
1731 }
1732 apex_key {
1733 name: "myapex.key",
1734 public_key: "testkey.avbpubkey",
1735 private_key: "testkey.pem",
1736 }`)
1737 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1738 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1739 if actual := rule.Args["certificates"]; actual != expected {
1740 t.Errorf("certificates should be %q, not %q", expected, actual)
1741 }
1742 })
1743 t.Run("override when specified as <name>", func(t *testing.T) {
1744 ctx, _ := testApex(t, `
1745 apex {
1746 name: "myapex_keytest",
1747 key: "myapex.key",
1748 file_contexts: ":myapex-file_contexts",
1749 certificate: "testkey",
1750 }
1751 apex_key {
1752 name: "myapex.key",
1753 public_key: "testkey.avbpubkey",
1754 private_key: "testkey.pem",
1755 }
1756 android_app_certificate {
1757 name: "myapex.certificate.override",
1758 certificate: "testkey.override",
1759 }`)
1760 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1761 expected := "testkey.override.x509.pem testkey.override.pk8"
1762 if actual := rule.Args["certificates"]; actual != expected {
1763 t.Errorf("certificates should be %q, not %q", expected, actual)
1764 }
1765 })
1766}
1767
Jiyong Park58e364a2019-01-19 19:24:06 +09001768func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001769 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001770 apex {
1771 name: "myapex",
1772 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001773 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001774 }
1775
1776 apex {
1777 name: "otherapex",
1778 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001779 native_shared_libs: ["mylib", "mylib2"],
Jooyung Han61c41542020-03-07 03:45:53 +09001780 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09001781 }
1782
1783 apex_key {
1784 name: "myapex.key",
1785 public_key: "testkey.avbpubkey",
1786 private_key: "testkey.pem",
1787 }
1788
1789 cc_library {
1790 name: "mylib",
1791 srcs: ["mylib.cpp"],
1792 system_shared_libs: [],
1793 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001794 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001795 "myapex",
1796 "otherapex",
1797 ],
Jooyung Hanc3e92632020-03-21 23:20:55 +09001798 recovery_available: true,
Jiyong Park58e364a2019-01-19 19:24:06 +09001799 }
Jooyung Han68e511e2020-03-02 17:44:33 +09001800 cc_library {
1801 name: "mylib2",
1802 srcs: ["mylib.cpp"],
1803 system_shared_libs: [],
1804 stl: "none",
1805 apex_available: [
1806 "myapex",
1807 "otherapex",
1808 ],
1809 use_apex_name_macro: true,
1810 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001811 `)
1812
Jooyung Han68e511e2020-03-02 17:44:33 +09001813 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001814 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001815 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han74066602020-03-20 04:29:24 +09001816 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jooyung Han68e511e2020-03-02 17:44:33 +09001817
Jooyung Han61c41542020-03-07 03:45:53 +09001818 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Han68e511e2020-03-02 17:44:33 +09001819 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1820 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han61c41542020-03-07 03:45:53 +09001821 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09001822 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han68e511e2020-03-02 17:44:33 +09001823
Jooyung Han61c41542020-03-07 03:45:53 +09001824 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Han68e511e2020-03-02 17:44:33 +09001825 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1826 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han61c41542020-03-07 03:45:53 +09001827 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09001828 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001829
Jooyung Han68e511e2020-03-02 17:44:33 +09001830 // When cc_library sets use_apex_name_macro: true
1831 // apex variants define additional macro to distinguish which apex variant it is built for
1832
1833 // non-APEX variant does not have __ANDROID_APEX__ defined
1834 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1835 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1836
1837 // APEX variant has __ANDROID_APEX__ defined
1838 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001839 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001840 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1841 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001842
Jooyung Han68e511e2020-03-02 17:44:33 +09001843 // APEX variant has __ANDROID_APEX__ defined
1844 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001845 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001846 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1847 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Hanc3e92632020-03-21 23:20:55 +09001848
1849 // recovery variant does not set __ANDROID_SDK_VERSION__
1850 mylibCFlags = ctx.ModuleForTests("mylib", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1851 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1852 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001853}
Jiyong Park7e636d02019-01-28 16:16:54 +09001854
1855func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001856 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001857 apex {
1858 name: "myapex",
1859 key: "myapex.key",
1860 native_shared_libs: ["mylib"],
1861 }
1862
1863 apex_key {
1864 name: "myapex.key",
1865 public_key: "testkey.avbpubkey",
1866 private_key: "testkey.pem",
1867 }
1868
1869 cc_library_headers {
1870 name: "mylib_headers",
1871 export_include_dirs: ["my_include"],
1872 system_shared_libs: [],
1873 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001874 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001875 }
1876
1877 cc_library {
1878 name: "mylib",
1879 srcs: ["mylib.cpp"],
1880 system_shared_libs: [],
1881 stl: "none",
1882 header_libs: ["mylib_headers"],
1883 export_header_lib_headers: ["mylib_headers"],
1884 stubs: {
1885 versions: ["1", "2", "3"],
1886 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001887 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001888 }
1889
1890 cc_library {
1891 name: "otherlib",
1892 srcs: ["mylib.cpp"],
1893 system_shared_libs: [],
1894 stl: "none",
1895 shared_libs: ["mylib"],
1896 }
1897 `)
1898
Colin Cross7113d202019-11-20 16:39:12 -08001899 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001900
1901 // Ensure that the include path of the header lib is exported to 'otherlib'
1902 ensureContains(t, cFlags, "-Imy_include")
1903}
Alex Light9670d332019-01-29 18:07:33 -08001904
Jiyong Park7cd10e32020-01-14 09:22:18 +09001905type fileInApex struct {
1906 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001907 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001908 isLink bool
1909}
1910
Jooyung Hana57af4a2020-01-23 05:36:59 +00001911func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001912 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001913 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001914 copyCmds := apexRule.Args["copy_commands"]
1915 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001916 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001917 for _, cmd := range strings.Split(copyCmds, "&&") {
1918 cmd = strings.TrimSpace(cmd)
1919 if cmd == "" {
1920 continue
1921 }
1922 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001923 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001924 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001925 switch terms[0] {
1926 case "mkdir":
1927 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001928 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001929 t.Fatal("copyCmds contains invalid cp command", cmd)
1930 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001931 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001932 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001933 isLink = false
1934 case "ln":
1935 if len(terms) != 3 && len(terms) != 4 {
1936 // ln LINK TARGET or ln -s LINK TARGET
1937 t.Fatal("copyCmds contains invalid ln command", cmd)
1938 }
1939 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001940 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001941 isLink = true
1942 default:
1943 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1944 }
1945 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001946 index := strings.Index(dst, imageApexDir)
1947 if index == -1 {
1948 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1949 }
1950 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001951 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001952 }
1953 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001954 return ret
1955}
1956
Jooyung Hana57af4a2020-01-23 05:36:59 +00001957func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1958 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001959 var failed bool
1960 var surplus []string
1961 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001962 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Han8d8906c2020-02-27 13:31:56 +09001963 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001964 for _, expected := range files {
1965 if matched, _ := path.Match(expected, file.path); matched {
1966 filesMatched[expected] = true
Jooyung Han8d8906c2020-02-27 13:31:56 +09001967 mactchFound = true
1968 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001969 }
1970 }
Jooyung Han8d8906c2020-02-27 13:31:56 +09001971 if !mactchFound {
1972 surplus = append(surplus, file.path)
1973 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001974 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001975
Jooyung Han31c470b2019-10-18 16:26:59 +09001976 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001977 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001978 t.Log("surplus files", surplus)
1979 failed = true
1980 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001981
1982 if len(files) > len(filesMatched) {
1983 var missing []string
1984 for _, expected := range files {
1985 if !filesMatched[expected] {
1986 missing = append(missing, expected)
1987 }
1988 }
1989 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001990 t.Log("missing files", missing)
1991 failed = true
1992 }
1993 if failed {
1994 t.Fail()
1995 }
1996}
1997
Jooyung Han344d5432019-08-23 11:17:39 +09001998func TestVndkApexCurrent(t *testing.T) {
1999 ctx, _ := testApex(t, `
2000 apex_vndk {
2001 name: "myapex",
2002 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002003 }
2004
2005 apex_key {
2006 name: "myapex.key",
2007 public_key: "testkey.avbpubkey",
2008 private_key: "testkey.pem",
2009 }
2010
2011 cc_library {
2012 name: "libvndk",
2013 srcs: ["mylib.cpp"],
2014 vendor_available: true,
2015 vndk: {
2016 enabled: true,
2017 },
2018 system_shared_libs: [],
2019 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002020 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002021 }
2022
2023 cc_library {
2024 name: "libvndksp",
2025 srcs: ["mylib.cpp"],
2026 vendor_available: true,
2027 vndk: {
2028 enabled: true,
2029 support_system_process: true,
2030 },
2031 system_shared_libs: [],
2032 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002033 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002034 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002035 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002036
Jooyung Hana57af4a2020-01-23 05:36:59 +00002037 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002038 "lib/libvndk.so",
2039 "lib/libvndksp.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002040 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002041 "lib64/libvndk.so",
2042 "lib64/libvndksp.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002043 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002044 "etc/llndk.libraries.VER.txt",
2045 "etc/vndkcore.libraries.VER.txt",
2046 "etc/vndksp.libraries.VER.txt",
2047 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002048 })
Jooyung Han344d5432019-08-23 11:17:39 +09002049}
2050
2051func TestVndkApexWithPrebuilt(t *testing.T) {
2052 ctx, _ := testApex(t, `
2053 apex_vndk {
2054 name: "myapex",
2055 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002056 }
2057
2058 apex_key {
2059 name: "myapex.key",
2060 public_key: "testkey.avbpubkey",
2061 private_key: "testkey.pem",
2062 }
2063
2064 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002065 name: "libvndk",
2066 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002067 vendor_available: true,
2068 vndk: {
2069 enabled: true,
2070 },
2071 system_shared_libs: [],
2072 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002073 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002074 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002075
2076 cc_prebuilt_library_shared {
2077 name: "libvndk.arm",
2078 srcs: ["libvndk.arm.so"],
2079 vendor_available: true,
2080 vndk: {
2081 enabled: true,
2082 },
2083 enabled: false,
2084 arch: {
2085 arm: {
2086 enabled: true,
2087 },
2088 },
2089 system_shared_libs: [],
2090 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002091 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002092 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002093 `+vndkLibrariesTxtFiles("current"),
2094 withFiles(map[string][]byte{
2095 "libvndk.so": nil,
2096 "libvndk.arm.so": nil,
2097 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002098
Jooyung Hana57af4a2020-01-23 05:36:59 +00002099 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002100 "lib/libvndk.so",
2101 "lib/libvndk.arm.so",
2102 "lib64/libvndk.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002103 "lib/libc++.so",
2104 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002105 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002106 })
Jooyung Han344d5432019-08-23 11:17:39 +09002107}
2108
Jooyung Han39edb6c2019-11-06 16:53:07 +09002109func vndkLibrariesTxtFiles(vers ...string) (result string) {
2110 for _, v := range vers {
2111 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002112 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002113 result += `
2114 vndk_libraries_txt {
2115 name: "` + txt + `.libraries.txt",
2116 }
2117 `
2118 }
2119 } else {
2120 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2121 result += `
2122 prebuilt_etc {
2123 name: "` + txt + `.libraries.` + v + `.txt",
2124 src: "dummy.txt",
2125 }
2126 `
2127 }
2128 }
2129 }
2130 return
2131}
2132
Jooyung Han344d5432019-08-23 11:17:39 +09002133func TestVndkApexVersion(t *testing.T) {
2134 ctx, _ := testApex(t, `
2135 apex_vndk {
2136 name: "myapex_v27",
2137 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002138 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002139 vndk_version: "27",
2140 }
2141
2142 apex_key {
2143 name: "myapex.key",
2144 public_key: "testkey.avbpubkey",
2145 private_key: "testkey.pem",
2146 }
2147
Jooyung Han31c470b2019-10-18 16:26:59 +09002148 vndk_prebuilt_shared {
2149 name: "libvndk27",
2150 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002151 vendor_available: true,
2152 vndk: {
2153 enabled: true,
2154 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002155 target_arch: "arm64",
2156 arch: {
2157 arm: {
2158 srcs: ["libvndk27_arm.so"],
2159 },
2160 arm64: {
2161 srcs: ["libvndk27_arm64.so"],
2162 },
2163 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002164 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002165 }
2166
2167 vndk_prebuilt_shared {
2168 name: "libvndk27",
2169 version: "27",
2170 vendor_available: true,
2171 vndk: {
2172 enabled: true,
2173 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002174 target_arch: "x86_64",
2175 arch: {
2176 x86: {
2177 srcs: ["libvndk27_x86.so"],
2178 },
2179 x86_64: {
2180 srcs: ["libvndk27_x86_64.so"],
2181 },
2182 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002183 }
2184 `+vndkLibrariesTxtFiles("27"),
2185 withFiles(map[string][]byte{
2186 "libvndk27_arm.so": nil,
2187 "libvndk27_arm64.so": nil,
2188 "libvndk27_x86.so": nil,
2189 "libvndk27_x86_64.so": nil,
2190 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002191
Jooyung Hana57af4a2020-01-23 05:36:59 +00002192 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002193 "lib/libvndk27_arm.so",
2194 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002195 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002196 })
Jooyung Han344d5432019-08-23 11:17:39 +09002197}
2198
2199func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2200 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2201 apex_vndk {
2202 name: "myapex_v27",
2203 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002204 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002205 vndk_version: "27",
2206 }
2207 apex_vndk {
2208 name: "myapex_v27_other",
2209 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002210 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002211 vndk_version: "27",
2212 }
2213
2214 apex_key {
2215 name: "myapex.key",
2216 public_key: "testkey.avbpubkey",
2217 private_key: "testkey.pem",
2218 }
2219
2220 cc_library {
2221 name: "libvndk",
2222 srcs: ["mylib.cpp"],
2223 vendor_available: true,
2224 vndk: {
2225 enabled: true,
2226 },
2227 system_shared_libs: [],
2228 stl: "none",
2229 }
2230
2231 vndk_prebuilt_shared {
2232 name: "libvndk",
2233 version: "27",
2234 vendor_available: true,
2235 vndk: {
2236 enabled: true,
2237 },
2238 srcs: ["libvndk.so"],
2239 }
2240 `, withFiles(map[string][]byte{
2241 "libvndk.so": nil,
2242 }))
2243}
2244
Jooyung Han90eee022019-10-01 20:02:42 +09002245func TestVndkApexNameRule(t *testing.T) {
2246 ctx, _ := testApex(t, `
2247 apex_vndk {
2248 name: "myapex",
2249 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002250 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002251 }
2252 apex_vndk {
2253 name: "myapex_v28",
2254 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002255 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002256 vndk_version: "28",
2257 }
2258 apex_key {
2259 name: "myapex.key",
2260 public_key: "testkey.avbpubkey",
2261 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002262 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002263
2264 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002265 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002266 actual := proptools.String(bundle.properties.Apex_name)
2267 if !reflect.DeepEqual(actual, expected) {
2268 t.Errorf("Got '%v', expected '%v'", actual, expected)
2269 }
2270 }
2271
2272 assertApexName("com.android.vndk.vVER", "myapex")
2273 assertApexName("com.android.vndk.v28", "myapex_v28")
2274}
2275
Jooyung Han344d5432019-08-23 11:17:39 +09002276func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2277 ctx, _ := testApex(t, `
2278 apex_vndk {
2279 name: "myapex",
2280 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002281 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002282 }
2283
2284 apex_key {
2285 name: "myapex.key",
2286 public_key: "testkey.avbpubkey",
2287 private_key: "testkey.pem",
2288 }
2289
2290 cc_library {
2291 name: "libvndk",
2292 srcs: ["mylib.cpp"],
2293 vendor_available: true,
2294 native_bridge_supported: true,
2295 host_supported: true,
2296 vndk: {
2297 enabled: true,
2298 },
2299 system_shared_libs: [],
2300 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002301 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002302 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002303 `+vndkLibrariesTxtFiles("current"),
2304 withTargets(map[android.OsType][]android.Target{
2305 android.Android: []android.Target{
2306 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2307 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2308 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2309 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2310 },
2311 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002312
Jooyung Hana57af4a2020-01-23 05:36:59 +00002313 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002314 "lib/libvndk.so",
2315 "lib64/libvndk.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002316 "lib/libc++.so",
2317 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002318 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002319 })
Jooyung Han344d5432019-08-23 11:17:39 +09002320}
2321
2322func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2323 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2324 apex_vndk {
2325 name: "myapex",
2326 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002327 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002328 native_bridge_supported: true,
2329 }
2330
2331 apex_key {
2332 name: "myapex.key",
2333 public_key: "testkey.avbpubkey",
2334 private_key: "testkey.pem",
2335 }
2336
2337 cc_library {
2338 name: "libvndk",
2339 srcs: ["mylib.cpp"],
2340 vendor_available: true,
2341 native_bridge_supported: true,
2342 host_supported: true,
2343 vndk: {
2344 enabled: true,
2345 },
2346 system_shared_libs: [],
2347 stl: "none",
2348 }
2349 `)
2350}
2351
Jooyung Han31c470b2019-10-18 16:26:59 +09002352func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002353 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002354 apex_vndk {
2355 name: "myapex_v27",
2356 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002357 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002358 vndk_version: "27",
2359 }
2360
2361 apex_key {
2362 name: "myapex.key",
2363 public_key: "testkey.avbpubkey",
2364 private_key: "testkey.pem",
2365 }
2366
2367 vndk_prebuilt_shared {
2368 name: "libvndk27",
2369 version: "27",
2370 target_arch: "arm",
2371 vendor_available: true,
2372 vndk: {
2373 enabled: true,
2374 },
2375 arch: {
2376 arm: {
2377 srcs: ["libvndk27.so"],
2378 }
2379 },
2380 }
2381
2382 vndk_prebuilt_shared {
2383 name: "libvndk27",
2384 version: "27",
2385 target_arch: "arm",
2386 binder32bit: true,
2387 vendor_available: true,
2388 vndk: {
2389 enabled: true,
2390 },
2391 arch: {
2392 arm: {
2393 srcs: ["libvndk27binder32.so"],
2394 }
2395 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002396 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002397 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002398 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002399 withFiles(map[string][]byte{
2400 "libvndk27.so": nil,
2401 "libvndk27binder32.so": nil,
2402 }),
2403 withBinder32bit,
2404 withTargets(map[android.OsType][]android.Target{
2405 android.Android: []android.Target{
2406 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2407 },
2408 }),
2409 )
2410
Jooyung Hana57af4a2020-01-23 05:36:59 +00002411 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002412 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002413 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002414 })
2415}
2416
Jooyung Hane1633032019-08-01 17:41:43 +09002417func TestDependenciesInApexManifest(t *testing.T) {
2418 ctx, _ := testApex(t, `
2419 apex {
2420 name: "myapex_nodep",
2421 key: "myapex.key",
2422 native_shared_libs: ["lib_nodep"],
2423 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002424 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002425 }
2426
2427 apex {
2428 name: "myapex_dep",
2429 key: "myapex.key",
2430 native_shared_libs: ["lib_dep"],
2431 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002432 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002433 }
2434
2435 apex {
2436 name: "myapex_provider",
2437 key: "myapex.key",
2438 native_shared_libs: ["libfoo"],
2439 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002440 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002441 }
2442
2443 apex {
2444 name: "myapex_selfcontained",
2445 key: "myapex.key",
2446 native_shared_libs: ["lib_dep", "libfoo"],
2447 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002448 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002449 }
2450
2451 apex_key {
2452 name: "myapex.key",
2453 public_key: "testkey.avbpubkey",
2454 private_key: "testkey.pem",
2455 }
2456
2457 cc_library {
2458 name: "lib_nodep",
2459 srcs: ["mylib.cpp"],
2460 system_shared_libs: [],
2461 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002462 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002463 }
2464
2465 cc_library {
2466 name: "lib_dep",
2467 srcs: ["mylib.cpp"],
2468 shared_libs: ["libfoo"],
2469 system_shared_libs: [],
2470 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002471 apex_available: [
2472 "myapex_dep",
2473 "myapex_provider",
2474 "myapex_selfcontained",
2475 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002476 }
2477
2478 cc_library {
2479 name: "libfoo",
2480 srcs: ["mytest.cpp"],
2481 stubs: {
2482 versions: ["1"],
2483 },
2484 system_shared_libs: [],
2485 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002486 apex_available: [
2487 "myapex_provider",
2488 "myapex_selfcontained",
2489 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002490 }
2491 `)
2492
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002493 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002494 var provideNativeLibs, requireNativeLibs []string
2495
Sundong Ahnabb64432019-10-22 13:58:29 +09002496 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002497 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2498 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002499 ensureListEmpty(t, provideNativeLibs)
2500 ensureListEmpty(t, requireNativeLibs)
2501
Sundong Ahnabb64432019-10-22 13:58:29 +09002502 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002503 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2504 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002505 ensureListEmpty(t, provideNativeLibs)
2506 ensureListContains(t, requireNativeLibs, "libfoo.so")
2507
Sundong Ahnabb64432019-10-22 13:58:29 +09002508 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002509 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2510 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002511 ensureListContains(t, provideNativeLibs, "libfoo.so")
2512 ensureListEmpty(t, requireNativeLibs)
2513
Sundong Ahnabb64432019-10-22 13:58:29 +09002514 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002515 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2516 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002517 ensureListContains(t, provideNativeLibs, "libfoo.so")
2518 ensureListEmpty(t, requireNativeLibs)
2519}
2520
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002521func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002522 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002523 apex {
2524 name: "myapex",
2525 key: "myapex.key",
2526 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002527 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002528 }
2529
2530 apex_key {
2531 name: "myapex.key",
2532 public_key: "testkey.avbpubkey",
2533 private_key: "testkey.pem",
2534 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002535
2536 cc_library {
2537 name: "mylib",
2538 srcs: ["mylib.cpp"],
2539 system_shared_libs: [],
2540 stl: "none",
2541 apex_available: [
2542 "//apex_available:platform",
2543 "myapex",
2544 ],
2545 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002546 `)
2547
Sundong Ahnabb64432019-10-22 13:58:29 +09002548 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002549 apexManifestRule := module.Rule("apexManifestRule")
2550 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2551 apexRule := module.Rule("apexRule")
2552 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002553
2554 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2555 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2556 name := apexBundle.BaseModuleName()
2557 prefix := "TARGET_"
2558 var builder strings.Builder
2559 data.Custom(&builder, name, prefix, "", data)
2560 androidMk := builder.String()
2561 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2562 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002563}
2564
Alex Light0851b882019-02-07 13:20:53 -08002565func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002566 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002567 apex {
2568 name: "myapex",
2569 key: "myapex.key",
2570 native_shared_libs: ["mylib_common"],
2571 }
2572
2573 apex_key {
2574 name: "myapex.key",
2575 public_key: "testkey.avbpubkey",
2576 private_key: "testkey.pem",
2577 }
2578
2579 cc_library {
2580 name: "mylib_common",
2581 srcs: ["mylib.cpp"],
2582 system_shared_libs: [],
2583 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002584 apex_available: [
2585 "//apex_available:platform",
2586 "myapex",
2587 ],
Alex Light0851b882019-02-07 13:20:53 -08002588 }
2589 `)
2590
Sundong Ahnabb64432019-10-22 13:58:29 +09002591 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002592 apexRule := module.Rule("apexRule")
2593 copyCmds := apexRule.Args["copy_commands"]
2594
2595 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2596 t.Log("Apex was a test apex!")
2597 t.Fail()
2598 }
2599 // Ensure that main rule creates an output
2600 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2601
2602 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002603 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002604
2605 // Ensure that both direct and indirect deps are copied into apex
2606 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2607
Colin Cross7113d202019-11-20 16:39:12 -08002608 // Ensure that the platform variant ends with _shared
2609 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002610
2611 if !android.InAnyApex("mylib_common") {
2612 t.Log("Found mylib_common not in any apex!")
2613 t.Fail()
2614 }
2615}
2616
2617func TestTestApex(t *testing.T) {
2618 if android.InAnyApex("mylib_common_test") {
2619 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!")
2620 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002621 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002622 apex_test {
2623 name: "myapex",
2624 key: "myapex.key",
2625 native_shared_libs: ["mylib_common_test"],
2626 }
2627
2628 apex_key {
2629 name: "myapex.key",
2630 public_key: "testkey.avbpubkey",
2631 private_key: "testkey.pem",
2632 }
2633
2634 cc_library {
2635 name: "mylib_common_test",
2636 srcs: ["mylib.cpp"],
2637 system_shared_libs: [],
2638 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002639 // TODO: remove //apex_available:platform
2640 apex_available: [
2641 "//apex_available:platform",
2642 "myapex",
2643 ],
Alex Light0851b882019-02-07 13:20:53 -08002644 }
2645 `)
2646
Sundong Ahnabb64432019-10-22 13:58:29 +09002647 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002648 apexRule := module.Rule("apexRule")
2649 copyCmds := apexRule.Args["copy_commands"]
2650
2651 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2652 t.Log("Apex was not a test apex!")
2653 t.Fail()
2654 }
2655 // Ensure that main rule creates an output
2656 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2657
2658 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002659 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002660
2661 // Ensure that both direct and indirect deps are copied into apex
2662 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2663
Colin Cross7113d202019-11-20 16:39:12 -08002664 // Ensure that the platform variant ends with _shared
2665 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002666}
2667
Alex Light9670d332019-01-29 18:07:33 -08002668func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002669 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002670 apex {
2671 name: "myapex",
2672 key: "myapex.key",
2673 multilib: {
2674 first: {
2675 native_shared_libs: ["mylib_common"],
2676 }
2677 },
2678 target: {
2679 android: {
2680 multilib: {
2681 first: {
2682 native_shared_libs: ["mylib"],
2683 }
2684 }
2685 },
2686 host: {
2687 multilib: {
2688 first: {
2689 native_shared_libs: ["mylib2"],
2690 }
2691 }
2692 }
2693 }
2694 }
2695
2696 apex_key {
2697 name: "myapex.key",
2698 public_key: "testkey.avbpubkey",
2699 private_key: "testkey.pem",
2700 }
2701
2702 cc_library {
2703 name: "mylib",
2704 srcs: ["mylib.cpp"],
2705 system_shared_libs: [],
2706 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002707 // TODO: remove //apex_available:platform
2708 apex_available: [
2709 "//apex_available:platform",
2710 "myapex",
2711 ],
Alex Light9670d332019-01-29 18:07:33 -08002712 }
2713
2714 cc_library {
2715 name: "mylib_common",
2716 srcs: ["mylib.cpp"],
2717 system_shared_libs: [],
2718 stl: "none",
2719 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002720 // TODO: remove //apex_available:platform
2721 apex_available: [
2722 "//apex_available:platform",
2723 "myapex",
2724 ],
Alex Light9670d332019-01-29 18:07:33 -08002725 }
2726
2727 cc_library {
2728 name: "mylib2",
2729 srcs: ["mylib.cpp"],
2730 system_shared_libs: [],
2731 stl: "none",
2732 compile_multilib: "first",
2733 }
2734 `)
2735
Sundong Ahnabb64432019-10-22 13:58:29 +09002736 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002737 copyCmds := apexRule.Args["copy_commands"]
2738
2739 // Ensure that main rule creates an output
2740 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2741
2742 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002743 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2744 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2745 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002746
2747 // Ensure that both direct and indirect deps are copied into apex
2748 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2749 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2750 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2751
Colin Cross7113d202019-11-20 16:39:12 -08002752 // Ensure that the platform variant ends with _shared
2753 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2754 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2755 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002756}
Jiyong Park04480cf2019-02-06 00:16:29 +09002757
2758func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002759 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002760 apex {
2761 name: "myapex",
2762 key: "myapex.key",
2763 binaries: ["myscript"],
2764 }
2765
2766 apex_key {
2767 name: "myapex.key",
2768 public_key: "testkey.avbpubkey",
2769 private_key: "testkey.pem",
2770 }
2771
2772 sh_binary {
2773 name: "myscript",
2774 src: "mylib.cpp",
2775 filename: "myscript.sh",
2776 sub_dir: "script",
2777 }
2778 `)
2779
Sundong Ahnabb64432019-10-22 13:58:29 +09002780 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002781 copyCmds := apexRule.Args["copy_commands"]
2782
2783 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2784}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002785
Jooyung Han91df2082019-11-20 01:49:42 +09002786func TestApexInVariousPartition(t *testing.T) {
2787 testcases := []struct {
2788 propName, parition, flattenedPartition string
2789 }{
2790 {"", "system", "system_ext"},
2791 {"product_specific: true", "product", "product"},
2792 {"soc_specific: true", "vendor", "vendor"},
2793 {"proprietary: true", "vendor", "vendor"},
2794 {"vendor: true", "vendor", "vendor"},
2795 {"system_ext_specific: true", "system_ext", "system_ext"},
2796 }
2797 for _, tc := range testcases {
2798 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2799 ctx, _ := testApex(t, `
2800 apex {
2801 name: "myapex",
2802 key: "myapex.key",
2803 `+tc.propName+`
2804 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002805
Jooyung Han91df2082019-11-20 01:49:42 +09002806 apex_key {
2807 name: "myapex.key",
2808 public_key: "testkey.avbpubkey",
2809 private_key: "testkey.pem",
2810 }
2811 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002812
Jooyung Han91df2082019-11-20 01:49:42 +09002813 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2814 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2815 actual := apex.installDir.String()
2816 if actual != expected {
2817 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2818 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002819
Jooyung Han91df2082019-11-20 01:49:42 +09002820 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2821 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2822 actual = flattened.installDir.String()
2823 if actual != expected {
2824 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2825 }
2826 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002827 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002828}
Jiyong Park67882562019-03-21 01:11:21 +09002829
Jooyung Han54aca7b2019-11-20 02:26:02 +09002830func TestFileContexts(t *testing.T) {
2831 ctx, _ := testApex(t, `
2832 apex {
2833 name: "myapex",
2834 key: "myapex.key",
2835 }
2836
2837 apex_key {
2838 name: "myapex.key",
2839 public_key: "testkey.avbpubkey",
2840 private_key: "testkey.pem",
2841 }
2842 `)
2843 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2844 apexRule := module.Rule("apexRule")
2845 actual := apexRule.Args["file_contexts"]
2846 expected := "system/sepolicy/apex/myapex-file_contexts"
2847 if actual != expected {
2848 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2849 }
2850
2851 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2852 apex {
2853 name: "myapex",
2854 key: "myapex.key",
2855 file_contexts: "my_own_file_contexts",
2856 }
2857
2858 apex_key {
2859 name: "myapex.key",
2860 public_key: "testkey.avbpubkey",
2861 private_key: "testkey.pem",
2862 }
2863 `, withFiles(map[string][]byte{
2864 "my_own_file_contexts": nil,
2865 }))
2866
2867 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2868 apex {
2869 name: "myapex",
2870 key: "myapex.key",
2871 product_specific: true,
2872 file_contexts: "product_specific_file_contexts",
2873 }
2874
2875 apex_key {
2876 name: "myapex.key",
2877 public_key: "testkey.avbpubkey",
2878 private_key: "testkey.pem",
2879 }
2880 `)
2881
2882 ctx, _ = testApex(t, `
2883 apex {
2884 name: "myapex",
2885 key: "myapex.key",
2886 product_specific: true,
2887 file_contexts: "product_specific_file_contexts",
2888 }
2889
2890 apex_key {
2891 name: "myapex.key",
2892 public_key: "testkey.avbpubkey",
2893 private_key: "testkey.pem",
2894 }
2895 `, withFiles(map[string][]byte{
2896 "product_specific_file_contexts": nil,
2897 }))
2898 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2899 apexRule = module.Rule("apexRule")
2900 actual = apexRule.Args["file_contexts"]
2901 expected = "product_specific_file_contexts"
2902 if actual != expected {
2903 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2904 }
2905
2906 ctx, _ = testApex(t, `
2907 apex {
2908 name: "myapex",
2909 key: "myapex.key",
2910 product_specific: true,
2911 file_contexts: ":my-file-contexts",
2912 }
2913
2914 apex_key {
2915 name: "myapex.key",
2916 public_key: "testkey.avbpubkey",
2917 private_key: "testkey.pem",
2918 }
2919
2920 filegroup {
2921 name: "my-file-contexts",
2922 srcs: ["product_specific_file_contexts"],
2923 }
2924 `, withFiles(map[string][]byte{
2925 "product_specific_file_contexts": nil,
2926 }))
2927 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2928 apexRule = module.Rule("apexRule")
2929 actual = apexRule.Args["file_contexts"]
2930 expected = "product_specific_file_contexts"
2931 if actual != expected {
2932 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2933 }
2934}
2935
Jiyong Park67882562019-03-21 01:11:21 +09002936func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002937 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002938 apex_key {
2939 name: "myapex.key",
2940 public_key: ":my.avbpubkey",
2941 private_key: ":my.pem",
2942 product_specific: true,
2943 }
2944
2945 filegroup {
2946 name: "my.avbpubkey",
2947 srcs: ["testkey2.avbpubkey"],
2948 }
2949
2950 filegroup {
2951 name: "my.pem",
2952 srcs: ["testkey2.pem"],
2953 }
2954 `)
2955
2956 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2957 expected_pubkey := "testkey2.avbpubkey"
2958 actual_pubkey := apex_key.public_key_file.String()
2959 if actual_pubkey != expected_pubkey {
2960 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2961 }
2962 expected_privkey := "testkey2.pem"
2963 actual_privkey := apex_key.private_key_file.String()
2964 if actual_privkey != expected_privkey {
2965 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2966 }
2967}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002968
2969func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002970 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002971 prebuilt_apex {
2972 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002973 arch: {
2974 arm64: {
2975 src: "myapex-arm64.apex",
2976 },
2977 arm: {
2978 src: "myapex-arm.apex",
2979 },
2980 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002981 }
2982 `)
2983
2984 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2985
Jiyong Parkc95714e2019-03-29 14:23:10 +09002986 expectedInput := "myapex-arm64.apex"
2987 if prebuilt.inputApex.String() != expectedInput {
2988 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2989 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002990}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002991
2992func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002993 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002994 prebuilt_apex {
2995 name: "myapex",
2996 src: "myapex-arm.apex",
2997 filename: "notmyapex.apex",
2998 }
2999 `)
3000
3001 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3002
3003 expected := "notmyapex.apex"
3004 if p.installFilename != expected {
3005 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
3006 }
3007}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003008
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003009func TestPrebuiltOverrides(t *testing.T) {
3010 ctx, config := testApex(t, `
3011 prebuilt_apex {
3012 name: "myapex.prebuilt",
3013 src: "myapex-arm.apex",
3014 overrides: [
3015 "myapex",
3016 ],
3017 }
3018 `)
3019
3020 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3021
3022 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003023 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003024 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003025 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003026 }
3027}
3028
Roland Levillain630846d2019-06-26 12:48:34 +01003029func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003030 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003031 apex_test {
3032 name: "myapex",
3033 key: "myapex.key",
3034 tests: [
3035 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003036 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003037 ],
3038 }
3039
3040 apex_key {
3041 name: "myapex.key",
3042 public_key: "testkey.avbpubkey",
3043 private_key: "testkey.pem",
3044 }
3045
3046 cc_test {
3047 name: "mytest",
3048 gtest: false,
3049 srcs: ["mytest.cpp"],
3050 relative_install_path: "test",
3051 system_shared_libs: [],
3052 static_executable: true,
3053 stl: "none",
3054 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003055
3056 cc_test {
3057 name: "mytests",
3058 gtest: false,
3059 srcs: [
3060 "mytest1.cpp",
3061 "mytest2.cpp",
3062 "mytest3.cpp",
3063 ],
3064 test_per_src: true,
3065 relative_install_path: "test",
3066 system_shared_libs: [],
3067 static_executable: true,
3068 stl: "none",
3069 }
Roland Levillain630846d2019-06-26 12:48:34 +01003070 `)
3071
Sundong Ahnabb64432019-10-22 13:58:29 +09003072 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003073 copyCmds := apexRule.Args["copy_commands"]
3074
3075 // Ensure that test dep is copied into apex.
3076 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003077
3078 // Ensure that test deps built with `test_per_src` are copied into apex.
3079 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3080 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3081 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003082
3083 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003084 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003085 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3086 name := apexBundle.BaseModuleName()
3087 prefix := "TARGET_"
3088 var builder strings.Builder
3089 data.Custom(&builder, name, prefix, "", data)
3090 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003091 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3092 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3093 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3094 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003095 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003096 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003097 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003098}
3099
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003100func TestInstallExtraFlattenedApexes(t *testing.T) {
3101 ctx, config := testApex(t, `
3102 apex {
3103 name: "myapex",
3104 key: "myapex.key",
3105 }
3106 apex_key {
3107 name: "myapex.key",
3108 public_key: "testkey.avbpubkey",
3109 private_key: "testkey.pem",
3110 }
3111 `, func(fs map[string][]byte, config android.Config) {
3112 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3113 })
3114 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003115 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003116 mk := android.AndroidMkDataForTest(t, config, "", ab)
3117 var builder strings.Builder
3118 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3119 androidMk := builder.String()
3120 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3121}
3122
Jooyung Han5c998b92019-06-27 11:30:33 +09003123func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003124 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003125 apex {
3126 name: "myapex",
3127 key: "myapex.key",
3128 native_shared_libs: ["mylib"],
3129 uses: ["commonapex"],
3130 }
3131
3132 apex {
3133 name: "commonapex",
3134 key: "myapex.key",
3135 native_shared_libs: ["libcommon"],
3136 provide_cpp_shared_libs: true,
3137 }
3138
3139 apex_key {
3140 name: "myapex.key",
3141 public_key: "testkey.avbpubkey",
3142 private_key: "testkey.pem",
3143 }
3144
3145 cc_library {
3146 name: "mylib",
3147 srcs: ["mylib.cpp"],
3148 shared_libs: ["libcommon"],
3149 system_shared_libs: [],
3150 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003151 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003152 }
3153
3154 cc_library {
3155 name: "libcommon",
3156 srcs: ["mylib_common.cpp"],
3157 system_shared_libs: [],
3158 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003159 // TODO: remove //apex_available:platform
3160 apex_available: [
3161 "//apex_available:platform",
3162 "commonapex",
3163 "myapex",
3164 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003165 }
3166 `)
3167
Sundong Ahnabb64432019-10-22 13:58:29 +09003168 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003169 apexRule1 := module1.Rule("apexRule")
3170 copyCmds1 := apexRule1.Args["copy_commands"]
3171
Sundong Ahnabb64432019-10-22 13:58:29 +09003172 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003173 apexRule2 := module2.Rule("apexRule")
3174 copyCmds2 := apexRule2.Args["copy_commands"]
3175
Colin Cross7113d202019-11-20 16:39:12 -08003176 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3177 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003178 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3179 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3180 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3181}
3182
3183func TestApexUsesFailsIfNotProvided(t *testing.T) {
3184 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3185 apex {
3186 name: "myapex",
3187 key: "myapex.key",
3188 uses: ["commonapex"],
3189 }
3190
3191 apex {
3192 name: "commonapex",
3193 key: "myapex.key",
3194 }
3195
3196 apex_key {
3197 name: "myapex.key",
3198 public_key: "testkey.avbpubkey",
3199 private_key: "testkey.pem",
3200 }
3201 `)
3202 testApexError(t, `uses: "commonapex" is not a provider`, `
3203 apex {
3204 name: "myapex",
3205 key: "myapex.key",
3206 uses: ["commonapex"],
3207 }
3208
3209 cc_library {
3210 name: "commonapex",
3211 system_shared_libs: [],
3212 stl: "none",
3213 }
3214
3215 apex_key {
3216 name: "myapex.key",
3217 public_key: "testkey.avbpubkey",
3218 private_key: "testkey.pem",
3219 }
3220 `)
3221}
3222
3223func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3224 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3225 apex {
3226 name: "myapex",
3227 key: "myapex.key",
3228 use_vendor: true,
3229 uses: ["commonapex"],
3230 }
3231
3232 apex {
3233 name: "commonapex",
3234 key: "myapex.key",
3235 provide_cpp_shared_libs: true,
3236 }
3237
3238 apex_key {
3239 name: "myapex.key",
3240 public_key: "testkey.avbpubkey",
3241 private_key: "testkey.pem",
3242 }
Jooyung Handc782442019-11-01 03:14:38 +09003243 `, func(fs map[string][]byte, config android.Config) {
3244 setUseVendorWhitelistForTest(config, []string{"myapex"})
3245 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003246}
3247
Jooyung Hand48f3c32019-08-23 11:18:57 +09003248func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3249 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3250 apex {
3251 name: "myapex",
3252 key: "myapex.key",
3253 native_shared_libs: ["libfoo"],
3254 }
3255
3256 apex_key {
3257 name: "myapex.key",
3258 public_key: "testkey.avbpubkey",
3259 private_key: "testkey.pem",
3260 }
3261
3262 cc_library {
3263 name: "libfoo",
3264 stl: "none",
3265 system_shared_libs: [],
3266 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003267 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003268 }
3269 `)
3270 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3271 apex {
3272 name: "myapex",
3273 key: "myapex.key",
3274 java_libs: ["myjar"],
3275 }
3276
3277 apex_key {
3278 name: "myapex.key",
3279 public_key: "testkey.avbpubkey",
3280 private_key: "testkey.pem",
3281 }
3282
3283 java_library {
3284 name: "myjar",
3285 srcs: ["foo/bar/MyClass.java"],
3286 sdk_version: "none",
3287 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003288 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003289 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003290 }
3291 `)
3292}
3293
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003294func TestApexWithApps(t *testing.T) {
3295 ctx, _ := testApex(t, `
3296 apex {
3297 name: "myapex",
3298 key: "myapex.key",
3299 apps: [
3300 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003301 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003302 ],
3303 }
3304
3305 apex_key {
3306 name: "myapex.key",
3307 public_key: "testkey.avbpubkey",
3308 private_key: "testkey.pem",
3309 }
3310
3311 android_app {
3312 name: "AppFoo",
3313 srcs: ["foo/bar/MyClass.java"],
3314 sdk_version: "none",
3315 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003316 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003317 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003318 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003319
3320 android_app {
3321 name: "AppFooPriv",
3322 srcs: ["foo/bar/MyClass.java"],
3323 sdk_version: "none",
3324 system_modules: "none",
3325 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003326 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003327 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003328
3329 cc_library_shared {
3330 name: "libjni",
3331 srcs: ["mylib.cpp"],
Jooyung Han65041792020-02-25 16:59:29 +09003332 shared_libs: ["libfoo"],
Jiyong Park8be103b2019-11-08 15:53:48 +09003333 stl: "none",
3334 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003335 apex_available: [ "myapex" ],
Jooyung Han65041792020-02-25 16:59:29 +09003336 sdk_version: "current",
3337 }
3338
3339 cc_library_shared {
3340 name: "libfoo",
3341 stl: "none",
3342 system_shared_libs: [],
3343 apex_available: [ "myapex" ],
3344 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003345 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003346 `)
3347
Sundong Ahnabb64432019-10-22 13:58:29 +09003348 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003349 apexRule := module.Rule("apexRule")
3350 copyCmds := apexRule.Args["copy_commands"]
3351
3352 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003353 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003354
Jooyung Han65041792020-02-25 16:59:29 +09003355 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3356 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003357 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Han65041792020-02-25 16:59:29 +09003358 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003359 }
Jooyung Han65041792020-02-25 16:59:29 +09003360 // JNI libraries including transitive deps are
3361 for _, jni := range []string{"libjni", "libfoo"} {
3362 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
3363 // ... embedded inside APK (jnilibs.zip)
3364 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3365 // ... and not directly inside the APEX
3366 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3367 }
Dario Frenicde2a032019-10-27 00:29:22 +01003368}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003369
Dario Frenicde2a032019-10-27 00:29:22 +01003370func TestApexWithAppImports(t *testing.T) {
3371 ctx, _ := testApex(t, `
3372 apex {
3373 name: "myapex",
3374 key: "myapex.key",
3375 apps: [
3376 "AppFooPrebuilt",
3377 "AppFooPrivPrebuilt",
3378 ],
3379 }
3380
3381 apex_key {
3382 name: "myapex.key",
3383 public_key: "testkey.avbpubkey",
3384 private_key: "testkey.pem",
3385 }
3386
3387 android_app_import {
3388 name: "AppFooPrebuilt",
3389 apk: "PrebuiltAppFoo.apk",
3390 presigned: true,
3391 dex_preopt: {
3392 enabled: false,
3393 },
3394 }
3395
3396 android_app_import {
3397 name: "AppFooPrivPrebuilt",
3398 apk: "PrebuiltAppFooPriv.apk",
3399 privileged: true,
3400 presigned: true,
3401 dex_preopt: {
3402 enabled: false,
3403 },
3404 }
3405 `)
3406
Sundong Ahnabb64432019-10-22 13:58:29 +09003407 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003408 apexRule := module.Rule("apexRule")
3409 copyCmds := apexRule.Args["copy_commands"]
3410
3411 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3412 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003413}
3414
Dario Freni6f3937c2019-12-20 22:58:03 +00003415func TestApexWithTestHelperApp(t *testing.T) {
3416 ctx, _ := testApex(t, `
3417 apex {
3418 name: "myapex",
3419 key: "myapex.key",
3420 apps: [
3421 "TesterHelpAppFoo",
3422 ],
3423 }
3424
3425 apex_key {
3426 name: "myapex.key",
3427 public_key: "testkey.avbpubkey",
3428 private_key: "testkey.pem",
3429 }
3430
3431 android_test_helper_app {
3432 name: "TesterHelpAppFoo",
3433 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003434 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003435 }
3436
3437 `)
3438
3439 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3440 apexRule := module.Rule("apexRule")
3441 copyCmds := apexRule.Args["copy_commands"]
3442
3443 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3444}
3445
Jooyung Han18020ea2019-11-13 10:50:48 +09003446func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3447 // libfoo's apex_available comes from cc_defaults
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003448 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003449 apex {
3450 name: "myapex",
3451 key: "myapex.key",
3452 native_shared_libs: ["libfoo"],
3453 }
3454
3455 apex_key {
3456 name: "myapex.key",
3457 public_key: "testkey.avbpubkey",
3458 private_key: "testkey.pem",
3459 }
3460
3461 apex {
3462 name: "otherapex",
3463 key: "myapex.key",
3464 native_shared_libs: ["libfoo"],
3465 }
3466
3467 cc_defaults {
3468 name: "libfoo-defaults",
3469 apex_available: ["otherapex"],
3470 }
3471
3472 cc_library {
3473 name: "libfoo",
3474 defaults: ["libfoo-defaults"],
3475 stl: "none",
3476 system_shared_libs: [],
3477 }`)
3478}
3479
Jiyong Park127b40b2019-09-30 16:04:35 +09003480func TestApexAvailable(t *testing.T) {
3481 // libfoo is not available to myapex, but only to otherapex
3482 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3483 apex {
3484 name: "myapex",
3485 key: "myapex.key",
3486 native_shared_libs: ["libfoo"],
3487 }
3488
3489 apex_key {
3490 name: "myapex.key",
3491 public_key: "testkey.avbpubkey",
3492 private_key: "testkey.pem",
3493 }
3494
3495 apex {
3496 name: "otherapex",
3497 key: "otherapex.key",
3498 native_shared_libs: ["libfoo"],
3499 }
3500
3501 apex_key {
3502 name: "otherapex.key",
3503 public_key: "testkey.avbpubkey",
3504 private_key: "testkey.pem",
3505 }
3506
3507 cc_library {
3508 name: "libfoo",
3509 stl: "none",
3510 system_shared_libs: [],
3511 apex_available: ["otherapex"],
3512 }`)
3513
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003514 // libbbaz is an indirect dep
3515 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003516 apex {
3517 name: "myapex",
3518 key: "myapex.key",
3519 native_shared_libs: ["libfoo"],
3520 }
3521
3522 apex_key {
3523 name: "myapex.key",
3524 public_key: "testkey.avbpubkey",
3525 private_key: "testkey.pem",
3526 }
3527
Jiyong Park127b40b2019-09-30 16:04:35 +09003528 cc_library {
3529 name: "libfoo",
3530 stl: "none",
3531 shared_libs: ["libbar"],
3532 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003533 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003534 }
3535
3536 cc_library {
3537 name: "libbar",
3538 stl: "none",
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003539 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003540 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003541 apex_available: ["myapex"],
3542 }
3543
3544 cc_library {
3545 name: "libbaz",
3546 stl: "none",
3547 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003548 }`)
3549
3550 testApexError(t, "\"otherapex\" is not a valid module name", `
3551 apex {
3552 name: "myapex",
3553 key: "myapex.key",
3554 native_shared_libs: ["libfoo"],
3555 }
3556
3557 apex_key {
3558 name: "myapex.key",
3559 public_key: "testkey.avbpubkey",
3560 private_key: "testkey.pem",
3561 }
3562
3563 cc_library {
3564 name: "libfoo",
3565 stl: "none",
3566 system_shared_libs: [],
3567 apex_available: ["otherapex"],
3568 }`)
3569
3570 ctx, _ := testApex(t, `
3571 apex {
3572 name: "myapex",
3573 key: "myapex.key",
3574 native_shared_libs: ["libfoo", "libbar"],
3575 }
3576
3577 apex_key {
3578 name: "myapex.key",
3579 public_key: "testkey.avbpubkey",
3580 private_key: "testkey.pem",
3581 }
3582
3583 cc_library {
3584 name: "libfoo",
3585 stl: "none",
3586 system_shared_libs: [],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003587 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003588 apex_available: ["myapex"],
3589 }
3590
3591 cc_library {
3592 name: "libbar",
3593 stl: "none",
3594 system_shared_libs: [],
3595 apex_available: ["//apex_available:anyapex"],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003596 }
3597
3598 cc_library {
3599 name: "libbaz",
3600 stl: "none",
3601 system_shared_libs: [],
3602 stubs: {
3603 versions: ["10", "20", "30"],
3604 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003605 }`)
3606
3607 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003608 // TODO(jiyong) the checks for the platform variant are removed because we now create
3609 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3610 // the platform variants are not used from other platform modules. When that is done,
3611 // these checks will be replaced by expecting a specific error message that will be
3612 // emitted when the platform variant is used.
3613 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3614 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3615 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3616 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003617
3618 ctx, _ = testApex(t, `
3619 apex {
3620 name: "myapex",
3621 key: "myapex.key",
3622 }
3623
3624 apex_key {
3625 name: "myapex.key",
3626 public_key: "testkey.avbpubkey",
3627 private_key: "testkey.pem",
3628 }
3629
3630 cc_library {
3631 name: "libfoo",
3632 stl: "none",
3633 system_shared_libs: [],
3634 apex_available: ["//apex_available:platform"],
3635 }`)
3636
3637 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003638 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3639 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003640
3641 ctx, _ = testApex(t, `
3642 apex {
3643 name: "myapex",
3644 key: "myapex.key",
3645 native_shared_libs: ["libfoo"],
3646 }
3647
3648 apex_key {
3649 name: "myapex.key",
3650 public_key: "testkey.avbpubkey",
3651 private_key: "testkey.pem",
3652 }
3653
3654 cc_library {
3655 name: "libfoo",
3656 stl: "none",
3657 system_shared_libs: [],
3658 apex_available: ["myapex"],
3659 static: {
3660 apex_available: ["//apex_available:platform"],
3661 },
3662 }`)
3663
3664 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003665 // TODO(jiyong) the checks for the platform variant are removed because we now create
3666 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3667 // the platform variants are not used from other platform modules. When that is done,
3668 // these checks will be replaced by expecting a specific error message that will be
3669 // emitted when the platform variant is used.
3670 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3671 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3672 // // but the static variant is available to both myapex and the platform
3673 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3674 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003675}
3676
Jiyong Park5d790c32019-11-15 18:40:32 +09003677func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003678 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003679 apex {
3680 name: "myapex",
3681 key: "myapex.key",
3682 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003683 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003684 }
3685
3686 override_apex {
3687 name: "override_myapex",
3688 base: "myapex",
3689 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003690 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003691 logging_parent: "com.foo.bar",
Baligh Uddincb6aa122020-03-15 13:01:05 -07003692 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09003693 }
3694
3695 apex_key {
3696 name: "myapex.key",
3697 public_key: "testkey.avbpubkey",
3698 private_key: "testkey.pem",
3699 }
3700
3701 android_app {
3702 name: "app",
3703 srcs: ["foo/bar/MyClass.java"],
3704 package_name: "foo",
3705 sdk_version: "none",
3706 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003707 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003708 }
3709
3710 override_android_app {
3711 name: "override_app",
3712 base: "app",
3713 package_name: "bar",
3714 }
Jiyong Parka519c542020-03-03 11:45:41 +09003715 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003716
Jiyong Park317645e2019-12-05 13:20:58 +09003717 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3718 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3719 if originalVariant.GetOverriddenBy() != "" {
3720 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3721 }
3722 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3723 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3724 }
3725
Jiyong Park5d790c32019-11-15 18:40:32 +09003726 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3727 apexRule := module.Rule("apexRule")
3728 copyCmds := apexRule.Args["copy_commands"]
3729
3730 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3731 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003732
3733 apexBundle := module.Module().(*apexBundle)
3734 name := apexBundle.Name()
3735 if name != "override_myapex" {
3736 t.Errorf("name should be \"override_myapex\", but was %q", name)
3737 }
3738
Baligh Uddin004d7172020-02-19 21:29:28 -08003739 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3740 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3741 }
3742
Jiyong Parka519c542020-03-03 11:45:41 +09003743 optFlags := apexRule.Args["opt_flags"]
Baligh Uddincb6aa122020-03-15 13:01:05 -07003744 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Parka519c542020-03-03 11:45:41 +09003745
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003746 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3747 var builder strings.Builder
3748 data.Custom(&builder, name, "TARGET_", "", data)
3749 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003750 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003751 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3752 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003753 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003754 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003755 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003756 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3757 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003758}
3759
Jooyung Han214bf372019-11-12 13:03:50 +09003760func TestLegacyAndroid10Support(t *testing.T) {
3761 ctx, _ := testApex(t, `
3762 apex {
3763 name: "myapex",
3764 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003765 native_shared_libs: ["mylib"],
Jooyung Han23b0adf2020-03-12 18:37:20 +09003766 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09003767 }
3768
3769 apex_key {
3770 name: "myapex.key",
3771 public_key: "testkey.avbpubkey",
3772 private_key: "testkey.pem",
3773 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003774
3775 cc_library {
3776 name: "mylib",
3777 srcs: ["mylib.cpp"],
3778 stl: "libc++",
3779 system_shared_libs: [],
3780 apex_available: [ "myapex" ],
3781 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003782 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003783
3784 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3785 args := module.Rule("apexRule").Args
3786 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003787 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003788
3789 // The copies of the libraries in the apex should have one more dependency than
3790 // the ones outside the apex, namely the unwinder. Ideally we should check
3791 // the dependency names directly here but for some reason the names are blank in
3792 // this test.
3793 for _, lib := range []string{"libc++", "mylib"} {
3794 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3795 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3796 if len(apexImplicits) != len(nonApexImplicits)+1 {
3797 t.Errorf("%q missing unwinder dep", lib)
3798 }
3799 }
Jooyung Han214bf372019-11-12 13:03:50 +09003800}
3801
Jooyung Han58f26ab2019-12-18 15:34:32 +09003802func TestJavaSDKLibrary(t *testing.T) {
3803 ctx, _ := testApex(t, `
3804 apex {
3805 name: "myapex",
3806 key: "myapex.key",
3807 java_libs: ["foo"],
3808 }
3809
3810 apex_key {
3811 name: "myapex.key",
3812 public_key: "testkey.avbpubkey",
3813 private_key: "testkey.pem",
3814 }
3815
3816 java_sdk_library {
3817 name: "foo",
3818 srcs: ["a.java"],
3819 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003820 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003821 }
3822 `, withFiles(map[string][]byte{
3823 "api/current.txt": nil,
3824 "api/removed.txt": nil,
3825 "api/system-current.txt": nil,
3826 "api/system-removed.txt": nil,
3827 "api/test-current.txt": nil,
3828 "api/test-removed.txt": nil,
3829 }))
3830
3831 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003832 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003833 "javalib/foo.jar",
3834 "etc/permissions/foo.xml",
3835 })
3836 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003837 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3838 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003839}
3840
atrost6e126252020-01-27 17:01:16 +00003841func TestCompatConfig(t *testing.T) {
3842 ctx, _ := testApex(t, `
3843 apex {
3844 name: "myapex",
3845 key: "myapex.key",
3846 prebuilts: ["myjar-platform-compat-config"],
3847 java_libs: ["myjar"],
3848 }
3849
3850 apex_key {
3851 name: "myapex.key",
3852 public_key: "testkey.avbpubkey",
3853 private_key: "testkey.pem",
3854 }
3855
3856 platform_compat_config {
3857 name: "myjar-platform-compat-config",
3858 src: ":myjar",
3859 }
3860
3861 java_library {
3862 name: "myjar",
3863 srcs: ["foo/bar/MyClass.java"],
3864 sdk_version: "none",
3865 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003866 apex_available: [ "myapex" ],
3867 }
3868 `)
3869 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3870 "etc/compatconfig/myjar-platform-compat-config.xml",
3871 "javalib/myjar.jar",
3872 })
3873}
3874
Jiyong Park479321d2019-12-16 11:47:12 +09003875func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3876 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3877 apex {
3878 name: "myapex",
3879 key: "myapex.key",
3880 java_libs: ["myjar"],
3881 }
3882
3883 apex_key {
3884 name: "myapex.key",
3885 public_key: "testkey.avbpubkey",
3886 private_key: "testkey.pem",
3887 }
3888
3889 java_library {
3890 name: "myjar",
3891 srcs: ["foo/bar/MyClass.java"],
3892 sdk_version: "none",
3893 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003894 compile_dex: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003895 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003896 }
3897 `)
3898}
3899
Jiyong Park7afd1072019-12-30 16:56:33 +09003900func TestCarryRequiredModuleNames(t *testing.T) {
3901 ctx, config := testApex(t, `
3902 apex {
3903 name: "myapex",
3904 key: "myapex.key",
3905 native_shared_libs: ["mylib"],
3906 }
3907
3908 apex_key {
3909 name: "myapex.key",
3910 public_key: "testkey.avbpubkey",
3911 private_key: "testkey.pem",
3912 }
3913
3914 cc_library {
3915 name: "mylib",
3916 srcs: ["mylib.cpp"],
3917 system_shared_libs: [],
3918 stl: "none",
3919 required: ["a", "b"],
3920 host_required: ["c", "d"],
3921 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003922 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003923 }
3924 `)
3925
3926 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3927 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3928 name := apexBundle.BaseModuleName()
3929 prefix := "TARGET_"
3930 var builder strings.Builder
3931 data.Custom(&builder, name, prefix, "", data)
3932 androidMk := builder.String()
3933 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3934 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3935 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3936}
3937
Jiyong Park7cd10e32020-01-14 09:22:18 +09003938func TestSymlinksFromApexToSystem(t *testing.T) {
3939 bp := `
3940 apex {
3941 name: "myapex",
3942 key: "myapex.key",
3943 native_shared_libs: ["mylib"],
3944 java_libs: ["myjar"],
3945 }
3946
Jiyong Park9d677202020-02-19 16:29:35 +09003947 apex {
3948 name: "myapex.updatable",
3949 key: "myapex.key",
3950 native_shared_libs: ["mylib"],
3951 java_libs: ["myjar"],
3952 updatable: true,
3953 }
3954
Jiyong Park7cd10e32020-01-14 09:22:18 +09003955 apex_key {
3956 name: "myapex.key",
3957 public_key: "testkey.avbpubkey",
3958 private_key: "testkey.pem",
3959 }
3960
3961 cc_library {
3962 name: "mylib",
3963 srcs: ["mylib.cpp"],
3964 shared_libs: ["myotherlib"],
3965 system_shared_libs: [],
3966 stl: "none",
3967 apex_available: [
3968 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003969 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003970 "//apex_available:platform",
3971 ],
3972 }
3973
3974 cc_library {
3975 name: "myotherlib",
3976 srcs: ["mylib.cpp"],
3977 system_shared_libs: [],
3978 stl: "none",
3979 apex_available: [
3980 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003981 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003982 "//apex_available:platform",
3983 ],
3984 }
3985
3986 java_library {
3987 name: "myjar",
3988 srcs: ["foo/bar/MyClass.java"],
3989 sdk_version: "none",
3990 system_modules: "none",
3991 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003992 apex_available: [
3993 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003994 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003995 "//apex_available:platform",
3996 ],
3997 }
3998
3999 java_library {
4000 name: "myotherjar",
4001 srcs: ["foo/bar/MyClass.java"],
4002 sdk_version: "none",
4003 system_modules: "none",
4004 apex_available: [
4005 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004006 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004007 "//apex_available:platform",
4008 ],
4009 }
4010 `
4011
4012 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
4013 for _, f := range files {
4014 if f.path == file {
4015 if f.isLink {
4016 t.Errorf("%q is not a real file", file)
4017 }
4018 return
4019 }
4020 }
4021 t.Errorf("%q is not found", file)
4022 }
4023
4024 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
4025 for _, f := range files {
4026 if f.path == file {
4027 if !f.isLink {
4028 t.Errorf("%q is not a symlink", file)
4029 }
4030 return
4031 }
4032 }
4033 t.Errorf("%q is not found", file)
4034 }
4035
Jiyong Park9d677202020-02-19 16:29:35 +09004036 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4037 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004038 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004039 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004040 ensureRealfileExists(t, files, "javalib/myjar.jar")
4041 ensureRealfileExists(t, files, "lib64/mylib.so")
4042 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4043
Jiyong Park9d677202020-02-19 16:29:35 +09004044 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4045 ensureRealfileExists(t, files, "javalib/myjar.jar")
4046 ensureRealfileExists(t, files, "lib64/mylib.so")
4047 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4048
4049 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004050 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004051 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004052 ensureRealfileExists(t, files, "javalib/myjar.jar")
4053 ensureRealfileExists(t, files, "lib64/mylib.so")
4054 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004055
4056 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4057 ensureRealfileExists(t, files, "javalib/myjar.jar")
4058 ensureRealfileExists(t, files, "lib64/mylib.so")
4059 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004060}
4061
Jiyong Parkd93e1b12020-02-28 15:22:21 +09004062func TestAppBundle(t *testing.T) {
4063 ctx, _ := testApex(t, `
4064 apex {
4065 name: "myapex",
4066 key: "myapex.key",
4067 apps: ["AppFoo"],
4068 }
4069
4070 apex_key {
4071 name: "myapex.key",
4072 public_key: "testkey.avbpubkey",
4073 private_key: "testkey.pem",
4074 }
4075
4076 android_app {
4077 name: "AppFoo",
4078 srcs: ["foo/bar/MyClass.java"],
4079 sdk_version: "none",
4080 system_modules: "none",
4081 apex_available: [ "myapex" ],
4082 }
Jiyong Parkaf8998c2020-02-28 16:51:07 +09004083 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkd93e1b12020-02-28 15:22:21 +09004084
4085 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4086 content := bundleConfigRule.Args["content"]
4087
4088 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkaf8998c2020-02-28 16:51:07 +09004089 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 +09004090}
4091
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004092func TestMain(m *testing.M) {
4093 run := func() int {
4094 setUp()
4095 defer tearDown()
4096
4097 return m.Run()
4098 }
4099
4100 os.Exit(run())
4101}