blob: 704bad6905a51e0d59dd9c49dafc5e37c1c3dfc2 [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 {
859 name: "should link to test latest",
860 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
1199func TestQApexesUseLatestStubsInBundledBuilds(t *testing.T) {
1200 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 }
1226 `)
1227 expectLink := func(from, from_variant, to, to_variant string) {
1228 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1229 libFlags := ld.Args["libFlags"]
1230 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1231 }
1232 expectLink("libx", "shared_myapex", "libbar", "shared_30")
1233}
1234
1235func TestQTargetApexUseStaticUnwinder(t *testing.T) {
1236 ctx, _ := testApex(t, `
1237 apex {
1238 name: "myapex",
1239 key: "myapex.key",
1240 native_shared_libs: ["libx"],
1241 min_sdk_version: "29",
1242 }
1243
1244 apex_key {
1245 name: "myapex.key",
1246 public_key: "testkey.avbpubkey",
1247 private_key: "testkey.pem",
1248 }
1249
1250 cc_library {
1251 name: "libx",
1252 apex_available: [ "myapex" ],
1253 }
1254
1255 `, withUnbundledBuild)
1256
1257 // ensure apex variant of c++ is linked with static unwinder
1258 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1259 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1260 // note that platform variant is not.
1261 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1262 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Han0c4e0162020-02-26 22:45:42 +09001263}
1264
1265func TestInvalidMinSdkVersion(t *testing.T) {
1266 testApexError(t, `"libz" .*: min_sdk_version is set 29.*`, `
1267 apex {
1268 name: "myapex",
1269 key: "myapex.key",
1270 native_shared_libs: ["libx"],
1271 min_sdk_version: "29",
1272 }
1273
1274 apex_key {
1275 name: "myapex.key",
1276 public_key: "testkey.avbpubkey",
1277 private_key: "testkey.pem",
1278 }
1279
1280 cc_library {
1281 name: "libx",
1282 shared_libs: ["libz"],
1283 system_shared_libs: [],
1284 stl: "none",
1285 apex_available: [ "myapex" ],
1286 }
1287
1288 cc_library {
1289 name: "libz",
1290 system_shared_libs: [],
1291 stl: "none",
1292 stubs: {
1293 versions: ["30"],
1294 },
1295 }
1296 `, withUnbundledBuild)
1297
1298 testApexError(t, `"myapex" .*: min_sdk_version: should be .*`, `
1299 apex {
1300 name: "myapex",
1301 key: "myapex.key",
1302 min_sdk_version: "R",
1303 }
1304
1305 apex_key {
1306 name: "myapex.key",
1307 public_key: "testkey.avbpubkey",
1308 private_key: "testkey.pem",
1309 }
1310 `)
1311}
1312
Jiyong Park7c2ee712018-12-07 00:42:25 +09001313func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001314 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001315 apex {
1316 name: "myapex",
1317 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001318 native_shared_libs: ["mylib"],
1319 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001320 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001321 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001322 }
1323
1324 apex_key {
1325 name: "myapex.key",
1326 public_key: "testkey.avbpubkey",
1327 private_key: "testkey.pem",
1328 }
1329
1330 prebuilt_etc {
1331 name: "myetc",
1332 src: "myprebuilt",
1333 sub_dir: "foo/bar",
1334 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001335
1336 cc_library {
1337 name: "mylib",
1338 srcs: ["mylib.cpp"],
1339 relative_install_path: "foo/bar",
1340 system_shared_libs: [],
1341 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001342 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001343 }
1344
1345 cc_binary {
1346 name: "mybin",
1347 srcs: ["mylib.cpp"],
1348 relative_install_path: "foo/bar",
1349 system_shared_libs: [],
1350 static_executable: true,
1351 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001352 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001353 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001354 `)
1355
Sundong Ahnabb64432019-10-22 13:58:29 +09001356 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001357 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1358
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001359 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001360 ensureListContains(t, dirs, "etc")
1361 ensureListContains(t, dirs, "etc/foo")
1362 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001363 ensureListContains(t, dirs, "lib64")
1364 ensureListContains(t, dirs, "lib64/foo")
1365 ensureListContains(t, dirs, "lib64/foo/bar")
1366 ensureListContains(t, dirs, "lib")
1367 ensureListContains(t, dirs, "lib/foo")
1368 ensureListContains(t, dirs, "lib/foo/bar")
1369
Jiyong Parkbd13e442019-03-15 18:10:35 +09001370 ensureListContains(t, dirs, "bin")
1371 ensureListContains(t, dirs, "bin/foo")
1372 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001373}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001374
1375func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001376 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001377 apex {
1378 name: "myapex",
1379 key: "myapex.key",
1380 native_shared_libs: ["mylib"],
1381 use_vendor: true,
1382 }
1383
1384 apex_key {
1385 name: "myapex.key",
1386 public_key: "testkey.avbpubkey",
1387 private_key: "testkey.pem",
1388 }
1389
1390 cc_library {
1391 name: "mylib",
1392 srcs: ["mylib.cpp"],
1393 shared_libs: ["mylib2"],
1394 system_shared_libs: [],
1395 vendor_available: true,
1396 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001397 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001398 }
1399
1400 cc_library {
1401 name: "mylib2",
1402 srcs: ["mylib.cpp"],
1403 system_shared_libs: [],
1404 vendor_available: true,
1405 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001406 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001407 }
Jooyung Handc782442019-11-01 03:14:38 +09001408 `, func(fs map[string][]byte, config android.Config) {
1409 setUseVendorWhitelistForTest(config, []string{"myapex"})
1410 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001411
1412 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001413 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001414 for _, implicit := range i.Implicits {
1415 inputsList = append(inputsList, implicit.String())
1416 }
1417 }
1418 inputsString := strings.Join(inputsList, " ")
1419
1420 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001421 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1422 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001423
1424 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001425 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1426 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001427}
Jiyong Park16e91a02018-12-20 18:18:08 +09001428
Jooyung Handc782442019-11-01 03:14:38 +09001429func TestUseVendorRestriction(t *testing.T) {
1430 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1431 apex {
1432 name: "myapex",
1433 key: "myapex.key",
1434 use_vendor: true,
1435 }
1436 apex_key {
1437 name: "myapex.key",
1438 public_key: "testkey.avbpubkey",
1439 private_key: "testkey.pem",
1440 }
1441 `, func(fs map[string][]byte, config android.Config) {
1442 setUseVendorWhitelistForTest(config, []string{""})
1443 })
1444 // no error with whitelist
1445 testApex(t, `
1446 apex {
1447 name: "myapex",
1448 key: "myapex.key",
1449 use_vendor: true,
1450 }
1451 apex_key {
1452 name: "myapex.key",
1453 public_key: "testkey.avbpubkey",
1454 private_key: "testkey.pem",
1455 }
1456 `, func(fs map[string][]byte, config android.Config) {
1457 setUseVendorWhitelistForTest(config, []string{"myapex"})
1458 })
1459}
1460
Jooyung Han5c998b92019-06-27 11:30:33 +09001461func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1462 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1463 apex {
1464 name: "myapex",
1465 key: "myapex.key",
1466 native_shared_libs: ["mylib"],
1467 use_vendor: true,
1468 }
1469
1470 apex_key {
1471 name: "myapex.key",
1472 public_key: "testkey.avbpubkey",
1473 private_key: "testkey.pem",
1474 }
1475
1476 cc_library {
1477 name: "mylib",
1478 srcs: ["mylib.cpp"],
1479 system_shared_libs: [],
1480 stl: "none",
1481 }
1482 `)
1483}
1484
Jiyong Park16e91a02018-12-20 18:18:08 +09001485func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001486 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001487 apex {
1488 name: "myapex",
1489 key: "myapex.key",
1490 native_shared_libs: ["mylib"],
1491 }
1492
1493 apex_key {
1494 name: "myapex.key",
1495 public_key: "testkey.avbpubkey",
1496 private_key: "testkey.pem",
1497 }
1498
1499 cc_library {
1500 name: "mylib",
1501 srcs: ["mylib.cpp"],
1502 system_shared_libs: [],
1503 stl: "none",
1504 stubs: {
1505 versions: ["1", "2", "3"],
1506 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001507 apex_available: [
1508 "//apex_available:platform",
1509 "myapex",
1510 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001511 }
1512
1513 cc_binary {
1514 name: "not_in_apex",
1515 srcs: ["mylib.cpp"],
1516 static_libs: ["mylib"],
1517 static_executable: true,
1518 system_shared_libs: [],
1519 stl: "none",
1520 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001521 `)
1522
Colin Cross7113d202019-11-20 16:39:12 -08001523 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001524
1525 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001526 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001527}
Jiyong Park9335a262018-12-24 11:31:58 +09001528
1529func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001530 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001531 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001532 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001533 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001534 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001535 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001536 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001537 }
1538
1539 cc_library {
1540 name: "mylib",
1541 srcs: ["mylib.cpp"],
1542 system_shared_libs: [],
1543 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001544 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001545 }
1546
1547 apex_key {
1548 name: "myapex.key",
1549 public_key: "testkey.avbpubkey",
1550 private_key: "testkey.pem",
1551 }
1552
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001553 android_app_certificate {
1554 name: "myapex.certificate",
1555 certificate: "testkey",
1556 }
1557
1558 android_app_certificate {
1559 name: "myapex.certificate.override",
1560 certificate: "testkey.override",
1561 }
1562
Jiyong Park9335a262018-12-24 11:31:58 +09001563 `)
1564
1565 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001566 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001567
1568 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1569 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1570 "vendor/foo/devkeys/testkey.avbpubkey")
1571 }
1572 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1573 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1574 "vendor/foo/devkeys/testkey.pem")
1575 }
1576
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001577 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001578 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001579 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001580 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001581 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001582 }
1583}
Jiyong Park58e364a2019-01-19 19:24:06 +09001584
Jooyung Hanf121a652019-12-17 14:30:11 +09001585func TestCertificate(t *testing.T) {
1586 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1587 ctx, _ := testApex(t, `
1588 apex {
1589 name: "myapex",
1590 key: "myapex.key",
1591 }
1592 apex_key {
1593 name: "myapex.key",
1594 public_key: "testkey.avbpubkey",
1595 private_key: "testkey.pem",
1596 }`)
1597 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1598 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1599 if actual := rule.Args["certificates"]; actual != expected {
1600 t.Errorf("certificates should be %q, not %q", expected, actual)
1601 }
1602 })
1603 t.Run("override when unspecified", func(t *testing.T) {
1604 ctx, _ := testApex(t, `
1605 apex {
1606 name: "myapex_keytest",
1607 key: "myapex.key",
1608 file_contexts: ":myapex-file_contexts",
1609 }
1610 apex_key {
1611 name: "myapex.key",
1612 public_key: "testkey.avbpubkey",
1613 private_key: "testkey.pem",
1614 }
1615 android_app_certificate {
1616 name: "myapex.certificate.override",
1617 certificate: "testkey.override",
1618 }`)
1619 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1620 expected := "testkey.override.x509.pem testkey.override.pk8"
1621 if actual := rule.Args["certificates"]; actual != expected {
1622 t.Errorf("certificates should be %q, not %q", expected, actual)
1623 }
1624 })
1625 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1626 ctx, _ := testApex(t, `
1627 apex {
1628 name: "myapex",
1629 key: "myapex.key",
1630 certificate: ":myapex.certificate",
1631 }
1632 apex_key {
1633 name: "myapex.key",
1634 public_key: "testkey.avbpubkey",
1635 private_key: "testkey.pem",
1636 }
1637 android_app_certificate {
1638 name: "myapex.certificate",
1639 certificate: "testkey",
1640 }`)
1641 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1642 expected := "testkey.x509.pem testkey.pk8"
1643 if actual := rule.Args["certificates"]; actual != expected {
1644 t.Errorf("certificates should be %q, not %q", expected, actual)
1645 }
1646 })
1647 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1648 ctx, _ := testApex(t, `
1649 apex {
1650 name: "myapex_keytest",
1651 key: "myapex.key",
1652 file_contexts: ":myapex-file_contexts",
1653 certificate: ":myapex.certificate",
1654 }
1655 apex_key {
1656 name: "myapex.key",
1657 public_key: "testkey.avbpubkey",
1658 private_key: "testkey.pem",
1659 }
1660 android_app_certificate {
1661 name: "myapex.certificate.override",
1662 certificate: "testkey.override",
1663 }`)
1664 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1665 expected := "testkey.override.x509.pem testkey.override.pk8"
1666 if actual := rule.Args["certificates"]; actual != expected {
1667 t.Errorf("certificates should be %q, not %q", expected, actual)
1668 }
1669 })
1670 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1671 ctx, _ := testApex(t, `
1672 apex {
1673 name: "myapex",
1674 key: "myapex.key",
1675 certificate: "testkey",
1676 }
1677 apex_key {
1678 name: "myapex.key",
1679 public_key: "testkey.avbpubkey",
1680 private_key: "testkey.pem",
1681 }`)
1682 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1683 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1684 if actual := rule.Args["certificates"]; actual != expected {
1685 t.Errorf("certificates should be %q, not %q", expected, actual)
1686 }
1687 })
1688 t.Run("override when specified as <name>", func(t *testing.T) {
1689 ctx, _ := testApex(t, `
1690 apex {
1691 name: "myapex_keytest",
1692 key: "myapex.key",
1693 file_contexts: ":myapex-file_contexts",
1694 certificate: "testkey",
1695 }
1696 apex_key {
1697 name: "myapex.key",
1698 public_key: "testkey.avbpubkey",
1699 private_key: "testkey.pem",
1700 }
1701 android_app_certificate {
1702 name: "myapex.certificate.override",
1703 certificate: "testkey.override",
1704 }`)
1705 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1706 expected := "testkey.override.x509.pem testkey.override.pk8"
1707 if actual := rule.Args["certificates"]; actual != expected {
1708 t.Errorf("certificates should be %q, not %q", expected, actual)
1709 }
1710 })
1711}
1712
Jiyong Park58e364a2019-01-19 19:24:06 +09001713func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001714 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001715 apex {
1716 name: "myapex",
1717 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001718 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001719 }
1720
1721 apex {
1722 name: "otherapex",
1723 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001724 native_shared_libs: ["mylib", "mylib2"],
Jooyung Han61c41542020-03-07 03:45:53 +09001725 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09001726 }
1727
1728 apex_key {
1729 name: "myapex.key",
1730 public_key: "testkey.avbpubkey",
1731 private_key: "testkey.pem",
1732 }
1733
1734 cc_library {
1735 name: "mylib",
1736 srcs: ["mylib.cpp"],
1737 system_shared_libs: [],
1738 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001739 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001740 "myapex",
1741 "otherapex",
1742 ],
Jooyung Hanc3e92632020-03-21 23:20:55 +09001743 recovery_available: true,
Jiyong Park58e364a2019-01-19 19:24:06 +09001744 }
Jooyung Han68e511e2020-03-02 17:44:33 +09001745 cc_library {
1746 name: "mylib2",
1747 srcs: ["mylib.cpp"],
1748 system_shared_libs: [],
1749 stl: "none",
1750 apex_available: [
1751 "myapex",
1752 "otherapex",
1753 ],
1754 use_apex_name_macro: true,
1755 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001756 `)
1757
Jooyung Han68e511e2020-03-02 17:44:33 +09001758 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001759 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001760 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc3e92632020-03-21 23:20:55 +09001761 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han68e511e2020-03-02 17:44:33 +09001762
Jooyung Han61c41542020-03-07 03:45:53 +09001763 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Han68e511e2020-03-02 17:44:33 +09001764 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1765 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han61c41542020-03-07 03:45:53 +09001766 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09001767 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han68e511e2020-03-02 17:44:33 +09001768
Jooyung Han61c41542020-03-07 03:45:53 +09001769 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Han68e511e2020-03-02 17:44:33 +09001770 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1771 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han61c41542020-03-07 03:45:53 +09001772 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09001773 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001774
Jooyung Han68e511e2020-03-02 17:44:33 +09001775 // When cc_library sets use_apex_name_macro: true
1776 // apex variants define additional macro to distinguish which apex variant it is built for
1777
1778 // non-APEX variant does not have __ANDROID_APEX__ defined
1779 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1780 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1781
1782 // APEX variant has __ANDROID_APEX__ defined
1783 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001784 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001785 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1786 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001787
Jooyung Han68e511e2020-03-02 17:44:33 +09001788 // APEX variant has __ANDROID_APEX__ defined
1789 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001790 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001791 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1792 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Hanc3e92632020-03-21 23:20:55 +09001793
1794 // recovery variant does not set __ANDROID_SDK_VERSION__
1795 mylibCFlags = ctx.ModuleForTests("mylib", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1796 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1797 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001798}
Jiyong Park7e636d02019-01-28 16:16:54 +09001799
1800func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001801 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001802 apex {
1803 name: "myapex",
1804 key: "myapex.key",
1805 native_shared_libs: ["mylib"],
1806 }
1807
1808 apex_key {
1809 name: "myapex.key",
1810 public_key: "testkey.avbpubkey",
1811 private_key: "testkey.pem",
1812 }
1813
1814 cc_library_headers {
1815 name: "mylib_headers",
1816 export_include_dirs: ["my_include"],
1817 system_shared_libs: [],
1818 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001819 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001820 }
1821
1822 cc_library {
1823 name: "mylib",
1824 srcs: ["mylib.cpp"],
1825 system_shared_libs: [],
1826 stl: "none",
1827 header_libs: ["mylib_headers"],
1828 export_header_lib_headers: ["mylib_headers"],
1829 stubs: {
1830 versions: ["1", "2", "3"],
1831 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001832 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001833 }
1834
1835 cc_library {
1836 name: "otherlib",
1837 srcs: ["mylib.cpp"],
1838 system_shared_libs: [],
1839 stl: "none",
1840 shared_libs: ["mylib"],
1841 }
1842 `)
1843
Colin Cross7113d202019-11-20 16:39:12 -08001844 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001845
1846 // Ensure that the include path of the header lib is exported to 'otherlib'
1847 ensureContains(t, cFlags, "-Imy_include")
1848}
Alex Light9670d332019-01-29 18:07:33 -08001849
Jiyong Park7cd10e32020-01-14 09:22:18 +09001850type fileInApex struct {
1851 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001852 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001853 isLink bool
1854}
1855
Jooyung Hana57af4a2020-01-23 05:36:59 +00001856func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001857 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001858 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001859 copyCmds := apexRule.Args["copy_commands"]
1860 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001861 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001862 for _, cmd := range strings.Split(copyCmds, "&&") {
1863 cmd = strings.TrimSpace(cmd)
1864 if cmd == "" {
1865 continue
1866 }
1867 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001868 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001869 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001870 switch terms[0] {
1871 case "mkdir":
1872 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001873 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001874 t.Fatal("copyCmds contains invalid cp command", cmd)
1875 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001876 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001877 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001878 isLink = false
1879 case "ln":
1880 if len(terms) != 3 && len(terms) != 4 {
1881 // ln LINK TARGET or ln -s LINK TARGET
1882 t.Fatal("copyCmds contains invalid ln command", cmd)
1883 }
1884 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001885 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001886 isLink = true
1887 default:
1888 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1889 }
1890 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001891 index := strings.Index(dst, imageApexDir)
1892 if index == -1 {
1893 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1894 }
1895 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001896 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001897 }
1898 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001899 return ret
1900}
1901
Jooyung Hana57af4a2020-01-23 05:36:59 +00001902func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1903 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001904 var failed bool
1905 var surplus []string
1906 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001907 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Han8d8906c2020-02-27 13:31:56 +09001908 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001909 for _, expected := range files {
1910 if matched, _ := path.Match(expected, file.path); matched {
1911 filesMatched[expected] = true
Jooyung Han8d8906c2020-02-27 13:31:56 +09001912 mactchFound = true
1913 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001914 }
1915 }
Jooyung Han8d8906c2020-02-27 13:31:56 +09001916 if !mactchFound {
1917 surplus = append(surplus, file.path)
1918 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001919 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001920
Jooyung Han31c470b2019-10-18 16:26:59 +09001921 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001922 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001923 t.Log("surplus files", surplus)
1924 failed = true
1925 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001926
1927 if len(files) > len(filesMatched) {
1928 var missing []string
1929 for _, expected := range files {
1930 if !filesMatched[expected] {
1931 missing = append(missing, expected)
1932 }
1933 }
1934 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001935 t.Log("missing files", missing)
1936 failed = true
1937 }
1938 if failed {
1939 t.Fail()
1940 }
1941}
1942
Jooyung Han344d5432019-08-23 11:17:39 +09001943func TestVndkApexCurrent(t *testing.T) {
1944 ctx, _ := testApex(t, `
1945 apex_vndk {
1946 name: "myapex",
1947 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001948 }
1949
1950 apex_key {
1951 name: "myapex.key",
1952 public_key: "testkey.avbpubkey",
1953 private_key: "testkey.pem",
1954 }
1955
1956 cc_library {
1957 name: "libvndk",
1958 srcs: ["mylib.cpp"],
1959 vendor_available: true,
1960 vndk: {
1961 enabled: true,
1962 },
1963 system_shared_libs: [],
1964 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001965 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001966 }
1967
1968 cc_library {
1969 name: "libvndksp",
1970 srcs: ["mylib.cpp"],
1971 vendor_available: true,
1972 vndk: {
1973 enabled: true,
1974 support_system_process: true,
1975 },
1976 system_shared_libs: [],
1977 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001978 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001979 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001980 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001981
Jooyung Hana57af4a2020-01-23 05:36:59 +00001982 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001983 "lib/libvndk.so",
1984 "lib/libvndksp.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09001985 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09001986 "lib64/libvndk.so",
1987 "lib64/libvndksp.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09001988 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001989 "etc/llndk.libraries.VER.txt",
1990 "etc/vndkcore.libraries.VER.txt",
1991 "etc/vndksp.libraries.VER.txt",
1992 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001993 })
Jooyung Han344d5432019-08-23 11:17:39 +09001994}
1995
1996func TestVndkApexWithPrebuilt(t *testing.T) {
1997 ctx, _ := testApex(t, `
1998 apex_vndk {
1999 name: "myapex",
2000 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002001 }
2002
2003 apex_key {
2004 name: "myapex.key",
2005 public_key: "testkey.avbpubkey",
2006 private_key: "testkey.pem",
2007 }
2008
2009 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002010 name: "libvndk",
2011 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002012 vendor_available: true,
2013 vndk: {
2014 enabled: true,
2015 },
2016 system_shared_libs: [],
2017 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002018 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002019 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002020
2021 cc_prebuilt_library_shared {
2022 name: "libvndk.arm",
2023 srcs: ["libvndk.arm.so"],
2024 vendor_available: true,
2025 vndk: {
2026 enabled: true,
2027 },
2028 enabled: false,
2029 arch: {
2030 arm: {
2031 enabled: true,
2032 },
2033 },
2034 system_shared_libs: [],
2035 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002036 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002037 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002038 `+vndkLibrariesTxtFiles("current"),
2039 withFiles(map[string][]byte{
2040 "libvndk.so": nil,
2041 "libvndk.arm.so": nil,
2042 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002043
Jooyung Hana57af4a2020-01-23 05:36:59 +00002044 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002045 "lib/libvndk.so",
2046 "lib/libvndk.arm.so",
2047 "lib64/libvndk.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002048 "lib/libc++.so",
2049 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002050 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002051 })
Jooyung Han344d5432019-08-23 11:17:39 +09002052}
2053
Jooyung Han39edb6c2019-11-06 16:53:07 +09002054func vndkLibrariesTxtFiles(vers ...string) (result string) {
2055 for _, v := range vers {
2056 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002057 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002058 result += `
2059 vndk_libraries_txt {
2060 name: "` + txt + `.libraries.txt",
2061 }
2062 `
2063 }
2064 } else {
2065 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2066 result += `
2067 prebuilt_etc {
2068 name: "` + txt + `.libraries.` + v + `.txt",
2069 src: "dummy.txt",
2070 }
2071 `
2072 }
2073 }
2074 }
2075 return
2076}
2077
Jooyung Han344d5432019-08-23 11:17:39 +09002078func TestVndkApexVersion(t *testing.T) {
2079 ctx, _ := testApex(t, `
2080 apex_vndk {
2081 name: "myapex_v27",
2082 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002083 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002084 vndk_version: "27",
2085 }
2086
2087 apex_key {
2088 name: "myapex.key",
2089 public_key: "testkey.avbpubkey",
2090 private_key: "testkey.pem",
2091 }
2092
Jooyung Han31c470b2019-10-18 16:26:59 +09002093 vndk_prebuilt_shared {
2094 name: "libvndk27",
2095 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002096 vendor_available: true,
2097 vndk: {
2098 enabled: true,
2099 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002100 target_arch: "arm64",
2101 arch: {
2102 arm: {
2103 srcs: ["libvndk27_arm.so"],
2104 },
2105 arm64: {
2106 srcs: ["libvndk27_arm64.so"],
2107 },
2108 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002109 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002110 }
2111
2112 vndk_prebuilt_shared {
2113 name: "libvndk27",
2114 version: "27",
2115 vendor_available: true,
2116 vndk: {
2117 enabled: true,
2118 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002119 target_arch: "x86_64",
2120 arch: {
2121 x86: {
2122 srcs: ["libvndk27_x86.so"],
2123 },
2124 x86_64: {
2125 srcs: ["libvndk27_x86_64.so"],
2126 },
2127 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002128 }
2129 `+vndkLibrariesTxtFiles("27"),
2130 withFiles(map[string][]byte{
2131 "libvndk27_arm.so": nil,
2132 "libvndk27_arm64.so": nil,
2133 "libvndk27_x86.so": nil,
2134 "libvndk27_x86_64.so": nil,
2135 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002136
Jooyung Hana57af4a2020-01-23 05:36:59 +00002137 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002138 "lib/libvndk27_arm.so",
2139 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002140 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002141 })
Jooyung Han344d5432019-08-23 11:17:39 +09002142}
2143
2144func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2145 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2146 apex_vndk {
2147 name: "myapex_v27",
2148 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002149 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002150 vndk_version: "27",
2151 }
2152 apex_vndk {
2153 name: "myapex_v27_other",
2154 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002155 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002156 vndk_version: "27",
2157 }
2158
2159 apex_key {
2160 name: "myapex.key",
2161 public_key: "testkey.avbpubkey",
2162 private_key: "testkey.pem",
2163 }
2164
2165 cc_library {
2166 name: "libvndk",
2167 srcs: ["mylib.cpp"],
2168 vendor_available: true,
2169 vndk: {
2170 enabled: true,
2171 },
2172 system_shared_libs: [],
2173 stl: "none",
2174 }
2175
2176 vndk_prebuilt_shared {
2177 name: "libvndk",
2178 version: "27",
2179 vendor_available: true,
2180 vndk: {
2181 enabled: true,
2182 },
2183 srcs: ["libvndk.so"],
2184 }
2185 `, withFiles(map[string][]byte{
2186 "libvndk.so": nil,
2187 }))
2188}
2189
Jooyung Han90eee022019-10-01 20:02:42 +09002190func TestVndkApexNameRule(t *testing.T) {
2191 ctx, _ := testApex(t, `
2192 apex_vndk {
2193 name: "myapex",
2194 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002195 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002196 }
2197 apex_vndk {
2198 name: "myapex_v28",
2199 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002200 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002201 vndk_version: "28",
2202 }
2203 apex_key {
2204 name: "myapex.key",
2205 public_key: "testkey.avbpubkey",
2206 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002207 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002208
2209 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002210 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002211 actual := proptools.String(bundle.properties.Apex_name)
2212 if !reflect.DeepEqual(actual, expected) {
2213 t.Errorf("Got '%v', expected '%v'", actual, expected)
2214 }
2215 }
2216
2217 assertApexName("com.android.vndk.vVER", "myapex")
2218 assertApexName("com.android.vndk.v28", "myapex_v28")
2219}
2220
Jooyung Han344d5432019-08-23 11:17:39 +09002221func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2222 ctx, _ := testApex(t, `
2223 apex_vndk {
2224 name: "myapex",
2225 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002226 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002227 }
2228
2229 apex_key {
2230 name: "myapex.key",
2231 public_key: "testkey.avbpubkey",
2232 private_key: "testkey.pem",
2233 }
2234
2235 cc_library {
2236 name: "libvndk",
2237 srcs: ["mylib.cpp"],
2238 vendor_available: true,
2239 native_bridge_supported: true,
2240 host_supported: true,
2241 vndk: {
2242 enabled: true,
2243 },
2244 system_shared_libs: [],
2245 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002246 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002247 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002248 `+vndkLibrariesTxtFiles("current"),
2249 withTargets(map[android.OsType][]android.Target{
2250 android.Android: []android.Target{
2251 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2252 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2253 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2254 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2255 },
2256 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002257
Jooyung Hana57af4a2020-01-23 05:36:59 +00002258 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002259 "lib/libvndk.so",
2260 "lib64/libvndk.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002261 "lib/libc++.so",
2262 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002263 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002264 })
Jooyung Han344d5432019-08-23 11:17:39 +09002265}
2266
2267func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2268 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2269 apex_vndk {
2270 name: "myapex",
2271 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002272 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002273 native_bridge_supported: true,
2274 }
2275
2276 apex_key {
2277 name: "myapex.key",
2278 public_key: "testkey.avbpubkey",
2279 private_key: "testkey.pem",
2280 }
2281
2282 cc_library {
2283 name: "libvndk",
2284 srcs: ["mylib.cpp"],
2285 vendor_available: true,
2286 native_bridge_supported: true,
2287 host_supported: true,
2288 vndk: {
2289 enabled: true,
2290 },
2291 system_shared_libs: [],
2292 stl: "none",
2293 }
2294 `)
2295}
2296
Jooyung Han31c470b2019-10-18 16:26:59 +09002297func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002298 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002299 apex_vndk {
2300 name: "myapex_v27",
2301 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002302 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002303 vndk_version: "27",
2304 }
2305
2306 apex_key {
2307 name: "myapex.key",
2308 public_key: "testkey.avbpubkey",
2309 private_key: "testkey.pem",
2310 }
2311
2312 vndk_prebuilt_shared {
2313 name: "libvndk27",
2314 version: "27",
2315 target_arch: "arm",
2316 vendor_available: true,
2317 vndk: {
2318 enabled: true,
2319 },
2320 arch: {
2321 arm: {
2322 srcs: ["libvndk27.so"],
2323 }
2324 },
2325 }
2326
2327 vndk_prebuilt_shared {
2328 name: "libvndk27",
2329 version: "27",
2330 target_arch: "arm",
2331 binder32bit: true,
2332 vendor_available: true,
2333 vndk: {
2334 enabled: true,
2335 },
2336 arch: {
2337 arm: {
2338 srcs: ["libvndk27binder32.so"],
2339 }
2340 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002341 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002342 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002343 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002344 withFiles(map[string][]byte{
2345 "libvndk27.so": nil,
2346 "libvndk27binder32.so": nil,
2347 }),
2348 withBinder32bit,
2349 withTargets(map[android.OsType][]android.Target{
2350 android.Android: []android.Target{
2351 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2352 },
2353 }),
2354 )
2355
Jooyung Hana57af4a2020-01-23 05:36:59 +00002356 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002357 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002358 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002359 })
2360}
2361
Jooyung Hane1633032019-08-01 17:41:43 +09002362func TestDependenciesInApexManifest(t *testing.T) {
2363 ctx, _ := testApex(t, `
2364 apex {
2365 name: "myapex_nodep",
2366 key: "myapex.key",
2367 native_shared_libs: ["lib_nodep"],
2368 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002369 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002370 }
2371
2372 apex {
2373 name: "myapex_dep",
2374 key: "myapex.key",
2375 native_shared_libs: ["lib_dep"],
2376 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002377 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002378 }
2379
2380 apex {
2381 name: "myapex_provider",
2382 key: "myapex.key",
2383 native_shared_libs: ["libfoo"],
2384 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002385 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002386 }
2387
2388 apex {
2389 name: "myapex_selfcontained",
2390 key: "myapex.key",
2391 native_shared_libs: ["lib_dep", "libfoo"],
2392 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002393 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002394 }
2395
2396 apex_key {
2397 name: "myapex.key",
2398 public_key: "testkey.avbpubkey",
2399 private_key: "testkey.pem",
2400 }
2401
2402 cc_library {
2403 name: "lib_nodep",
2404 srcs: ["mylib.cpp"],
2405 system_shared_libs: [],
2406 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002407 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002408 }
2409
2410 cc_library {
2411 name: "lib_dep",
2412 srcs: ["mylib.cpp"],
2413 shared_libs: ["libfoo"],
2414 system_shared_libs: [],
2415 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002416 apex_available: [
2417 "myapex_dep",
2418 "myapex_provider",
2419 "myapex_selfcontained",
2420 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002421 }
2422
2423 cc_library {
2424 name: "libfoo",
2425 srcs: ["mytest.cpp"],
2426 stubs: {
2427 versions: ["1"],
2428 },
2429 system_shared_libs: [],
2430 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002431 apex_available: [
2432 "myapex_provider",
2433 "myapex_selfcontained",
2434 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002435 }
2436 `)
2437
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002438 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002439 var provideNativeLibs, requireNativeLibs []string
2440
Sundong Ahnabb64432019-10-22 13:58:29 +09002441 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002442 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2443 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002444 ensureListEmpty(t, provideNativeLibs)
2445 ensureListEmpty(t, requireNativeLibs)
2446
Sundong Ahnabb64432019-10-22 13:58:29 +09002447 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002448 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2449 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002450 ensureListEmpty(t, provideNativeLibs)
2451 ensureListContains(t, requireNativeLibs, "libfoo.so")
2452
Sundong Ahnabb64432019-10-22 13:58:29 +09002453 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002454 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2455 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002456 ensureListContains(t, provideNativeLibs, "libfoo.so")
2457 ensureListEmpty(t, requireNativeLibs)
2458
Sundong Ahnabb64432019-10-22 13:58:29 +09002459 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002460 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2461 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002462 ensureListContains(t, provideNativeLibs, "libfoo.so")
2463 ensureListEmpty(t, requireNativeLibs)
2464}
2465
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002466func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002467 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002468 apex {
2469 name: "myapex",
2470 key: "myapex.key",
2471 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002472 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002473 }
2474
2475 apex_key {
2476 name: "myapex.key",
2477 public_key: "testkey.avbpubkey",
2478 private_key: "testkey.pem",
2479 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002480
2481 cc_library {
2482 name: "mylib",
2483 srcs: ["mylib.cpp"],
2484 system_shared_libs: [],
2485 stl: "none",
2486 apex_available: [
2487 "//apex_available:platform",
2488 "myapex",
2489 ],
2490 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002491 `)
2492
Sundong Ahnabb64432019-10-22 13:58:29 +09002493 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002494 apexManifestRule := module.Rule("apexManifestRule")
2495 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2496 apexRule := module.Rule("apexRule")
2497 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002498
2499 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2500 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2501 name := apexBundle.BaseModuleName()
2502 prefix := "TARGET_"
2503 var builder strings.Builder
2504 data.Custom(&builder, name, prefix, "", data)
2505 androidMk := builder.String()
2506 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2507 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002508}
2509
Alex Light0851b882019-02-07 13:20:53 -08002510func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002511 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002512 apex {
2513 name: "myapex",
2514 key: "myapex.key",
2515 native_shared_libs: ["mylib_common"],
2516 }
2517
2518 apex_key {
2519 name: "myapex.key",
2520 public_key: "testkey.avbpubkey",
2521 private_key: "testkey.pem",
2522 }
2523
2524 cc_library {
2525 name: "mylib_common",
2526 srcs: ["mylib.cpp"],
2527 system_shared_libs: [],
2528 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002529 apex_available: [
2530 "//apex_available:platform",
2531 "myapex",
2532 ],
Alex Light0851b882019-02-07 13:20:53 -08002533 }
2534 `)
2535
Sundong Ahnabb64432019-10-22 13:58:29 +09002536 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002537 apexRule := module.Rule("apexRule")
2538 copyCmds := apexRule.Args["copy_commands"]
2539
2540 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2541 t.Log("Apex was a test apex!")
2542 t.Fail()
2543 }
2544 // Ensure that main rule creates an output
2545 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2546
2547 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002548 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002549
2550 // Ensure that both direct and indirect deps are copied into apex
2551 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2552
Colin Cross7113d202019-11-20 16:39:12 -08002553 // Ensure that the platform variant ends with _shared
2554 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002555
2556 if !android.InAnyApex("mylib_common") {
2557 t.Log("Found mylib_common not in any apex!")
2558 t.Fail()
2559 }
2560}
2561
2562func TestTestApex(t *testing.T) {
2563 if android.InAnyApex("mylib_common_test") {
2564 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!")
2565 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002566 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002567 apex_test {
2568 name: "myapex",
2569 key: "myapex.key",
2570 native_shared_libs: ["mylib_common_test"],
2571 }
2572
2573 apex_key {
2574 name: "myapex.key",
2575 public_key: "testkey.avbpubkey",
2576 private_key: "testkey.pem",
2577 }
2578
2579 cc_library {
2580 name: "mylib_common_test",
2581 srcs: ["mylib.cpp"],
2582 system_shared_libs: [],
2583 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002584 // TODO: remove //apex_available:platform
2585 apex_available: [
2586 "//apex_available:platform",
2587 "myapex",
2588 ],
Alex Light0851b882019-02-07 13:20:53 -08002589 }
2590 `)
2591
Sundong Ahnabb64432019-10-22 13:58:29 +09002592 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002593 apexRule := module.Rule("apexRule")
2594 copyCmds := apexRule.Args["copy_commands"]
2595
2596 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2597 t.Log("Apex was not a test apex!")
2598 t.Fail()
2599 }
2600 // Ensure that main rule creates an output
2601 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2602
2603 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002604 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002605
2606 // Ensure that both direct and indirect deps are copied into apex
2607 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2608
Colin Cross7113d202019-11-20 16:39:12 -08002609 // Ensure that the platform variant ends with _shared
2610 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002611}
2612
Alex Light9670d332019-01-29 18:07:33 -08002613func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002614 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002615 apex {
2616 name: "myapex",
2617 key: "myapex.key",
2618 multilib: {
2619 first: {
2620 native_shared_libs: ["mylib_common"],
2621 }
2622 },
2623 target: {
2624 android: {
2625 multilib: {
2626 first: {
2627 native_shared_libs: ["mylib"],
2628 }
2629 }
2630 },
2631 host: {
2632 multilib: {
2633 first: {
2634 native_shared_libs: ["mylib2"],
2635 }
2636 }
2637 }
2638 }
2639 }
2640
2641 apex_key {
2642 name: "myapex.key",
2643 public_key: "testkey.avbpubkey",
2644 private_key: "testkey.pem",
2645 }
2646
2647 cc_library {
2648 name: "mylib",
2649 srcs: ["mylib.cpp"],
2650 system_shared_libs: [],
2651 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002652 // TODO: remove //apex_available:platform
2653 apex_available: [
2654 "//apex_available:platform",
2655 "myapex",
2656 ],
Alex Light9670d332019-01-29 18:07:33 -08002657 }
2658
2659 cc_library {
2660 name: "mylib_common",
2661 srcs: ["mylib.cpp"],
2662 system_shared_libs: [],
2663 stl: "none",
2664 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002665 // TODO: remove //apex_available:platform
2666 apex_available: [
2667 "//apex_available:platform",
2668 "myapex",
2669 ],
Alex Light9670d332019-01-29 18:07:33 -08002670 }
2671
2672 cc_library {
2673 name: "mylib2",
2674 srcs: ["mylib.cpp"],
2675 system_shared_libs: [],
2676 stl: "none",
2677 compile_multilib: "first",
2678 }
2679 `)
2680
Sundong Ahnabb64432019-10-22 13:58:29 +09002681 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002682 copyCmds := apexRule.Args["copy_commands"]
2683
2684 // Ensure that main rule creates an output
2685 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2686
2687 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002688 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2689 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2690 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002691
2692 // Ensure that both direct and indirect deps are copied into apex
2693 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2694 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2695 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2696
Colin Cross7113d202019-11-20 16:39:12 -08002697 // Ensure that the platform variant ends with _shared
2698 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2699 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2700 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002701}
Jiyong Park04480cf2019-02-06 00:16:29 +09002702
2703func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002704 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002705 apex {
2706 name: "myapex",
2707 key: "myapex.key",
2708 binaries: ["myscript"],
2709 }
2710
2711 apex_key {
2712 name: "myapex.key",
2713 public_key: "testkey.avbpubkey",
2714 private_key: "testkey.pem",
2715 }
2716
2717 sh_binary {
2718 name: "myscript",
2719 src: "mylib.cpp",
2720 filename: "myscript.sh",
2721 sub_dir: "script",
2722 }
2723 `)
2724
Sundong Ahnabb64432019-10-22 13:58:29 +09002725 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002726 copyCmds := apexRule.Args["copy_commands"]
2727
2728 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2729}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002730
Jooyung Han91df2082019-11-20 01:49:42 +09002731func TestApexInVariousPartition(t *testing.T) {
2732 testcases := []struct {
2733 propName, parition, flattenedPartition string
2734 }{
2735 {"", "system", "system_ext"},
2736 {"product_specific: true", "product", "product"},
2737 {"soc_specific: true", "vendor", "vendor"},
2738 {"proprietary: true", "vendor", "vendor"},
2739 {"vendor: true", "vendor", "vendor"},
2740 {"system_ext_specific: true", "system_ext", "system_ext"},
2741 }
2742 for _, tc := range testcases {
2743 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2744 ctx, _ := testApex(t, `
2745 apex {
2746 name: "myapex",
2747 key: "myapex.key",
2748 `+tc.propName+`
2749 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002750
Jooyung Han91df2082019-11-20 01:49:42 +09002751 apex_key {
2752 name: "myapex.key",
2753 public_key: "testkey.avbpubkey",
2754 private_key: "testkey.pem",
2755 }
2756 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002757
Jooyung Han91df2082019-11-20 01:49:42 +09002758 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2759 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2760 actual := apex.installDir.String()
2761 if actual != expected {
2762 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2763 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002764
Jooyung Han91df2082019-11-20 01:49:42 +09002765 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2766 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2767 actual = flattened.installDir.String()
2768 if actual != expected {
2769 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2770 }
2771 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002772 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002773}
Jiyong Park67882562019-03-21 01:11:21 +09002774
Jooyung Han54aca7b2019-11-20 02:26:02 +09002775func TestFileContexts(t *testing.T) {
2776 ctx, _ := testApex(t, `
2777 apex {
2778 name: "myapex",
2779 key: "myapex.key",
2780 }
2781
2782 apex_key {
2783 name: "myapex.key",
2784 public_key: "testkey.avbpubkey",
2785 private_key: "testkey.pem",
2786 }
2787 `)
2788 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2789 apexRule := module.Rule("apexRule")
2790 actual := apexRule.Args["file_contexts"]
2791 expected := "system/sepolicy/apex/myapex-file_contexts"
2792 if actual != expected {
2793 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2794 }
2795
2796 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2797 apex {
2798 name: "myapex",
2799 key: "myapex.key",
2800 file_contexts: "my_own_file_contexts",
2801 }
2802
2803 apex_key {
2804 name: "myapex.key",
2805 public_key: "testkey.avbpubkey",
2806 private_key: "testkey.pem",
2807 }
2808 `, withFiles(map[string][]byte{
2809 "my_own_file_contexts": nil,
2810 }))
2811
2812 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2813 apex {
2814 name: "myapex",
2815 key: "myapex.key",
2816 product_specific: true,
2817 file_contexts: "product_specific_file_contexts",
2818 }
2819
2820 apex_key {
2821 name: "myapex.key",
2822 public_key: "testkey.avbpubkey",
2823 private_key: "testkey.pem",
2824 }
2825 `)
2826
2827 ctx, _ = testApex(t, `
2828 apex {
2829 name: "myapex",
2830 key: "myapex.key",
2831 product_specific: true,
2832 file_contexts: "product_specific_file_contexts",
2833 }
2834
2835 apex_key {
2836 name: "myapex.key",
2837 public_key: "testkey.avbpubkey",
2838 private_key: "testkey.pem",
2839 }
2840 `, withFiles(map[string][]byte{
2841 "product_specific_file_contexts": nil,
2842 }))
2843 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2844 apexRule = module.Rule("apexRule")
2845 actual = apexRule.Args["file_contexts"]
2846 expected = "product_specific_file_contexts"
2847 if actual != expected {
2848 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2849 }
2850
2851 ctx, _ = testApex(t, `
2852 apex {
2853 name: "myapex",
2854 key: "myapex.key",
2855 product_specific: true,
2856 file_contexts: ":my-file-contexts",
2857 }
2858
2859 apex_key {
2860 name: "myapex.key",
2861 public_key: "testkey.avbpubkey",
2862 private_key: "testkey.pem",
2863 }
2864
2865 filegroup {
2866 name: "my-file-contexts",
2867 srcs: ["product_specific_file_contexts"],
2868 }
2869 `, withFiles(map[string][]byte{
2870 "product_specific_file_contexts": nil,
2871 }))
2872 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2873 apexRule = module.Rule("apexRule")
2874 actual = apexRule.Args["file_contexts"]
2875 expected = "product_specific_file_contexts"
2876 if actual != expected {
2877 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2878 }
2879}
2880
Jiyong Park67882562019-03-21 01:11:21 +09002881func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002882 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002883 apex_key {
2884 name: "myapex.key",
2885 public_key: ":my.avbpubkey",
2886 private_key: ":my.pem",
2887 product_specific: true,
2888 }
2889
2890 filegroup {
2891 name: "my.avbpubkey",
2892 srcs: ["testkey2.avbpubkey"],
2893 }
2894
2895 filegroup {
2896 name: "my.pem",
2897 srcs: ["testkey2.pem"],
2898 }
2899 `)
2900
2901 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2902 expected_pubkey := "testkey2.avbpubkey"
2903 actual_pubkey := apex_key.public_key_file.String()
2904 if actual_pubkey != expected_pubkey {
2905 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2906 }
2907 expected_privkey := "testkey2.pem"
2908 actual_privkey := apex_key.private_key_file.String()
2909 if actual_privkey != expected_privkey {
2910 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2911 }
2912}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002913
2914func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002915 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002916 prebuilt_apex {
2917 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002918 arch: {
2919 arm64: {
2920 src: "myapex-arm64.apex",
2921 },
2922 arm: {
2923 src: "myapex-arm.apex",
2924 },
2925 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002926 }
2927 `)
2928
2929 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2930
Jiyong Parkc95714e2019-03-29 14:23:10 +09002931 expectedInput := "myapex-arm64.apex"
2932 if prebuilt.inputApex.String() != expectedInput {
2933 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2934 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002935}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002936
2937func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002938 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002939 prebuilt_apex {
2940 name: "myapex",
2941 src: "myapex-arm.apex",
2942 filename: "notmyapex.apex",
2943 }
2944 `)
2945
2946 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2947
2948 expected := "notmyapex.apex"
2949 if p.installFilename != expected {
2950 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2951 }
2952}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002953
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002954func TestPrebuiltOverrides(t *testing.T) {
2955 ctx, config := testApex(t, `
2956 prebuilt_apex {
2957 name: "myapex.prebuilt",
2958 src: "myapex-arm.apex",
2959 overrides: [
2960 "myapex",
2961 ],
2962 }
2963 `)
2964
2965 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2966
2967 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002968 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002969 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002970 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002971 }
2972}
2973
Roland Levillain630846d2019-06-26 12:48:34 +01002974func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002975 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002976 apex_test {
2977 name: "myapex",
2978 key: "myapex.key",
2979 tests: [
2980 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002981 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002982 ],
2983 }
2984
2985 apex_key {
2986 name: "myapex.key",
2987 public_key: "testkey.avbpubkey",
2988 private_key: "testkey.pem",
2989 }
2990
2991 cc_test {
2992 name: "mytest",
2993 gtest: false,
2994 srcs: ["mytest.cpp"],
2995 relative_install_path: "test",
2996 system_shared_libs: [],
2997 static_executable: true,
2998 stl: "none",
2999 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003000
3001 cc_test {
3002 name: "mytests",
3003 gtest: false,
3004 srcs: [
3005 "mytest1.cpp",
3006 "mytest2.cpp",
3007 "mytest3.cpp",
3008 ],
3009 test_per_src: true,
3010 relative_install_path: "test",
3011 system_shared_libs: [],
3012 static_executable: true,
3013 stl: "none",
3014 }
Roland Levillain630846d2019-06-26 12:48:34 +01003015 `)
3016
Sundong Ahnabb64432019-10-22 13:58:29 +09003017 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003018 copyCmds := apexRule.Args["copy_commands"]
3019
3020 // Ensure that test dep is copied into apex.
3021 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003022
3023 // Ensure that test deps built with `test_per_src` are copied into apex.
3024 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3025 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3026 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003027
3028 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003029 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003030 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3031 name := apexBundle.BaseModuleName()
3032 prefix := "TARGET_"
3033 var builder strings.Builder
3034 data.Custom(&builder, name, prefix, "", data)
3035 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003036 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3037 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3038 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3039 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003040 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003041 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003042 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003043}
3044
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003045func TestInstallExtraFlattenedApexes(t *testing.T) {
3046 ctx, config := testApex(t, `
3047 apex {
3048 name: "myapex",
3049 key: "myapex.key",
3050 }
3051 apex_key {
3052 name: "myapex.key",
3053 public_key: "testkey.avbpubkey",
3054 private_key: "testkey.pem",
3055 }
3056 `, func(fs map[string][]byte, config android.Config) {
3057 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3058 })
3059 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003060 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003061 mk := android.AndroidMkDataForTest(t, config, "", ab)
3062 var builder strings.Builder
3063 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3064 androidMk := builder.String()
3065 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3066}
3067
Jooyung Han5c998b92019-06-27 11:30:33 +09003068func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003069 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003070 apex {
3071 name: "myapex",
3072 key: "myapex.key",
3073 native_shared_libs: ["mylib"],
3074 uses: ["commonapex"],
3075 }
3076
3077 apex {
3078 name: "commonapex",
3079 key: "myapex.key",
3080 native_shared_libs: ["libcommon"],
3081 provide_cpp_shared_libs: true,
3082 }
3083
3084 apex_key {
3085 name: "myapex.key",
3086 public_key: "testkey.avbpubkey",
3087 private_key: "testkey.pem",
3088 }
3089
3090 cc_library {
3091 name: "mylib",
3092 srcs: ["mylib.cpp"],
3093 shared_libs: ["libcommon"],
3094 system_shared_libs: [],
3095 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003096 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003097 }
3098
3099 cc_library {
3100 name: "libcommon",
3101 srcs: ["mylib_common.cpp"],
3102 system_shared_libs: [],
3103 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003104 // TODO: remove //apex_available:platform
3105 apex_available: [
3106 "//apex_available:platform",
3107 "commonapex",
3108 "myapex",
3109 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003110 }
3111 `)
3112
Sundong Ahnabb64432019-10-22 13:58:29 +09003113 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003114 apexRule1 := module1.Rule("apexRule")
3115 copyCmds1 := apexRule1.Args["copy_commands"]
3116
Sundong Ahnabb64432019-10-22 13:58:29 +09003117 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003118 apexRule2 := module2.Rule("apexRule")
3119 copyCmds2 := apexRule2.Args["copy_commands"]
3120
Colin Cross7113d202019-11-20 16:39:12 -08003121 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3122 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003123 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3124 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3125 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3126}
3127
3128func TestApexUsesFailsIfNotProvided(t *testing.T) {
3129 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3130 apex {
3131 name: "myapex",
3132 key: "myapex.key",
3133 uses: ["commonapex"],
3134 }
3135
3136 apex {
3137 name: "commonapex",
3138 key: "myapex.key",
3139 }
3140
3141 apex_key {
3142 name: "myapex.key",
3143 public_key: "testkey.avbpubkey",
3144 private_key: "testkey.pem",
3145 }
3146 `)
3147 testApexError(t, `uses: "commonapex" is not a provider`, `
3148 apex {
3149 name: "myapex",
3150 key: "myapex.key",
3151 uses: ["commonapex"],
3152 }
3153
3154 cc_library {
3155 name: "commonapex",
3156 system_shared_libs: [],
3157 stl: "none",
3158 }
3159
3160 apex_key {
3161 name: "myapex.key",
3162 public_key: "testkey.avbpubkey",
3163 private_key: "testkey.pem",
3164 }
3165 `)
3166}
3167
3168func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3169 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3170 apex {
3171 name: "myapex",
3172 key: "myapex.key",
3173 use_vendor: true,
3174 uses: ["commonapex"],
3175 }
3176
3177 apex {
3178 name: "commonapex",
3179 key: "myapex.key",
3180 provide_cpp_shared_libs: true,
3181 }
3182
3183 apex_key {
3184 name: "myapex.key",
3185 public_key: "testkey.avbpubkey",
3186 private_key: "testkey.pem",
3187 }
Jooyung Handc782442019-11-01 03:14:38 +09003188 `, func(fs map[string][]byte, config android.Config) {
3189 setUseVendorWhitelistForTest(config, []string{"myapex"})
3190 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003191}
3192
Jooyung Hand48f3c32019-08-23 11:18:57 +09003193func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3194 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3195 apex {
3196 name: "myapex",
3197 key: "myapex.key",
3198 native_shared_libs: ["libfoo"],
3199 }
3200
3201 apex_key {
3202 name: "myapex.key",
3203 public_key: "testkey.avbpubkey",
3204 private_key: "testkey.pem",
3205 }
3206
3207 cc_library {
3208 name: "libfoo",
3209 stl: "none",
3210 system_shared_libs: [],
3211 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003212 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003213 }
3214 `)
3215 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3216 apex {
3217 name: "myapex",
3218 key: "myapex.key",
3219 java_libs: ["myjar"],
3220 }
3221
3222 apex_key {
3223 name: "myapex.key",
3224 public_key: "testkey.avbpubkey",
3225 private_key: "testkey.pem",
3226 }
3227
3228 java_library {
3229 name: "myjar",
3230 srcs: ["foo/bar/MyClass.java"],
3231 sdk_version: "none",
3232 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003233 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003234 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003235 }
3236 `)
3237}
3238
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003239func TestApexWithApps(t *testing.T) {
3240 ctx, _ := testApex(t, `
3241 apex {
3242 name: "myapex",
3243 key: "myapex.key",
3244 apps: [
3245 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003246 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003247 ],
3248 }
3249
3250 apex_key {
3251 name: "myapex.key",
3252 public_key: "testkey.avbpubkey",
3253 private_key: "testkey.pem",
3254 }
3255
3256 android_app {
3257 name: "AppFoo",
3258 srcs: ["foo/bar/MyClass.java"],
3259 sdk_version: "none",
3260 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003261 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003262 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003263 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003264
3265 android_app {
3266 name: "AppFooPriv",
3267 srcs: ["foo/bar/MyClass.java"],
3268 sdk_version: "none",
3269 system_modules: "none",
3270 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003271 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003272 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003273
3274 cc_library_shared {
3275 name: "libjni",
3276 srcs: ["mylib.cpp"],
Jooyung Han65041792020-02-25 16:59:29 +09003277 shared_libs: ["libfoo"],
Jiyong Park8be103b2019-11-08 15:53:48 +09003278 stl: "none",
3279 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003280 apex_available: [ "myapex" ],
Jooyung Han65041792020-02-25 16:59:29 +09003281 sdk_version: "current",
3282 }
3283
3284 cc_library_shared {
3285 name: "libfoo",
3286 stl: "none",
3287 system_shared_libs: [],
3288 apex_available: [ "myapex" ],
3289 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003290 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003291 `)
3292
Sundong Ahnabb64432019-10-22 13:58:29 +09003293 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003294 apexRule := module.Rule("apexRule")
3295 copyCmds := apexRule.Args["copy_commands"]
3296
3297 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003298 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003299
Jooyung Han65041792020-02-25 16:59:29 +09003300 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3301 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003302 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Han65041792020-02-25 16:59:29 +09003303 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003304 }
Jooyung Han65041792020-02-25 16:59:29 +09003305 // JNI libraries including transitive deps are
3306 for _, jni := range []string{"libjni", "libfoo"} {
3307 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
3308 // ... embedded inside APK (jnilibs.zip)
3309 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3310 // ... and not directly inside the APEX
3311 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3312 }
Dario Frenicde2a032019-10-27 00:29:22 +01003313}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003314
Dario Frenicde2a032019-10-27 00:29:22 +01003315func TestApexWithAppImports(t *testing.T) {
3316 ctx, _ := testApex(t, `
3317 apex {
3318 name: "myapex",
3319 key: "myapex.key",
3320 apps: [
3321 "AppFooPrebuilt",
3322 "AppFooPrivPrebuilt",
3323 ],
3324 }
3325
3326 apex_key {
3327 name: "myapex.key",
3328 public_key: "testkey.avbpubkey",
3329 private_key: "testkey.pem",
3330 }
3331
3332 android_app_import {
3333 name: "AppFooPrebuilt",
3334 apk: "PrebuiltAppFoo.apk",
3335 presigned: true,
3336 dex_preopt: {
3337 enabled: false,
3338 },
3339 }
3340
3341 android_app_import {
3342 name: "AppFooPrivPrebuilt",
3343 apk: "PrebuiltAppFooPriv.apk",
3344 privileged: true,
3345 presigned: true,
3346 dex_preopt: {
3347 enabled: false,
3348 },
3349 }
3350 `)
3351
Sundong Ahnabb64432019-10-22 13:58:29 +09003352 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003353 apexRule := module.Rule("apexRule")
3354 copyCmds := apexRule.Args["copy_commands"]
3355
3356 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3357 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003358}
3359
Dario Freni6f3937c2019-12-20 22:58:03 +00003360func TestApexWithTestHelperApp(t *testing.T) {
3361 ctx, _ := testApex(t, `
3362 apex {
3363 name: "myapex",
3364 key: "myapex.key",
3365 apps: [
3366 "TesterHelpAppFoo",
3367 ],
3368 }
3369
3370 apex_key {
3371 name: "myapex.key",
3372 public_key: "testkey.avbpubkey",
3373 private_key: "testkey.pem",
3374 }
3375
3376 android_test_helper_app {
3377 name: "TesterHelpAppFoo",
3378 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003379 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003380 }
3381
3382 `)
3383
3384 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3385 apexRule := module.Rule("apexRule")
3386 copyCmds := apexRule.Args["copy_commands"]
3387
3388 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3389}
3390
Jooyung Han18020ea2019-11-13 10:50:48 +09003391func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3392 // libfoo's apex_available comes from cc_defaults
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003393 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003394 apex {
3395 name: "myapex",
3396 key: "myapex.key",
3397 native_shared_libs: ["libfoo"],
3398 }
3399
3400 apex_key {
3401 name: "myapex.key",
3402 public_key: "testkey.avbpubkey",
3403 private_key: "testkey.pem",
3404 }
3405
3406 apex {
3407 name: "otherapex",
3408 key: "myapex.key",
3409 native_shared_libs: ["libfoo"],
3410 }
3411
3412 cc_defaults {
3413 name: "libfoo-defaults",
3414 apex_available: ["otherapex"],
3415 }
3416
3417 cc_library {
3418 name: "libfoo",
3419 defaults: ["libfoo-defaults"],
3420 stl: "none",
3421 system_shared_libs: [],
3422 }`)
3423}
3424
Jiyong Park127b40b2019-09-30 16:04:35 +09003425func TestApexAvailable(t *testing.T) {
3426 // libfoo is not available to myapex, but only to otherapex
3427 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3428 apex {
3429 name: "myapex",
3430 key: "myapex.key",
3431 native_shared_libs: ["libfoo"],
3432 }
3433
3434 apex_key {
3435 name: "myapex.key",
3436 public_key: "testkey.avbpubkey",
3437 private_key: "testkey.pem",
3438 }
3439
3440 apex {
3441 name: "otherapex",
3442 key: "otherapex.key",
3443 native_shared_libs: ["libfoo"],
3444 }
3445
3446 apex_key {
3447 name: "otherapex.key",
3448 public_key: "testkey.avbpubkey",
3449 private_key: "testkey.pem",
3450 }
3451
3452 cc_library {
3453 name: "libfoo",
3454 stl: "none",
3455 system_shared_libs: [],
3456 apex_available: ["otherapex"],
3457 }`)
3458
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003459 // libbbaz is an indirect dep
3460 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003461 apex {
3462 name: "myapex",
3463 key: "myapex.key",
3464 native_shared_libs: ["libfoo"],
3465 }
3466
3467 apex_key {
3468 name: "myapex.key",
3469 public_key: "testkey.avbpubkey",
3470 private_key: "testkey.pem",
3471 }
3472
Jiyong Park127b40b2019-09-30 16:04:35 +09003473 cc_library {
3474 name: "libfoo",
3475 stl: "none",
3476 shared_libs: ["libbar"],
3477 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003478 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003479 }
3480
3481 cc_library {
3482 name: "libbar",
3483 stl: "none",
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003484 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003485 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003486 apex_available: ["myapex"],
3487 }
3488
3489 cc_library {
3490 name: "libbaz",
3491 stl: "none",
3492 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003493 }`)
3494
3495 testApexError(t, "\"otherapex\" is not a valid module name", `
3496 apex {
3497 name: "myapex",
3498 key: "myapex.key",
3499 native_shared_libs: ["libfoo"],
3500 }
3501
3502 apex_key {
3503 name: "myapex.key",
3504 public_key: "testkey.avbpubkey",
3505 private_key: "testkey.pem",
3506 }
3507
3508 cc_library {
3509 name: "libfoo",
3510 stl: "none",
3511 system_shared_libs: [],
3512 apex_available: ["otherapex"],
3513 }`)
3514
3515 ctx, _ := testApex(t, `
3516 apex {
3517 name: "myapex",
3518 key: "myapex.key",
3519 native_shared_libs: ["libfoo", "libbar"],
3520 }
3521
3522 apex_key {
3523 name: "myapex.key",
3524 public_key: "testkey.avbpubkey",
3525 private_key: "testkey.pem",
3526 }
3527
3528 cc_library {
3529 name: "libfoo",
3530 stl: "none",
3531 system_shared_libs: [],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003532 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003533 apex_available: ["myapex"],
3534 }
3535
3536 cc_library {
3537 name: "libbar",
3538 stl: "none",
3539 system_shared_libs: [],
3540 apex_available: ["//apex_available:anyapex"],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003541 }
3542
3543 cc_library {
3544 name: "libbaz",
3545 stl: "none",
3546 system_shared_libs: [],
3547 stubs: {
3548 versions: ["10", "20", "30"],
3549 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003550 }`)
3551
3552 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003553 // TODO(jiyong) the checks for the platform variant are removed because we now create
3554 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3555 // the platform variants are not used from other platform modules. When that is done,
3556 // these checks will be replaced by expecting a specific error message that will be
3557 // emitted when the platform variant is used.
3558 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3559 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3560 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3561 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003562
3563 ctx, _ = testApex(t, `
3564 apex {
3565 name: "myapex",
3566 key: "myapex.key",
3567 }
3568
3569 apex_key {
3570 name: "myapex.key",
3571 public_key: "testkey.avbpubkey",
3572 private_key: "testkey.pem",
3573 }
3574
3575 cc_library {
3576 name: "libfoo",
3577 stl: "none",
3578 system_shared_libs: [],
3579 apex_available: ["//apex_available:platform"],
3580 }`)
3581
3582 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003583 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3584 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003585
3586 ctx, _ = testApex(t, `
3587 apex {
3588 name: "myapex",
3589 key: "myapex.key",
3590 native_shared_libs: ["libfoo"],
3591 }
3592
3593 apex_key {
3594 name: "myapex.key",
3595 public_key: "testkey.avbpubkey",
3596 private_key: "testkey.pem",
3597 }
3598
3599 cc_library {
3600 name: "libfoo",
3601 stl: "none",
3602 system_shared_libs: [],
3603 apex_available: ["myapex"],
3604 static: {
3605 apex_available: ["//apex_available:platform"],
3606 },
3607 }`)
3608
3609 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003610 // TODO(jiyong) the checks for the platform variant are removed because we now create
3611 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3612 // the platform variants are not used from other platform modules. When that is done,
3613 // these checks will be replaced by expecting a specific error message that will be
3614 // emitted when the platform variant is used.
3615 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3616 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3617 // // but the static variant is available to both myapex and the platform
3618 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3619 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003620}
3621
Jiyong Park5d790c32019-11-15 18:40:32 +09003622func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003623 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003624 apex {
3625 name: "myapex",
3626 key: "myapex.key",
3627 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003628 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003629 }
3630
3631 override_apex {
3632 name: "override_myapex",
3633 base: "myapex",
3634 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003635 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003636 logging_parent: "com.foo.bar",
Baligh Uddincb6aa122020-03-15 13:01:05 -07003637 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09003638 }
3639
3640 apex_key {
3641 name: "myapex.key",
3642 public_key: "testkey.avbpubkey",
3643 private_key: "testkey.pem",
3644 }
3645
3646 android_app {
3647 name: "app",
3648 srcs: ["foo/bar/MyClass.java"],
3649 package_name: "foo",
3650 sdk_version: "none",
3651 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003652 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003653 }
3654
3655 override_android_app {
3656 name: "override_app",
3657 base: "app",
3658 package_name: "bar",
3659 }
Jiyong Parka519c542020-03-03 11:45:41 +09003660 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003661
Jiyong Park317645e2019-12-05 13:20:58 +09003662 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3663 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3664 if originalVariant.GetOverriddenBy() != "" {
3665 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3666 }
3667 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3668 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3669 }
3670
Jiyong Park5d790c32019-11-15 18:40:32 +09003671 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3672 apexRule := module.Rule("apexRule")
3673 copyCmds := apexRule.Args["copy_commands"]
3674
3675 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3676 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003677
3678 apexBundle := module.Module().(*apexBundle)
3679 name := apexBundle.Name()
3680 if name != "override_myapex" {
3681 t.Errorf("name should be \"override_myapex\", but was %q", name)
3682 }
3683
Baligh Uddin004d7172020-02-19 21:29:28 -08003684 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3685 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3686 }
3687
Jiyong Parka519c542020-03-03 11:45:41 +09003688 optFlags := apexRule.Args["opt_flags"]
Baligh Uddincb6aa122020-03-15 13:01:05 -07003689 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Parka519c542020-03-03 11:45:41 +09003690
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003691 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3692 var builder strings.Builder
3693 data.Custom(&builder, name, "TARGET_", "", data)
3694 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003695 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003696 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3697 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003698 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003699 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003700 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003701 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3702 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003703}
3704
Jooyung Han214bf372019-11-12 13:03:50 +09003705func TestLegacyAndroid10Support(t *testing.T) {
3706 ctx, _ := testApex(t, `
3707 apex {
3708 name: "myapex",
3709 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003710 native_shared_libs: ["mylib"],
Jooyung Han23b0adf2020-03-12 18:37:20 +09003711 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09003712 }
3713
3714 apex_key {
3715 name: "myapex.key",
3716 public_key: "testkey.avbpubkey",
3717 private_key: "testkey.pem",
3718 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003719
3720 cc_library {
3721 name: "mylib",
3722 srcs: ["mylib.cpp"],
3723 stl: "libc++",
3724 system_shared_libs: [],
3725 apex_available: [ "myapex" ],
3726 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003727 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003728
3729 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3730 args := module.Rule("apexRule").Args
3731 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003732 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003733
3734 // The copies of the libraries in the apex should have one more dependency than
3735 // the ones outside the apex, namely the unwinder. Ideally we should check
3736 // the dependency names directly here but for some reason the names are blank in
3737 // this test.
3738 for _, lib := range []string{"libc++", "mylib"} {
3739 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3740 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3741 if len(apexImplicits) != len(nonApexImplicits)+1 {
3742 t.Errorf("%q missing unwinder dep", lib)
3743 }
3744 }
Jooyung Han214bf372019-11-12 13:03:50 +09003745}
3746
Jooyung Han58f26ab2019-12-18 15:34:32 +09003747func TestJavaSDKLibrary(t *testing.T) {
3748 ctx, _ := testApex(t, `
3749 apex {
3750 name: "myapex",
3751 key: "myapex.key",
3752 java_libs: ["foo"],
3753 }
3754
3755 apex_key {
3756 name: "myapex.key",
3757 public_key: "testkey.avbpubkey",
3758 private_key: "testkey.pem",
3759 }
3760
3761 java_sdk_library {
3762 name: "foo",
3763 srcs: ["a.java"],
3764 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003765 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003766 }
3767 `, withFiles(map[string][]byte{
3768 "api/current.txt": nil,
3769 "api/removed.txt": nil,
3770 "api/system-current.txt": nil,
3771 "api/system-removed.txt": nil,
3772 "api/test-current.txt": nil,
3773 "api/test-removed.txt": nil,
3774 }))
3775
3776 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003777 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003778 "javalib/foo.jar",
3779 "etc/permissions/foo.xml",
3780 })
3781 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003782 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3783 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003784}
3785
atrost6e126252020-01-27 17:01:16 +00003786func TestCompatConfig(t *testing.T) {
3787 ctx, _ := testApex(t, `
3788 apex {
3789 name: "myapex",
3790 key: "myapex.key",
3791 prebuilts: ["myjar-platform-compat-config"],
3792 java_libs: ["myjar"],
3793 }
3794
3795 apex_key {
3796 name: "myapex.key",
3797 public_key: "testkey.avbpubkey",
3798 private_key: "testkey.pem",
3799 }
3800
3801 platform_compat_config {
3802 name: "myjar-platform-compat-config",
3803 src: ":myjar",
3804 }
3805
3806 java_library {
3807 name: "myjar",
3808 srcs: ["foo/bar/MyClass.java"],
3809 sdk_version: "none",
3810 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003811 apex_available: [ "myapex" ],
3812 }
3813 `)
3814 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3815 "etc/compatconfig/myjar-platform-compat-config.xml",
3816 "javalib/myjar.jar",
3817 })
3818}
3819
Jiyong Park479321d2019-12-16 11:47:12 +09003820func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3821 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3822 apex {
3823 name: "myapex",
3824 key: "myapex.key",
3825 java_libs: ["myjar"],
3826 }
3827
3828 apex_key {
3829 name: "myapex.key",
3830 public_key: "testkey.avbpubkey",
3831 private_key: "testkey.pem",
3832 }
3833
3834 java_library {
3835 name: "myjar",
3836 srcs: ["foo/bar/MyClass.java"],
3837 sdk_version: "none",
3838 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003839 compile_dex: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003840 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003841 }
3842 `)
3843}
3844
Jiyong Park7afd1072019-12-30 16:56:33 +09003845func TestCarryRequiredModuleNames(t *testing.T) {
3846 ctx, config := testApex(t, `
3847 apex {
3848 name: "myapex",
3849 key: "myapex.key",
3850 native_shared_libs: ["mylib"],
3851 }
3852
3853 apex_key {
3854 name: "myapex.key",
3855 public_key: "testkey.avbpubkey",
3856 private_key: "testkey.pem",
3857 }
3858
3859 cc_library {
3860 name: "mylib",
3861 srcs: ["mylib.cpp"],
3862 system_shared_libs: [],
3863 stl: "none",
3864 required: ["a", "b"],
3865 host_required: ["c", "d"],
3866 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003867 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003868 }
3869 `)
3870
3871 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3872 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3873 name := apexBundle.BaseModuleName()
3874 prefix := "TARGET_"
3875 var builder strings.Builder
3876 data.Custom(&builder, name, prefix, "", data)
3877 androidMk := builder.String()
3878 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3879 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3880 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3881}
3882
Jiyong Park7cd10e32020-01-14 09:22:18 +09003883func TestSymlinksFromApexToSystem(t *testing.T) {
3884 bp := `
3885 apex {
3886 name: "myapex",
3887 key: "myapex.key",
3888 native_shared_libs: ["mylib"],
3889 java_libs: ["myjar"],
3890 }
3891
Jiyong Park9d677202020-02-19 16:29:35 +09003892 apex {
3893 name: "myapex.updatable",
3894 key: "myapex.key",
3895 native_shared_libs: ["mylib"],
3896 java_libs: ["myjar"],
3897 updatable: true,
3898 }
3899
Jiyong Park7cd10e32020-01-14 09:22:18 +09003900 apex_key {
3901 name: "myapex.key",
3902 public_key: "testkey.avbpubkey",
3903 private_key: "testkey.pem",
3904 }
3905
3906 cc_library {
3907 name: "mylib",
3908 srcs: ["mylib.cpp"],
3909 shared_libs: ["myotherlib"],
3910 system_shared_libs: [],
3911 stl: "none",
3912 apex_available: [
3913 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003914 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003915 "//apex_available:platform",
3916 ],
3917 }
3918
3919 cc_library {
3920 name: "myotherlib",
3921 srcs: ["mylib.cpp"],
3922 system_shared_libs: [],
3923 stl: "none",
3924 apex_available: [
3925 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003926 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003927 "//apex_available:platform",
3928 ],
3929 }
3930
3931 java_library {
3932 name: "myjar",
3933 srcs: ["foo/bar/MyClass.java"],
3934 sdk_version: "none",
3935 system_modules: "none",
3936 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003937 apex_available: [
3938 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003939 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003940 "//apex_available:platform",
3941 ],
3942 }
3943
3944 java_library {
3945 name: "myotherjar",
3946 srcs: ["foo/bar/MyClass.java"],
3947 sdk_version: "none",
3948 system_modules: "none",
3949 apex_available: [
3950 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003951 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003952 "//apex_available:platform",
3953 ],
3954 }
3955 `
3956
3957 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3958 for _, f := range files {
3959 if f.path == file {
3960 if f.isLink {
3961 t.Errorf("%q is not a real file", file)
3962 }
3963 return
3964 }
3965 }
3966 t.Errorf("%q is not found", file)
3967 }
3968
3969 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3970 for _, f := range files {
3971 if f.path == file {
3972 if !f.isLink {
3973 t.Errorf("%q is not a symlink", file)
3974 }
3975 return
3976 }
3977 }
3978 t.Errorf("%q is not found", file)
3979 }
3980
Jiyong Park9d677202020-02-19 16:29:35 +09003981 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3982 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003983 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003984 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003985 ensureRealfileExists(t, files, "javalib/myjar.jar")
3986 ensureRealfileExists(t, files, "lib64/mylib.so")
3987 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3988
Jiyong Park9d677202020-02-19 16:29:35 +09003989 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3990 ensureRealfileExists(t, files, "javalib/myjar.jar")
3991 ensureRealfileExists(t, files, "lib64/mylib.so")
3992 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3993
3994 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003995 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003996 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003997 ensureRealfileExists(t, files, "javalib/myjar.jar")
3998 ensureRealfileExists(t, files, "lib64/mylib.so")
3999 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004000
4001 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4002 ensureRealfileExists(t, files, "javalib/myjar.jar")
4003 ensureRealfileExists(t, files, "lib64/mylib.so")
4004 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004005}
4006
Jiyong Parkd93e1b12020-02-28 15:22:21 +09004007func TestAppBundle(t *testing.T) {
4008 ctx, _ := testApex(t, `
4009 apex {
4010 name: "myapex",
4011 key: "myapex.key",
4012 apps: ["AppFoo"],
4013 }
4014
4015 apex_key {
4016 name: "myapex.key",
4017 public_key: "testkey.avbpubkey",
4018 private_key: "testkey.pem",
4019 }
4020
4021 android_app {
4022 name: "AppFoo",
4023 srcs: ["foo/bar/MyClass.java"],
4024 sdk_version: "none",
4025 system_modules: "none",
4026 apex_available: [ "myapex" ],
4027 }
Jiyong Parkaf8998c2020-02-28 16:51:07 +09004028 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkd93e1b12020-02-28 15:22:21 +09004029
4030 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4031 content := bundleConfigRule.Args["content"]
4032
4033 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkaf8998c2020-02-28 16:51:07 +09004034 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 +09004035}
4036
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004037func TestMain(m *testing.M) {
4038 run := func() int {
4039 setUp()
4040 defer tearDown()
4041
4042 return m.Run()
4043 }
4044
4045 os.Exit(run())
4046}