blob: 716e263e10ee1736c27ce392c9012a613077d02e [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"
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +000030 "android/soong/dexpreopt"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090031 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090032)
33
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070034var buildDir string
35
Jooyung Hand3639552019-08-09 12:57:43 +090036// names returns name list from white space separated string
37func names(s string) (ns []string) {
38 for _, n := range strings.Split(s, " ") {
39 if len(n) > 0 {
40 ns = append(ns, n)
41 }
42 }
43 return
44}
45
Jooyung Han344d5432019-08-23 11:17:39 +090046func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
47 t.Helper()
48 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090049 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
50 if len(errs) > 0 {
51 android.FailIfNoMatchingErrors(t, pattern, errs)
52 return
53 }
54 _, errs = ctx.PrepareBuildActions(config)
55 if len(errs) > 0 {
56 android.FailIfNoMatchingErrors(t, pattern, errs)
57 return
58 }
59
60 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
61}
62
Jooyung Han344d5432019-08-23 11:17:39 +090063func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
64 t.Helper()
65 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090066 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
67 android.FailIfErrored(t, errs)
68 _, errs = ctx.PrepareBuildActions(config)
69 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070070 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090071}
72
Jooyung Han344d5432019-08-23 11:17:39 +090073type testCustomizer func(fs map[string][]byte, config android.Config)
74
75func withFiles(files map[string][]byte) testCustomizer {
76 return func(fs map[string][]byte, config android.Config) {
77 for k, v := range files {
78 fs[k] = v
79 }
80 }
81}
82
83func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
84 return func(fs map[string][]byte, config android.Config) {
85 for k, v := range targets {
86 config.Targets[k] = v
87 }
88 }
89}
90
Jiyong Parkaf8998c2020-02-28 16:51:07 +090091func withManifestPackageNameOverrides(specs []string) testCustomizer {
92 return func(fs map[string][]byte, config android.Config) {
93 config.TestProductVariables.ManifestPackageNameOverrides = specs
94 }
95}
96
Jooyung Han31c470b2019-10-18 16:26:59 +090097func withBinder32bit(fs map[string][]byte, config android.Config) {
98 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
99}
100
Jiyong Park7cd10e32020-01-14 09:22:18 +0900101func withUnbundledBuild(fs map[string][]byte, config android.Config) {
102 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
103}
104
Jooyung Han344d5432019-08-23 11:17:39 +0900105func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +0900106 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900107
108 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900109 filegroup {
110 name: "myapex-file_contexts",
111 srcs: [
112 "system/sepolicy/apex/myapex-file_contexts",
113 ],
114 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900115 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800116
Colin Crossf9aabd72020-02-15 11:29:50 -0800117 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
118
Dario Frenicde2a032019-10-27 00:29:22 +0100119 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900120
Jooyung Han344d5432019-08-23 11:17:39 +0900121 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900122 "a.java": nil,
123 "PrebuiltAppFoo.apk": nil,
124 "PrebuiltAppFooPriv.apk": nil,
125 "build/make/target/product/security": nil,
126 "apex_manifest.json": nil,
127 "AndroidManifest.xml": nil,
128 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900129 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900130 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900131 "system/sepolicy/apex/otherapex-file_contexts": nil,
132 "system/sepolicy/apex/commonapex-file_contexts": nil,
133 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800134 "mylib.cpp": nil,
135 "mylib_common.cpp": nil,
136 "mytest.cpp": nil,
137 "mytest1.cpp": nil,
138 "mytest2.cpp": nil,
139 "mytest3.cpp": nil,
140 "myprebuilt": nil,
141 "my_include": nil,
142 "foo/bar/MyClass.java": nil,
143 "prebuilt.jar": nil,
144 "vendor/foo/devkeys/test.x509.pem": nil,
145 "vendor/foo/devkeys/test.pk8": nil,
146 "testkey.x509.pem": nil,
147 "testkey.pk8": nil,
148 "testkey.override.x509.pem": nil,
149 "testkey.override.pk8": nil,
150 "vendor/foo/devkeys/testkey.avbpubkey": nil,
151 "vendor/foo/devkeys/testkey.pem": nil,
152 "NOTICE": nil,
153 "custom_notice": nil,
Jiyong Park162e8442020-03-17 19:16:40 +0900154 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800155 "testkey2.avbpubkey": nil,
156 "testkey2.pem": nil,
157 "myapex-arm64.apex": nil,
158 "myapex-arm.apex": nil,
159 "frameworks/base/api/current.txt": nil,
160 "framework/aidl/a.aidl": nil,
161 "build/make/core/proguard.flags": nil,
162 "build/make/core/proguard_basic_keeps.flags": nil,
163 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900164 }
165
Colin Crossf9aabd72020-02-15 11:29:50 -0800166 cc.GatherRequiredFilesForTest(fs)
167
Jooyung Han344d5432019-08-23 11:17:39 +0900168 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800169 // The fs now needs to be populated before creating the config, call handlers twice
170 // for now, once to get any fs changes, and later after the config was created to
171 // set product variables or targets.
172 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
173 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900174 }
175
Colin Cross98be1bb2019-12-13 20:41:13 -0800176 config := android.TestArchConfig(buildDir, nil, bp, fs)
177 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
178 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
179 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
180 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
181 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
182 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
183
184 for _, handler := range handlers {
185 // The fs now needs to be populated before creating the config, call handlers twice
186 // for now, earlier to get any fs changes, and now after the config was created to
187 // set product variables or targets.
188 tempFS := map[string][]byte{}
189 handler(tempFS, config)
190 }
191
192 ctx := android.NewTestArchContext()
193 ctx.RegisterModuleType("apex", BundleFactory)
194 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
195 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
196 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
197 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
198 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
199 ctx.RegisterModuleType("override_apex", overrideApexFactory)
200
Jooyung Hana57af4a2020-01-23 05:36:59 +0000201 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
202 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
203
Paul Duffin77980a82019-12-19 16:01:36 +0000204 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800205 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800206 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
207 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800208 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000209 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800210 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800211 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000212 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000213 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000214 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900215 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800216
Colin Cross98be1bb2019-12-13 20:41:13 -0800217 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800218 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800219
220 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900221
Jooyung Han5c998b92019-06-27 11:30:33 +0900222 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900223}
224
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700225func setUp() {
226 var err error
227 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900228 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700229 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900230 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900231}
232
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700233func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900234 os.RemoveAll(buildDir)
235}
236
237// ensure that 'result' contains 'expected'
238func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900239 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900240 if !strings.Contains(result, expected) {
241 t.Errorf("%q is not found in %q", expected, result)
242 }
243}
244
245// ensures that 'result' does not contain 'notExpected'
246func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900247 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900248 if strings.Contains(result, notExpected) {
249 t.Errorf("%q is found in %q", notExpected, result)
250 }
251}
252
253func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900254 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 if !android.InList(expected, result) {
256 t.Errorf("%q is not found in %v", expected, result)
257 }
258}
259
260func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if android.InList(notExpected, result) {
263 t.Errorf("%q is found in %v", notExpected, result)
264 }
265}
266
Jooyung Hane1633032019-08-01 17:41:43 +0900267func ensureListEmpty(t *testing.T, result []string) {
268 t.Helper()
269 if len(result) > 0 {
270 t.Errorf("%q is expected to be empty", result)
271 }
272}
273
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274// Minimal test
275func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700276 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900277 apex_defaults {
278 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900279 manifest: ":myapex.manifest",
280 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281 key: "myapex.key",
282 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800283 multilib: {
284 both: {
285 binaries: ["foo",],
286 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900287 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900288 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900289 }
290
Jiyong Park30ca9372019-02-07 16:27:23 +0900291 apex {
292 name: "myapex",
293 defaults: ["myapex-defaults"],
294 }
295
Jiyong Park25fc6a92018-11-18 18:02:45 +0900296 apex_key {
297 name: "myapex.key",
298 public_key: "testkey.avbpubkey",
299 private_key: "testkey.pem",
300 }
301
Jiyong Park809bb722019-02-13 21:33:49 +0900302 filegroup {
303 name: "myapex.manifest",
304 srcs: ["apex_manifest.json"],
305 }
306
307 filegroup {
308 name: "myapex.androidmanifest",
309 srcs: ["AndroidManifest.xml"],
310 }
311
Jiyong Park25fc6a92018-11-18 18:02:45 +0900312 cc_library {
313 name: "mylib",
314 srcs: ["mylib.cpp"],
315 shared_libs: ["mylib2"],
316 system_shared_libs: [],
317 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000318 // TODO: remove //apex_available:platform
319 apex_available: [
320 "//apex_available:platform",
321 "myapex",
322 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
324
Alex Light3d673592019-01-18 14:37:31 -0800325 cc_binary {
326 name: "foo",
327 srcs: ["mylib.cpp"],
328 compile_multilib: "both",
329 multilib: {
330 lib32: {
331 suffix: "32",
332 },
333 lib64: {
334 suffix: "64",
335 },
336 },
337 symlinks: ["foo_link_"],
338 symlink_preferred_arch: true,
339 system_shared_libs: [],
340 static_executable: true,
341 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000342 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800343 }
344
Jiyong Park25fc6a92018-11-18 18:02:45 +0900345 cc_library {
346 name: "mylib2",
347 srcs: ["mylib.cpp"],
348 system_shared_libs: [],
349 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900350 notice: "custom_notice",
Jiyong Park162e8442020-03-17 19:16:40 +0900351 static_libs: ["libstatic"],
352 // TODO: remove //apex_available:platform
353 apex_available: [
354 "//apex_available:platform",
355 "myapex",
356 ],
357 }
358
359 cc_library_static {
360 name: "libstatic",
361 srcs: ["mylib.cpp"],
362 system_shared_libs: [],
363 stl: "none",
364 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000365 // TODO: remove //apex_available:platform
366 apex_available: [
367 "//apex_available:platform",
368 "myapex",
369 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900370 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900371
372 java_library {
373 name: "myjar",
374 srcs: ["foo/bar/MyClass.java"],
375 sdk_version: "none",
376 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900377 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900378 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000379 // TODO: remove //apex_available:platform
380 apex_available: [
381 "//apex_available:platform",
382 "myapex",
383 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900384 }
385
386 java_library {
387 name: "myotherjar",
388 srcs: ["foo/bar/MyClass.java"],
389 sdk_version: "none",
390 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900391 // TODO: remove //apex_available:platform
392 apex_available: [
393 "//apex_available:platform",
394 "myapex",
395 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900396 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900397
398 java_library {
399 name: "mysharedjar",
400 srcs: ["foo/bar/MyClass.java"],
401 sdk_version: "none",
402 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900403 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900404 `)
405
Sundong Ahnabb64432019-10-22 13:58:29 +0900406 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900407
408 optFlags := apexRule.Args["opt_flags"]
409 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700410 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900411 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900412
Jiyong Park25fc6a92018-11-18 18:02:45 +0900413 copyCmds := apexRule.Args["copy_commands"]
414
415 // Ensure that main rule creates an output
416 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
417
418 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800419 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900420 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900421
422 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800423 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900424 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900425
426 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800427 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
428 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900429 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
430 // .. but not for java libs
431 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900432 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800433
Colin Cross7113d202019-11-20 16:39:12 -0800434 // Ensure that the platform variant ends with _shared or _common
435 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
436 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900437 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
438 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900439 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
440
441 // Ensure that dynamic dependency to java libs are not included
442 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800443
444 // Ensure that all symlinks are present.
445 found_foo_link_64 := false
446 found_foo := false
447 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900448 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800449 if strings.HasSuffix(cmd, "bin/foo") {
450 found_foo = true
451 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
452 found_foo_link_64 = true
453 }
454 }
455 }
456 good := found_foo && found_foo_link_64
457 if !good {
458 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
459 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900460
Sundong Ahnabb64432019-10-22 13:58:29 +0900461 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700462 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park162e8442020-03-17 19:16:40 +0900463 if len(noticeInputs) != 3 {
464 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900465 }
466 ensureListContains(t, noticeInputs, "NOTICE")
467 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park162e8442020-03-17 19:16:40 +0900468 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900469
470 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 +0900471 ensureListContains(t, depsInfo, "myjar <- myapex")
472 ensureListContains(t, depsInfo, "mylib <- myapex")
473 ensureListContains(t, depsInfo, "mylib2 <- mylib")
474 ensureListContains(t, depsInfo, "myotherjar <- myjar")
475 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800476}
477
Jooyung Hanf21c7972019-12-16 22:32:06 +0900478func TestDefaults(t *testing.T) {
479 ctx, _ := testApex(t, `
480 apex_defaults {
481 name: "myapex-defaults",
482 key: "myapex.key",
483 prebuilts: ["myetc"],
484 native_shared_libs: ["mylib"],
485 java_libs: ["myjar"],
486 apps: ["AppFoo"],
487 }
488
489 prebuilt_etc {
490 name: "myetc",
491 src: "myprebuilt",
492 }
493
494 apex {
495 name: "myapex",
496 defaults: ["myapex-defaults"],
497 }
498
499 apex_key {
500 name: "myapex.key",
501 public_key: "testkey.avbpubkey",
502 private_key: "testkey.pem",
503 }
504
505 cc_library {
506 name: "mylib",
507 system_shared_libs: [],
508 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000509 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900510 }
511
512 java_library {
513 name: "myjar",
514 srcs: ["foo/bar/MyClass.java"],
515 sdk_version: "none",
516 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000517 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900518 }
519
520 android_app {
521 name: "AppFoo",
522 srcs: ["foo/bar/MyClass.java"],
523 sdk_version: "none",
524 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000525 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900526 }
527 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000528 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900529 "etc/myetc",
530 "javalib/myjar.jar",
531 "lib64/mylib.so",
532 "app/AppFoo/AppFoo.apk",
533 })
534}
535
Jooyung Han01a3ee22019-11-02 02:52:25 +0900536func TestApexManifest(t *testing.T) {
537 ctx, _ := testApex(t, `
538 apex {
539 name: "myapex",
540 key: "myapex.key",
541 }
542
543 apex_key {
544 name: "myapex.key",
545 public_key: "testkey.avbpubkey",
546 private_key: "testkey.pem",
547 }
548 `)
549
550 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900551 args := module.Rule("apexRule").Args
552 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
553 t.Error("manifest should be apex_manifest.pb, but " + manifest)
554 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900555}
556
Alex Light5098a612018-11-29 17:12:15 -0800557func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700558 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800559 apex {
560 name: "myapex",
561 key: "myapex.key",
562 payload_type: "zip",
563 native_shared_libs: ["mylib"],
564 }
565
566 apex_key {
567 name: "myapex.key",
568 public_key: "testkey.avbpubkey",
569 private_key: "testkey.pem",
570 }
571
572 cc_library {
573 name: "mylib",
574 srcs: ["mylib.cpp"],
575 shared_libs: ["mylib2"],
576 system_shared_libs: [],
577 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000578 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800579 }
580
581 cc_library {
582 name: "mylib2",
583 srcs: ["mylib.cpp"],
584 system_shared_libs: [],
585 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000586 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800587 }
588 `)
589
Sundong Ahnabb64432019-10-22 13:58:29 +0900590 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800591 copyCmds := zipApexRule.Args["copy_commands"]
592
593 // Ensure that main rule creates an output
594 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
595
596 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800597 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800598
599 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800600 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800601
602 // Ensure that both direct and indirect deps are copied into apex
603 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
604 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900605}
606
607func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700608 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900609 apex {
610 name: "myapex",
611 key: "myapex.key",
612 native_shared_libs: ["mylib", "mylib3"],
613 }
614
615 apex_key {
616 name: "myapex.key",
617 public_key: "testkey.avbpubkey",
618 private_key: "testkey.pem",
619 }
620
621 cc_library {
622 name: "mylib",
623 srcs: ["mylib.cpp"],
624 shared_libs: ["mylib2", "mylib3"],
625 system_shared_libs: [],
626 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000627 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900628 }
629
630 cc_library {
631 name: "mylib2",
632 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900633 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900634 system_shared_libs: [],
635 stl: "none",
636 stubs: {
637 versions: ["1", "2", "3"],
638 },
639 }
640
641 cc_library {
642 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900643 srcs: ["mylib.cpp"],
644 shared_libs: ["mylib4"],
645 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900646 stl: "none",
647 stubs: {
648 versions: ["10", "11", "12"],
649 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000650 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900651 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900652
653 cc_library {
654 name: "mylib4",
655 srcs: ["mylib.cpp"],
656 system_shared_libs: [],
657 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000658 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900659 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900660 `)
661
Sundong Ahnabb64432019-10-22 13:58:29 +0900662 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900663 copyCmds := apexRule.Args["copy_commands"]
664
665 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800666 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900667
668 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800669 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900670
671 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800672 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900673
Colin Cross7113d202019-11-20 16:39:12 -0800674 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900675
676 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900677 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900678 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900679 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900680
681 // 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 -0800682 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900683 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800684 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900685
686 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900687 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900688 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900689
690 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900691 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900692
Jooyung Hana57af4a2020-01-23 05:36:59 +0000693 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900694 "lib64/mylib.so",
695 "lib64/mylib3.so",
696 "lib64/mylib4.so",
697 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900698}
699
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900700func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700701 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900702 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900703 name: "myapex2",
704 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900705 native_shared_libs: ["mylib"],
706 }
707
708 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900709 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900710 public_key: "testkey.avbpubkey",
711 private_key: "testkey.pem",
712 }
713
714 cc_library {
715 name: "mylib",
716 srcs: ["mylib.cpp"],
717 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900718 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900719 system_shared_libs: [],
720 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000721 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900722 }
723
724 cc_library {
725 name: "libfoo",
726 srcs: ["mylib.cpp"],
727 shared_libs: ["libbar"],
728 system_shared_libs: [],
729 stl: "none",
730 stubs: {
731 versions: ["10", "20", "30"],
732 },
733 }
734
735 cc_library {
736 name: "libbar",
737 srcs: ["mylib.cpp"],
738 system_shared_libs: [],
739 stl: "none",
740 }
741
Jiyong Park678c8812020-02-07 17:25:49 +0900742 cc_library_static {
743 name: "libbaz",
744 srcs: ["mylib.cpp"],
745 system_shared_libs: [],
746 stl: "none",
747 apex_available: [ "myapex2" ],
748 }
749
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900750 `)
751
Jiyong Park83dc74b2020-01-14 18:38:44 +0900752 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900753 copyCmds := apexRule.Args["copy_commands"]
754
755 // Ensure that direct non-stubs dep is always included
756 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
757
758 // Ensure that indirect stubs dep is not included
759 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
760
761 // Ensure that dependency of stubs is not included
762 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
763
Jiyong Park83dc74b2020-01-14 18:38:44 +0900764 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900765
766 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900767 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900768 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900769 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900770
Jiyong Park3ff16992019-12-27 14:11:47 +0900771 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900772
773 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
774 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900775
776 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 +0900777
778 ensureListContains(t, depsInfo, "mylib <- myapex2")
779 ensureListContains(t, depsInfo, "libbaz <- mylib")
780 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900781}
782
Jooyung Hand3639552019-08-09 12:57:43 +0900783func TestApexWithRuntimeLibsDependency(t *testing.T) {
784 /*
785 myapex
786 |
787 v (runtime_libs)
788 mylib ------+------> libfoo [provides stub]
789 |
790 `------> libbar
791 */
792 ctx, _ := testApex(t, `
793 apex {
794 name: "myapex",
795 key: "myapex.key",
796 native_shared_libs: ["mylib"],
797 }
798
799 apex_key {
800 name: "myapex.key",
801 public_key: "testkey.avbpubkey",
802 private_key: "testkey.pem",
803 }
804
805 cc_library {
806 name: "mylib",
807 srcs: ["mylib.cpp"],
808 runtime_libs: ["libfoo", "libbar"],
809 system_shared_libs: [],
810 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000811 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900812 }
813
814 cc_library {
815 name: "libfoo",
816 srcs: ["mylib.cpp"],
817 system_shared_libs: [],
818 stl: "none",
819 stubs: {
820 versions: ["10", "20", "30"],
821 },
822 }
823
824 cc_library {
825 name: "libbar",
826 srcs: ["mylib.cpp"],
827 system_shared_libs: [],
828 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000829 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900830 }
831
832 `)
833
Sundong Ahnabb64432019-10-22 13:58:29 +0900834 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900835 copyCmds := apexRule.Args["copy_commands"]
836
837 // Ensure that direct non-stubs dep is always included
838 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
839
840 // Ensure that indirect stubs dep is not included
841 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
842
843 // Ensure that runtime_libs dep in included
844 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
845
Sundong Ahnabb64432019-10-22 13:58:29 +0900846 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900847 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
848 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900849
850}
851
Jooyung Han67a96cd2020-03-13 15:23:36 +0900852func TestApexDependsOnLLNDKTransitively(t *testing.T) {
853 testcases := []struct {
854 name string
855 minSdkVersion string
856 shouldLink string
857 shouldNotLink []string
858 }{
859 {
Jooyung Han74066602020-03-20 04:29:24 +0900860 name: "should link to the latest",
Jooyung Han67a96cd2020-03-13 15:23:36 +0900861 minSdkVersion: "current",
862 shouldLink: "30",
863 shouldNotLink: []string{"29"},
864 },
865 {
866 name: "should link to llndk#29",
867 minSdkVersion: "29",
868 shouldLink: "29",
869 shouldNotLink: []string{"30"},
870 },
871 }
872 for _, tc := range testcases {
873 t.Run(tc.name, func(t *testing.T) {
874 ctx, _ := testApex(t, `
875 apex {
876 name: "myapex",
877 key: "myapex.key",
878 use_vendor: true,
879 native_shared_libs: ["mylib"],
880 min_sdk_version: "`+tc.minSdkVersion+`",
881 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900882
Jooyung Han67a96cd2020-03-13 15:23:36 +0900883 apex_key {
884 name: "myapex.key",
885 public_key: "testkey.avbpubkey",
886 private_key: "testkey.pem",
887 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900888
Jooyung Han67a96cd2020-03-13 15:23:36 +0900889 cc_library {
890 name: "mylib",
891 srcs: ["mylib.cpp"],
892 vendor_available: true,
893 shared_libs: ["libbar"],
894 system_shared_libs: [],
895 stl: "none",
896 apex_available: [ "myapex" ],
897 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900898
Jooyung Han67a96cd2020-03-13 15:23:36 +0900899 cc_library {
900 name: "libbar",
901 srcs: ["mylib.cpp"],
902 system_shared_libs: [],
903 stl: "none",
904 stubs: { versions: ["29","30"] },
905 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900906
Jooyung Han67a96cd2020-03-13 15:23:36 +0900907 llndk_library {
908 name: "libbar",
909 symbol_file: "",
910 }
911 `, func(fs map[string][]byte, config android.Config) {
912 setUseVendorWhitelistForTest(config, []string{"myapex"})
913 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +0900914
Jooyung Han67a96cd2020-03-13 15:23:36 +0900915 // Ensure that LLNDK dep is not included
916 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
917 "lib64/mylib.so",
918 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900919
Jooyung Han67a96cd2020-03-13 15:23:36 +0900920 // Ensure that LLNDK dep is required
921 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
922 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
923 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900924
Jooyung Han67a96cd2020-03-13 15:23:36 +0900925 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
926 ensureContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
927 for _, ver := range tc.shouldNotLink {
928 ensureNotContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
929 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900930
Jooyung Han67a96cd2020-03-13 15:23:36 +0900931 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
932 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
933 })
934 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900935}
936
Jiyong Park25fc6a92018-11-18 18:02:45 +0900937func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700938 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900939 apex {
940 name: "myapex",
941 key: "myapex.key",
942 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
943 }
944
945 apex_key {
946 name: "myapex.key",
947 public_key: "testkey.avbpubkey",
948 private_key: "testkey.pem",
949 }
950
951 cc_library {
952 name: "mylib",
953 srcs: ["mylib.cpp"],
954 shared_libs: ["libdl#27"],
955 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000956 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900957 }
958
959 cc_library_shared {
960 name: "mylib_shared",
961 srcs: ["mylib.cpp"],
962 shared_libs: ["libdl#27"],
963 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000964 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900965 }
966
967 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900968 name: "libBootstrap",
969 srcs: ["mylib.cpp"],
970 stl: "none",
971 bootstrap: true,
972 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900973 `)
974
Sundong Ahnabb64432019-10-22 13:58:29 +0900975 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900976 copyCmds := apexRule.Args["copy_commands"]
977
978 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800979 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900980 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
981 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900982
983 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900984 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900985
Colin Cross7113d202019-11-20 16:39:12 -0800986 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
987 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
988 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900989
990 // For dependency to libc
991 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900992 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900993 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900994 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900995 // ... Cflags from stub is correctly exported to mylib
996 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
997 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
998
999 // For dependency to libm
1000 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001001 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001002 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001003 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001004 // ... and is not compiling with the stub
1005 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1006 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1007
1008 // For dependency to libdl
1009 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001010 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001012 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1013 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001014 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001015 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001016 // ... Cflags from stub is correctly exported to mylib
1017 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1018 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001019
1020 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001021 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1022 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1023 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1024 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001025}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001026
Jooyung Han0c4e0162020-02-26 22:45:42 +09001027func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
1028 // there are three links between liba --> libz
1029 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
1030 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
1031 // 3) (platform) -> liba -> libz : this should be non-stub link
1032 ctx, _ := testApex(t, `
1033 apex {
1034 name: "myapex",
1035 key: "myapex.key",
1036 native_shared_libs: ["libx"],
1037 min_sdk_version: "2",
1038 }
1039
1040 apex {
1041 name: "otherapex",
1042 key: "myapex.key",
1043 native_shared_libs: ["liby"],
1044 min_sdk_version: "3",
1045 }
1046
1047 apex_key {
1048 name: "myapex.key",
1049 public_key: "testkey.avbpubkey",
1050 private_key: "testkey.pem",
1051 }
1052
1053 cc_library {
1054 name: "libx",
1055 shared_libs: ["liba"],
1056 system_shared_libs: [],
1057 stl: "none",
1058 apex_available: [ "myapex" ],
1059 }
1060
1061 cc_library {
1062 name: "liby",
1063 shared_libs: ["liba"],
1064 system_shared_libs: [],
1065 stl: "none",
1066 apex_available: [ "otherapex" ],
1067 }
1068
1069 cc_library {
1070 name: "liba",
1071 shared_libs: ["libz"],
1072 system_shared_libs: [],
1073 stl: "none",
1074 apex_available: [
1075 "//apex_available:anyapex",
1076 "//apex_available:platform",
1077 ],
1078 }
1079
1080 cc_library {
1081 name: "libz",
1082 system_shared_libs: [],
1083 stl: "none",
1084 stubs: {
1085 versions: ["1", "3"],
1086 },
1087 }
1088 `, withUnbundledBuild)
1089
1090 expectLink := func(from, from_variant, to, to_variant string) {
1091 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1092 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1093 }
1094 expectNoLink := func(from, from_variant, to, to_variant string) {
1095 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1096 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1097 }
1098 // platform liba is linked to non-stub version
1099 expectLink("liba", "shared", "libz", "shared")
1100 // liba in myapex is linked to #1
1101 expectLink("liba", "shared_myapex", "libz", "shared_1")
1102 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1103 expectNoLink("liba", "shared_myapex", "libz", "shared")
1104 // liba in otherapex is linked to #3
1105 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1106 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1107 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1108}
1109
Jooyung Han29e91d22020-04-02 01:41:41 +09001110func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
1111 ctx, _ := testApex(t, `
1112 apex {
1113 name: "myapex",
1114 key: "myapex.key",
1115 native_shared_libs: ["libx"],
1116 min_sdk_version: "R",
1117 }
1118
1119 apex_key {
1120 name: "myapex.key",
1121 public_key: "testkey.avbpubkey",
1122 private_key: "testkey.pem",
1123 }
1124
1125 cc_library {
1126 name: "libx",
1127 shared_libs: ["libz"],
1128 system_shared_libs: [],
1129 stl: "none",
1130 apex_available: [ "myapex" ],
1131 }
1132
1133 cc_library {
1134 name: "libz",
1135 system_shared_libs: [],
1136 stl: "none",
1137 stubs: {
1138 versions: ["29", "R"],
1139 },
1140 }
1141 `, func(fs map[string][]byte, config android.Config) {
1142 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
1143 })
1144
1145 expectLink := func(from, from_variant, to, to_variant string) {
1146 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1147 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1148 }
1149 expectNoLink := func(from, from_variant, to, to_variant string) {
1150 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1151 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1152 }
1153 // 9000 is quite a magic number.
1154 // Finalized SDK codenames are mapped as P(28), Q(29), ...
1155 // And, codenames which are not finalized yet(active_codenames + future_codenames) are numbered from 9000, 9001, ...
1156 // to distinguish them from finalized and future_api(10000)
1157 // In this test, "R" is assumed not finalized yet( listed in Platform_version_active_codenames) and translated into 9000
1158 // (refer android/api_levels.go)
1159 expectLink("libx", "shared_myapex", "libz", "shared_9000")
1160 expectNoLink("libx", "shared_myapex", "libz", "shared_29")
1161 expectNoLink("libx", "shared_myapex", "libz", "shared")
1162}
1163
Jooyung Han0c4e0162020-02-26 22:45:42 +09001164func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1165 ctx, _ := testApex(t, `
1166 apex {
1167 name: "myapex",
1168 key: "myapex.key",
1169 native_shared_libs: ["libx"],
1170 }
1171
1172 apex_key {
1173 name: "myapex.key",
1174 public_key: "testkey.avbpubkey",
1175 private_key: "testkey.pem",
1176 }
1177
1178 cc_library {
1179 name: "libx",
1180 shared_libs: ["libz"],
1181 system_shared_libs: [],
1182 stl: "none",
1183 apex_available: [ "myapex" ],
1184 }
1185
1186 cc_library {
1187 name: "libz",
1188 system_shared_libs: [],
1189 stl: "none",
1190 stubs: {
1191 versions: ["1", "2"],
1192 },
1193 }
1194 `)
1195
1196 expectLink := func(from, from_variant, to, to_variant string) {
1197 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1198 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1199 }
1200 expectNoLink := func(from, from_variant, to, to_variant string) {
1201 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1202 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1203 }
1204 expectLink("libx", "shared_myapex", "libz", "shared_2")
1205 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1206 expectNoLink("libx", "shared_myapex", "libz", "shared")
1207}
1208
1209func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1210 ctx, _ := testApex(t, `
1211 apex {
1212 name: "myapex",
1213 key: "myapex.key",
1214 native_shared_libs: ["libx"],
1215 }
1216
1217 apex_key {
1218 name: "myapex.key",
1219 public_key: "testkey.avbpubkey",
1220 private_key: "testkey.pem",
1221 }
1222
1223 cc_library {
1224 name: "libx",
1225 system_shared_libs: [],
1226 stl: "none",
1227 apex_available: [ "myapex" ],
1228 stubs: {
1229 versions: ["1", "2"],
1230 },
1231 }
1232
1233 cc_library {
1234 name: "libz",
1235 shared_libs: ["libx"],
1236 system_shared_libs: [],
1237 stl: "none",
1238 }
1239 `)
1240
1241 expectLink := func(from, from_variant, to, to_variant string) {
1242 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1243 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1244 }
1245 expectNoLink := func(from, from_variant, to, to_variant string) {
1246 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1247 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1248 }
1249 expectLink("libz", "shared", "libx", "shared_2")
1250 expectNoLink("libz", "shared", "libz", "shared_1")
1251 expectNoLink("libz", "shared", "libz", "shared")
1252}
1253
Jooyung Han74066602020-03-20 04:29:24 +09001254func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Jooyung Han0c4e0162020-02-26 22:45:42 +09001255 ctx, _ := testApex(t, `
1256 apex {
1257 name: "myapex",
1258 key: "myapex.key",
1259 native_shared_libs: ["libx"],
1260 min_sdk_version: "29",
1261 }
1262
1263 apex_key {
1264 name: "myapex.key",
1265 public_key: "testkey.avbpubkey",
1266 private_key: "testkey.pem",
1267 }
1268
1269 cc_library {
1270 name: "libx",
1271 shared_libs: ["libbar"],
1272 apex_available: [ "myapex" ],
1273 }
1274
1275 cc_library {
1276 name: "libbar",
1277 stubs: {
1278 versions: ["29", "30"],
1279 },
1280 }
Jooyung Han74066602020-03-20 04:29:24 +09001281 `, func(fs map[string][]byte, config android.Config) {
1282 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1283 })
Jooyung Han0c4e0162020-02-26 22:45:42 +09001284 expectLink := func(from, from_variant, to, to_variant string) {
1285 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1286 libFlags := ld.Args["libFlags"]
1287 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1288 }
Jooyung Han74066602020-03-20 04:29:24 +09001289 expectLink("libx", "shared_hwasan_myapex", "libbar", "shared_30")
Jooyung Han0c4e0162020-02-26 22:45:42 +09001290}
1291
Jooyung Han74066602020-03-20 04:29:24 +09001292func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Jooyung Han0c4e0162020-02-26 22:45:42 +09001293 ctx, _ := testApex(t, `
1294 apex {
1295 name: "myapex",
1296 key: "myapex.key",
1297 native_shared_libs: ["libx"],
1298 min_sdk_version: "29",
1299 }
1300
1301 apex_key {
1302 name: "myapex.key",
1303 public_key: "testkey.avbpubkey",
1304 private_key: "testkey.pem",
1305 }
1306
1307 cc_library {
1308 name: "libx",
1309 apex_available: [ "myapex" ],
1310 }
Jooyung Han74066602020-03-20 04:29:24 +09001311 `)
Jooyung Han0c4e0162020-02-26 22:45:42 +09001312
1313 // ensure apex variant of c++ is linked with static unwinder
1314 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1315 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1316 // note that platform variant is not.
1317 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1318 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Han0c4e0162020-02-26 22:45:42 +09001319}
1320
1321func TestInvalidMinSdkVersion(t *testing.T) {
Jooyung Han74066602020-03-20 04:29:24 +09001322 testApexError(t, `"libz" .*: not found a version\(<=29\)`, `
Jooyung Han0c4e0162020-02-26 22:45:42 +09001323 apex {
1324 name: "myapex",
1325 key: "myapex.key",
1326 native_shared_libs: ["libx"],
1327 min_sdk_version: "29",
1328 }
1329
1330 apex_key {
1331 name: "myapex.key",
1332 public_key: "testkey.avbpubkey",
1333 private_key: "testkey.pem",
1334 }
1335
1336 cc_library {
1337 name: "libx",
1338 shared_libs: ["libz"],
1339 system_shared_libs: [],
1340 stl: "none",
1341 apex_available: [ "myapex" ],
1342 }
1343
1344 cc_library {
1345 name: "libz",
1346 system_shared_libs: [],
1347 stl: "none",
1348 stubs: {
1349 versions: ["30"],
1350 },
1351 }
Jooyung Han74066602020-03-20 04:29:24 +09001352 `)
Jooyung Han0c4e0162020-02-26 22:45:42 +09001353
Jooyung Han29e91d22020-04-02 01:41:41 +09001354 testApexError(t, `"myapex" .*: min_sdk_version: SDK version should be .*`, `
Jooyung Han0c4e0162020-02-26 22:45:42 +09001355 apex {
1356 name: "myapex",
1357 key: "myapex.key",
Jooyung Han29e91d22020-04-02 01:41:41 +09001358 min_sdk_version: "abc",
Jooyung Han0c4e0162020-02-26 22:45:42 +09001359 }
1360
1361 apex_key {
1362 name: "myapex.key",
1363 public_key: "testkey.avbpubkey",
1364 private_key: "testkey.pem",
1365 }
1366 `)
1367}
1368
Jiyong Park7c2ee712018-12-07 00:42:25 +09001369func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001370 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001371 apex {
1372 name: "myapex",
1373 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001374 native_shared_libs: ["mylib"],
1375 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001376 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001377 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001378 }
1379
1380 apex_key {
1381 name: "myapex.key",
1382 public_key: "testkey.avbpubkey",
1383 private_key: "testkey.pem",
1384 }
1385
1386 prebuilt_etc {
1387 name: "myetc",
1388 src: "myprebuilt",
1389 sub_dir: "foo/bar",
1390 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001391
1392 cc_library {
1393 name: "mylib",
1394 srcs: ["mylib.cpp"],
1395 relative_install_path: "foo/bar",
1396 system_shared_libs: [],
1397 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001398 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001399 }
1400
1401 cc_binary {
1402 name: "mybin",
1403 srcs: ["mylib.cpp"],
1404 relative_install_path: "foo/bar",
1405 system_shared_libs: [],
1406 static_executable: true,
1407 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001408 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001409 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001410 `)
1411
Sundong Ahnabb64432019-10-22 13:58:29 +09001412 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001413 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1414
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001415 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001416 ensureListContains(t, dirs, "etc")
1417 ensureListContains(t, dirs, "etc/foo")
1418 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001419 ensureListContains(t, dirs, "lib64")
1420 ensureListContains(t, dirs, "lib64/foo")
1421 ensureListContains(t, dirs, "lib64/foo/bar")
1422 ensureListContains(t, dirs, "lib")
1423 ensureListContains(t, dirs, "lib/foo")
1424 ensureListContains(t, dirs, "lib/foo/bar")
1425
Jiyong Parkbd13e442019-03-15 18:10:35 +09001426 ensureListContains(t, dirs, "bin")
1427 ensureListContains(t, dirs, "bin/foo")
1428 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001429}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001430
1431func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001432 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001433 apex {
1434 name: "myapex",
1435 key: "myapex.key",
1436 native_shared_libs: ["mylib"],
1437 use_vendor: true,
1438 }
1439
1440 apex_key {
1441 name: "myapex.key",
1442 public_key: "testkey.avbpubkey",
1443 private_key: "testkey.pem",
1444 }
1445
1446 cc_library {
1447 name: "mylib",
1448 srcs: ["mylib.cpp"],
1449 shared_libs: ["mylib2"],
1450 system_shared_libs: [],
1451 vendor_available: true,
1452 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001453 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001454 }
1455
1456 cc_library {
1457 name: "mylib2",
1458 srcs: ["mylib.cpp"],
1459 system_shared_libs: [],
1460 vendor_available: true,
1461 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001462 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001463 }
Jooyung Handc782442019-11-01 03:14:38 +09001464 `, func(fs map[string][]byte, config android.Config) {
1465 setUseVendorWhitelistForTest(config, []string{"myapex"})
1466 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001467
1468 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001469 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001470 for _, implicit := range i.Implicits {
1471 inputsList = append(inputsList, implicit.String())
1472 }
1473 }
1474 inputsString := strings.Join(inputsList, " ")
1475
1476 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001477 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1478 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001479
1480 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001481 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1482 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001483}
Jiyong Park16e91a02018-12-20 18:18:08 +09001484
Jooyung Handc782442019-11-01 03:14:38 +09001485func TestUseVendorRestriction(t *testing.T) {
1486 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1487 apex {
1488 name: "myapex",
1489 key: "myapex.key",
1490 use_vendor: true,
1491 }
1492 apex_key {
1493 name: "myapex.key",
1494 public_key: "testkey.avbpubkey",
1495 private_key: "testkey.pem",
1496 }
1497 `, func(fs map[string][]byte, config android.Config) {
1498 setUseVendorWhitelistForTest(config, []string{""})
1499 })
1500 // no error with whitelist
1501 testApex(t, `
1502 apex {
1503 name: "myapex",
1504 key: "myapex.key",
1505 use_vendor: true,
1506 }
1507 apex_key {
1508 name: "myapex.key",
1509 public_key: "testkey.avbpubkey",
1510 private_key: "testkey.pem",
1511 }
1512 `, func(fs map[string][]byte, config android.Config) {
1513 setUseVendorWhitelistForTest(config, []string{"myapex"})
1514 })
1515}
1516
Jooyung Han5c998b92019-06-27 11:30:33 +09001517func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1518 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1519 apex {
1520 name: "myapex",
1521 key: "myapex.key",
1522 native_shared_libs: ["mylib"],
1523 use_vendor: true,
1524 }
1525
1526 apex_key {
1527 name: "myapex.key",
1528 public_key: "testkey.avbpubkey",
1529 private_key: "testkey.pem",
1530 }
1531
1532 cc_library {
1533 name: "mylib",
1534 srcs: ["mylib.cpp"],
1535 system_shared_libs: [],
1536 stl: "none",
1537 }
1538 `)
1539}
1540
Jiyong Park16e91a02018-12-20 18:18:08 +09001541func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001542 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001543 apex {
1544 name: "myapex",
1545 key: "myapex.key",
1546 native_shared_libs: ["mylib"],
1547 }
1548
1549 apex_key {
1550 name: "myapex.key",
1551 public_key: "testkey.avbpubkey",
1552 private_key: "testkey.pem",
1553 }
1554
1555 cc_library {
1556 name: "mylib",
1557 srcs: ["mylib.cpp"],
1558 system_shared_libs: [],
1559 stl: "none",
1560 stubs: {
1561 versions: ["1", "2", "3"],
1562 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001563 apex_available: [
1564 "//apex_available:platform",
1565 "myapex",
1566 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001567 }
1568
1569 cc_binary {
1570 name: "not_in_apex",
1571 srcs: ["mylib.cpp"],
1572 static_libs: ["mylib"],
1573 static_executable: true,
1574 system_shared_libs: [],
1575 stl: "none",
1576 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001577 `)
1578
Colin Cross7113d202019-11-20 16:39:12 -08001579 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001580
1581 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001582 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001583}
Jiyong Park9335a262018-12-24 11:31:58 +09001584
1585func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001586 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001587 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001588 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001589 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001590 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001591 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001592 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001593 }
1594
1595 cc_library {
1596 name: "mylib",
1597 srcs: ["mylib.cpp"],
1598 system_shared_libs: [],
1599 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001600 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001601 }
1602
1603 apex_key {
1604 name: "myapex.key",
1605 public_key: "testkey.avbpubkey",
1606 private_key: "testkey.pem",
1607 }
1608
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001609 android_app_certificate {
1610 name: "myapex.certificate",
1611 certificate: "testkey",
1612 }
1613
1614 android_app_certificate {
1615 name: "myapex.certificate.override",
1616 certificate: "testkey.override",
1617 }
1618
Jiyong Park9335a262018-12-24 11:31:58 +09001619 `)
1620
1621 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001622 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001623
1624 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1625 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1626 "vendor/foo/devkeys/testkey.avbpubkey")
1627 }
1628 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1629 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1630 "vendor/foo/devkeys/testkey.pem")
1631 }
1632
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001633 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001634 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001635 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001636 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001637 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001638 }
1639}
Jiyong Park58e364a2019-01-19 19:24:06 +09001640
Jooyung Hanf121a652019-12-17 14:30:11 +09001641func TestCertificate(t *testing.T) {
1642 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1643 ctx, _ := testApex(t, `
1644 apex {
1645 name: "myapex",
1646 key: "myapex.key",
1647 }
1648 apex_key {
1649 name: "myapex.key",
1650 public_key: "testkey.avbpubkey",
1651 private_key: "testkey.pem",
1652 }`)
1653 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1654 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1655 if actual := rule.Args["certificates"]; actual != expected {
1656 t.Errorf("certificates should be %q, not %q", expected, actual)
1657 }
1658 })
1659 t.Run("override when unspecified", func(t *testing.T) {
1660 ctx, _ := testApex(t, `
1661 apex {
1662 name: "myapex_keytest",
1663 key: "myapex.key",
1664 file_contexts: ":myapex-file_contexts",
1665 }
1666 apex_key {
1667 name: "myapex.key",
1668 public_key: "testkey.avbpubkey",
1669 private_key: "testkey.pem",
1670 }
1671 android_app_certificate {
1672 name: "myapex.certificate.override",
1673 certificate: "testkey.override",
1674 }`)
1675 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1676 expected := "testkey.override.x509.pem testkey.override.pk8"
1677 if actual := rule.Args["certificates"]; actual != expected {
1678 t.Errorf("certificates should be %q, not %q", expected, actual)
1679 }
1680 })
1681 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1682 ctx, _ := testApex(t, `
1683 apex {
1684 name: "myapex",
1685 key: "myapex.key",
1686 certificate: ":myapex.certificate",
1687 }
1688 apex_key {
1689 name: "myapex.key",
1690 public_key: "testkey.avbpubkey",
1691 private_key: "testkey.pem",
1692 }
1693 android_app_certificate {
1694 name: "myapex.certificate",
1695 certificate: "testkey",
1696 }`)
1697 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1698 expected := "testkey.x509.pem testkey.pk8"
1699 if actual := rule.Args["certificates"]; actual != expected {
1700 t.Errorf("certificates should be %q, not %q", expected, actual)
1701 }
1702 })
1703 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1704 ctx, _ := testApex(t, `
1705 apex {
1706 name: "myapex_keytest",
1707 key: "myapex.key",
1708 file_contexts: ":myapex-file_contexts",
1709 certificate: ":myapex.certificate",
1710 }
1711 apex_key {
1712 name: "myapex.key",
1713 public_key: "testkey.avbpubkey",
1714 private_key: "testkey.pem",
1715 }
1716 android_app_certificate {
1717 name: "myapex.certificate.override",
1718 certificate: "testkey.override",
1719 }`)
1720 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1721 expected := "testkey.override.x509.pem testkey.override.pk8"
1722 if actual := rule.Args["certificates"]; actual != expected {
1723 t.Errorf("certificates should be %q, not %q", expected, actual)
1724 }
1725 })
1726 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1727 ctx, _ := testApex(t, `
1728 apex {
1729 name: "myapex",
1730 key: "myapex.key",
1731 certificate: "testkey",
1732 }
1733 apex_key {
1734 name: "myapex.key",
1735 public_key: "testkey.avbpubkey",
1736 private_key: "testkey.pem",
1737 }`)
1738 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1739 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1740 if actual := rule.Args["certificates"]; actual != expected {
1741 t.Errorf("certificates should be %q, not %q", expected, actual)
1742 }
1743 })
1744 t.Run("override when specified as <name>", func(t *testing.T) {
1745 ctx, _ := testApex(t, `
1746 apex {
1747 name: "myapex_keytest",
1748 key: "myapex.key",
1749 file_contexts: ":myapex-file_contexts",
1750 certificate: "testkey",
1751 }
1752 apex_key {
1753 name: "myapex.key",
1754 public_key: "testkey.avbpubkey",
1755 private_key: "testkey.pem",
1756 }
1757 android_app_certificate {
1758 name: "myapex.certificate.override",
1759 certificate: "testkey.override",
1760 }`)
1761 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1762 expected := "testkey.override.x509.pem testkey.override.pk8"
1763 if actual := rule.Args["certificates"]; actual != expected {
1764 t.Errorf("certificates should be %q, not %q", expected, actual)
1765 }
1766 })
1767}
1768
Jiyong Park58e364a2019-01-19 19:24:06 +09001769func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001770 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001771 apex {
1772 name: "myapex",
1773 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001774 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001775 }
1776
1777 apex {
1778 name: "otherapex",
1779 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001780 native_shared_libs: ["mylib", "mylib2"],
Jooyung Han61c41542020-03-07 03:45:53 +09001781 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09001782 }
1783
1784 apex_key {
1785 name: "myapex.key",
1786 public_key: "testkey.avbpubkey",
1787 private_key: "testkey.pem",
1788 }
1789
1790 cc_library {
1791 name: "mylib",
1792 srcs: ["mylib.cpp"],
1793 system_shared_libs: [],
1794 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001795 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001796 "myapex",
1797 "otherapex",
1798 ],
Jooyung Hanc3e92632020-03-21 23:20:55 +09001799 recovery_available: true,
Jiyong Park58e364a2019-01-19 19:24:06 +09001800 }
Jooyung Han68e511e2020-03-02 17:44:33 +09001801 cc_library {
1802 name: "mylib2",
1803 srcs: ["mylib.cpp"],
1804 system_shared_libs: [],
1805 stl: "none",
1806 apex_available: [
1807 "myapex",
1808 "otherapex",
1809 ],
1810 use_apex_name_macro: true,
1811 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001812 `)
1813
Jooyung Han68e511e2020-03-02 17:44:33 +09001814 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001815 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001816 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han74066602020-03-20 04:29:24 +09001817 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jooyung Han68e511e2020-03-02 17:44:33 +09001818
Jooyung Han61c41542020-03-07 03:45:53 +09001819 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Han68e511e2020-03-02 17:44:33 +09001820 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1821 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han61c41542020-03-07 03:45:53 +09001822 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09001823 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han68e511e2020-03-02 17:44:33 +09001824
Jooyung Han61c41542020-03-07 03:45:53 +09001825 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Han68e511e2020-03-02 17:44:33 +09001826 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1827 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han61c41542020-03-07 03:45:53 +09001828 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09001829 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001830
Jooyung Han68e511e2020-03-02 17:44:33 +09001831 // When cc_library sets use_apex_name_macro: true
1832 // apex variants define additional macro to distinguish which apex variant it is built for
1833
1834 // non-APEX variant does not have __ANDROID_APEX__ defined
1835 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1836 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1837
1838 // APEX variant has __ANDROID_APEX__ defined
1839 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001840 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001841 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1842 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001843
Jooyung Han68e511e2020-03-02 17:44:33 +09001844 // APEX variant has __ANDROID_APEX__ defined
1845 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001846 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001847 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1848 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Hanc3e92632020-03-21 23:20:55 +09001849
1850 // recovery variant does not set __ANDROID_SDK_VERSION__
1851 mylibCFlags = ctx.ModuleForTests("mylib", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1852 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1853 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001854}
Jiyong Park7e636d02019-01-28 16:16:54 +09001855
1856func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001857 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001858 apex {
1859 name: "myapex",
1860 key: "myapex.key",
1861 native_shared_libs: ["mylib"],
1862 }
1863
1864 apex_key {
1865 name: "myapex.key",
1866 public_key: "testkey.avbpubkey",
1867 private_key: "testkey.pem",
1868 }
1869
1870 cc_library_headers {
1871 name: "mylib_headers",
1872 export_include_dirs: ["my_include"],
1873 system_shared_libs: [],
1874 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001875 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001876 }
1877
1878 cc_library {
1879 name: "mylib",
1880 srcs: ["mylib.cpp"],
1881 system_shared_libs: [],
1882 stl: "none",
1883 header_libs: ["mylib_headers"],
1884 export_header_lib_headers: ["mylib_headers"],
1885 stubs: {
1886 versions: ["1", "2", "3"],
1887 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001888 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001889 }
1890
1891 cc_library {
1892 name: "otherlib",
1893 srcs: ["mylib.cpp"],
1894 system_shared_libs: [],
1895 stl: "none",
1896 shared_libs: ["mylib"],
1897 }
1898 `)
1899
Colin Cross7113d202019-11-20 16:39:12 -08001900 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001901
1902 // Ensure that the include path of the header lib is exported to 'otherlib'
1903 ensureContains(t, cFlags, "-Imy_include")
1904}
Alex Light9670d332019-01-29 18:07:33 -08001905
Jiyong Park7cd10e32020-01-14 09:22:18 +09001906type fileInApex struct {
1907 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001908 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001909 isLink bool
1910}
1911
Jooyung Hana57af4a2020-01-23 05:36:59 +00001912func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001913 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001914 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001915 copyCmds := apexRule.Args["copy_commands"]
1916 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001917 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001918 for _, cmd := range strings.Split(copyCmds, "&&") {
1919 cmd = strings.TrimSpace(cmd)
1920 if cmd == "" {
1921 continue
1922 }
1923 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001924 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001925 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001926 switch terms[0] {
1927 case "mkdir":
1928 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001929 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001930 t.Fatal("copyCmds contains invalid cp command", cmd)
1931 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001932 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001933 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001934 isLink = false
1935 case "ln":
1936 if len(terms) != 3 && len(terms) != 4 {
1937 // ln LINK TARGET or ln -s LINK TARGET
1938 t.Fatal("copyCmds contains invalid ln command", cmd)
1939 }
1940 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001941 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001942 isLink = true
1943 default:
1944 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1945 }
1946 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001947 index := strings.Index(dst, imageApexDir)
1948 if index == -1 {
1949 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1950 }
1951 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001952 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001953 }
1954 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001955 return ret
1956}
1957
Jooyung Hana57af4a2020-01-23 05:36:59 +00001958func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1959 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001960 var failed bool
1961 var surplus []string
1962 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001963 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Han8d8906c2020-02-27 13:31:56 +09001964 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001965 for _, expected := range files {
1966 if matched, _ := path.Match(expected, file.path); matched {
1967 filesMatched[expected] = true
Jooyung Han8d8906c2020-02-27 13:31:56 +09001968 mactchFound = true
1969 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001970 }
1971 }
Jooyung Han8d8906c2020-02-27 13:31:56 +09001972 if !mactchFound {
1973 surplus = append(surplus, file.path)
1974 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001975 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001976
Jooyung Han31c470b2019-10-18 16:26:59 +09001977 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001978 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001979 t.Log("surplus files", surplus)
1980 failed = true
1981 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001982
1983 if len(files) > len(filesMatched) {
1984 var missing []string
1985 for _, expected := range files {
1986 if !filesMatched[expected] {
1987 missing = append(missing, expected)
1988 }
1989 }
1990 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001991 t.Log("missing files", missing)
1992 failed = true
1993 }
1994 if failed {
1995 t.Fail()
1996 }
1997}
1998
Jooyung Han344d5432019-08-23 11:17:39 +09001999func TestVndkApexCurrent(t *testing.T) {
2000 ctx, _ := testApex(t, `
2001 apex_vndk {
2002 name: "myapex",
2003 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002004 }
2005
2006 apex_key {
2007 name: "myapex.key",
2008 public_key: "testkey.avbpubkey",
2009 private_key: "testkey.pem",
2010 }
2011
2012 cc_library {
2013 name: "libvndk",
2014 srcs: ["mylib.cpp"],
2015 vendor_available: true,
2016 vndk: {
2017 enabled: true,
2018 },
2019 system_shared_libs: [],
2020 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002021 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002022 }
2023
2024 cc_library {
2025 name: "libvndksp",
2026 srcs: ["mylib.cpp"],
2027 vendor_available: true,
2028 vndk: {
2029 enabled: true,
2030 support_system_process: true,
2031 },
2032 system_shared_libs: [],
2033 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002034 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002035 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002036 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002037
Jooyung Hana57af4a2020-01-23 05:36:59 +00002038 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002039 "lib/libvndk.so",
2040 "lib/libvndksp.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002041 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002042 "lib64/libvndk.so",
2043 "lib64/libvndksp.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002044 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002045 "etc/llndk.libraries.VER.txt",
2046 "etc/vndkcore.libraries.VER.txt",
2047 "etc/vndksp.libraries.VER.txt",
2048 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002049 })
Jooyung Han344d5432019-08-23 11:17:39 +09002050}
2051
2052func TestVndkApexWithPrebuilt(t *testing.T) {
2053 ctx, _ := testApex(t, `
2054 apex_vndk {
2055 name: "myapex",
2056 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002057 }
2058
2059 apex_key {
2060 name: "myapex.key",
2061 public_key: "testkey.avbpubkey",
2062 private_key: "testkey.pem",
2063 }
2064
2065 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002066 name: "libvndk",
2067 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002068 vendor_available: true,
2069 vndk: {
2070 enabled: true,
2071 },
2072 system_shared_libs: [],
2073 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002074 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002075 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002076
2077 cc_prebuilt_library_shared {
2078 name: "libvndk.arm",
2079 srcs: ["libvndk.arm.so"],
2080 vendor_available: true,
2081 vndk: {
2082 enabled: true,
2083 },
2084 enabled: false,
2085 arch: {
2086 arm: {
2087 enabled: true,
2088 },
2089 },
2090 system_shared_libs: [],
2091 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002092 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002093 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002094 `+vndkLibrariesTxtFiles("current"),
2095 withFiles(map[string][]byte{
2096 "libvndk.so": nil,
2097 "libvndk.arm.so": nil,
2098 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002099
Jooyung Hana57af4a2020-01-23 05:36:59 +00002100 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002101 "lib/libvndk.so",
2102 "lib/libvndk.arm.so",
2103 "lib64/libvndk.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002104 "lib/libc++.so",
2105 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002106 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002107 })
Jooyung Han344d5432019-08-23 11:17:39 +09002108}
2109
Jooyung Han39edb6c2019-11-06 16:53:07 +09002110func vndkLibrariesTxtFiles(vers ...string) (result string) {
2111 for _, v := range vers {
2112 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002113 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002114 result += `
2115 vndk_libraries_txt {
2116 name: "` + txt + `.libraries.txt",
2117 }
2118 `
2119 }
2120 } else {
2121 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2122 result += `
2123 prebuilt_etc {
2124 name: "` + txt + `.libraries.` + v + `.txt",
2125 src: "dummy.txt",
2126 }
2127 `
2128 }
2129 }
2130 }
2131 return
2132}
2133
Jooyung Han344d5432019-08-23 11:17:39 +09002134func TestVndkApexVersion(t *testing.T) {
2135 ctx, _ := testApex(t, `
2136 apex_vndk {
2137 name: "myapex_v27",
2138 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002139 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002140 vndk_version: "27",
2141 }
2142
2143 apex_key {
2144 name: "myapex.key",
2145 public_key: "testkey.avbpubkey",
2146 private_key: "testkey.pem",
2147 }
2148
Jooyung Han31c470b2019-10-18 16:26:59 +09002149 vndk_prebuilt_shared {
2150 name: "libvndk27",
2151 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002152 vendor_available: true,
2153 vndk: {
2154 enabled: true,
2155 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002156 target_arch: "arm64",
2157 arch: {
2158 arm: {
2159 srcs: ["libvndk27_arm.so"],
2160 },
2161 arm64: {
2162 srcs: ["libvndk27_arm64.so"],
2163 },
2164 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002165 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002166 }
2167
2168 vndk_prebuilt_shared {
2169 name: "libvndk27",
2170 version: "27",
2171 vendor_available: true,
2172 vndk: {
2173 enabled: true,
2174 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002175 target_arch: "x86_64",
2176 arch: {
2177 x86: {
2178 srcs: ["libvndk27_x86.so"],
2179 },
2180 x86_64: {
2181 srcs: ["libvndk27_x86_64.so"],
2182 },
2183 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002184 }
2185 `+vndkLibrariesTxtFiles("27"),
2186 withFiles(map[string][]byte{
2187 "libvndk27_arm.so": nil,
2188 "libvndk27_arm64.so": nil,
2189 "libvndk27_x86.so": nil,
2190 "libvndk27_x86_64.so": nil,
2191 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002192
Jooyung Hana57af4a2020-01-23 05:36:59 +00002193 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002194 "lib/libvndk27_arm.so",
2195 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002196 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002197 })
Jooyung Han344d5432019-08-23 11:17:39 +09002198}
2199
2200func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2201 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2202 apex_vndk {
2203 name: "myapex_v27",
2204 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002205 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002206 vndk_version: "27",
2207 }
2208 apex_vndk {
2209 name: "myapex_v27_other",
2210 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002211 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002212 vndk_version: "27",
2213 }
2214
2215 apex_key {
2216 name: "myapex.key",
2217 public_key: "testkey.avbpubkey",
2218 private_key: "testkey.pem",
2219 }
2220
2221 cc_library {
2222 name: "libvndk",
2223 srcs: ["mylib.cpp"],
2224 vendor_available: true,
2225 vndk: {
2226 enabled: true,
2227 },
2228 system_shared_libs: [],
2229 stl: "none",
2230 }
2231
2232 vndk_prebuilt_shared {
2233 name: "libvndk",
2234 version: "27",
2235 vendor_available: true,
2236 vndk: {
2237 enabled: true,
2238 },
2239 srcs: ["libvndk.so"],
2240 }
2241 `, withFiles(map[string][]byte{
2242 "libvndk.so": nil,
2243 }))
2244}
2245
Jooyung Han90eee022019-10-01 20:02:42 +09002246func TestVndkApexNameRule(t *testing.T) {
2247 ctx, _ := testApex(t, `
2248 apex_vndk {
2249 name: "myapex",
2250 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002251 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002252 }
2253 apex_vndk {
2254 name: "myapex_v28",
2255 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002256 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002257 vndk_version: "28",
2258 }
2259 apex_key {
2260 name: "myapex.key",
2261 public_key: "testkey.avbpubkey",
2262 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002263 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002264
2265 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002266 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002267 actual := proptools.String(bundle.properties.Apex_name)
2268 if !reflect.DeepEqual(actual, expected) {
2269 t.Errorf("Got '%v', expected '%v'", actual, expected)
2270 }
2271 }
2272
2273 assertApexName("com.android.vndk.vVER", "myapex")
2274 assertApexName("com.android.vndk.v28", "myapex_v28")
2275}
2276
Jooyung Han344d5432019-08-23 11:17:39 +09002277func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2278 ctx, _ := testApex(t, `
2279 apex_vndk {
2280 name: "myapex",
2281 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002282 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002283 }
2284
2285 apex_key {
2286 name: "myapex.key",
2287 public_key: "testkey.avbpubkey",
2288 private_key: "testkey.pem",
2289 }
2290
2291 cc_library {
2292 name: "libvndk",
2293 srcs: ["mylib.cpp"],
2294 vendor_available: true,
2295 native_bridge_supported: true,
2296 host_supported: true,
2297 vndk: {
2298 enabled: true,
2299 },
2300 system_shared_libs: [],
2301 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002302 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002303 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002304 `+vndkLibrariesTxtFiles("current"),
2305 withTargets(map[android.OsType][]android.Target{
2306 android.Android: []android.Target{
2307 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2308 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2309 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2310 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2311 },
2312 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002313
Jooyung Hana57af4a2020-01-23 05:36:59 +00002314 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002315 "lib/libvndk.so",
2316 "lib64/libvndk.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002317 "lib/libc++.so",
2318 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002319 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002320 })
Jooyung Han344d5432019-08-23 11:17:39 +09002321}
2322
2323func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2324 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2325 apex_vndk {
2326 name: "myapex",
2327 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002328 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002329 native_bridge_supported: true,
2330 }
2331
2332 apex_key {
2333 name: "myapex.key",
2334 public_key: "testkey.avbpubkey",
2335 private_key: "testkey.pem",
2336 }
2337
2338 cc_library {
2339 name: "libvndk",
2340 srcs: ["mylib.cpp"],
2341 vendor_available: true,
2342 native_bridge_supported: true,
2343 host_supported: true,
2344 vndk: {
2345 enabled: true,
2346 },
2347 system_shared_libs: [],
2348 stl: "none",
2349 }
2350 `)
2351}
2352
Jooyung Han31c470b2019-10-18 16:26:59 +09002353func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002354 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002355 apex_vndk {
2356 name: "myapex_v27",
2357 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002358 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002359 vndk_version: "27",
2360 }
2361
2362 apex_key {
2363 name: "myapex.key",
2364 public_key: "testkey.avbpubkey",
2365 private_key: "testkey.pem",
2366 }
2367
2368 vndk_prebuilt_shared {
2369 name: "libvndk27",
2370 version: "27",
2371 target_arch: "arm",
2372 vendor_available: true,
2373 vndk: {
2374 enabled: true,
2375 },
2376 arch: {
2377 arm: {
2378 srcs: ["libvndk27.so"],
2379 }
2380 },
2381 }
2382
2383 vndk_prebuilt_shared {
2384 name: "libvndk27",
2385 version: "27",
2386 target_arch: "arm",
2387 binder32bit: true,
2388 vendor_available: true,
2389 vndk: {
2390 enabled: true,
2391 },
2392 arch: {
2393 arm: {
2394 srcs: ["libvndk27binder32.so"],
2395 }
2396 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002397 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002398 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002399 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002400 withFiles(map[string][]byte{
2401 "libvndk27.so": nil,
2402 "libvndk27binder32.so": nil,
2403 }),
2404 withBinder32bit,
2405 withTargets(map[android.OsType][]android.Target{
2406 android.Android: []android.Target{
2407 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2408 },
2409 }),
2410 )
2411
Jooyung Hana57af4a2020-01-23 05:36:59 +00002412 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002413 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002414 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002415 })
2416}
2417
Jooyung Hane1633032019-08-01 17:41:43 +09002418func TestDependenciesInApexManifest(t *testing.T) {
2419 ctx, _ := testApex(t, `
2420 apex {
2421 name: "myapex_nodep",
2422 key: "myapex.key",
2423 native_shared_libs: ["lib_nodep"],
2424 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002425 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002426 }
2427
2428 apex {
2429 name: "myapex_dep",
2430 key: "myapex.key",
2431 native_shared_libs: ["lib_dep"],
2432 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002433 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002434 }
2435
2436 apex {
2437 name: "myapex_provider",
2438 key: "myapex.key",
2439 native_shared_libs: ["libfoo"],
2440 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002441 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002442 }
2443
2444 apex {
2445 name: "myapex_selfcontained",
2446 key: "myapex.key",
2447 native_shared_libs: ["lib_dep", "libfoo"],
2448 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002449 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002450 }
2451
2452 apex_key {
2453 name: "myapex.key",
2454 public_key: "testkey.avbpubkey",
2455 private_key: "testkey.pem",
2456 }
2457
2458 cc_library {
2459 name: "lib_nodep",
2460 srcs: ["mylib.cpp"],
2461 system_shared_libs: [],
2462 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002463 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002464 }
2465
2466 cc_library {
2467 name: "lib_dep",
2468 srcs: ["mylib.cpp"],
2469 shared_libs: ["libfoo"],
2470 system_shared_libs: [],
2471 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002472 apex_available: [
2473 "myapex_dep",
2474 "myapex_provider",
2475 "myapex_selfcontained",
2476 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002477 }
2478
2479 cc_library {
2480 name: "libfoo",
2481 srcs: ["mytest.cpp"],
2482 stubs: {
2483 versions: ["1"],
2484 },
2485 system_shared_libs: [],
2486 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002487 apex_available: [
2488 "myapex_provider",
2489 "myapex_selfcontained",
2490 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002491 }
2492 `)
2493
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002494 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002495 var provideNativeLibs, requireNativeLibs []string
2496
Sundong Ahnabb64432019-10-22 13:58:29 +09002497 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002498 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2499 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002500 ensureListEmpty(t, provideNativeLibs)
2501 ensureListEmpty(t, requireNativeLibs)
2502
Sundong Ahnabb64432019-10-22 13:58:29 +09002503 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002504 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2505 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002506 ensureListEmpty(t, provideNativeLibs)
2507 ensureListContains(t, requireNativeLibs, "libfoo.so")
2508
Sundong Ahnabb64432019-10-22 13:58:29 +09002509 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002510 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2511 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002512 ensureListContains(t, provideNativeLibs, "libfoo.so")
2513 ensureListEmpty(t, requireNativeLibs)
2514
Sundong Ahnabb64432019-10-22 13:58:29 +09002515 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002516 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2517 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002518 ensureListContains(t, provideNativeLibs, "libfoo.so")
2519 ensureListEmpty(t, requireNativeLibs)
2520}
2521
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002522func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002523 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002524 apex {
2525 name: "myapex",
2526 key: "myapex.key",
2527 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002528 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002529 }
2530
2531 apex_key {
2532 name: "myapex.key",
2533 public_key: "testkey.avbpubkey",
2534 private_key: "testkey.pem",
2535 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002536
2537 cc_library {
2538 name: "mylib",
2539 srcs: ["mylib.cpp"],
2540 system_shared_libs: [],
2541 stl: "none",
2542 apex_available: [
2543 "//apex_available:platform",
2544 "myapex",
2545 ],
2546 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002547 `)
2548
Sundong Ahnabb64432019-10-22 13:58:29 +09002549 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002550 apexManifestRule := module.Rule("apexManifestRule")
2551 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2552 apexRule := module.Rule("apexRule")
2553 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002554
2555 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2556 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2557 name := apexBundle.BaseModuleName()
2558 prefix := "TARGET_"
2559 var builder strings.Builder
2560 data.Custom(&builder, name, prefix, "", data)
2561 androidMk := builder.String()
2562 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2563 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002564}
2565
Alex Light0851b882019-02-07 13:20:53 -08002566func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002567 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002568 apex {
2569 name: "myapex",
2570 key: "myapex.key",
2571 native_shared_libs: ["mylib_common"],
2572 }
2573
2574 apex_key {
2575 name: "myapex.key",
2576 public_key: "testkey.avbpubkey",
2577 private_key: "testkey.pem",
2578 }
2579
2580 cc_library {
2581 name: "mylib_common",
2582 srcs: ["mylib.cpp"],
2583 system_shared_libs: [],
2584 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002585 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 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"), "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.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"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002611
2612 if !android.InAnyApex("mylib_common") {
2613 t.Log("Found mylib_common not in any apex!")
2614 t.Fail()
2615 }
2616}
2617
2618func TestTestApex(t *testing.T) {
2619 if android.InAnyApex("mylib_common_test") {
2620 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!")
2621 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002622 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002623 apex_test {
2624 name: "myapex",
2625 key: "myapex.key",
2626 native_shared_libs: ["mylib_common_test"],
2627 }
2628
2629 apex_key {
2630 name: "myapex.key",
2631 public_key: "testkey.avbpubkey",
2632 private_key: "testkey.pem",
2633 }
2634
2635 cc_library {
2636 name: "mylib_common_test",
2637 srcs: ["mylib.cpp"],
2638 system_shared_libs: [],
2639 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002640 // TODO: remove //apex_available:platform
2641 apex_available: [
2642 "//apex_available:platform",
2643 "myapex",
2644 ],
Alex Light0851b882019-02-07 13:20:53 -08002645 }
2646 `)
2647
Sundong Ahnabb64432019-10-22 13:58:29 +09002648 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002649 apexRule := module.Rule("apexRule")
2650 copyCmds := apexRule.Args["copy_commands"]
2651
2652 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2653 t.Log("Apex was not a test apex!")
2654 t.Fail()
2655 }
2656 // Ensure that main rule creates an output
2657 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2658
2659 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002660 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002661
2662 // Ensure that both direct and indirect deps are copied into apex
2663 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2664
Colin Cross7113d202019-11-20 16:39:12 -08002665 // Ensure that the platform variant ends with _shared
2666 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002667}
2668
Alex Light9670d332019-01-29 18:07:33 -08002669func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002670 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002671 apex {
2672 name: "myapex",
2673 key: "myapex.key",
2674 multilib: {
2675 first: {
2676 native_shared_libs: ["mylib_common"],
2677 }
2678 },
2679 target: {
2680 android: {
2681 multilib: {
2682 first: {
2683 native_shared_libs: ["mylib"],
2684 }
2685 }
2686 },
2687 host: {
2688 multilib: {
2689 first: {
2690 native_shared_libs: ["mylib2"],
2691 }
2692 }
2693 }
2694 }
2695 }
2696
2697 apex_key {
2698 name: "myapex.key",
2699 public_key: "testkey.avbpubkey",
2700 private_key: "testkey.pem",
2701 }
2702
2703 cc_library {
2704 name: "mylib",
2705 srcs: ["mylib.cpp"],
2706 system_shared_libs: [],
2707 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002708 // TODO: remove //apex_available:platform
2709 apex_available: [
2710 "//apex_available:platform",
2711 "myapex",
2712 ],
Alex Light9670d332019-01-29 18:07:33 -08002713 }
2714
2715 cc_library {
2716 name: "mylib_common",
2717 srcs: ["mylib.cpp"],
2718 system_shared_libs: [],
2719 stl: "none",
2720 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002721 // TODO: remove //apex_available:platform
2722 apex_available: [
2723 "//apex_available:platform",
2724 "myapex",
2725 ],
Alex Light9670d332019-01-29 18:07:33 -08002726 }
2727
2728 cc_library {
2729 name: "mylib2",
2730 srcs: ["mylib.cpp"],
2731 system_shared_libs: [],
2732 stl: "none",
2733 compile_multilib: "first",
2734 }
2735 `)
2736
Sundong Ahnabb64432019-10-22 13:58:29 +09002737 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002738 copyCmds := apexRule.Args["copy_commands"]
2739
2740 // Ensure that main rule creates an output
2741 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2742
2743 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002744 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2745 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2746 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002747
2748 // Ensure that both direct and indirect deps are copied into apex
2749 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2750 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2751 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2752
Colin Cross7113d202019-11-20 16:39:12 -08002753 // Ensure that the platform variant ends with _shared
2754 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2755 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2756 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002757}
Jiyong Park04480cf2019-02-06 00:16:29 +09002758
2759func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002760 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002761 apex {
2762 name: "myapex",
2763 key: "myapex.key",
2764 binaries: ["myscript"],
2765 }
2766
2767 apex_key {
2768 name: "myapex.key",
2769 public_key: "testkey.avbpubkey",
2770 private_key: "testkey.pem",
2771 }
2772
2773 sh_binary {
2774 name: "myscript",
2775 src: "mylib.cpp",
2776 filename: "myscript.sh",
2777 sub_dir: "script",
2778 }
2779 `)
2780
Sundong Ahnabb64432019-10-22 13:58:29 +09002781 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002782 copyCmds := apexRule.Args["copy_commands"]
2783
2784 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2785}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002786
Jooyung Han91df2082019-11-20 01:49:42 +09002787func TestApexInVariousPartition(t *testing.T) {
2788 testcases := []struct {
2789 propName, parition, flattenedPartition string
2790 }{
2791 {"", "system", "system_ext"},
2792 {"product_specific: true", "product", "product"},
2793 {"soc_specific: true", "vendor", "vendor"},
2794 {"proprietary: true", "vendor", "vendor"},
2795 {"vendor: true", "vendor", "vendor"},
2796 {"system_ext_specific: true", "system_ext", "system_ext"},
2797 }
2798 for _, tc := range testcases {
2799 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2800 ctx, _ := testApex(t, `
2801 apex {
2802 name: "myapex",
2803 key: "myapex.key",
2804 `+tc.propName+`
2805 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002806
Jooyung Han91df2082019-11-20 01:49:42 +09002807 apex_key {
2808 name: "myapex.key",
2809 public_key: "testkey.avbpubkey",
2810 private_key: "testkey.pem",
2811 }
2812 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002813
Jooyung Han91df2082019-11-20 01:49:42 +09002814 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2815 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2816 actual := apex.installDir.String()
2817 if actual != expected {
2818 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2819 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002820
Jooyung Han91df2082019-11-20 01:49:42 +09002821 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2822 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2823 actual = flattened.installDir.String()
2824 if actual != expected {
2825 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2826 }
2827 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002828 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002829}
Jiyong Park67882562019-03-21 01:11:21 +09002830
Jooyung Han54aca7b2019-11-20 02:26:02 +09002831func TestFileContexts(t *testing.T) {
2832 ctx, _ := testApex(t, `
2833 apex {
2834 name: "myapex",
2835 key: "myapex.key",
2836 }
2837
2838 apex_key {
2839 name: "myapex.key",
2840 public_key: "testkey.avbpubkey",
2841 private_key: "testkey.pem",
2842 }
2843 `)
2844 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2845 apexRule := module.Rule("apexRule")
2846 actual := apexRule.Args["file_contexts"]
2847 expected := "system/sepolicy/apex/myapex-file_contexts"
2848 if actual != expected {
2849 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2850 }
2851
2852 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2853 apex {
2854 name: "myapex",
2855 key: "myapex.key",
2856 file_contexts: "my_own_file_contexts",
2857 }
2858
2859 apex_key {
2860 name: "myapex.key",
2861 public_key: "testkey.avbpubkey",
2862 private_key: "testkey.pem",
2863 }
2864 `, withFiles(map[string][]byte{
2865 "my_own_file_contexts": nil,
2866 }))
2867
2868 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2869 apex {
2870 name: "myapex",
2871 key: "myapex.key",
2872 product_specific: true,
2873 file_contexts: "product_specific_file_contexts",
2874 }
2875
2876 apex_key {
2877 name: "myapex.key",
2878 public_key: "testkey.avbpubkey",
2879 private_key: "testkey.pem",
2880 }
2881 `)
2882
2883 ctx, _ = testApex(t, `
2884 apex {
2885 name: "myapex",
2886 key: "myapex.key",
2887 product_specific: true,
2888 file_contexts: "product_specific_file_contexts",
2889 }
2890
2891 apex_key {
2892 name: "myapex.key",
2893 public_key: "testkey.avbpubkey",
2894 private_key: "testkey.pem",
2895 }
2896 `, withFiles(map[string][]byte{
2897 "product_specific_file_contexts": nil,
2898 }))
2899 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2900 apexRule = module.Rule("apexRule")
2901 actual = apexRule.Args["file_contexts"]
2902 expected = "product_specific_file_contexts"
2903 if actual != expected {
2904 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2905 }
2906
2907 ctx, _ = testApex(t, `
2908 apex {
2909 name: "myapex",
2910 key: "myapex.key",
2911 product_specific: true,
2912 file_contexts: ":my-file-contexts",
2913 }
2914
2915 apex_key {
2916 name: "myapex.key",
2917 public_key: "testkey.avbpubkey",
2918 private_key: "testkey.pem",
2919 }
2920
2921 filegroup {
2922 name: "my-file-contexts",
2923 srcs: ["product_specific_file_contexts"],
2924 }
2925 `, withFiles(map[string][]byte{
2926 "product_specific_file_contexts": nil,
2927 }))
2928 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2929 apexRule = module.Rule("apexRule")
2930 actual = apexRule.Args["file_contexts"]
2931 expected = "product_specific_file_contexts"
2932 if actual != expected {
2933 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2934 }
2935}
2936
Jiyong Park67882562019-03-21 01:11:21 +09002937func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002938 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002939 apex_key {
2940 name: "myapex.key",
2941 public_key: ":my.avbpubkey",
2942 private_key: ":my.pem",
2943 product_specific: true,
2944 }
2945
2946 filegroup {
2947 name: "my.avbpubkey",
2948 srcs: ["testkey2.avbpubkey"],
2949 }
2950
2951 filegroup {
2952 name: "my.pem",
2953 srcs: ["testkey2.pem"],
2954 }
2955 `)
2956
2957 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2958 expected_pubkey := "testkey2.avbpubkey"
2959 actual_pubkey := apex_key.public_key_file.String()
2960 if actual_pubkey != expected_pubkey {
2961 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2962 }
2963 expected_privkey := "testkey2.pem"
2964 actual_privkey := apex_key.private_key_file.String()
2965 if actual_privkey != expected_privkey {
2966 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2967 }
2968}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002969
2970func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002971 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002972 prebuilt_apex {
2973 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002974 arch: {
2975 arm64: {
2976 src: "myapex-arm64.apex",
2977 },
2978 arm: {
2979 src: "myapex-arm.apex",
2980 },
2981 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002982 }
2983 `)
2984
2985 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2986
Jiyong Parkc95714e2019-03-29 14:23:10 +09002987 expectedInput := "myapex-arm64.apex"
2988 if prebuilt.inputApex.String() != expectedInput {
2989 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2990 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002991}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002992
2993func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002994 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002995 prebuilt_apex {
2996 name: "myapex",
2997 src: "myapex-arm.apex",
2998 filename: "notmyapex.apex",
2999 }
3000 `)
3001
3002 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3003
3004 expected := "notmyapex.apex"
3005 if p.installFilename != expected {
3006 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
3007 }
3008}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003009
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003010func TestPrebuiltOverrides(t *testing.T) {
3011 ctx, config := testApex(t, `
3012 prebuilt_apex {
3013 name: "myapex.prebuilt",
3014 src: "myapex-arm.apex",
3015 overrides: [
3016 "myapex",
3017 ],
3018 }
3019 `)
3020
3021 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3022
3023 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003024 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003025 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003026 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003027 }
3028}
3029
Roland Levillain630846d2019-06-26 12:48:34 +01003030func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003031 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003032 apex_test {
3033 name: "myapex",
3034 key: "myapex.key",
3035 tests: [
3036 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003037 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003038 ],
3039 }
3040
3041 apex_key {
3042 name: "myapex.key",
3043 public_key: "testkey.avbpubkey",
3044 private_key: "testkey.pem",
3045 }
3046
3047 cc_test {
3048 name: "mytest",
3049 gtest: false,
3050 srcs: ["mytest.cpp"],
3051 relative_install_path: "test",
3052 system_shared_libs: [],
3053 static_executable: true,
3054 stl: "none",
3055 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003056
3057 cc_test {
3058 name: "mytests",
3059 gtest: false,
3060 srcs: [
3061 "mytest1.cpp",
3062 "mytest2.cpp",
3063 "mytest3.cpp",
3064 ],
3065 test_per_src: true,
3066 relative_install_path: "test",
3067 system_shared_libs: [],
3068 static_executable: true,
3069 stl: "none",
3070 }
Roland Levillain630846d2019-06-26 12:48:34 +01003071 `)
3072
Sundong Ahnabb64432019-10-22 13:58:29 +09003073 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003074 copyCmds := apexRule.Args["copy_commands"]
3075
3076 // Ensure that test dep is copied into apex.
3077 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003078
3079 // Ensure that test deps built with `test_per_src` are copied into apex.
3080 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3081 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3082 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003083
3084 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003085 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003086 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3087 name := apexBundle.BaseModuleName()
3088 prefix := "TARGET_"
3089 var builder strings.Builder
3090 data.Custom(&builder, name, prefix, "", data)
3091 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003092 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3093 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3094 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3095 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003096 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003097 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003098 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003099}
3100
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003101func TestInstallExtraFlattenedApexes(t *testing.T) {
3102 ctx, config := testApex(t, `
3103 apex {
3104 name: "myapex",
3105 key: "myapex.key",
3106 }
3107 apex_key {
3108 name: "myapex.key",
3109 public_key: "testkey.avbpubkey",
3110 private_key: "testkey.pem",
3111 }
3112 `, func(fs map[string][]byte, config android.Config) {
3113 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3114 })
3115 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003116 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003117 mk := android.AndroidMkDataForTest(t, config, "", ab)
3118 var builder strings.Builder
3119 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3120 androidMk := builder.String()
3121 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3122}
3123
Jooyung Han5c998b92019-06-27 11:30:33 +09003124func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003125 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003126 apex {
3127 name: "myapex",
3128 key: "myapex.key",
3129 native_shared_libs: ["mylib"],
3130 uses: ["commonapex"],
3131 }
3132
3133 apex {
3134 name: "commonapex",
3135 key: "myapex.key",
3136 native_shared_libs: ["libcommon"],
3137 provide_cpp_shared_libs: true,
3138 }
3139
3140 apex_key {
3141 name: "myapex.key",
3142 public_key: "testkey.avbpubkey",
3143 private_key: "testkey.pem",
3144 }
3145
3146 cc_library {
3147 name: "mylib",
3148 srcs: ["mylib.cpp"],
3149 shared_libs: ["libcommon"],
3150 system_shared_libs: [],
3151 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003152 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003153 }
3154
3155 cc_library {
3156 name: "libcommon",
3157 srcs: ["mylib_common.cpp"],
3158 system_shared_libs: [],
3159 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003160 // TODO: remove //apex_available:platform
3161 apex_available: [
3162 "//apex_available:platform",
3163 "commonapex",
3164 "myapex",
3165 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003166 }
3167 `)
3168
Sundong Ahnabb64432019-10-22 13:58:29 +09003169 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003170 apexRule1 := module1.Rule("apexRule")
3171 copyCmds1 := apexRule1.Args["copy_commands"]
3172
Sundong Ahnabb64432019-10-22 13:58:29 +09003173 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003174 apexRule2 := module2.Rule("apexRule")
3175 copyCmds2 := apexRule2.Args["copy_commands"]
3176
Colin Cross7113d202019-11-20 16:39:12 -08003177 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3178 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003179 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3180 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3181 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3182}
3183
3184func TestApexUsesFailsIfNotProvided(t *testing.T) {
3185 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3186 apex {
3187 name: "myapex",
3188 key: "myapex.key",
3189 uses: ["commonapex"],
3190 }
3191
3192 apex {
3193 name: "commonapex",
3194 key: "myapex.key",
3195 }
3196
3197 apex_key {
3198 name: "myapex.key",
3199 public_key: "testkey.avbpubkey",
3200 private_key: "testkey.pem",
3201 }
3202 `)
3203 testApexError(t, `uses: "commonapex" is not a provider`, `
3204 apex {
3205 name: "myapex",
3206 key: "myapex.key",
3207 uses: ["commonapex"],
3208 }
3209
3210 cc_library {
3211 name: "commonapex",
3212 system_shared_libs: [],
3213 stl: "none",
3214 }
3215
3216 apex_key {
3217 name: "myapex.key",
3218 public_key: "testkey.avbpubkey",
3219 private_key: "testkey.pem",
3220 }
3221 `)
3222}
3223
3224func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3225 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3226 apex {
3227 name: "myapex",
3228 key: "myapex.key",
3229 use_vendor: true,
3230 uses: ["commonapex"],
3231 }
3232
3233 apex {
3234 name: "commonapex",
3235 key: "myapex.key",
3236 provide_cpp_shared_libs: true,
3237 }
3238
3239 apex_key {
3240 name: "myapex.key",
3241 public_key: "testkey.avbpubkey",
3242 private_key: "testkey.pem",
3243 }
Jooyung Handc782442019-11-01 03:14:38 +09003244 `, func(fs map[string][]byte, config android.Config) {
3245 setUseVendorWhitelistForTest(config, []string{"myapex"})
3246 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003247}
3248
Jooyung Hand48f3c32019-08-23 11:18:57 +09003249func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3250 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3251 apex {
3252 name: "myapex",
3253 key: "myapex.key",
3254 native_shared_libs: ["libfoo"],
3255 }
3256
3257 apex_key {
3258 name: "myapex.key",
3259 public_key: "testkey.avbpubkey",
3260 private_key: "testkey.pem",
3261 }
3262
3263 cc_library {
3264 name: "libfoo",
3265 stl: "none",
3266 system_shared_libs: [],
3267 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003268 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003269 }
3270 `)
3271 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3272 apex {
3273 name: "myapex",
3274 key: "myapex.key",
3275 java_libs: ["myjar"],
3276 }
3277
3278 apex_key {
3279 name: "myapex.key",
3280 public_key: "testkey.avbpubkey",
3281 private_key: "testkey.pem",
3282 }
3283
3284 java_library {
3285 name: "myjar",
3286 srcs: ["foo/bar/MyClass.java"],
3287 sdk_version: "none",
3288 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003289 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003290 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003291 }
3292 `)
3293}
3294
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003295func TestApexWithApps(t *testing.T) {
3296 ctx, _ := testApex(t, `
3297 apex {
3298 name: "myapex",
3299 key: "myapex.key",
3300 apps: [
3301 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003302 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003303 ],
3304 }
3305
3306 apex_key {
3307 name: "myapex.key",
3308 public_key: "testkey.avbpubkey",
3309 private_key: "testkey.pem",
3310 }
3311
3312 android_app {
3313 name: "AppFoo",
3314 srcs: ["foo/bar/MyClass.java"],
3315 sdk_version: "none",
3316 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003317 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003318 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003319 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003320
3321 android_app {
3322 name: "AppFooPriv",
3323 srcs: ["foo/bar/MyClass.java"],
3324 sdk_version: "none",
3325 system_modules: "none",
3326 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003327 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003328 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003329
3330 cc_library_shared {
3331 name: "libjni",
3332 srcs: ["mylib.cpp"],
Jooyung Han65041792020-02-25 16:59:29 +09003333 shared_libs: ["libfoo"],
Jiyong Park8be103b2019-11-08 15:53:48 +09003334 stl: "none",
3335 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003336 apex_available: [ "myapex" ],
Jooyung Han65041792020-02-25 16:59:29 +09003337 sdk_version: "current",
3338 }
3339
3340 cc_library_shared {
3341 name: "libfoo",
3342 stl: "none",
3343 system_shared_libs: [],
3344 apex_available: [ "myapex" ],
3345 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003346 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003347 `)
3348
Sundong Ahnabb64432019-10-22 13:58:29 +09003349 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003350 apexRule := module.Rule("apexRule")
3351 copyCmds := apexRule.Args["copy_commands"]
3352
3353 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003354 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003355
Jooyung Han65041792020-02-25 16:59:29 +09003356 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3357 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003358 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Han65041792020-02-25 16:59:29 +09003359 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003360 }
Jooyung Han65041792020-02-25 16:59:29 +09003361 // JNI libraries including transitive deps are
3362 for _, jni := range []string{"libjni", "libfoo"} {
3363 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
3364 // ... embedded inside APK (jnilibs.zip)
3365 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3366 // ... and not directly inside the APEX
3367 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3368 }
Dario Frenicde2a032019-10-27 00:29:22 +01003369}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003370
Dario Frenicde2a032019-10-27 00:29:22 +01003371func TestApexWithAppImports(t *testing.T) {
3372 ctx, _ := testApex(t, `
3373 apex {
3374 name: "myapex",
3375 key: "myapex.key",
3376 apps: [
3377 "AppFooPrebuilt",
3378 "AppFooPrivPrebuilt",
3379 ],
3380 }
3381
3382 apex_key {
3383 name: "myapex.key",
3384 public_key: "testkey.avbpubkey",
3385 private_key: "testkey.pem",
3386 }
3387
3388 android_app_import {
3389 name: "AppFooPrebuilt",
3390 apk: "PrebuiltAppFoo.apk",
3391 presigned: true,
3392 dex_preopt: {
3393 enabled: false,
3394 },
3395 }
3396
3397 android_app_import {
3398 name: "AppFooPrivPrebuilt",
3399 apk: "PrebuiltAppFooPriv.apk",
3400 privileged: true,
3401 presigned: true,
3402 dex_preopt: {
3403 enabled: false,
3404 },
3405 }
3406 `)
3407
Sundong Ahnabb64432019-10-22 13:58:29 +09003408 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003409 apexRule := module.Rule("apexRule")
3410 copyCmds := apexRule.Args["copy_commands"]
3411
3412 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3413 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003414}
3415
Dario Freni6f3937c2019-12-20 22:58:03 +00003416func TestApexWithTestHelperApp(t *testing.T) {
3417 ctx, _ := testApex(t, `
3418 apex {
3419 name: "myapex",
3420 key: "myapex.key",
3421 apps: [
3422 "TesterHelpAppFoo",
3423 ],
3424 }
3425
3426 apex_key {
3427 name: "myapex.key",
3428 public_key: "testkey.avbpubkey",
3429 private_key: "testkey.pem",
3430 }
3431
3432 android_test_helper_app {
3433 name: "TesterHelpAppFoo",
3434 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003435 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003436 }
3437
3438 `)
3439
3440 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3441 apexRule := module.Rule("apexRule")
3442 copyCmds := apexRule.Args["copy_commands"]
3443
3444 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3445}
3446
Jooyung Han18020ea2019-11-13 10:50:48 +09003447func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3448 // libfoo's apex_available comes from cc_defaults
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003449 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003450 apex {
3451 name: "myapex",
3452 key: "myapex.key",
3453 native_shared_libs: ["libfoo"],
3454 }
3455
3456 apex_key {
3457 name: "myapex.key",
3458 public_key: "testkey.avbpubkey",
3459 private_key: "testkey.pem",
3460 }
3461
3462 apex {
3463 name: "otherapex",
3464 key: "myapex.key",
3465 native_shared_libs: ["libfoo"],
3466 }
3467
3468 cc_defaults {
3469 name: "libfoo-defaults",
3470 apex_available: ["otherapex"],
3471 }
3472
3473 cc_library {
3474 name: "libfoo",
3475 defaults: ["libfoo-defaults"],
3476 stl: "none",
3477 system_shared_libs: [],
3478 }`)
3479}
3480
Paul Duffinde7464c2020-03-30 17:54:29 +01003481func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003482 // libfoo is not available to myapex, but only to otherapex
3483 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3484 apex {
3485 name: "myapex",
3486 key: "myapex.key",
3487 native_shared_libs: ["libfoo"],
3488 }
3489
3490 apex_key {
3491 name: "myapex.key",
3492 public_key: "testkey.avbpubkey",
3493 private_key: "testkey.pem",
3494 }
3495
3496 apex {
3497 name: "otherapex",
3498 key: "otherapex.key",
3499 native_shared_libs: ["libfoo"],
3500 }
3501
3502 apex_key {
3503 name: "otherapex.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 }`)
Paul Duffinde7464c2020-03-30 17:54:29 +01003514}
Jiyong Park127b40b2019-09-30 16:04:35 +09003515
Paul Duffinde7464c2020-03-30 17:54:29 +01003516func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003517 // libbbaz is an indirect dep
Paul Duffin868ecfd2020-03-30 17:58:21 +01003518 testApexError(t, `requires "libbaz" that is not available for the APEX. Dependency path:
Paul Duffinf0207962020-03-31 11:31:36 +01003519.*via tag apex\.dependencyTag.*"sharedLib".*
Paul Duffin868ecfd2020-03-30 17:58:21 +01003520.*-> libfoo.*link:shared.*
Paul Duffinb20ad0a2020-03-31 15:23:40 +01003521.*via tag cc\.DependencyTag.*"shared".*
Paul Duffin868ecfd2020-03-30 17:58:21 +01003522.*-> libbar.*link:shared.*
Paul Duffinb20ad0a2020-03-31 15:23:40 +01003523.*via tag cc\.DependencyTag.*"shared".*
3524.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003525 apex {
3526 name: "myapex",
3527 key: "myapex.key",
3528 native_shared_libs: ["libfoo"],
3529 }
3530
3531 apex_key {
3532 name: "myapex.key",
3533 public_key: "testkey.avbpubkey",
3534 private_key: "testkey.pem",
3535 }
3536
Jiyong Park127b40b2019-09-30 16:04:35 +09003537 cc_library {
3538 name: "libfoo",
3539 stl: "none",
3540 shared_libs: ["libbar"],
3541 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003542 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003543 }
3544
3545 cc_library {
3546 name: "libbar",
3547 stl: "none",
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003548 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003549 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003550 apex_available: ["myapex"],
3551 }
3552
3553 cc_library {
3554 name: "libbaz",
3555 stl: "none",
3556 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003557 }`)
Paul Duffinde7464c2020-03-30 17:54:29 +01003558}
Jiyong Park127b40b2019-09-30 16:04:35 +09003559
Paul Duffinde7464c2020-03-30 17:54:29 +01003560func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003561 testApexError(t, "\"otherapex\" is not a valid module name", `
3562 apex {
3563 name: "myapex",
3564 key: "myapex.key",
3565 native_shared_libs: ["libfoo"],
3566 }
3567
3568 apex_key {
3569 name: "myapex.key",
3570 public_key: "testkey.avbpubkey",
3571 private_key: "testkey.pem",
3572 }
3573
3574 cc_library {
3575 name: "libfoo",
3576 stl: "none",
3577 system_shared_libs: [],
3578 apex_available: ["otherapex"],
3579 }`)
3580
Paul Duffinde7464c2020-03-30 17:54:29 +01003581 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003582 apex {
3583 name: "myapex",
3584 key: "myapex.key",
3585 native_shared_libs: ["libfoo", "libbar"],
3586 }
3587
3588 apex_key {
3589 name: "myapex.key",
3590 public_key: "testkey.avbpubkey",
3591 private_key: "testkey.pem",
3592 }
3593
3594 cc_library {
3595 name: "libfoo",
3596 stl: "none",
3597 system_shared_libs: [],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003598 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003599 apex_available: ["myapex"],
3600 }
3601
3602 cc_library {
3603 name: "libbar",
3604 stl: "none",
3605 system_shared_libs: [],
3606 apex_available: ["//apex_available:anyapex"],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003607 }
3608
3609 cc_library {
3610 name: "libbaz",
3611 stl: "none",
3612 system_shared_libs: [],
3613 stubs: {
3614 versions: ["10", "20", "30"],
3615 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003616 }`)
Paul Duffinde7464c2020-03-30 17:54:29 +01003617}
Jiyong Park127b40b2019-09-30 16:04:35 +09003618
Paul Duffinde7464c2020-03-30 17:54:29 +01003619func TestApexAvailable_CreatedForPlatform(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003620 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003621 // TODO(jiyong) the checks for the platform variant are removed because we now create
3622 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3623 // the platform variants are not used from other platform modules. When that is done,
3624 // these checks will be replaced by expecting a specific error message that will be
3625 // emitted when the platform variant is used.
3626 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3627 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3628 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3629 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003630
Paul Duffinde7464c2020-03-30 17:54:29 +01003631 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003632 apex {
3633 name: "myapex",
3634 key: "myapex.key",
3635 }
3636
3637 apex_key {
3638 name: "myapex.key",
3639 public_key: "testkey.avbpubkey",
3640 private_key: "testkey.pem",
3641 }
3642
3643 cc_library {
3644 name: "libfoo",
3645 stl: "none",
3646 system_shared_libs: [],
3647 apex_available: ["//apex_available:platform"],
3648 }`)
3649
3650 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003651 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3652 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Paul Duffinde7464c2020-03-30 17:54:29 +01003653}
Jiyong Parka90ca002019-10-07 15:47:24 +09003654
Paul Duffinde7464c2020-03-30 17:54:29 +01003655func TestApexAvailable_CreatedForApex(t *testing.T) {
3656 testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09003657 apex {
3658 name: "myapex",
3659 key: "myapex.key",
3660 native_shared_libs: ["libfoo"],
3661 }
3662
3663 apex_key {
3664 name: "myapex.key",
3665 public_key: "testkey.avbpubkey",
3666 private_key: "testkey.pem",
3667 }
3668
3669 cc_library {
3670 name: "libfoo",
3671 stl: "none",
3672 system_shared_libs: [],
3673 apex_available: ["myapex"],
3674 static: {
3675 apex_available: ["//apex_available:platform"],
3676 },
3677 }`)
3678
3679 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003680 // TODO(jiyong) the checks for the platform variant are removed because we now create
3681 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3682 // the platform variants are not used from other platform modules. When that is done,
3683 // these checks will be replaced by expecting a specific error message that will be
3684 // emitted when the platform variant is used.
3685 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3686 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3687 // // but the static variant is available to both myapex and the platform
3688 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3689 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003690}
3691
Jiyong Park5d790c32019-11-15 18:40:32 +09003692func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003693 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003694 apex {
3695 name: "myapex",
3696 key: "myapex.key",
3697 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003698 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003699 }
3700
3701 override_apex {
3702 name: "override_myapex",
3703 base: "myapex",
3704 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003705 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003706 logging_parent: "com.foo.bar",
Baligh Uddincb6aa122020-03-15 13:01:05 -07003707 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09003708 }
3709
3710 apex_key {
3711 name: "myapex.key",
3712 public_key: "testkey.avbpubkey",
3713 private_key: "testkey.pem",
3714 }
3715
3716 android_app {
3717 name: "app",
3718 srcs: ["foo/bar/MyClass.java"],
3719 package_name: "foo",
3720 sdk_version: "none",
3721 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003722 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003723 }
3724
3725 override_android_app {
3726 name: "override_app",
3727 base: "app",
3728 package_name: "bar",
3729 }
Jiyong Parka519c542020-03-03 11:45:41 +09003730 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003731
Jiyong Park317645e2019-12-05 13:20:58 +09003732 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3733 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3734 if originalVariant.GetOverriddenBy() != "" {
3735 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3736 }
3737 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3738 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3739 }
3740
Jiyong Park5d790c32019-11-15 18:40:32 +09003741 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3742 apexRule := module.Rule("apexRule")
3743 copyCmds := apexRule.Args["copy_commands"]
3744
3745 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3746 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003747
3748 apexBundle := module.Module().(*apexBundle)
3749 name := apexBundle.Name()
3750 if name != "override_myapex" {
3751 t.Errorf("name should be \"override_myapex\", but was %q", name)
3752 }
3753
Baligh Uddin004d7172020-02-19 21:29:28 -08003754 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3755 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3756 }
3757
Jiyong Parka519c542020-03-03 11:45:41 +09003758 optFlags := apexRule.Args["opt_flags"]
Baligh Uddincb6aa122020-03-15 13:01:05 -07003759 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Parka519c542020-03-03 11:45:41 +09003760
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003761 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3762 var builder strings.Builder
3763 data.Custom(&builder, name, "TARGET_", "", data)
3764 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003765 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003766 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3767 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003768 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003769 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003770 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003771 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3772 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003773}
3774
Jooyung Han214bf372019-11-12 13:03:50 +09003775func TestLegacyAndroid10Support(t *testing.T) {
3776 ctx, _ := testApex(t, `
3777 apex {
3778 name: "myapex",
3779 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003780 native_shared_libs: ["mylib"],
Jooyung Han23b0adf2020-03-12 18:37:20 +09003781 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09003782 }
3783
3784 apex_key {
3785 name: "myapex.key",
3786 public_key: "testkey.avbpubkey",
3787 private_key: "testkey.pem",
3788 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003789
3790 cc_library {
3791 name: "mylib",
3792 srcs: ["mylib.cpp"],
3793 stl: "libc++",
3794 system_shared_libs: [],
3795 apex_available: [ "myapex" ],
3796 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003797 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003798
3799 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3800 args := module.Rule("apexRule").Args
3801 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003802 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003803
3804 // The copies of the libraries in the apex should have one more dependency than
3805 // the ones outside the apex, namely the unwinder. Ideally we should check
3806 // the dependency names directly here but for some reason the names are blank in
3807 // this test.
3808 for _, lib := range []string{"libc++", "mylib"} {
3809 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3810 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3811 if len(apexImplicits) != len(nonApexImplicits)+1 {
3812 t.Errorf("%q missing unwinder dep", lib)
3813 }
3814 }
Jooyung Han214bf372019-11-12 13:03:50 +09003815}
3816
Jooyung Han58f26ab2019-12-18 15:34:32 +09003817func TestJavaSDKLibrary(t *testing.T) {
3818 ctx, _ := testApex(t, `
3819 apex {
3820 name: "myapex",
3821 key: "myapex.key",
3822 java_libs: ["foo"],
3823 }
3824
3825 apex_key {
3826 name: "myapex.key",
3827 public_key: "testkey.avbpubkey",
3828 private_key: "testkey.pem",
3829 }
3830
3831 java_sdk_library {
3832 name: "foo",
3833 srcs: ["a.java"],
3834 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003835 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003836 }
3837 `, withFiles(map[string][]byte{
3838 "api/current.txt": nil,
3839 "api/removed.txt": nil,
3840 "api/system-current.txt": nil,
3841 "api/system-removed.txt": nil,
3842 "api/test-current.txt": nil,
3843 "api/test-removed.txt": nil,
3844 }))
3845
3846 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003847 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003848 "javalib/foo.jar",
3849 "etc/permissions/foo.xml",
3850 })
3851 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003852 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3853 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003854}
3855
atrost6e126252020-01-27 17:01:16 +00003856func TestCompatConfig(t *testing.T) {
3857 ctx, _ := testApex(t, `
3858 apex {
3859 name: "myapex",
3860 key: "myapex.key",
3861 prebuilts: ["myjar-platform-compat-config"],
3862 java_libs: ["myjar"],
3863 }
3864
3865 apex_key {
3866 name: "myapex.key",
3867 public_key: "testkey.avbpubkey",
3868 private_key: "testkey.pem",
3869 }
3870
3871 platform_compat_config {
3872 name: "myjar-platform-compat-config",
3873 src: ":myjar",
3874 }
3875
3876 java_library {
3877 name: "myjar",
3878 srcs: ["foo/bar/MyClass.java"],
3879 sdk_version: "none",
3880 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003881 apex_available: [ "myapex" ],
3882 }
3883 `)
3884 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3885 "etc/compatconfig/myjar-platform-compat-config.xml",
3886 "javalib/myjar.jar",
3887 })
3888}
3889
Jiyong Park479321d2019-12-16 11:47:12 +09003890func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3891 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3892 apex {
3893 name: "myapex",
3894 key: "myapex.key",
3895 java_libs: ["myjar"],
3896 }
3897
3898 apex_key {
3899 name: "myapex.key",
3900 public_key: "testkey.avbpubkey",
3901 private_key: "testkey.pem",
3902 }
3903
3904 java_library {
3905 name: "myjar",
3906 srcs: ["foo/bar/MyClass.java"],
3907 sdk_version: "none",
3908 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003909 compile_dex: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003910 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003911 }
3912 `)
3913}
3914
Jiyong Park7afd1072019-12-30 16:56:33 +09003915func TestCarryRequiredModuleNames(t *testing.T) {
3916 ctx, config := testApex(t, `
3917 apex {
3918 name: "myapex",
3919 key: "myapex.key",
3920 native_shared_libs: ["mylib"],
3921 }
3922
3923 apex_key {
3924 name: "myapex.key",
3925 public_key: "testkey.avbpubkey",
3926 private_key: "testkey.pem",
3927 }
3928
3929 cc_library {
3930 name: "mylib",
3931 srcs: ["mylib.cpp"],
3932 system_shared_libs: [],
3933 stl: "none",
3934 required: ["a", "b"],
3935 host_required: ["c", "d"],
3936 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003937 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003938 }
3939 `)
3940
3941 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3942 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3943 name := apexBundle.BaseModuleName()
3944 prefix := "TARGET_"
3945 var builder strings.Builder
3946 data.Custom(&builder, name, prefix, "", data)
3947 androidMk := builder.String()
3948 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3949 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3950 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3951}
3952
Jiyong Park7cd10e32020-01-14 09:22:18 +09003953func TestSymlinksFromApexToSystem(t *testing.T) {
3954 bp := `
3955 apex {
3956 name: "myapex",
3957 key: "myapex.key",
3958 native_shared_libs: ["mylib"],
3959 java_libs: ["myjar"],
3960 }
3961
Jiyong Park9d677202020-02-19 16:29:35 +09003962 apex {
3963 name: "myapex.updatable",
3964 key: "myapex.key",
3965 native_shared_libs: ["mylib"],
3966 java_libs: ["myjar"],
3967 updatable: true,
3968 }
3969
Jiyong Park7cd10e32020-01-14 09:22:18 +09003970 apex_key {
3971 name: "myapex.key",
3972 public_key: "testkey.avbpubkey",
3973 private_key: "testkey.pem",
3974 }
3975
3976 cc_library {
3977 name: "mylib",
3978 srcs: ["mylib.cpp"],
3979 shared_libs: ["myotherlib"],
3980 system_shared_libs: [],
3981 stl: "none",
3982 apex_available: [
3983 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003984 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003985 "//apex_available:platform",
3986 ],
3987 }
3988
3989 cc_library {
3990 name: "myotherlib",
3991 srcs: ["mylib.cpp"],
3992 system_shared_libs: [],
3993 stl: "none",
3994 apex_available: [
3995 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003996 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003997 "//apex_available:platform",
3998 ],
3999 }
4000
4001 java_library {
4002 name: "myjar",
4003 srcs: ["foo/bar/MyClass.java"],
4004 sdk_version: "none",
4005 system_modules: "none",
4006 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09004007 apex_available: [
4008 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004009 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004010 "//apex_available:platform",
4011 ],
4012 }
4013
4014 java_library {
4015 name: "myotherjar",
4016 srcs: ["foo/bar/MyClass.java"],
4017 sdk_version: "none",
4018 system_modules: "none",
4019 apex_available: [
4020 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004021 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004022 "//apex_available:platform",
4023 ],
4024 }
4025 `
4026
4027 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
4028 for _, f := range files {
4029 if f.path == file {
4030 if f.isLink {
4031 t.Errorf("%q is not a real file", file)
4032 }
4033 return
4034 }
4035 }
4036 t.Errorf("%q is not found", file)
4037 }
4038
4039 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
4040 for _, f := range files {
4041 if f.path == file {
4042 if !f.isLink {
4043 t.Errorf("%q is not a symlink", file)
4044 }
4045 return
4046 }
4047 }
4048 t.Errorf("%q is not found", file)
4049 }
4050
Jiyong Park9d677202020-02-19 16:29:35 +09004051 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4052 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004053 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004054 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004055 ensureRealfileExists(t, files, "javalib/myjar.jar")
4056 ensureRealfileExists(t, files, "lib64/mylib.so")
4057 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4058
Jiyong Park9d677202020-02-19 16:29:35 +09004059 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4060 ensureRealfileExists(t, files, "javalib/myjar.jar")
4061 ensureRealfileExists(t, files, "lib64/mylib.so")
4062 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4063
4064 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004065 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004066 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004067 ensureRealfileExists(t, files, "javalib/myjar.jar")
4068 ensureRealfileExists(t, files, "lib64/mylib.so")
4069 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004070
4071 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4072 ensureRealfileExists(t, files, "javalib/myjar.jar")
4073 ensureRealfileExists(t, files, "lib64/mylib.so")
4074 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004075}
4076
Jiyong Parkd93e1b12020-02-28 15:22:21 +09004077func TestAppBundle(t *testing.T) {
4078 ctx, _ := testApex(t, `
4079 apex {
4080 name: "myapex",
4081 key: "myapex.key",
4082 apps: ["AppFoo"],
4083 }
4084
4085 apex_key {
4086 name: "myapex.key",
4087 public_key: "testkey.avbpubkey",
4088 private_key: "testkey.pem",
4089 }
4090
4091 android_app {
4092 name: "AppFoo",
4093 srcs: ["foo/bar/MyClass.java"],
4094 sdk_version: "none",
4095 system_modules: "none",
4096 apex_available: [ "myapex" ],
4097 }
Jiyong Parkaf8998c2020-02-28 16:51:07 +09004098 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkd93e1b12020-02-28 15:22:21 +09004099
4100 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4101 content := bundleConfigRule.Args["content"]
4102
4103 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkaf8998c2020-02-28 16:51:07 +09004104 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 +09004105}
4106
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004107func testNoUpdatableJarsInBootImage(t *testing.T, errmsg, bp string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
4108 t.Helper()
4109
4110 bp = bp + `
4111 filegroup {
4112 name: "some-updatable-apex-file_contexts",
4113 srcs: [
4114 "system/sepolicy/apex/some-updatable-apex-file_contexts",
4115 ],
4116 }
4117 `
4118 bp += cc.GatherRequiredDepsForTest(android.Android)
4119 bp += java.GatherRequiredDepsForTest()
4120 bp += dexpreopt.BpToolModulesForTest()
4121
4122 fs := map[string][]byte{
4123 "a.java": nil,
4124 "a.jar": nil,
4125 "build/make/target/product/security": nil,
4126 "apex_manifest.json": nil,
4127 "AndroidManifest.xml": nil,
4128 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
4129 "system/sepolicy/apex/com.android.art.something-file_contexts": nil,
4130 "framework/aidl/a.aidl": nil,
4131 }
4132 cc.GatherRequiredFilesForTest(fs)
4133
4134 ctx := android.NewTestArchContext()
4135 ctx.RegisterModuleType("apex", BundleFactory)
4136 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
4137 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
4138 cc.RegisterRequiredBuildComponentsForTest(ctx)
4139 java.RegisterJavaBuildComponents(ctx)
4140 java.RegisterSystemModulesBuildComponents(ctx)
4141 java.RegisterAppBuildComponents(ctx)
4142 java.RegisterDexpreoptBootJarsComponents(ctx)
4143 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
4144 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
4145 ctx.PreDepsMutators(RegisterPreDepsMutators)
4146 ctx.PostDepsMutators(RegisterPostDepsMutators)
4147
4148 config := android.TestArchConfig(buildDir, nil, bp, fs)
4149 ctx.Register(config)
4150
4151 _ = dexpreopt.GlobalSoongConfigForTests(config)
4152 dexpreopt.RegisterToolModulesForTest(ctx)
4153 pathCtx := android.PathContextForTesting(config)
4154 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
4155 transformDexpreoptConfig(dexpreoptConfig)
4156 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
4157
4158 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
4159 android.FailIfErrored(t, errs)
4160
4161 _, errs = ctx.PrepareBuildActions(config)
4162 if errmsg == "" {
4163 android.FailIfErrored(t, errs)
4164 } else if len(errs) > 0 {
4165 android.FailIfNoMatchingErrors(t, errmsg, errs)
4166 return
4167 } else {
4168 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
4169 }
4170}
4171
4172func TestNoUpdatableJarsInBootImage(t *testing.T) {
4173 bp := `
4174 java_library {
4175 name: "some-updatable-apex-lib",
4176 srcs: ["a.java"],
4177 apex_available: [
4178 "some-updatable-apex",
4179 ],
4180 }
4181
4182 java_library {
4183 name: "some-platform-lib",
4184 srcs: ["a.java"],
4185 installable: true,
4186 }
4187
4188 java_library {
4189 name: "some-art-lib",
4190 srcs: ["a.java"],
4191 apex_available: [
4192 "com.android.art.something",
4193 ],
4194 hostdex: true,
4195 }
4196
4197 apex {
4198 name: "some-updatable-apex",
4199 key: "some-updatable-apex.key",
4200 java_libs: ["some-updatable-apex-lib"],
4201 }
4202
4203 apex_key {
4204 name: "some-updatable-apex.key",
4205 }
4206
4207 apex {
4208 name: "com.android.art.something",
4209 key: "com.android.art.something.key",
4210 java_libs: ["some-art-lib"],
4211 }
4212
4213 apex_key {
4214 name: "com.android.art.something.key",
4215 }
4216 `
4217
4218 var error string
4219 var transform func(*dexpreopt.GlobalConfig)
4220
4221 // updatable jar from ART apex in the ART boot image => ok
4222 transform = func(config *dexpreopt.GlobalConfig) {
4223 config.ArtApexJars = []string{"some-art-lib"}
4224 }
4225 testNoUpdatableJarsInBootImage(t, "", bp, transform)
4226
4227 // updatable jar from ART apex in the framework boot image => error
4228 error = "module 'some-art-lib' from updatable apex 'com.android.art.something' is not allowed in the framework boot image"
4229 transform = func(config *dexpreopt.GlobalConfig) {
4230 config.BootJars = []string{"some-art-lib"}
4231 }
4232 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4233
4234 // updatable jar from some other apex in the ART boot image => error
4235 error = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the ART boot image"
4236 transform = func(config *dexpreopt.GlobalConfig) {
4237 config.ArtApexJars = []string{"some-updatable-apex-lib"}
4238 }
4239 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4240
4241 // updatable jar from some other apex in the framework boot image => error
4242 error = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the framework boot image"
4243 transform = func(config *dexpreopt.GlobalConfig) {
4244 config.BootJars = []string{"some-updatable-apex-lib"}
4245 }
4246 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4247
4248 // nonexistent jar in the ART boot image => error
4249 error = "failed to find a dex jar path for module 'nonexistent'"
4250 transform = func(config *dexpreopt.GlobalConfig) {
4251 config.ArtApexJars = []string{"nonexistent"}
4252 }
4253 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4254
4255 // nonexistent jar in the framework boot image => error
4256 error = "failed to find a dex jar path for module 'nonexistent'"
4257 transform = func(config *dexpreopt.GlobalConfig) {
4258 config.BootJars = []string{"nonexistent"}
4259 }
4260 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4261
4262 // platform jar in the ART boot image => error
4263 error = "module 'some-platform-lib' is part of the platform and not allowed in the ART boot image"
4264 transform = func(config *dexpreopt.GlobalConfig) {
4265 config.ArtApexJars = []string{"some-platform-lib"}
4266 }
4267 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4268
4269 // platform jar in the framework boot image => ok
4270 transform = func(config *dexpreopt.GlobalConfig) {
4271 config.BootJars = []string{"some-platform-lib"}
4272 }
4273 testNoUpdatableJarsInBootImage(t, "", bp, transform)
4274}
4275
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004276func TestMain(m *testing.M) {
4277 run := func() int {
4278 setUp()
4279 defer tearDown()
4280
4281 return m.Run()
4282 }
4283
4284 os.Exit(run())
4285}