blob: ba7d916399039e5f69fdc2b5630228ef361425bf [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
1109func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1110 ctx, _ := testApex(t, `
1111 apex {
1112 name: "myapex",
1113 key: "myapex.key",
1114 native_shared_libs: ["libx"],
1115 }
1116
1117 apex_key {
1118 name: "myapex.key",
1119 public_key: "testkey.avbpubkey",
1120 private_key: "testkey.pem",
1121 }
1122
1123 cc_library {
1124 name: "libx",
1125 shared_libs: ["libz"],
1126 system_shared_libs: [],
1127 stl: "none",
1128 apex_available: [ "myapex" ],
1129 }
1130
1131 cc_library {
1132 name: "libz",
1133 system_shared_libs: [],
1134 stl: "none",
1135 stubs: {
1136 versions: ["1", "2"],
1137 },
1138 }
1139 `)
1140
1141 expectLink := func(from, from_variant, to, to_variant string) {
1142 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1143 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1144 }
1145 expectNoLink := func(from, from_variant, to, to_variant string) {
1146 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1147 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1148 }
1149 expectLink("libx", "shared_myapex", "libz", "shared_2")
1150 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1151 expectNoLink("libx", "shared_myapex", "libz", "shared")
1152}
1153
1154func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1155 ctx, _ := testApex(t, `
1156 apex {
1157 name: "myapex",
1158 key: "myapex.key",
1159 native_shared_libs: ["libx"],
1160 }
1161
1162 apex_key {
1163 name: "myapex.key",
1164 public_key: "testkey.avbpubkey",
1165 private_key: "testkey.pem",
1166 }
1167
1168 cc_library {
1169 name: "libx",
1170 system_shared_libs: [],
1171 stl: "none",
1172 apex_available: [ "myapex" ],
1173 stubs: {
1174 versions: ["1", "2"],
1175 },
1176 }
1177
1178 cc_library {
1179 name: "libz",
1180 shared_libs: ["libx"],
1181 system_shared_libs: [],
1182 stl: "none",
1183 }
1184 `)
1185
1186 expectLink := func(from, from_variant, to, to_variant string) {
1187 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1188 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1189 }
1190 expectNoLink := func(from, from_variant, to, to_variant string) {
1191 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1192 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1193 }
1194 expectLink("libz", "shared", "libx", "shared_2")
1195 expectNoLink("libz", "shared", "libz", "shared_1")
1196 expectNoLink("libz", "shared", "libz", "shared")
1197}
1198
Jooyung Han74066602020-03-20 04:29:24 +09001199func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Jooyung Han0c4e0162020-02-26 22:45:42 +09001200 ctx, _ := testApex(t, `
1201 apex {
1202 name: "myapex",
1203 key: "myapex.key",
1204 native_shared_libs: ["libx"],
1205 min_sdk_version: "29",
1206 }
1207
1208 apex_key {
1209 name: "myapex.key",
1210 public_key: "testkey.avbpubkey",
1211 private_key: "testkey.pem",
1212 }
1213
1214 cc_library {
1215 name: "libx",
1216 shared_libs: ["libbar"],
1217 apex_available: [ "myapex" ],
1218 }
1219
1220 cc_library {
1221 name: "libbar",
1222 stubs: {
1223 versions: ["29", "30"],
1224 },
1225 }
Jooyung Han74066602020-03-20 04:29:24 +09001226 `, func(fs map[string][]byte, config android.Config) {
1227 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1228 })
Jooyung Han0c4e0162020-02-26 22:45:42 +09001229 expectLink := func(from, from_variant, to, to_variant string) {
1230 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1231 libFlags := ld.Args["libFlags"]
1232 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1233 }
Jooyung Han74066602020-03-20 04:29:24 +09001234 expectLink("libx", "shared_hwasan_myapex", "libbar", "shared_30")
Jooyung Han0c4e0162020-02-26 22:45:42 +09001235}
1236
Jooyung Han74066602020-03-20 04:29:24 +09001237func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Jooyung Han0c4e0162020-02-26 22:45:42 +09001238 ctx, _ := testApex(t, `
1239 apex {
1240 name: "myapex",
1241 key: "myapex.key",
1242 native_shared_libs: ["libx"],
1243 min_sdk_version: "29",
1244 }
1245
1246 apex_key {
1247 name: "myapex.key",
1248 public_key: "testkey.avbpubkey",
1249 private_key: "testkey.pem",
1250 }
1251
1252 cc_library {
1253 name: "libx",
1254 apex_available: [ "myapex" ],
1255 }
Jooyung Han74066602020-03-20 04:29:24 +09001256 `)
Jooyung Han0c4e0162020-02-26 22:45:42 +09001257
1258 // ensure apex variant of c++ is linked with static unwinder
1259 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1260 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1261 // note that platform variant is not.
1262 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1263 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Han0c4e0162020-02-26 22:45:42 +09001264}
1265
1266func TestInvalidMinSdkVersion(t *testing.T) {
Jooyung Han74066602020-03-20 04:29:24 +09001267 testApexError(t, `"libz" .*: not found a version\(<=29\)`, `
Jooyung Han0c4e0162020-02-26 22:45:42 +09001268 apex {
1269 name: "myapex",
1270 key: "myapex.key",
1271 native_shared_libs: ["libx"],
1272 min_sdk_version: "29",
1273 }
1274
1275 apex_key {
1276 name: "myapex.key",
1277 public_key: "testkey.avbpubkey",
1278 private_key: "testkey.pem",
1279 }
1280
1281 cc_library {
1282 name: "libx",
1283 shared_libs: ["libz"],
1284 system_shared_libs: [],
1285 stl: "none",
1286 apex_available: [ "myapex" ],
1287 }
1288
1289 cc_library {
1290 name: "libz",
1291 system_shared_libs: [],
1292 stl: "none",
1293 stubs: {
1294 versions: ["30"],
1295 },
1296 }
Jooyung Han74066602020-03-20 04:29:24 +09001297 `)
Jooyung Han0c4e0162020-02-26 22:45:42 +09001298
Jooyung Han74066602020-03-20 04:29:24 +09001299 testApexError(t, `"myapex" .*: min_sdk_version: should be "current" or <number>`, `
Jooyung Han0c4e0162020-02-26 22:45:42 +09001300 apex {
1301 name: "myapex",
1302 key: "myapex.key",
1303 min_sdk_version: "R",
1304 }
1305
1306 apex_key {
1307 name: "myapex.key",
1308 public_key: "testkey.avbpubkey",
1309 private_key: "testkey.pem",
1310 }
1311 `)
1312}
1313
Jiyong Park7c2ee712018-12-07 00:42:25 +09001314func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001315 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001316 apex {
1317 name: "myapex",
1318 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001319 native_shared_libs: ["mylib"],
1320 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001321 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001322 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001323 }
1324
1325 apex_key {
1326 name: "myapex.key",
1327 public_key: "testkey.avbpubkey",
1328 private_key: "testkey.pem",
1329 }
1330
1331 prebuilt_etc {
1332 name: "myetc",
1333 src: "myprebuilt",
1334 sub_dir: "foo/bar",
1335 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001336
1337 cc_library {
1338 name: "mylib",
1339 srcs: ["mylib.cpp"],
1340 relative_install_path: "foo/bar",
1341 system_shared_libs: [],
1342 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001343 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001344 }
1345
1346 cc_binary {
1347 name: "mybin",
1348 srcs: ["mylib.cpp"],
1349 relative_install_path: "foo/bar",
1350 system_shared_libs: [],
1351 static_executable: true,
1352 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001353 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001354 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001355 `)
1356
Sundong Ahnabb64432019-10-22 13:58:29 +09001357 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001358 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1359
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001360 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001361 ensureListContains(t, dirs, "etc")
1362 ensureListContains(t, dirs, "etc/foo")
1363 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001364 ensureListContains(t, dirs, "lib64")
1365 ensureListContains(t, dirs, "lib64/foo")
1366 ensureListContains(t, dirs, "lib64/foo/bar")
1367 ensureListContains(t, dirs, "lib")
1368 ensureListContains(t, dirs, "lib/foo")
1369 ensureListContains(t, dirs, "lib/foo/bar")
1370
Jiyong Parkbd13e442019-03-15 18:10:35 +09001371 ensureListContains(t, dirs, "bin")
1372 ensureListContains(t, dirs, "bin/foo")
1373 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001374}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001375
1376func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001377 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001378 apex {
1379 name: "myapex",
1380 key: "myapex.key",
1381 native_shared_libs: ["mylib"],
1382 use_vendor: true,
1383 }
1384
1385 apex_key {
1386 name: "myapex.key",
1387 public_key: "testkey.avbpubkey",
1388 private_key: "testkey.pem",
1389 }
1390
1391 cc_library {
1392 name: "mylib",
1393 srcs: ["mylib.cpp"],
1394 shared_libs: ["mylib2"],
1395 system_shared_libs: [],
1396 vendor_available: true,
1397 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001398 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001399 }
1400
1401 cc_library {
1402 name: "mylib2",
1403 srcs: ["mylib.cpp"],
1404 system_shared_libs: [],
1405 vendor_available: true,
1406 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001407 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001408 }
Jooyung Handc782442019-11-01 03:14:38 +09001409 `, func(fs map[string][]byte, config android.Config) {
1410 setUseVendorWhitelistForTest(config, []string{"myapex"})
1411 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001412
1413 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001414 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001415 for _, implicit := range i.Implicits {
1416 inputsList = append(inputsList, implicit.String())
1417 }
1418 }
1419 inputsString := strings.Join(inputsList, " ")
1420
1421 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001422 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1423 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001424
1425 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001426 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1427 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001428}
Jiyong Park16e91a02018-12-20 18:18:08 +09001429
Jooyung Handc782442019-11-01 03:14:38 +09001430func TestUseVendorRestriction(t *testing.T) {
1431 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1432 apex {
1433 name: "myapex",
1434 key: "myapex.key",
1435 use_vendor: true,
1436 }
1437 apex_key {
1438 name: "myapex.key",
1439 public_key: "testkey.avbpubkey",
1440 private_key: "testkey.pem",
1441 }
1442 `, func(fs map[string][]byte, config android.Config) {
1443 setUseVendorWhitelistForTest(config, []string{""})
1444 })
1445 // no error with whitelist
1446 testApex(t, `
1447 apex {
1448 name: "myapex",
1449 key: "myapex.key",
1450 use_vendor: true,
1451 }
1452 apex_key {
1453 name: "myapex.key",
1454 public_key: "testkey.avbpubkey",
1455 private_key: "testkey.pem",
1456 }
1457 `, func(fs map[string][]byte, config android.Config) {
1458 setUseVendorWhitelistForTest(config, []string{"myapex"})
1459 })
1460}
1461
Jooyung Han5c998b92019-06-27 11:30:33 +09001462func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1463 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1464 apex {
1465 name: "myapex",
1466 key: "myapex.key",
1467 native_shared_libs: ["mylib"],
1468 use_vendor: true,
1469 }
1470
1471 apex_key {
1472 name: "myapex.key",
1473 public_key: "testkey.avbpubkey",
1474 private_key: "testkey.pem",
1475 }
1476
1477 cc_library {
1478 name: "mylib",
1479 srcs: ["mylib.cpp"],
1480 system_shared_libs: [],
1481 stl: "none",
1482 }
1483 `)
1484}
1485
Jiyong Park16e91a02018-12-20 18:18:08 +09001486func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001487 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001488 apex {
1489 name: "myapex",
1490 key: "myapex.key",
1491 native_shared_libs: ["mylib"],
1492 }
1493
1494 apex_key {
1495 name: "myapex.key",
1496 public_key: "testkey.avbpubkey",
1497 private_key: "testkey.pem",
1498 }
1499
1500 cc_library {
1501 name: "mylib",
1502 srcs: ["mylib.cpp"],
1503 system_shared_libs: [],
1504 stl: "none",
1505 stubs: {
1506 versions: ["1", "2", "3"],
1507 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001508 apex_available: [
1509 "//apex_available:platform",
1510 "myapex",
1511 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001512 }
1513
1514 cc_binary {
1515 name: "not_in_apex",
1516 srcs: ["mylib.cpp"],
1517 static_libs: ["mylib"],
1518 static_executable: true,
1519 system_shared_libs: [],
1520 stl: "none",
1521 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001522 `)
1523
Colin Cross7113d202019-11-20 16:39:12 -08001524 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001525
1526 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001527 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001528}
Jiyong Park9335a262018-12-24 11:31:58 +09001529
1530func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001531 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001532 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001533 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001534 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001535 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001536 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001537 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001538 }
1539
1540 cc_library {
1541 name: "mylib",
1542 srcs: ["mylib.cpp"],
1543 system_shared_libs: [],
1544 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001545 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001546 }
1547
1548 apex_key {
1549 name: "myapex.key",
1550 public_key: "testkey.avbpubkey",
1551 private_key: "testkey.pem",
1552 }
1553
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001554 android_app_certificate {
1555 name: "myapex.certificate",
1556 certificate: "testkey",
1557 }
1558
1559 android_app_certificate {
1560 name: "myapex.certificate.override",
1561 certificate: "testkey.override",
1562 }
1563
Jiyong Park9335a262018-12-24 11:31:58 +09001564 `)
1565
1566 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001567 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001568
1569 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1570 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1571 "vendor/foo/devkeys/testkey.avbpubkey")
1572 }
1573 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1574 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1575 "vendor/foo/devkeys/testkey.pem")
1576 }
1577
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001578 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001579 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001580 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001581 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001582 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001583 }
1584}
Jiyong Park58e364a2019-01-19 19:24:06 +09001585
Jooyung Hanf121a652019-12-17 14:30:11 +09001586func TestCertificate(t *testing.T) {
1587 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1588 ctx, _ := testApex(t, `
1589 apex {
1590 name: "myapex",
1591 key: "myapex.key",
1592 }
1593 apex_key {
1594 name: "myapex.key",
1595 public_key: "testkey.avbpubkey",
1596 private_key: "testkey.pem",
1597 }`)
1598 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1599 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1600 if actual := rule.Args["certificates"]; actual != expected {
1601 t.Errorf("certificates should be %q, not %q", expected, actual)
1602 }
1603 })
1604 t.Run("override when unspecified", func(t *testing.T) {
1605 ctx, _ := testApex(t, `
1606 apex {
1607 name: "myapex_keytest",
1608 key: "myapex.key",
1609 file_contexts: ":myapex-file_contexts",
1610 }
1611 apex_key {
1612 name: "myapex.key",
1613 public_key: "testkey.avbpubkey",
1614 private_key: "testkey.pem",
1615 }
1616 android_app_certificate {
1617 name: "myapex.certificate.override",
1618 certificate: "testkey.override",
1619 }`)
1620 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1621 expected := "testkey.override.x509.pem testkey.override.pk8"
1622 if actual := rule.Args["certificates"]; actual != expected {
1623 t.Errorf("certificates should be %q, not %q", expected, actual)
1624 }
1625 })
1626 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1627 ctx, _ := testApex(t, `
1628 apex {
1629 name: "myapex",
1630 key: "myapex.key",
1631 certificate: ":myapex.certificate",
1632 }
1633 apex_key {
1634 name: "myapex.key",
1635 public_key: "testkey.avbpubkey",
1636 private_key: "testkey.pem",
1637 }
1638 android_app_certificate {
1639 name: "myapex.certificate",
1640 certificate: "testkey",
1641 }`)
1642 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1643 expected := "testkey.x509.pem testkey.pk8"
1644 if actual := rule.Args["certificates"]; actual != expected {
1645 t.Errorf("certificates should be %q, not %q", expected, actual)
1646 }
1647 })
1648 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1649 ctx, _ := testApex(t, `
1650 apex {
1651 name: "myapex_keytest",
1652 key: "myapex.key",
1653 file_contexts: ":myapex-file_contexts",
1654 certificate: ":myapex.certificate",
1655 }
1656 apex_key {
1657 name: "myapex.key",
1658 public_key: "testkey.avbpubkey",
1659 private_key: "testkey.pem",
1660 }
1661 android_app_certificate {
1662 name: "myapex.certificate.override",
1663 certificate: "testkey.override",
1664 }`)
1665 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1666 expected := "testkey.override.x509.pem testkey.override.pk8"
1667 if actual := rule.Args["certificates"]; actual != expected {
1668 t.Errorf("certificates should be %q, not %q", expected, actual)
1669 }
1670 })
1671 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1672 ctx, _ := testApex(t, `
1673 apex {
1674 name: "myapex",
1675 key: "myapex.key",
1676 certificate: "testkey",
1677 }
1678 apex_key {
1679 name: "myapex.key",
1680 public_key: "testkey.avbpubkey",
1681 private_key: "testkey.pem",
1682 }`)
1683 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1684 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1685 if actual := rule.Args["certificates"]; actual != expected {
1686 t.Errorf("certificates should be %q, not %q", expected, actual)
1687 }
1688 })
1689 t.Run("override when specified as <name>", func(t *testing.T) {
1690 ctx, _ := testApex(t, `
1691 apex {
1692 name: "myapex_keytest",
1693 key: "myapex.key",
1694 file_contexts: ":myapex-file_contexts",
1695 certificate: "testkey",
1696 }
1697 apex_key {
1698 name: "myapex.key",
1699 public_key: "testkey.avbpubkey",
1700 private_key: "testkey.pem",
1701 }
1702 android_app_certificate {
1703 name: "myapex.certificate.override",
1704 certificate: "testkey.override",
1705 }`)
1706 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1707 expected := "testkey.override.x509.pem testkey.override.pk8"
1708 if actual := rule.Args["certificates"]; actual != expected {
1709 t.Errorf("certificates should be %q, not %q", expected, actual)
1710 }
1711 })
1712}
1713
Jiyong Park58e364a2019-01-19 19:24:06 +09001714func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001715 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001716 apex {
1717 name: "myapex",
1718 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001719 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001720 }
1721
1722 apex {
1723 name: "otherapex",
1724 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001725 native_shared_libs: ["mylib", "mylib2"],
Jooyung Han61c41542020-03-07 03:45:53 +09001726 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09001727 }
1728
1729 apex_key {
1730 name: "myapex.key",
1731 public_key: "testkey.avbpubkey",
1732 private_key: "testkey.pem",
1733 }
1734
1735 cc_library {
1736 name: "mylib",
1737 srcs: ["mylib.cpp"],
1738 system_shared_libs: [],
1739 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001740 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001741 "myapex",
1742 "otherapex",
1743 ],
Jooyung Hanc3e92632020-03-21 23:20:55 +09001744 recovery_available: true,
Jiyong Park58e364a2019-01-19 19:24:06 +09001745 }
Jooyung Han68e511e2020-03-02 17:44:33 +09001746 cc_library {
1747 name: "mylib2",
1748 srcs: ["mylib.cpp"],
1749 system_shared_libs: [],
1750 stl: "none",
1751 apex_available: [
1752 "myapex",
1753 "otherapex",
1754 ],
1755 use_apex_name_macro: true,
1756 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001757 `)
1758
Jooyung Han68e511e2020-03-02 17:44:33 +09001759 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001760 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001761 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han74066602020-03-20 04:29:24 +09001762 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jooyung Han68e511e2020-03-02 17:44:33 +09001763
Jooyung Han61c41542020-03-07 03:45:53 +09001764 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Han68e511e2020-03-02 17:44:33 +09001765 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1766 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han61c41542020-03-07 03:45:53 +09001767 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09001768 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han68e511e2020-03-02 17:44:33 +09001769
Jooyung Han61c41542020-03-07 03:45:53 +09001770 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Han68e511e2020-03-02 17:44:33 +09001771 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1772 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han61c41542020-03-07 03:45:53 +09001773 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09001774 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001775
Jooyung Han68e511e2020-03-02 17:44:33 +09001776 // When cc_library sets use_apex_name_macro: true
1777 // apex variants define additional macro to distinguish which apex variant it is built for
1778
1779 // non-APEX variant does not have __ANDROID_APEX__ defined
1780 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1781 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1782
1783 // APEX variant has __ANDROID_APEX__ defined
1784 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001785 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001786 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1787 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001788
Jooyung Han68e511e2020-03-02 17:44:33 +09001789 // APEX variant has __ANDROID_APEX__ defined
1790 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001791 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001792 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1793 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Hanc3e92632020-03-21 23:20:55 +09001794
1795 // recovery variant does not set __ANDROID_SDK_VERSION__
1796 mylibCFlags = ctx.ModuleForTests("mylib", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1797 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1798 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001799}
Jiyong Park7e636d02019-01-28 16:16:54 +09001800
1801func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001802 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001803 apex {
1804 name: "myapex",
1805 key: "myapex.key",
1806 native_shared_libs: ["mylib"],
1807 }
1808
1809 apex_key {
1810 name: "myapex.key",
1811 public_key: "testkey.avbpubkey",
1812 private_key: "testkey.pem",
1813 }
1814
1815 cc_library_headers {
1816 name: "mylib_headers",
1817 export_include_dirs: ["my_include"],
1818 system_shared_libs: [],
1819 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001820 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001821 }
1822
1823 cc_library {
1824 name: "mylib",
1825 srcs: ["mylib.cpp"],
1826 system_shared_libs: [],
1827 stl: "none",
1828 header_libs: ["mylib_headers"],
1829 export_header_lib_headers: ["mylib_headers"],
1830 stubs: {
1831 versions: ["1", "2", "3"],
1832 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001833 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001834 }
1835
1836 cc_library {
1837 name: "otherlib",
1838 srcs: ["mylib.cpp"],
1839 system_shared_libs: [],
1840 stl: "none",
1841 shared_libs: ["mylib"],
1842 }
1843 `)
1844
Colin Cross7113d202019-11-20 16:39:12 -08001845 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001846
1847 // Ensure that the include path of the header lib is exported to 'otherlib'
1848 ensureContains(t, cFlags, "-Imy_include")
1849}
Alex Light9670d332019-01-29 18:07:33 -08001850
Jiyong Park7cd10e32020-01-14 09:22:18 +09001851type fileInApex struct {
1852 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001853 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001854 isLink bool
1855}
1856
Jooyung Hana57af4a2020-01-23 05:36:59 +00001857func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001858 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001859 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001860 copyCmds := apexRule.Args["copy_commands"]
1861 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001862 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001863 for _, cmd := range strings.Split(copyCmds, "&&") {
1864 cmd = strings.TrimSpace(cmd)
1865 if cmd == "" {
1866 continue
1867 }
1868 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001869 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001870 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001871 switch terms[0] {
1872 case "mkdir":
1873 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001874 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001875 t.Fatal("copyCmds contains invalid cp command", cmd)
1876 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001877 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001878 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001879 isLink = false
1880 case "ln":
1881 if len(terms) != 3 && len(terms) != 4 {
1882 // ln LINK TARGET or ln -s LINK TARGET
1883 t.Fatal("copyCmds contains invalid ln command", cmd)
1884 }
1885 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001886 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001887 isLink = true
1888 default:
1889 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1890 }
1891 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001892 index := strings.Index(dst, imageApexDir)
1893 if index == -1 {
1894 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1895 }
1896 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001897 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001898 }
1899 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001900 return ret
1901}
1902
Jooyung Hana57af4a2020-01-23 05:36:59 +00001903func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1904 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001905 var failed bool
1906 var surplus []string
1907 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001908 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Han8d8906c2020-02-27 13:31:56 +09001909 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001910 for _, expected := range files {
1911 if matched, _ := path.Match(expected, file.path); matched {
1912 filesMatched[expected] = true
Jooyung Han8d8906c2020-02-27 13:31:56 +09001913 mactchFound = true
1914 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001915 }
1916 }
Jooyung Han8d8906c2020-02-27 13:31:56 +09001917 if !mactchFound {
1918 surplus = append(surplus, file.path)
1919 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001920 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001921
Jooyung Han31c470b2019-10-18 16:26:59 +09001922 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001923 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001924 t.Log("surplus files", surplus)
1925 failed = true
1926 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001927
1928 if len(files) > len(filesMatched) {
1929 var missing []string
1930 for _, expected := range files {
1931 if !filesMatched[expected] {
1932 missing = append(missing, expected)
1933 }
1934 }
1935 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001936 t.Log("missing files", missing)
1937 failed = true
1938 }
1939 if failed {
1940 t.Fail()
1941 }
1942}
1943
Jooyung Han344d5432019-08-23 11:17:39 +09001944func TestVndkApexCurrent(t *testing.T) {
1945 ctx, _ := testApex(t, `
1946 apex_vndk {
1947 name: "myapex",
1948 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001949 }
1950
1951 apex_key {
1952 name: "myapex.key",
1953 public_key: "testkey.avbpubkey",
1954 private_key: "testkey.pem",
1955 }
1956
1957 cc_library {
1958 name: "libvndk",
1959 srcs: ["mylib.cpp"],
1960 vendor_available: true,
1961 vndk: {
1962 enabled: true,
1963 },
1964 system_shared_libs: [],
1965 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001966 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001967 }
1968
1969 cc_library {
1970 name: "libvndksp",
1971 srcs: ["mylib.cpp"],
1972 vendor_available: true,
1973 vndk: {
1974 enabled: true,
1975 support_system_process: true,
1976 },
1977 system_shared_libs: [],
1978 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001979 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001980 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001981 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001982
Jooyung Hana57af4a2020-01-23 05:36:59 +00001983 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001984 "lib/libvndk.so",
1985 "lib/libvndksp.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09001986 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09001987 "lib64/libvndk.so",
1988 "lib64/libvndksp.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09001989 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001990 "etc/llndk.libraries.VER.txt",
1991 "etc/vndkcore.libraries.VER.txt",
1992 "etc/vndksp.libraries.VER.txt",
1993 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001994 })
Jooyung Han344d5432019-08-23 11:17:39 +09001995}
1996
1997func TestVndkApexWithPrebuilt(t *testing.T) {
1998 ctx, _ := testApex(t, `
1999 apex_vndk {
2000 name: "myapex",
2001 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002002 }
2003
2004 apex_key {
2005 name: "myapex.key",
2006 public_key: "testkey.avbpubkey",
2007 private_key: "testkey.pem",
2008 }
2009
2010 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002011 name: "libvndk",
2012 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002013 vendor_available: true,
2014 vndk: {
2015 enabled: true,
2016 },
2017 system_shared_libs: [],
2018 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002019 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002020 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002021
2022 cc_prebuilt_library_shared {
2023 name: "libvndk.arm",
2024 srcs: ["libvndk.arm.so"],
2025 vendor_available: true,
2026 vndk: {
2027 enabled: true,
2028 },
2029 enabled: false,
2030 arch: {
2031 arm: {
2032 enabled: true,
2033 },
2034 },
2035 system_shared_libs: [],
2036 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002037 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002038 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002039 `+vndkLibrariesTxtFiles("current"),
2040 withFiles(map[string][]byte{
2041 "libvndk.so": nil,
2042 "libvndk.arm.so": nil,
2043 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002044
Jooyung Hana57af4a2020-01-23 05:36:59 +00002045 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002046 "lib/libvndk.so",
2047 "lib/libvndk.arm.so",
2048 "lib64/libvndk.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002049 "lib/libc++.so",
2050 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002051 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002052 })
Jooyung Han344d5432019-08-23 11:17:39 +09002053}
2054
Jooyung Han39edb6c2019-11-06 16:53:07 +09002055func vndkLibrariesTxtFiles(vers ...string) (result string) {
2056 for _, v := range vers {
2057 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002058 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002059 result += `
2060 vndk_libraries_txt {
2061 name: "` + txt + `.libraries.txt",
2062 }
2063 `
2064 }
2065 } else {
2066 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2067 result += `
2068 prebuilt_etc {
2069 name: "` + txt + `.libraries.` + v + `.txt",
2070 src: "dummy.txt",
2071 }
2072 `
2073 }
2074 }
2075 }
2076 return
2077}
2078
Jooyung Han344d5432019-08-23 11:17:39 +09002079func TestVndkApexVersion(t *testing.T) {
2080 ctx, _ := testApex(t, `
2081 apex_vndk {
2082 name: "myapex_v27",
2083 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002084 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002085 vndk_version: "27",
2086 }
2087
2088 apex_key {
2089 name: "myapex.key",
2090 public_key: "testkey.avbpubkey",
2091 private_key: "testkey.pem",
2092 }
2093
Jooyung Han31c470b2019-10-18 16:26:59 +09002094 vndk_prebuilt_shared {
2095 name: "libvndk27",
2096 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002097 vendor_available: true,
2098 vndk: {
2099 enabled: true,
2100 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002101 target_arch: "arm64",
2102 arch: {
2103 arm: {
2104 srcs: ["libvndk27_arm.so"],
2105 },
2106 arm64: {
2107 srcs: ["libvndk27_arm64.so"],
2108 },
2109 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002110 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002111 }
2112
2113 vndk_prebuilt_shared {
2114 name: "libvndk27",
2115 version: "27",
2116 vendor_available: true,
2117 vndk: {
2118 enabled: true,
2119 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002120 target_arch: "x86_64",
2121 arch: {
2122 x86: {
2123 srcs: ["libvndk27_x86.so"],
2124 },
2125 x86_64: {
2126 srcs: ["libvndk27_x86_64.so"],
2127 },
2128 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002129 }
2130 `+vndkLibrariesTxtFiles("27"),
2131 withFiles(map[string][]byte{
2132 "libvndk27_arm.so": nil,
2133 "libvndk27_arm64.so": nil,
2134 "libvndk27_x86.so": nil,
2135 "libvndk27_x86_64.so": nil,
2136 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002137
Jooyung Hana57af4a2020-01-23 05:36:59 +00002138 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002139 "lib/libvndk27_arm.so",
2140 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002141 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002142 })
Jooyung Han344d5432019-08-23 11:17:39 +09002143}
2144
2145func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2146 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2147 apex_vndk {
2148 name: "myapex_v27",
2149 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002150 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002151 vndk_version: "27",
2152 }
2153 apex_vndk {
2154 name: "myapex_v27_other",
2155 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002156 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002157 vndk_version: "27",
2158 }
2159
2160 apex_key {
2161 name: "myapex.key",
2162 public_key: "testkey.avbpubkey",
2163 private_key: "testkey.pem",
2164 }
2165
2166 cc_library {
2167 name: "libvndk",
2168 srcs: ["mylib.cpp"],
2169 vendor_available: true,
2170 vndk: {
2171 enabled: true,
2172 },
2173 system_shared_libs: [],
2174 stl: "none",
2175 }
2176
2177 vndk_prebuilt_shared {
2178 name: "libvndk",
2179 version: "27",
2180 vendor_available: true,
2181 vndk: {
2182 enabled: true,
2183 },
2184 srcs: ["libvndk.so"],
2185 }
2186 `, withFiles(map[string][]byte{
2187 "libvndk.so": nil,
2188 }))
2189}
2190
Jooyung Han90eee022019-10-01 20:02:42 +09002191func TestVndkApexNameRule(t *testing.T) {
2192 ctx, _ := testApex(t, `
2193 apex_vndk {
2194 name: "myapex",
2195 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002196 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002197 }
2198 apex_vndk {
2199 name: "myapex_v28",
2200 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002201 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002202 vndk_version: "28",
2203 }
2204 apex_key {
2205 name: "myapex.key",
2206 public_key: "testkey.avbpubkey",
2207 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002208 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002209
2210 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002211 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002212 actual := proptools.String(bundle.properties.Apex_name)
2213 if !reflect.DeepEqual(actual, expected) {
2214 t.Errorf("Got '%v', expected '%v'", actual, expected)
2215 }
2216 }
2217
2218 assertApexName("com.android.vndk.vVER", "myapex")
2219 assertApexName("com.android.vndk.v28", "myapex_v28")
2220}
2221
Jooyung Han344d5432019-08-23 11:17:39 +09002222func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2223 ctx, _ := testApex(t, `
2224 apex_vndk {
2225 name: "myapex",
2226 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002227 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002228 }
2229
2230 apex_key {
2231 name: "myapex.key",
2232 public_key: "testkey.avbpubkey",
2233 private_key: "testkey.pem",
2234 }
2235
2236 cc_library {
2237 name: "libvndk",
2238 srcs: ["mylib.cpp"],
2239 vendor_available: true,
2240 native_bridge_supported: true,
2241 host_supported: true,
2242 vndk: {
2243 enabled: true,
2244 },
2245 system_shared_libs: [],
2246 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002247 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002248 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002249 `+vndkLibrariesTxtFiles("current"),
2250 withTargets(map[android.OsType][]android.Target{
2251 android.Android: []android.Target{
2252 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2253 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2254 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2255 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2256 },
2257 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002258
Jooyung Hana57af4a2020-01-23 05:36:59 +00002259 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002260 "lib/libvndk.so",
2261 "lib64/libvndk.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002262 "lib/libc++.so",
2263 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002264 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002265 })
Jooyung Han344d5432019-08-23 11:17:39 +09002266}
2267
2268func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2269 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2270 apex_vndk {
2271 name: "myapex",
2272 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002273 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002274 native_bridge_supported: true,
2275 }
2276
2277 apex_key {
2278 name: "myapex.key",
2279 public_key: "testkey.avbpubkey",
2280 private_key: "testkey.pem",
2281 }
2282
2283 cc_library {
2284 name: "libvndk",
2285 srcs: ["mylib.cpp"],
2286 vendor_available: true,
2287 native_bridge_supported: true,
2288 host_supported: true,
2289 vndk: {
2290 enabled: true,
2291 },
2292 system_shared_libs: [],
2293 stl: "none",
2294 }
2295 `)
2296}
2297
Jooyung Han31c470b2019-10-18 16:26:59 +09002298func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002299 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002300 apex_vndk {
2301 name: "myapex_v27",
2302 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002303 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002304 vndk_version: "27",
2305 }
2306
2307 apex_key {
2308 name: "myapex.key",
2309 public_key: "testkey.avbpubkey",
2310 private_key: "testkey.pem",
2311 }
2312
2313 vndk_prebuilt_shared {
2314 name: "libvndk27",
2315 version: "27",
2316 target_arch: "arm",
2317 vendor_available: true,
2318 vndk: {
2319 enabled: true,
2320 },
2321 arch: {
2322 arm: {
2323 srcs: ["libvndk27.so"],
2324 }
2325 },
2326 }
2327
2328 vndk_prebuilt_shared {
2329 name: "libvndk27",
2330 version: "27",
2331 target_arch: "arm",
2332 binder32bit: true,
2333 vendor_available: true,
2334 vndk: {
2335 enabled: true,
2336 },
2337 arch: {
2338 arm: {
2339 srcs: ["libvndk27binder32.so"],
2340 }
2341 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002342 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002343 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002344 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002345 withFiles(map[string][]byte{
2346 "libvndk27.so": nil,
2347 "libvndk27binder32.so": nil,
2348 }),
2349 withBinder32bit,
2350 withTargets(map[android.OsType][]android.Target{
2351 android.Android: []android.Target{
2352 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2353 },
2354 }),
2355 )
2356
Jooyung Hana57af4a2020-01-23 05:36:59 +00002357 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002358 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002359 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002360 })
2361}
2362
Jooyung Hane1633032019-08-01 17:41:43 +09002363func TestDependenciesInApexManifest(t *testing.T) {
2364 ctx, _ := testApex(t, `
2365 apex {
2366 name: "myapex_nodep",
2367 key: "myapex.key",
2368 native_shared_libs: ["lib_nodep"],
2369 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002370 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002371 }
2372
2373 apex {
2374 name: "myapex_dep",
2375 key: "myapex.key",
2376 native_shared_libs: ["lib_dep"],
2377 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002378 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002379 }
2380
2381 apex {
2382 name: "myapex_provider",
2383 key: "myapex.key",
2384 native_shared_libs: ["libfoo"],
2385 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002386 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002387 }
2388
2389 apex {
2390 name: "myapex_selfcontained",
2391 key: "myapex.key",
2392 native_shared_libs: ["lib_dep", "libfoo"],
2393 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002394 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002395 }
2396
2397 apex_key {
2398 name: "myapex.key",
2399 public_key: "testkey.avbpubkey",
2400 private_key: "testkey.pem",
2401 }
2402
2403 cc_library {
2404 name: "lib_nodep",
2405 srcs: ["mylib.cpp"],
2406 system_shared_libs: [],
2407 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002408 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002409 }
2410
2411 cc_library {
2412 name: "lib_dep",
2413 srcs: ["mylib.cpp"],
2414 shared_libs: ["libfoo"],
2415 system_shared_libs: [],
2416 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002417 apex_available: [
2418 "myapex_dep",
2419 "myapex_provider",
2420 "myapex_selfcontained",
2421 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002422 }
2423
2424 cc_library {
2425 name: "libfoo",
2426 srcs: ["mytest.cpp"],
2427 stubs: {
2428 versions: ["1"],
2429 },
2430 system_shared_libs: [],
2431 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002432 apex_available: [
2433 "myapex_provider",
2434 "myapex_selfcontained",
2435 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002436 }
2437 `)
2438
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002439 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002440 var provideNativeLibs, requireNativeLibs []string
2441
Sundong Ahnabb64432019-10-22 13:58:29 +09002442 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002443 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2444 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002445 ensureListEmpty(t, provideNativeLibs)
2446 ensureListEmpty(t, requireNativeLibs)
2447
Sundong Ahnabb64432019-10-22 13:58:29 +09002448 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002449 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2450 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002451 ensureListEmpty(t, provideNativeLibs)
2452 ensureListContains(t, requireNativeLibs, "libfoo.so")
2453
Sundong Ahnabb64432019-10-22 13:58:29 +09002454 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002455 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2456 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002457 ensureListContains(t, provideNativeLibs, "libfoo.so")
2458 ensureListEmpty(t, requireNativeLibs)
2459
Sundong Ahnabb64432019-10-22 13:58:29 +09002460 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002461 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2462 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002463 ensureListContains(t, provideNativeLibs, "libfoo.so")
2464 ensureListEmpty(t, requireNativeLibs)
2465}
2466
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002467func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002468 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002469 apex {
2470 name: "myapex",
2471 key: "myapex.key",
2472 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002473 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002474 }
2475
2476 apex_key {
2477 name: "myapex.key",
2478 public_key: "testkey.avbpubkey",
2479 private_key: "testkey.pem",
2480 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002481
2482 cc_library {
2483 name: "mylib",
2484 srcs: ["mylib.cpp"],
2485 system_shared_libs: [],
2486 stl: "none",
2487 apex_available: [
2488 "//apex_available:platform",
2489 "myapex",
2490 ],
2491 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002492 `)
2493
Sundong Ahnabb64432019-10-22 13:58:29 +09002494 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002495 apexManifestRule := module.Rule("apexManifestRule")
2496 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2497 apexRule := module.Rule("apexRule")
2498 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002499
2500 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2501 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2502 name := apexBundle.BaseModuleName()
2503 prefix := "TARGET_"
2504 var builder strings.Builder
2505 data.Custom(&builder, name, prefix, "", data)
2506 androidMk := builder.String()
2507 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2508 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002509}
2510
Alex Light0851b882019-02-07 13:20:53 -08002511func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002512 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002513 apex {
2514 name: "myapex",
2515 key: "myapex.key",
2516 native_shared_libs: ["mylib_common"],
2517 }
2518
2519 apex_key {
2520 name: "myapex.key",
2521 public_key: "testkey.avbpubkey",
2522 private_key: "testkey.pem",
2523 }
2524
2525 cc_library {
2526 name: "mylib_common",
2527 srcs: ["mylib.cpp"],
2528 system_shared_libs: [],
2529 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002530 apex_available: [
2531 "//apex_available:platform",
2532 "myapex",
2533 ],
Alex Light0851b882019-02-07 13:20:53 -08002534 }
2535 `)
2536
Sundong Ahnabb64432019-10-22 13:58:29 +09002537 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002538 apexRule := module.Rule("apexRule")
2539 copyCmds := apexRule.Args["copy_commands"]
2540
2541 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2542 t.Log("Apex was a test apex!")
2543 t.Fail()
2544 }
2545 // Ensure that main rule creates an output
2546 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2547
2548 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002549 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002550
2551 // Ensure that both direct and indirect deps are copied into apex
2552 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2553
Colin Cross7113d202019-11-20 16:39:12 -08002554 // Ensure that the platform variant ends with _shared
2555 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002556
2557 if !android.InAnyApex("mylib_common") {
2558 t.Log("Found mylib_common not in any apex!")
2559 t.Fail()
2560 }
2561}
2562
2563func TestTestApex(t *testing.T) {
2564 if android.InAnyApex("mylib_common_test") {
2565 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!")
2566 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002567 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002568 apex_test {
2569 name: "myapex",
2570 key: "myapex.key",
2571 native_shared_libs: ["mylib_common_test"],
2572 }
2573
2574 apex_key {
2575 name: "myapex.key",
2576 public_key: "testkey.avbpubkey",
2577 private_key: "testkey.pem",
2578 }
2579
2580 cc_library {
2581 name: "mylib_common_test",
2582 srcs: ["mylib.cpp"],
2583 system_shared_libs: [],
2584 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002585 // TODO: remove //apex_available:platform
2586 apex_available: [
2587 "//apex_available:platform",
2588 "myapex",
2589 ],
Alex Light0851b882019-02-07 13:20:53 -08002590 }
2591 `)
2592
Sundong Ahnabb64432019-10-22 13:58:29 +09002593 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002594 apexRule := module.Rule("apexRule")
2595 copyCmds := apexRule.Args["copy_commands"]
2596
2597 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2598 t.Log("Apex was not a test apex!")
2599 t.Fail()
2600 }
2601 // Ensure that main rule creates an output
2602 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2603
2604 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002605 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002606
2607 // Ensure that both direct and indirect deps are copied into apex
2608 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2609
Colin Cross7113d202019-11-20 16:39:12 -08002610 // Ensure that the platform variant ends with _shared
2611 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002612}
2613
Alex Light9670d332019-01-29 18:07:33 -08002614func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002615 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002616 apex {
2617 name: "myapex",
2618 key: "myapex.key",
2619 multilib: {
2620 first: {
2621 native_shared_libs: ["mylib_common"],
2622 }
2623 },
2624 target: {
2625 android: {
2626 multilib: {
2627 first: {
2628 native_shared_libs: ["mylib"],
2629 }
2630 }
2631 },
2632 host: {
2633 multilib: {
2634 first: {
2635 native_shared_libs: ["mylib2"],
2636 }
2637 }
2638 }
2639 }
2640 }
2641
2642 apex_key {
2643 name: "myapex.key",
2644 public_key: "testkey.avbpubkey",
2645 private_key: "testkey.pem",
2646 }
2647
2648 cc_library {
2649 name: "mylib",
2650 srcs: ["mylib.cpp"],
2651 system_shared_libs: [],
2652 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002653 // TODO: remove //apex_available:platform
2654 apex_available: [
2655 "//apex_available:platform",
2656 "myapex",
2657 ],
Alex Light9670d332019-01-29 18:07:33 -08002658 }
2659
2660 cc_library {
2661 name: "mylib_common",
2662 srcs: ["mylib.cpp"],
2663 system_shared_libs: [],
2664 stl: "none",
2665 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002666 // TODO: remove //apex_available:platform
2667 apex_available: [
2668 "//apex_available:platform",
2669 "myapex",
2670 ],
Alex Light9670d332019-01-29 18:07:33 -08002671 }
2672
2673 cc_library {
2674 name: "mylib2",
2675 srcs: ["mylib.cpp"],
2676 system_shared_libs: [],
2677 stl: "none",
2678 compile_multilib: "first",
2679 }
2680 `)
2681
Sundong Ahnabb64432019-10-22 13:58:29 +09002682 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002683 copyCmds := apexRule.Args["copy_commands"]
2684
2685 // Ensure that main rule creates an output
2686 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2687
2688 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002689 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2690 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2691 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002692
2693 // Ensure that both direct and indirect deps are copied into apex
2694 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2695 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2696 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2697
Colin Cross7113d202019-11-20 16:39:12 -08002698 // Ensure that the platform variant ends with _shared
2699 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2700 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2701 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002702}
Jiyong Park04480cf2019-02-06 00:16:29 +09002703
2704func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002705 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002706 apex {
2707 name: "myapex",
2708 key: "myapex.key",
2709 binaries: ["myscript"],
2710 }
2711
2712 apex_key {
2713 name: "myapex.key",
2714 public_key: "testkey.avbpubkey",
2715 private_key: "testkey.pem",
2716 }
2717
2718 sh_binary {
2719 name: "myscript",
2720 src: "mylib.cpp",
2721 filename: "myscript.sh",
2722 sub_dir: "script",
2723 }
2724 `)
2725
Sundong Ahnabb64432019-10-22 13:58:29 +09002726 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002727 copyCmds := apexRule.Args["copy_commands"]
2728
2729 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2730}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002731
Jooyung Han91df2082019-11-20 01:49:42 +09002732func TestApexInVariousPartition(t *testing.T) {
2733 testcases := []struct {
2734 propName, parition, flattenedPartition string
2735 }{
2736 {"", "system", "system_ext"},
2737 {"product_specific: true", "product", "product"},
2738 {"soc_specific: true", "vendor", "vendor"},
2739 {"proprietary: true", "vendor", "vendor"},
2740 {"vendor: true", "vendor", "vendor"},
2741 {"system_ext_specific: true", "system_ext", "system_ext"},
2742 }
2743 for _, tc := range testcases {
2744 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2745 ctx, _ := testApex(t, `
2746 apex {
2747 name: "myapex",
2748 key: "myapex.key",
2749 `+tc.propName+`
2750 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002751
Jooyung Han91df2082019-11-20 01:49:42 +09002752 apex_key {
2753 name: "myapex.key",
2754 public_key: "testkey.avbpubkey",
2755 private_key: "testkey.pem",
2756 }
2757 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002758
Jooyung Han91df2082019-11-20 01:49:42 +09002759 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2760 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2761 actual := apex.installDir.String()
2762 if actual != expected {
2763 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2764 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002765
Jooyung Han91df2082019-11-20 01:49:42 +09002766 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2767 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2768 actual = flattened.installDir.String()
2769 if actual != expected {
2770 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2771 }
2772 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002773 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002774}
Jiyong Park67882562019-03-21 01:11:21 +09002775
Jooyung Han54aca7b2019-11-20 02:26:02 +09002776func TestFileContexts(t *testing.T) {
2777 ctx, _ := testApex(t, `
2778 apex {
2779 name: "myapex",
2780 key: "myapex.key",
2781 }
2782
2783 apex_key {
2784 name: "myapex.key",
2785 public_key: "testkey.avbpubkey",
2786 private_key: "testkey.pem",
2787 }
2788 `)
2789 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2790 apexRule := module.Rule("apexRule")
2791 actual := apexRule.Args["file_contexts"]
2792 expected := "system/sepolicy/apex/myapex-file_contexts"
2793 if actual != expected {
2794 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2795 }
2796
2797 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2798 apex {
2799 name: "myapex",
2800 key: "myapex.key",
2801 file_contexts: "my_own_file_contexts",
2802 }
2803
2804 apex_key {
2805 name: "myapex.key",
2806 public_key: "testkey.avbpubkey",
2807 private_key: "testkey.pem",
2808 }
2809 `, withFiles(map[string][]byte{
2810 "my_own_file_contexts": nil,
2811 }))
2812
2813 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2814 apex {
2815 name: "myapex",
2816 key: "myapex.key",
2817 product_specific: true,
2818 file_contexts: "product_specific_file_contexts",
2819 }
2820
2821 apex_key {
2822 name: "myapex.key",
2823 public_key: "testkey.avbpubkey",
2824 private_key: "testkey.pem",
2825 }
2826 `)
2827
2828 ctx, _ = testApex(t, `
2829 apex {
2830 name: "myapex",
2831 key: "myapex.key",
2832 product_specific: true,
2833 file_contexts: "product_specific_file_contexts",
2834 }
2835
2836 apex_key {
2837 name: "myapex.key",
2838 public_key: "testkey.avbpubkey",
2839 private_key: "testkey.pem",
2840 }
2841 `, withFiles(map[string][]byte{
2842 "product_specific_file_contexts": nil,
2843 }))
2844 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2845 apexRule = module.Rule("apexRule")
2846 actual = apexRule.Args["file_contexts"]
2847 expected = "product_specific_file_contexts"
2848 if actual != expected {
2849 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2850 }
2851
2852 ctx, _ = testApex(t, `
2853 apex {
2854 name: "myapex",
2855 key: "myapex.key",
2856 product_specific: true,
2857 file_contexts: ":my-file-contexts",
2858 }
2859
2860 apex_key {
2861 name: "myapex.key",
2862 public_key: "testkey.avbpubkey",
2863 private_key: "testkey.pem",
2864 }
2865
2866 filegroup {
2867 name: "my-file-contexts",
2868 srcs: ["product_specific_file_contexts"],
2869 }
2870 `, withFiles(map[string][]byte{
2871 "product_specific_file_contexts": nil,
2872 }))
2873 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2874 apexRule = module.Rule("apexRule")
2875 actual = apexRule.Args["file_contexts"]
2876 expected = "product_specific_file_contexts"
2877 if actual != expected {
2878 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2879 }
2880}
2881
Jiyong Park67882562019-03-21 01:11:21 +09002882func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002883 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002884 apex_key {
2885 name: "myapex.key",
2886 public_key: ":my.avbpubkey",
2887 private_key: ":my.pem",
2888 product_specific: true,
2889 }
2890
2891 filegroup {
2892 name: "my.avbpubkey",
2893 srcs: ["testkey2.avbpubkey"],
2894 }
2895
2896 filegroup {
2897 name: "my.pem",
2898 srcs: ["testkey2.pem"],
2899 }
2900 `)
2901
2902 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2903 expected_pubkey := "testkey2.avbpubkey"
2904 actual_pubkey := apex_key.public_key_file.String()
2905 if actual_pubkey != expected_pubkey {
2906 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2907 }
2908 expected_privkey := "testkey2.pem"
2909 actual_privkey := apex_key.private_key_file.String()
2910 if actual_privkey != expected_privkey {
2911 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2912 }
2913}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002914
2915func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002916 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002917 prebuilt_apex {
2918 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002919 arch: {
2920 arm64: {
2921 src: "myapex-arm64.apex",
2922 },
2923 arm: {
2924 src: "myapex-arm.apex",
2925 },
2926 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002927 }
2928 `)
2929
2930 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2931
Jiyong Parkc95714e2019-03-29 14:23:10 +09002932 expectedInput := "myapex-arm64.apex"
2933 if prebuilt.inputApex.String() != expectedInput {
2934 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2935 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002936}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002937
2938func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002939 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002940 prebuilt_apex {
2941 name: "myapex",
2942 src: "myapex-arm.apex",
2943 filename: "notmyapex.apex",
2944 }
2945 `)
2946
2947 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2948
2949 expected := "notmyapex.apex"
2950 if p.installFilename != expected {
2951 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2952 }
2953}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002954
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002955func TestPrebuiltOverrides(t *testing.T) {
2956 ctx, config := testApex(t, `
2957 prebuilt_apex {
2958 name: "myapex.prebuilt",
2959 src: "myapex-arm.apex",
2960 overrides: [
2961 "myapex",
2962 ],
2963 }
2964 `)
2965
2966 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2967
2968 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002969 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002970 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002971 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002972 }
2973}
2974
Roland Levillain630846d2019-06-26 12:48:34 +01002975func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002976 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002977 apex_test {
2978 name: "myapex",
2979 key: "myapex.key",
2980 tests: [
2981 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002982 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002983 ],
2984 }
2985
2986 apex_key {
2987 name: "myapex.key",
2988 public_key: "testkey.avbpubkey",
2989 private_key: "testkey.pem",
2990 }
2991
2992 cc_test {
2993 name: "mytest",
2994 gtest: false,
2995 srcs: ["mytest.cpp"],
2996 relative_install_path: "test",
2997 system_shared_libs: [],
2998 static_executable: true,
2999 stl: "none",
3000 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003001
3002 cc_test {
3003 name: "mytests",
3004 gtest: false,
3005 srcs: [
3006 "mytest1.cpp",
3007 "mytest2.cpp",
3008 "mytest3.cpp",
3009 ],
3010 test_per_src: true,
3011 relative_install_path: "test",
3012 system_shared_libs: [],
3013 static_executable: true,
3014 stl: "none",
3015 }
Roland Levillain630846d2019-06-26 12:48:34 +01003016 `)
3017
Sundong Ahnabb64432019-10-22 13:58:29 +09003018 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003019 copyCmds := apexRule.Args["copy_commands"]
3020
3021 // Ensure that test dep is copied into apex.
3022 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003023
3024 // Ensure that test deps built with `test_per_src` are copied into apex.
3025 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3026 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3027 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003028
3029 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003030 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003031 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3032 name := apexBundle.BaseModuleName()
3033 prefix := "TARGET_"
3034 var builder strings.Builder
3035 data.Custom(&builder, name, prefix, "", data)
3036 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003037 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3038 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3039 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3040 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003041 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003042 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003043 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003044}
3045
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003046func TestInstallExtraFlattenedApexes(t *testing.T) {
3047 ctx, config := testApex(t, `
3048 apex {
3049 name: "myapex",
3050 key: "myapex.key",
3051 }
3052 apex_key {
3053 name: "myapex.key",
3054 public_key: "testkey.avbpubkey",
3055 private_key: "testkey.pem",
3056 }
3057 `, func(fs map[string][]byte, config android.Config) {
3058 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3059 })
3060 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003061 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003062 mk := android.AndroidMkDataForTest(t, config, "", ab)
3063 var builder strings.Builder
3064 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3065 androidMk := builder.String()
3066 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3067}
3068
Jooyung Han5c998b92019-06-27 11:30:33 +09003069func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003070 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003071 apex {
3072 name: "myapex",
3073 key: "myapex.key",
3074 native_shared_libs: ["mylib"],
3075 uses: ["commonapex"],
3076 }
3077
3078 apex {
3079 name: "commonapex",
3080 key: "myapex.key",
3081 native_shared_libs: ["libcommon"],
3082 provide_cpp_shared_libs: true,
3083 }
3084
3085 apex_key {
3086 name: "myapex.key",
3087 public_key: "testkey.avbpubkey",
3088 private_key: "testkey.pem",
3089 }
3090
3091 cc_library {
3092 name: "mylib",
3093 srcs: ["mylib.cpp"],
3094 shared_libs: ["libcommon"],
3095 system_shared_libs: [],
3096 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003097 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003098 }
3099
3100 cc_library {
3101 name: "libcommon",
3102 srcs: ["mylib_common.cpp"],
3103 system_shared_libs: [],
3104 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003105 // TODO: remove //apex_available:platform
3106 apex_available: [
3107 "//apex_available:platform",
3108 "commonapex",
3109 "myapex",
3110 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003111 }
3112 `)
3113
Sundong Ahnabb64432019-10-22 13:58:29 +09003114 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003115 apexRule1 := module1.Rule("apexRule")
3116 copyCmds1 := apexRule1.Args["copy_commands"]
3117
Sundong Ahnabb64432019-10-22 13:58:29 +09003118 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003119 apexRule2 := module2.Rule("apexRule")
3120 copyCmds2 := apexRule2.Args["copy_commands"]
3121
Colin Cross7113d202019-11-20 16:39:12 -08003122 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3123 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003124 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3125 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3126 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3127}
3128
3129func TestApexUsesFailsIfNotProvided(t *testing.T) {
3130 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3131 apex {
3132 name: "myapex",
3133 key: "myapex.key",
3134 uses: ["commonapex"],
3135 }
3136
3137 apex {
3138 name: "commonapex",
3139 key: "myapex.key",
3140 }
3141
3142 apex_key {
3143 name: "myapex.key",
3144 public_key: "testkey.avbpubkey",
3145 private_key: "testkey.pem",
3146 }
3147 `)
3148 testApexError(t, `uses: "commonapex" is not a provider`, `
3149 apex {
3150 name: "myapex",
3151 key: "myapex.key",
3152 uses: ["commonapex"],
3153 }
3154
3155 cc_library {
3156 name: "commonapex",
3157 system_shared_libs: [],
3158 stl: "none",
3159 }
3160
3161 apex_key {
3162 name: "myapex.key",
3163 public_key: "testkey.avbpubkey",
3164 private_key: "testkey.pem",
3165 }
3166 `)
3167}
3168
3169func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3170 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3171 apex {
3172 name: "myapex",
3173 key: "myapex.key",
3174 use_vendor: true,
3175 uses: ["commonapex"],
3176 }
3177
3178 apex {
3179 name: "commonapex",
3180 key: "myapex.key",
3181 provide_cpp_shared_libs: true,
3182 }
3183
3184 apex_key {
3185 name: "myapex.key",
3186 public_key: "testkey.avbpubkey",
3187 private_key: "testkey.pem",
3188 }
Jooyung Handc782442019-11-01 03:14:38 +09003189 `, func(fs map[string][]byte, config android.Config) {
3190 setUseVendorWhitelistForTest(config, []string{"myapex"})
3191 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003192}
3193
Jooyung Hand48f3c32019-08-23 11:18:57 +09003194func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3195 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3196 apex {
3197 name: "myapex",
3198 key: "myapex.key",
3199 native_shared_libs: ["libfoo"],
3200 }
3201
3202 apex_key {
3203 name: "myapex.key",
3204 public_key: "testkey.avbpubkey",
3205 private_key: "testkey.pem",
3206 }
3207
3208 cc_library {
3209 name: "libfoo",
3210 stl: "none",
3211 system_shared_libs: [],
3212 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003213 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003214 }
3215 `)
3216 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3217 apex {
3218 name: "myapex",
3219 key: "myapex.key",
3220 java_libs: ["myjar"],
3221 }
3222
3223 apex_key {
3224 name: "myapex.key",
3225 public_key: "testkey.avbpubkey",
3226 private_key: "testkey.pem",
3227 }
3228
3229 java_library {
3230 name: "myjar",
3231 srcs: ["foo/bar/MyClass.java"],
3232 sdk_version: "none",
3233 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003234 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003235 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003236 }
3237 `)
3238}
3239
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003240func TestApexWithApps(t *testing.T) {
3241 ctx, _ := testApex(t, `
3242 apex {
3243 name: "myapex",
3244 key: "myapex.key",
3245 apps: [
3246 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003247 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003248 ],
3249 }
3250
3251 apex_key {
3252 name: "myapex.key",
3253 public_key: "testkey.avbpubkey",
3254 private_key: "testkey.pem",
3255 }
3256
3257 android_app {
3258 name: "AppFoo",
3259 srcs: ["foo/bar/MyClass.java"],
3260 sdk_version: "none",
3261 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003262 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003263 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003264 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003265
3266 android_app {
3267 name: "AppFooPriv",
3268 srcs: ["foo/bar/MyClass.java"],
3269 sdk_version: "none",
3270 system_modules: "none",
3271 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003272 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003273 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003274
3275 cc_library_shared {
3276 name: "libjni",
3277 srcs: ["mylib.cpp"],
Jooyung Han65041792020-02-25 16:59:29 +09003278 shared_libs: ["libfoo"],
Jiyong Park8be103b2019-11-08 15:53:48 +09003279 stl: "none",
3280 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003281 apex_available: [ "myapex" ],
Jooyung Han65041792020-02-25 16:59:29 +09003282 sdk_version: "current",
3283 }
3284
3285 cc_library_shared {
3286 name: "libfoo",
3287 stl: "none",
3288 system_shared_libs: [],
3289 apex_available: [ "myapex" ],
3290 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003291 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003292 `)
3293
Sundong Ahnabb64432019-10-22 13:58:29 +09003294 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003295 apexRule := module.Rule("apexRule")
3296 copyCmds := apexRule.Args["copy_commands"]
3297
3298 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003299 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003300
Jooyung Han65041792020-02-25 16:59:29 +09003301 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3302 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003303 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Han65041792020-02-25 16:59:29 +09003304 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003305 }
Jooyung Han65041792020-02-25 16:59:29 +09003306 // JNI libraries including transitive deps are
3307 for _, jni := range []string{"libjni", "libfoo"} {
3308 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
3309 // ... embedded inside APK (jnilibs.zip)
3310 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3311 // ... and not directly inside the APEX
3312 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3313 }
Dario Frenicde2a032019-10-27 00:29:22 +01003314}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003315
Dario Frenicde2a032019-10-27 00:29:22 +01003316func TestApexWithAppImports(t *testing.T) {
3317 ctx, _ := testApex(t, `
3318 apex {
3319 name: "myapex",
3320 key: "myapex.key",
3321 apps: [
3322 "AppFooPrebuilt",
3323 "AppFooPrivPrebuilt",
3324 ],
3325 }
3326
3327 apex_key {
3328 name: "myapex.key",
3329 public_key: "testkey.avbpubkey",
3330 private_key: "testkey.pem",
3331 }
3332
3333 android_app_import {
3334 name: "AppFooPrebuilt",
3335 apk: "PrebuiltAppFoo.apk",
3336 presigned: true,
3337 dex_preopt: {
3338 enabled: false,
3339 },
3340 }
3341
3342 android_app_import {
3343 name: "AppFooPrivPrebuilt",
3344 apk: "PrebuiltAppFooPriv.apk",
3345 privileged: true,
3346 presigned: true,
3347 dex_preopt: {
3348 enabled: false,
3349 },
3350 }
3351 `)
3352
Sundong Ahnabb64432019-10-22 13:58:29 +09003353 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003354 apexRule := module.Rule("apexRule")
3355 copyCmds := apexRule.Args["copy_commands"]
3356
3357 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3358 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003359}
3360
Dario Freni6f3937c2019-12-20 22:58:03 +00003361func TestApexWithTestHelperApp(t *testing.T) {
3362 ctx, _ := testApex(t, `
3363 apex {
3364 name: "myapex",
3365 key: "myapex.key",
3366 apps: [
3367 "TesterHelpAppFoo",
3368 ],
3369 }
3370
3371 apex_key {
3372 name: "myapex.key",
3373 public_key: "testkey.avbpubkey",
3374 private_key: "testkey.pem",
3375 }
3376
3377 android_test_helper_app {
3378 name: "TesterHelpAppFoo",
3379 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003380 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003381 }
3382
3383 `)
3384
3385 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3386 apexRule := module.Rule("apexRule")
3387 copyCmds := apexRule.Args["copy_commands"]
3388
3389 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3390}
3391
Jooyung Han18020ea2019-11-13 10:50:48 +09003392func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3393 // libfoo's apex_available comes from cc_defaults
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003394 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003395 apex {
3396 name: "myapex",
3397 key: "myapex.key",
3398 native_shared_libs: ["libfoo"],
3399 }
3400
3401 apex_key {
3402 name: "myapex.key",
3403 public_key: "testkey.avbpubkey",
3404 private_key: "testkey.pem",
3405 }
3406
3407 apex {
3408 name: "otherapex",
3409 key: "myapex.key",
3410 native_shared_libs: ["libfoo"],
3411 }
3412
3413 cc_defaults {
3414 name: "libfoo-defaults",
3415 apex_available: ["otherapex"],
3416 }
3417
3418 cc_library {
3419 name: "libfoo",
3420 defaults: ["libfoo-defaults"],
3421 stl: "none",
3422 system_shared_libs: [],
3423 }`)
3424}
3425
Jiyong Park127b40b2019-09-30 16:04:35 +09003426func TestApexAvailable(t *testing.T) {
3427 // libfoo is not available to myapex, but only to otherapex
3428 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3429 apex {
3430 name: "myapex",
3431 key: "myapex.key",
3432 native_shared_libs: ["libfoo"],
3433 }
3434
3435 apex_key {
3436 name: "myapex.key",
3437 public_key: "testkey.avbpubkey",
3438 private_key: "testkey.pem",
3439 }
3440
3441 apex {
3442 name: "otherapex",
3443 key: "otherapex.key",
3444 native_shared_libs: ["libfoo"],
3445 }
3446
3447 apex_key {
3448 name: "otherapex.key",
3449 public_key: "testkey.avbpubkey",
3450 private_key: "testkey.pem",
3451 }
3452
3453 cc_library {
3454 name: "libfoo",
3455 stl: "none",
3456 system_shared_libs: [],
3457 apex_available: ["otherapex"],
3458 }`)
3459
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003460 // libbbaz is an indirect dep
3461 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003462 apex {
3463 name: "myapex",
3464 key: "myapex.key",
3465 native_shared_libs: ["libfoo"],
3466 }
3467
3468 apex_key {
3469 name: "myapex.key",
3470 public_key: "testkey.avbpubkey",
3471 private_key: "testkey.pem",
3472 }
3473
Jiyong Park127b40b2019-09-30 16:04:35 +09003474 cc_library {
3475 name: "libfoo",
3476 stl: "none",
3477 shared_libs: ["libbar"],
3478 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003479 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003480 }
3481
3482 cc_library {
3483 name: "libbar",
3484 stl: "none",
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003485 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003486 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003487 apex_available: ["myapex"],
3488 }
3489
3490 cc_library {
3491 name: "libbaz",
3492 stl: "none",
3493 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003494 }`)
3495
3496 testApexError(t, "\"otherapex\" is not a valid module name", `
3497 apex {
3498 name: "myapex",
3499 key: "myapex.key",
3500 native_shared_libs: ["libfoo"],
3501 }
3502
3503 apex_key {
3504 name: "myapex.key",
3505 public_key: "testkey.avbpubkey",
3506 private_key: "testkey.pem",
3507 }
3508
3509 cc_library {
3510 name: "libfoo",
3511 stl: "none",
3512 system_shared_libs: [],
3513 apex_available: ["otherapex"],
3514 }`)
3515
3516 ctx, _ := testApex(t, `
3517 apex {
3518 name: "myapex",
3519 key: "myapex.key",
3520 native_shared_libs: ["libfoo", "libbar"],
3521 }
3522
3523 apex_key {
3524 name: "myapex.key",
3525 public_key: "testkey.avbpubkey",
3526 private_key: "testkey.pem",
3527 }
3528
3529 cc_library {
3530 name: "libfoo",
3531 stl: "none",
3532 system_shared_libs: [],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003533 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003534 apex_available: ["myapex"],
3535 }
3536
3537 cc_library {
3538 name: "libbar",
3539 stl: "none",
3540 system_shared_libs: [],
3541 apex_available: ["//apex_available:anyapex"],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003542 }
3543
3544 cc_library {
3545 name: "libbaz",
3546 stl: "none",
3547 system_shared_libs: [],
3548 stubs: {
3549 versions: ["10", "20", "30"],
3550 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003551 }`)
3552
3553 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003554 // TODO(jiyong) the checks for the platform variant are removed because we now create
3555 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3556 // the platform variants are not used from other platform modules. When that is done,
3557 // these checks will be replaced by expecting a specific error message that will be
3558 // emitted when the platform variant is used.
3559 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3560 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3561 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3562 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003563
3564 ctx, _ = testApex(t, `
3565 apex {
3566 name: "myapex",
3567 key: "myapex.key",
3568 }
3569
3570 apex_key {
3571 name: "myapex.key",
3572 public_key: "testkey.avbpubkey",
3573 private_key: "testkey.pem",
3574 }
3575
3576 cc_library {
3577 name: "libfoo",
3578 stl: "none",
3579 system_shared_libs: [],
3580 apex_available: ["//apex_available:platform"],
3581 }`)
3582
3583 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003584 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3585 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003586
3587 ctx, _ = testApex(t, `
3588 apex {
3589 name: "myapex",
3590 key: "myapex.key",
3591 native_shared_libs: ["libfoo"],
3592 }
3593
3594 apex_key {
3595 name: "myapex.key",
3596 public_key: "testkey.avbpubkey",
3597 private_key: "testkey.pem",
3598 }
3599
3600 cc_library {
3601 name: "libfoo",
3602 stl: "none",
3603 system_shared_libs: [],
3604 apex_available: ["myapex"],
3605 static: {
3606 apex_available: ["//apex_available:platform"],
3607 },
3608 }`)
3609
3610 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003611 // TODO(jiyong) the checks for the platform variant are removed because we now create
3612 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3613 // the platform variants are not used from other platform modules. When that is done,
3614 // these checks will be replaced by expecting a specific error message that will be
3615 // emitted when the platform variant is used.
3616 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3617 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3618 // // but the static variant is available to both myapex and the platform
3619 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3620 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003621}
3622
Jiyong Park5d790c32019-11-15 18:40:32 +09003623func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003624 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003625 apex {
3626 name: "myapex",
3627 key: "myapex.key",
3628 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003629 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003630 }
3631
3632 override_apex {
3633 name: "override_myapex",
3634 base: "myapex",
3635 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003636 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003637 logging_parent: "com.foo.bar",
Baligh Uddincb6aa122020-03-15 13:01:05 -07003638 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09003639 }
3640
3641 apex_key {
3642 name: "myapex.key",
3643 public_key: "testkey.avbpubkey",
3644 private_key: "testkey.pem",
3645 }
3646
3647 android_app {
3648 name: "app",
3649 srcs: ["foo/bar/MyClass.java"],
3650 package_name: "foo",
3651 sdk_version: "none",
3652 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003653 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003654 }
3655
3656 override_android_app {
3657 name: "override_app",
3658 base: "app",
3659 package_name: "bar",
3660 }
Jiyong Parka519c542020-03-03 11:45:41 +09003661 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003662
Jiyong Park317645e2019-12-05 13:20:58 +09003663 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3664 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3665 if originalVariant.GetOverriddenBy() != "" {
3666 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3667 }
3668 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3669 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3670 }
3671
Jiyong Park5d790c32019-11-15 18:40:32 +09003672 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3673 apexRule := module.Rule("apexRule")
3674 copyCmds := apexRule.Args["copy_commands"]
3675
3676 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3677 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003678
3679 apexBundle := module.Module().(*apexBundle)
3680 name := apexBundle.Name()
3681 if name != "override_myapex" {
3682 t.Errorf("name should be \"override_myapex\", but was %q", name)
3683 }
3684
Baligh Uddin004d7172020-02-19 21:29:28 -08003685 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3686 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3687 }
3688
Jiyong Parka519c542020-03-03 11:45:41 +09003689 optFlags := apexRule.Args["opt_flags"]
Baligh Uddincb6aa122020-03-15 13:01:05 -07003690 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Parka519c542020-03-03 11:45:41 +09003691
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003692 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3693 var builder strings.Builder
3694 data.Custom(&builder, name, "TARGET_", "", data)
3695 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003696 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003697 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3698 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003699 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003700 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003701 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003702 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3703 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003704}
3705
Jooyung Han214bf372019-11-12 13:03:50 +09003706func TestLegacyAndroid10Support(t *testing.T) {
3707 ctx, _ := testApex(t, `
3708 apex {
3709 name: "myapex",
3710 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003711 native_shared_libs: ["mylib"],
Jooyung Han23b0adf2020-03-12 18:37:20 +09003712 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09003713 }
3714
3715 apex_key {
3716 name: "myapex.key",
3717 public_key: "testkey.avbpubkey",
3718 private_key: "testkey.pem",
3719 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003720
3721 cc_library {
3722 name: "mylib",
3723 srcs: ["mylib.cpp"],
3724 stl: "libc++",
3725 system_shared_libs: [],
3726 apex_available: [ "myapex" ],
3727 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003728 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003729
3730 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3731 args := module.Rule("apexRule").Args
3732 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003733 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003734
3735 // The copies of the libraries in the apex should have one more dependency than
3736 // the ones outside the apex, namely the unwinder. Ideally we should check
3737 // the dependency names directly here but for some reason the names are blank in
3738 // this test.
3739 for _, lib := range []string{"libc++", "mylib"} {
3740 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3741 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3742 if len(apexImplicits) != len(nonApexImplicits)+1 {
3743 t.Errorf("%q missing unwinder dep", lib)
3744 }
3745 }
Jooyung Han214bf372019-11-12 13:03:50 +09003746}
3747
Jooyung Han58f26ab2019-12-18 15:34:32 +09003748func TestJavaSDKLibrary(t *testing.T) {
3749 ctx, _ := testApex(t, `
3750 apex {
3751 name: "myapex",
3752 key: "myapex.key",
3753 java_libs: ["foo"],
3754 }
3755
3756 apex_key {
3757 name: "myapex.key",
3758 public_key: "testkey.avbpubkey",
3759 private_key: "testkey.pem",
3760 }
3761
3762 java_sdk_library {
3763 name: "foo",
3764 srcs: ["a.java"],
3765 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003766 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003767 }
3768 `, withFiles(map[string][]byte{
3769 "api/current.txt": nil,
3770 "api/removed.txt": nil,
3771 "api/system-current.txt": nil,
3772 "api/system-removed.txt": nil,
3773 "api/test-current.txt": nil,
3774 "api/test-removed.txt": nil,
3775 }))
3776
3777 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003778 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003779 "javalib/foo.jar",
3780 "etc/permissions/foo.xml",
3781 })
3782 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003783 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3784 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003785}
3786
atrost6e126252020-01-27 17:01:16 +00003787func TestCompatConfig(t *testing.T) {
3788 ctx, _ := testApex(t, `
3789 apex {
3790 name: "myapex",
3791 key: "myapex.key",
3792 prebuilts: ["myjar-platform-compat-config"],
3793 java_libs: ["myjar"],
3794 }
3795
3796 apex_key {
3797 name: "myapex.key",
3798 public_key: "testkey.avbpubkey",
3799 private_key: "testkey.pem",
3800 }
3801
3802 platform_compat_config {
3803 name: "myjar-platform-compat-config",
3804 src: ":myjar",
3805 }
3806
3807 java_library {
3808 name: "myjar",
3809 srcs: ["foo/bar/MyClass.java"],
3810 sdk_version: "none",
3811 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003812 apex_available: [ "myapex" ],
3813 }
3814 `)
3815 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3816 "etc/compatconfig/myjar-platform-compat-config.xml",
3817 "javalib/myjar.jar",
3818 })
3819}
3820
Jiyong Park479321d2019-12-16 11:47:12 +09003821func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3822 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3823 apex {
3824 name: "myapex",
3825 key: "myapex.key",
3826 java_libs: ["myjar"],
3827 }
3828
3829 apex_key {
3830 name: "myapex.key",
3831 public_key: "testkey.avbpubkey",
3832 private_key: "testkey.pem",
3833 }
3834
3835 java_library {
3836 name: "myjar",
3837 srcs: ["foo/bar/MyClass.java"],
3838 sdk_version: "none",
3839 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003840 compile_dex: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003841 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003842 }
3843 `)
3844}
3845
Jiyong Park7afd1072019-12-30 16:56:33 +09003846func TestCarryRequiredModuleNames(t *testing.T) {
3847 ctx, config := testApex(t, `
3848 apex {
3849 name: "myapex",
3850 key: "myapex.key",
3851 native_shared_libs: ["mylib"],
3852 }
3853
3854 apex_key {
3855 name: "myapex.key",
3856 public_key: "testkey.avbpubkey",
3857 private_key: "testkey.pem",
3858 }
3859
3860 cc_library {
3861 name: "mylib",
3862 srcs: ["mylib.cpp"],
3863 system_shared_libs: [],
3864 stl: "none",
3865 required: ["a", "b"],
3866 host_required: ["c", "d"],
3867 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003868 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003869 }
3870 `)
3871
3872 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3873 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3874 name := apexBundle.BaseModuleName()
3875 prefix := "TARGET_"
3876 var builder strings.Builder
3877 data.Custom(&builder, name, prefix, "", data)
3878 androidMk := builder.String()
3879 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3880 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3881 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3882}
3883
Jiyong Park7cd10e32020-01-14 09:22:18 +09003884func TestSymlinksFromApexToSystem(t *testing.T) {
3885 bp := `
3886 apex {
3887 name: "myapex",
3888 key: "myapex.key",
3889 native_shared_libs: ["mylib"],
3890 java_libs: ["myjar"],
3891 }
3892
Jiyong Park9d677202020-02-19 16:29:35 +09003893 apex {
3894 name: "myapex.updatable",
3895 key: "myapex.key",
3896 native_shared_libs: ["mylib"],
3897 java_libs: ["myjar"],
3898 updatable: true,
3899 }
3900
Jiyong Park7cd10e32020-01-14 09:22:18 +09003901 apex_key {
3902 name: "myapex.key",
3903 public_key: "testkey.avbpubkey",
3904 private_key: "testkey.pem",
3905 }
3906
3907 cc_library {
3908 name: "mylib",
3909 srcs: ["mylib.cpp"],
3910 shared_libs: ["myotherlib"],
3911 system_shared_libs: [],
3912 stl: "none",
3913 apex_available: [
3914 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003915 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003916 "//apex_available:platform",
3917 ],
3918 }
3919
3920 cc_library {
3921 name: "myotherlib",
3922 srcs: ["mylib.cpp"],
3923 system_shared_libs: [],
3924 stl: "none",
3925 apex_available: [
3926 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003927 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003928 "//apex_available:platform",
3929 ],
3930 }
3931
3932 java_library {
3933 name: "myjar",
3934 srcs: ["foo/bar/MyClass.java"],
3935 sdk_version: "none",
3936 system_modules: "none",
3937 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003938 apex_available: [
3939 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003940 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003941 "//apex_available:platform",
3942 ],
3943 }
3944
3945 java_library {
3946 name: "myotherjar",
3947 srcs: ["foo/bar/MyClass.java"],
3948 sdk_version: "none",
3949 system_modules: "none",
3950 apex_available: [
3951 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003952 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003953 "//apex_available:platform",
3954 ],
3955 }
3956 `
3957
3958 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3959 for _, f := range files {
3960 if f.path == file {
3961 if f.isLink {
3962 t.Errorf("%q is not a real file", file)
3963 }
3964 return
3965 }
3966 }
3967 t.Errorf("%q is not found", file)
3968 }
3969
3970 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3971 for _, f := range files {
3972 if f.path == file {
3973 if !f.isLink {
3974 t.Errorf("%q is not a symlink", file)
3975 }
3976 return
3977 }
3978 }
3979 t.Errorf("%q is not found", file)
3980 }
3981
Jiyong Park9d677202020-02-19 16:29:35 +09003982 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3983 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003984 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003985 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003986 ensureRealfileExists(t, files, "javalib/myjar.jar")
3987 ensureRealfileExists(t, files, "lib64/mylib.so")
3988 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3989
Jiyong Park9d677202020-02-19 16:29:35 +09003990 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3991 ensureRealfileExists(t, files, "javalib/myjar.jar")
3992 ensureRealfileExists(t, files, "lib64/mylib.so")
3993 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3994
3995 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003996 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003997 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003998 ensureRealfileExists(t, files, "javalib/myjar.jar")
3999 ensureRealfileExists(t, files, "lib64/mylib.so")
4000 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004001
4002 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4003 ensureRealfileExists(t, files, "javalib/myjar.jar")
4004 ensureRealfileExists(t, files, "lib64/mylib.so")
4005 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004006}
4007
Jiyong Parkd93e1b12020-02-28 15:22:21 +09004008func TestAppBundle(t *testing.T) {
4009 ctx, _ := testApex(t, `
4010 apex {
4011 name: "myapex",
4012 key: "myapex.key",
4013 apps: ["AppFoo"],
4014 }
4015
4016 apex_key {
4017 name: "myapex.key",
4018 public_key: "testkey.avbpubkey",
4019 private_key: "testkey.pem",
4020 }
4021
4022 android_app {
4023 name: "AppFoo",
4024 srcs: ["foo/bar/MyClass.java"],
4025 sdk_version: "none",
4026 system_modules: "none",
4027 apex_available: [ "myapex" ],
4028 }
Jiyong Parkaf8998c2020-02-28 16:51:07 +09004029 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkd93e1b12020-02-28 15:22:21 +09004030
4031 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4032 content := bundleConfigRule.Args["content"]
4033
4034 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkaf8998c2020-02-28 16:51:07 +09004035 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 +09004036}
4037
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004038func TestMain(m *testing.M) {
4039 run := func() int {
4040 setUp()
4041 defer tearDown()
4042
4043 return m.Run()
4044 }
4045
4046 os.Exit(run())
4047}