blob: 674446af27e59d3fe9376e1a619ce89b02c7cdfd [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jiyong Parkcfaa1642020-02-28 16:51:07 +090090func withManifestPackageNameOverrides(specs []string) testCustomizer {
91 return func(fs map[string][]byte, config android.Config) {
92 config.TestProductVariables.ManifestPackageNameOverrides = specs
93 }
94}
95
Jooyung Han31c470b2019-10-18 16:26:59 +090096func withBinder32bit(fs map[string][]byte, config android.Config) {
97 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
98}
99
Jiyong Park7cd10e32020-01-14 09:22:18 +0900100func withUnbundledBuild(fs map[string][]byte, config android.Config) {
101 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
102}
103
Jooyung Han344d5432019-08-23 11:17:39 +0900104func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +0900105 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900106
107 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900108 filegroup {
109 name: "myapex-file_contexts",
110 srcs: [
111 "system/sepolicy/apex/myapex-file_contexts",
112 ],
113 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900114 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800115
Colin Crossf9aabd72020-02-15 11:29:50 -0800116 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
117
Dario Frenicde2a032019-10-27 00:29:22 +0100118 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900119
Jooyung Han344d5432019-08-23 11:17:39 +0900120 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900121 "a.java": nil,
122 "PrebuiltAppFoo.apk": nil,
123 "PrebuiltAppFooPriv.apk": nil,
124 "build/make/target/product/security": nil,
125 "apex_manifest.json": nil,
126 "AndroidManifest.xml": nil,
127 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900128 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900129 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900130 "system/sepolicy/apex/otherapex-file_contexts": nil,
131 "system/sepolicy/apex/commonapex-file_contexts": nil,
132 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800133 "mylib.cpp": nil,
134 "mylib_common.cpp": nil,
135 "mytest.cpp": nil,
136 "mytest1.cpp": nil,
137 "mytest2.cpp": nil,
138 "mytest3.cpp": nil,
139 "myprebuilt": nil,
140 "my_include": nil,
141 "foo/bar/MyClass.java": nil,
142 "prebuilt.jar": nil,
143 "vendor/foo/devkeys/test.x509.pem": nil,
144 "vendor/foo/devkeys/test.pk8": nil,
145 "testkey.x509.pem": nil,
146 "testkey.pk8": nil,
147 "testkey.override.x509.pem": nil,
148 "testkey.override.pk8": nil,
149 "vendor/foo/devkeys/testkey.avbpubkey": nil,
150 "vendor/foo/devkeys/testkey.pem": nil,
151 "NOTICE": nil,
152 "custom_notice": nil,
153 "testkey2.avbpubkey": nil,
154 "testkey2.pem": nil,
155 "myapex-arm64.apex": nil,
156 "myapex-arm.apex": nil,
157 "frameworks/base/api/current.txt": nil,
158 "framework/aidl/a.aidl": nil,
159 "build/make/core/proguard.flags": nil,
160 "build/make/core/proguard_basic_keeps.flags": nil,
161 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900162 }
163
Colin Crossf9aabd72020-02-15 11:29:50 -0800164 cc.GatherRequiredFilesForTest(fs)
165
Jooyung Han344d5432019-08-23 11:17:39 +0900166 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800167 // The fs now needs to be populated before creating the config, call handlers twice
168 // for now, once to get any fs changes, and later after the config was created to
169 // set product variables or targets.
170 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
171 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900172 }
173
Colin Cross98be1bb2019-12-13 20:41:13 -0800174 config := android.TestArchConfig(buildDir, nil, bp, fs)
175 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
176 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
177 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
178 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
179 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
180 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
181
182 for _, handler := range handlers {
183 // The fs now needs to be populated before creating the config, call handlers twice
184 // for now, earlier to get any fs changes, and now after the config was created to
185 // set product variables or targets.
186 tempFS := map[string][]byte{}
187 handler(tempFS, config)
188 }
189
190 ctx := android.NewTestArchContext()
191 ctx.RegisterModuleType("apex", BundleFactory)
192 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
193 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
194 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
195 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
196 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
197 ctx.RegisterModuleType("override_apex", overrideApexFactory)
198
Jooyung Hana57af4a2020-01-23 05:36:59 +0000199 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
200 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
201
Paul Duffin77980a82019-12-19 16:01:36 +0000202 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800203 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800204 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
205 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800206 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000207 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800208 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800209 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000210 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000211 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000212 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900213 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800214
Colin Cross98be1bb2019-12-13 20:41:13 -0800215 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800216 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800217
218 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900219
Jooyung Han5c998b92019-06-27 11:30:33 +0900220 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900221}
222
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700223func setUp() {
224 var err error
225 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900226 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700227 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900228 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229}
230
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700231func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900232 os.RemoveAll(buildDir)
233}
234
Jooyung Han643adc42020-02-27 13:50:06 +0900235// ensure that 'result' equals 'expected'
236func ensureEquals(t *testing.T, result string, expected string) {
237 t.Helper()
238 if result != expected {
239 t.Errorf("%q != %q", expected, result)
240 }
241}
242
Jiyong Park25fc6a92018-11-18 18:02:45 +0900243// ensure that 'result' contains 'expected'
244func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900245 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900246 if !strings.Contains(result, expected) {
247 t.Errorf("%q is not found in %q", expected, result)
248 }
249}
250
251// ensures that 'result' does not contain 'notExpected'
252func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900253 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900254 if strings.Contains(result, notExpected) {
255 t.Errorf("%q is found in %q", notExpected, result)
256 }
257}
258
259func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900260 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261 if !android.InList(expected, result) {
262 t.Errorf("%q is not found in %v", expected, result)
263 }
264}
265
266func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900267 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900268 if android.InList(notExpected, result) {
269 t.Errorf("%q is found in %v", notExpected, result)
270 }
271}
272
Jooyung Hane1633032019-08-01 17:41:43 +0900273func ensureListEmpty(t *testing.T, result []string) {
274 t.Helper()
275 if len(result) > 0 {
276 t.Errorf("%q is expected to be empty", result)
277 }
278}
279
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280// Minimal test
281func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700282 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900283 apex_defaults {
284 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900285 manifest: ":myapex.manifest",
286 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 key: "myapex.key",
288 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800289 multilib: {
290 both: {
291 binaries: ["foo",],
292 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900293 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900294 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900295 }
296
Jiyong Park30ca9372019-02-07 16:27:23 +0900297 apex {
298 name: "myapex",
299 defaults: ["myapex-defaults"],
300 }
301
Jiyong Park25fc6a92018-11-18 18:02:45 +0900302 apex_key {
303 name: "myapex.key",
304 public_key: "testkey.avbpubkey",
305 private_key: "testkey.pem",
306 }
307
Jiyong Park809bb722019-02-13 21:33:49 +0900308 filegroup {
309 name: "myapex.manifest",
310 srcs: ["apex_manifest.json"],
311 }
312
313 filegroup {
314 name: "myapex.androidmanifest",
315 srcs: ["AndroidManifest.xml"],
316 }
317
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318 cc_library {
319 name: "mylib",
320 srcs: ["mylib.cpp"],
321 shared_libs: ["mylib2"],
322 system_shared_libs: [],
323 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000324 // TODO: remove //apex_available:platform
325 apex_available: [
326 "//apex_available:platform",
327 "myapex",
328 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900329 }
330
Alex Light3d673592019-01-18 14:37:31 -0800331 cc_binary {
332 name: "foo",
333 srcs: ["mylib.cpp"],
334 compile_multilib: "both",
335 multilib: {
336 lib32: {
337 suffix: "32",
338 },
339 lib64: {
340 suffix: "64",
341 },
342 },
343 symlinks: ["foo_link_"],
344 symlink_preferred_arch: true,
345 system_shared_libs: [],
346 static_executable: true,
347 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000348 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800349 }
350
Jiyong Park25fc6a92018-11-18 18:02:45 +0900351 cc_library {
352 name: "mylib2",
353 srcs: ["mylib.cpp"],
354 system_shared_libs: [],
355 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900356 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000357 // TODO: remove //apex_available:platform
358 apex_available: [
359 "//apex_available:platform",
360 "myapex",
361 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900363
364 java_library {
365 name: "myjar",
366 srcs: ["foo/bar/MyClass.java"],
367 sdk_version: "none",
368 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900369 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900370 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000371 // TODO: remove //apex_available:platform
372 apex_available: [
373 "//apex_available:platform",
374 "myapex",
375 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900376 }
377
378 java_library {
379 name: "myotherjar",
380 srcs: ["foo/bar/MyClass.java"],
381 sdk_version: "none",
382 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900383 // TODO: remove //apex_available:platform
384 apex_available: [
385 "//apex_available:platform",
386 "myapex",
387 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900388 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900389
390 java_library {
391 name: "mysharedjar",
392 srcs: ["foo/bar/MyClass.java"],
393 sdk_version: "none",
394 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900395 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900396 `)
397
Sundong Ahnabb64432019-10-22 13:58:29 +0900398 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900399
400 optFlags := apexRule.Args["opt_flags"]
401 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700402 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900403 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900404
Jiyong Park25fc6a92018-11-18 18:02:45 +0900405 copyCmds := apexRule.Args["copy_commands"]
406
407 // Ensure that main rule creates an output
408 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
409
410 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800411 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900412 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900413
414 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800415 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900416 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900417
418 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800419 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
420 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900421 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
422 // .. but not for java libs
423 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900424 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800425
Colin Cross7113d202019-11-20 16:39:12 -0800426 // Ensure that the platform variant ends with _shared or _common
427 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
428 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900429 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
430 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900431 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
432
433 // Ensure that dynamic dependency to java libs are not included
434 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800435
436 // Ensure that all symlinks are present.
437 found_foo_link_64 := false
438 found_foo := false
439 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900440 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800441 if strings.HasSuffix(cmd, "bin/foo") {
442 found_foo = true
443 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
444 found_foo_link_64 = true
445 }
446 }
447 }
448 good := found_foo && found_foo_link_64
449 if !good {
450 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
451 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900452
Sundong Ahnabb64432019-10-22 13:58:29 +0900453 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700454 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700455 if len(noticeInputs) != 2 {
456 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900457 }
458 ensureListContains(t, noticeInputs, "NOTICE")
459 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900460
461 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 +0900462 ensureListContains(t, depsInfo, "myjar <- myapex")
463 ensureListContains(t, depsInfo, "mylib <- myapex")
464 ensureListContains(t, depsInfo, "mylib2 <- mylib")
465 ensureListContains(t, depsInfo, "myotherjar <- myjar")
466 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800467}
468
Jooyung Hanf21c7972019-12-16 22:32:06 +0900469func TestDefaults(t *testing.T) {
470 ctx, _ := testApex(t, `
471 apex_defaults {
472 name: "myapex-defaults",
473 key: "myapex.key",
474 prebuilts: ["myetc"],
475 native_shared_libs: ["mylib"],
476 java_libs: ["myjar"],
477 apps: ["AppFoo"],
478 }
479
480 prebuilt_etc {
481 name: "myetc",
482 src: "myprebuilt",
483 }
484
485 apex {
486 name: "myapex",
487 defaults: ["myapex-defaults"],
488 }
489
490 apex_key {
491 name: "myapex.key",
492 public_key: "testkey.avbpubkey",
493 private_key: "testkey.pem",
494 }
495
496 cc_library {
497 name: "mylib",
498 system_shared_libs: [],
499 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000500 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900501 }
502
503 java_library {
504 name: "myjar",
505 srcs: ["foo/bar/MyClass.java"],
506 sdk_version: "none",
507 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000508 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900509 }
510
511 android_app {
512 name: "AppFoo",
513 srcs: ["foo/bar/MyClass.java"],
514 sdk_version: "none",
515 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000516 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900517 }
518 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000519 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900520 "etc/myetc",
521 "javalib/myjar.jar",
522 "lib64/mylib.so",
523 "app/AppFoo/AppFoo.apk",
524 })
525}
526
Jooyung Han01a3ee22019-11-02 02:52:25 +0900527func TestApexManifest(t *testing.T) {
528 ctx, _ := testApex(t, `
529 apex {
530 name: "myapex",
531 key: "myapex.key",
532 }
533
534 apex_key {
535 name: "myapex.key",
536 public_key: "testkey.avbpubkey",
537 private_key: "testkey.pem",
538 }
539 `)
540
541 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900542 args := module.Rule("apexRule").Args
543 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
544 t.Error("manifest should be apex_manifest.pb, but " + manifest)
545 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900546}
547
Alex Light5098a612018-11-29 17:12:15 -0800548func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700549 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800550 apex {
551 name: "myapex",
552 key: "myapex.key",
553 payload_type: "zip",
554 native_shared_libs: ["mylib"],
555 }
556
557 apex_key {
558 name: "myapex.key",
559 public_key: "testkey.avbpubkey",
560 private_key: "testkey.pem",
561 }
562
563 cc_library {
564 name: "mylib",
565 srcs: ["mylib.cpp"],
566 shared_libs: ["mylib2"],
567 system_shared_libs: [],
568 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000569 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800570 }
571
572 cc_library {
573 name: "mylib2",
574 srcs: ["mylib.cpp"],
575 system_shared_libs: [],
576 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000577 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800578 }
579 `)
580
Sundong Ahnabb64432019-10-22 13:58:29 +0900581 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800582 copyCmds := zipApexRule.Args["copy_commands"]
583
584 // Ensure that main rule creates an output
585 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
586
587 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800588 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800589
590 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800591 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800592
593 // Ensure that both direct and indirect deps are copied into apex
594 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
595 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900596}
597
598func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700599 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900600 apex {
601 name: "myapex",
602 key: "myapex.key",
603 native_shared_libs: ["mylib", "mylib3"],
604 }
605
606 apex_key {
607 name: "myapex.key",
608 public_key: "testkey.avbpubkey",
609 private_key: "testkey.pem",
610 }
611
612 cc_library {
613 name: "mylib",
614 srcs: ["mylib.cpp"],
615 shared_libs: ["mylib2", "mylib3"],
616 system_shared_libs: [],
617 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000618 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900619 }
620
621 cc_library {
622 name: "mylib2",
623 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900624 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900625 system_shared_libs: [],
626 stl: "none",
627 stubs: {
628 versions: ["1", "2", "3"],
629 },
630 }
631
632 cc_library {
633 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900634 srcs: ["mylib.cpp"],
635 shared_libs: ["mylib4"],
636 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900637 stl: "none",
638 stubs: {
639 versions: ["10", "11", "12"],
640 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000641 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900642 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900643
644 cc_library {
645 name: "mylib4",
646 srcs: ["mylib.cpp"],
647 system_shared_libs: [],
648 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900650 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900651 `)
652
Sundong Ahnabb64432019-10-22 13:58:29 +0900653 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900654 copyCmds := apexRule.Args["copy_commands"]
655
656 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800657 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900658
659 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800660 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900661
662 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800663 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900664
Colin Cross7113d202019-11-20 16:39:12 -0800665 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900666
667 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900668 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900670 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900671
672 // 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 -0800673 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900674 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800675 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900676
677 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900678 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900679 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900680
681 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900682 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900683
Jooyung Hana57af4a2020-01-23 05:36:59 +0000684 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900685 "lib64/mylib.so",
686 "lib64/mylib3.so",
687 "lib64/mylib4.so",
688 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900689}
690
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900691func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700692 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900693 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900694 name: "myapex2",
695 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900696 native_shared_libs: ["mylib"],
697 }
698
699 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900700 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900701 public_key: "testkey.avbpubkey",
702 private_key: "testkey.pem",
703 }
704
705 cc_library {
706 name: "mylib",
707 srcs: ["mylib.cpp"],
708 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900709 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900710 system_shared_libs: [],
711 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000712 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900713 }
714
715 cc_library {
716 name: "libfoo",
717 srcs: ["mylib.cpp"],
718 shared_libs: ["libbar"],
719 system_shared_libs: [],
720 stl: "none",
721 stubs: {
722 versions: ["10", "20", "30"],
723 },
724 }
725
726 cc_library {
727 name: "libbar",
728 srcs: ["mylib.cpp"],
729 system_shared_libs: [],
730 stl: "none",
731 }
732
Jiyong Park678c8812020-02-07 17:25:49 +0900733 cc_library_static {
734 name: "libbaz",
735 srcs: ["mylib.cpp"],
736 system_shared_libs: [],
737 stl: "none",
738 apex_available: [ "myapex2" ],
739 }
740
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900741 `)
742
Jiyong Park83dc74b2020-01-14 18:38:44 +0900743 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900744 copyCmds := apexRule.Args["copy_commands"]
745
746 // Ensure that direct non-stubs dep is always included
747 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
748
749 // Ensure that indirect stubs dep is not included
750 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
751
752 // Ensure that dependency of stubs is not included
753 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
754
Jiyong Park83dc74b2020-01-14 18:38:44 +0900755 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900756
757 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900758 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900759 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900760 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900761
Jiyong Park3ff16992019-12-27 14:11:47 +0900762 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900763
764 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
765 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900766
767 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 +0900768
769 ensureListContains(t, depsInfo, "mylib <- myapex2")
770 ensureListContains(t, depsInfo, "libbaz <- mylib")
771 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900772}
773
Jooyung Hand3639552019-08-09 12:57:43 +0900774func TestApexWithRuntimeLibsDependency(t *testing.T) {
775 /*
776 myapex
777 |
778 v (runtime_libs)
779 mylib ------+------> libfoo [provides stub]
780 |
781 `------> libbar
782 */
783 ctx, _ := testApex(t, `
784 apex {
785 name: "myapex",
786 key: "myapex.key",
787 native_shared_libs: ["mylib"],
788 }
789
790 apex_key {
791 name: "myapex.key",
792 public_key: "testkey.avbpubkey",
793 private_key: "testkey.pem",
794 }
795
796 cc_library {
797 name: "mylib",
798 srcs: ["mylib.cpp"],
799 runtime_libs: ["libfoo", "libbar"],
800 system_shared_libs: [],
801 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000802 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900803 }
804
805 cc_library {
806 name: "libfoo",
807 srcs: ["mylib.cpp"],
808 system_shared_libs: [],
809 stl: "none",
810 stubs: {
811 versions: ["10", "20", "30"],
812 },
813 }
814
815 cc_library {
816 name: "libbar",
817 srcs: ["mylib.cpp"],
818 system_shared_libs: [],
819 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000820 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900821 }
822
823 `)
824
Sundong Ahnabb64432019-10-22 13:58:29 +0900825 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900826 copyCmds := apexRule.Args["copy_commands"]
827
828 // Ensure that direct non-stubs dep is always included
829 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
830
831 // Ensure that indirect stubs dep is not included
832 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
833
834 // Ensure that runtime_libs dep in included
835 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
836
Sundong Ahnabb64432019-10-22 13:58:29 +0900837 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900838 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
839 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900840
841}
842
Jooyung Han9c80bae2019-08-20 17:30:57 +0900843func TestApexDependencyToLLNDK(t *testing.T) {
844 ctx, _ := testApex(t, `
845 apex {
846 name: "myapex",
847 key: "myapex.key",
848 use_vendor: true,
849 native_shared_libs: ["mylib"],
850 }
851
852 apex_key {
853 name: "myapex.key",
854 public_key: "testkey.avbpubkey",
855 private_key: "testkey.pem",
856 }
857
858 cc_library {
859 name: "mylib",
860 srcs: ["mylib.cpp"],
861 vendor_available: true,
862 shared_libs: ["libbar"],
863 system_shared_libs: [],
864 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000865 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900866 }
867
868 cc_library {
869 name: "libbar",
870 srcs: ["mylib.cpp"],
871 system_shared_libs: [],
872 stl: "none",
873 }
874
875 llndk_library {
876 name: "libbar",
877 symbol_file: "",
878 }
Jooyung Handc782442019-11-01 03:14:38 +0900879 `, func(fs map[string][]byte, config android.Config) {
880 setUseVendorWhitelistForTest(config, []string{"myapex"})
881 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900882
Sundong Ahnabb64432019-10-22 13:58:29 +0900883 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900884 copyCmds := apexRule.Args["copy_commands"]
885
886 // Ensure that LLNDK dep is not included
887 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
888
Sundong Ahnabb64432019-10-22 13:58:29 +0900889 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900890 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900891
892 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900893 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900894
895}
896
Jiyong Park25fc6a92018-11-18 18:02:45 +0900897func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700898 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900899 apex {
900 name: "myapex",
901 key: "myapex.key",
902 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
903 }
904
905 apex_key {
906 name: "myapex.key",
907 public_key: "testkey.avbpubkey",
908 private_key: "testkey.pem",
909 }
910
911 cc_library {
912 name: "mylib",
913 srcs: ["mylib.cpp"],
914 shared_libs: ["libdl#27"],
915 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000916 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900917 }
918
919 cc_library_shared {
920 name: "mylib_shared",
921 srcs: ["mylib.cpp"],
922 shared_libs: ["libdl#27"],
923 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000924 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925 }
926
927 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900928 name: "libBootstrap",
929 srcs: ["mylib.cpp"],
930 stl: "none",
931 bootstrap: true,
932 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900933 `)
934
Sundong Ahnabb64432019-10-22 13:58:29 +0900935 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900936 copyCmds := apexRule.Args["copy_commands"]
937
938 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800939 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900940 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
941 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900942
943 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900944 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900945
Colin Cross7113d202019-11-20 16:39:12 -0800946 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
947 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
948 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900949
950 // For dependency to libc
951 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900952 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900953 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900954 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900955 // ... Cflags from stub is correctly exported to mylib
956 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
957 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
958
959 // For dependency to libm
960 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800961 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900963 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964 // ... and is not compiling with the stub
965 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
966 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
967
968 // For dependency to libdl
969 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900970 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900971 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900972 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
973 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900974 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800975 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900976 // ... Cflags from stub is correctly exported to mylib
977 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
978 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900979
980 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800981 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
982 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
983 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
984 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900985}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900986
987func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700988 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900989 apex {
990 name: "myapex",
991 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900992 native_shared_libs: ["mylib"],
993 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900994 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900995 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900996 }
997
998 apex_key {
999 name: "myapex.key",
1000 public_key: "testkey.avbpubkey",
1001 private_key: "testkey.pem",
1002 }
1003
1004 prebuilt_etc {
1005 name: "myetc",
1006 src: "myprebuilt",
1007 sub_dir: "foo/bar",
1008 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001009
1010 cc_library {
1011 name: "mylib",
1012 srcs: ["mylib.cpp"],
1013 relative_install_path: "foo/bar",
1014 system_shared_libs: [],
1015 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001016 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001017 }
1018
1019 cc_binary {
1020 name: "mybin",
1021 srcs: ["mylib.cpp"],
1022 relative_install_path: "foo/bar",
1023 system_shared_libs: [],
1024 static_executable: true,
1025 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001026 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001027 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001028 `)
1029
Sundong Ahnabb64432019-10-22 13:58:29 +09001030 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001031 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1032
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001033 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001034 ensureListContains(t, dirs, "etc")
1035 ensureListContains(t, dirs, "etc/foo")
1036 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001037 ensureListContains(t, dirs, "lib64")
1038 ensureListContains(t, dirs, "lib64/foo")
1039 ensureListContains(t, dirs, "lib64/foo/bar")
1040 ensureListContains(t, dirs, "lib")
1041 ensureListContains(t, dirs, "lib/foo")
1042 ensureListContains(t, dirs, "lib/foo/bar")
1043
Jiyong Parkbd13e442019-03-15 18:10:35 +09001044 ensureListContains(t, dirs, "bin")
1045 ensureListContains(t, dirs, "bin/foo")
1046 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001047}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001048
1049func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001050 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001051 apex {
1052 name: "myapex",
1053 key: "myapex.key",
1054 native_shared_libs: ["mylib"],
1055 use_vendor: true,
1056 }
1057
1058 apex_key {
1059 name: "myapex.key",
1060 public_key: "testkey.avbpubkey",
1061 private_key: "testkey.pem",
1062 }
1063
1064 cc_library {
1065 name: "mylib",
1066 srcs: ["mylib.cpp"],
1067 shared_libs: ["mylib2"],
1068 system_shared_libs: [],
1069 vendor_available: true,
1070 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001071 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001072 }
1073
1074 cc_library {
1075 name: "mylib2",
1076 srcs: ["mylib.cpp"],
1077 system_shared_libs: [],
1078 vendor_available: true,
1079 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001080 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001081 }
Jooyung Handc782442019-11-01 03:14:38 +09001082 `, func(fs map[string][]byte, config android.Config) {
1083 setUseVendorWhitelistForTest(config, []string{"myapex"})
1084 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001085
1086 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001087 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001088 for _, implicit := range i.Implicits {
1089 inputsList = append(inputsList, implicit.String())
1090 }
1091 }
1092 inputsString := strings.Join(inputsList, " ")
1093
1094 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001095 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1096 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001097
1098 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001099 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1100 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001101}
Jiyong Park16e91a02018-12-20 18:18:08 +09001102
Jooyung Handc782442019-11-01 03:14:38 +09001103func TestUseVendorRestriction(t *testing.T) {
1104 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1105 apex {
1106 name: "myapex",
1107 key: "myapex.key",
1108 use_vendor: true,
1109 }
1110 apex_key {
1111 name: "myapex.key",
1112 public_key: "testkey.avbpubkey",
1113 private_key: "testkey.pem",
1114 }
1115 `, func(fs map[string][]byte, config android.Config) {
1116 setUseVendorWhitelistForTest(config, []string{""})
1117 })
1118 // no error with whitelist
1119 testApex(t, `
1120 apex {
1121 name: "myapex",
1122 key: "myapex.key",
1123 use_vendor: true,
1124 }
1125 apex_key {
1126 name: "myapex.key",
1127 public_key: "testkey.avbpubkey",
1128 private_key: "testkey.pem",
1129 }
1130 `, func(fs map[string][]byte, config android.Config) {
1131 setUseVendorWhitelistForTest(config, []string{"myapex"})
1132 })
1133}
1134
Jooyung Han5c998b92019-06-27 11:30:33 +09001135func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1136 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1137 apex {
1138 name: "myapex",
1139 key: "myapex.key",
1140 native_shared_libs: ["mylib"],
1141 use_vendor: true,
1142 }
1143
1144 apex_key {
1145 name: "myapex.key",
1146 public_key: "testkey.avbpubkey",
1147 private_key: "testkey.pem",
1148 }
1149
1150 cc_library {
1151 name: "mylib",
1152 srcs: ["mylib.cpp"],
1153 system_shared_libs: [],
1154 stl: "none",
1155 }
1156 `)
1157}
1158
Jiyong Park16e91a02018-12-20 18:18:08 +09001159func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001160 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001161 apex {
1162 name: "myapex",
1163 key: "myapex.key",
1164 native_shared_libs: ["mylib"],
1165 }
1166
1167 apex_key {
1168 name: "myapex.key",
1169 public_key: "testkey.avbpubkey",
1170 private_key: "testkey.pem",
1171 }
1172
1173 cc_library {
1174 name: "mylib",
1175 srcs: ["mylib.cpp"],
1176 system_shared_libs: [],
1177 stl: "none",
1178 stubs: {
1179 versions: ["1", "2", "3"],
1180 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001181 apex_available: [
1182 "//apex_available:platform",
1183 "myapex",
1184 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001185 }
1186
1187 cc_binary {
1188 name: "not_in_apex",
1189 srcs: ["mylib.cpp"],
1190 static_libs: ["mylib"],
1191 static_executable: true,
1192 system_shared_libs: [],
1193 stl: "none",
1194 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001195 `)
1196
Colin Cross7113d202019-11-20 16:39:12 -08001197 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001198
1199 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001200 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001201}
Jiyong Park9335a262018-12-24 11:31:58 +09001202
1203func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001204 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001205 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001206 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001207 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001208 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001209 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001210 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001211 }
1212
1213 cc_library {
1214 name: "mylib",
1215 srcs: ["mylib.cpp"],
1216 system_shared_libs: [],
1217 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001218 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001219 }
1220
1221 apex_key {
1222 name: "myapex.key",
1223 public_key: "testkey.avbpubkey",
1224 private_key: "testkey.pem",
1225 }
1226
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001227 android_app_certificate {
1228 name: "myapex.certificate",
1229 certificate: "testkey",
1230 }
1231
1232 android_app_certificate {
1233 name: "myapex.certificate.override",
1234 certificate: "testkey.override",
1235 }
1236
Jiyong Park9335a262018-12-24 11:31:58 +09001237 `)
1238
1239 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001240 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001241
1242 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1243 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1244 "vendor/foo/devkeys/testkey.avbpubkey")
1245 }
1246 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1247 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1248 "vendor/foo/devkeys/testkey.pem")
1249 }
1250
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001251 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001252 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001253 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001254 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001255 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001256 }
1257}
Jiyong Park58e364a2019-01-19 19:24:06 +09001258
Jooyung Hanf121a652019-12-17 14:30:11 +09001259func TestCertificate(t *testing.T) {
1260 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1261 ctx, _ := testApex(t, `
1262 apex {
1263 name: "myapex",
1264 key: "myapex.key",
1265 }
1266 apex_key {
1267 name: "myapex.key",
1268 public_key: "testkey.avbpubkey",
1269 private_key: "testkey.pem",
1270 }`)
1271 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1272 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1273 if actual := rule.Args["certificates"]; actual != expected {
1274 t.Errorf("certificates should be %q, not %q", expected, actual)
1275 }
1276 })
1277 t.Run("override when unspecified", func(t *testing.T) {
1278 ctx, _ := testApex(t, `
1279 apex {
1280 name: "myapex_keytest",
1281 key: "myapex.key",
1282 file_contexts: ":myapex-file_contexts",
1283 }
1284 apex_key {
1285 name: "myapex.key",
1286 public_key: "testkey.avbpubkey",
1287 private_key: "testkey.pem",
1288 }
1289 android_app_certificate {
1290 name: "myapex.certificate.override",
1291 certificate: "testkey.override",
1292 }`)
1293 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1294 expected := "testkey.override.x509.pem testkey.override.pk8"
1295 if actual := rule.Args["certificates"]; actual != expected {
1296 t.Errorf("certificates should be %q, not %q", expected, actual)
1297 }
1298 })
1299 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1300 ctx, _ := testApex(t, `
1301 apex {
1302 name: "myapex",
1303 key: "myapex.key",
1304 certificate: ":myapex.certificate",
1305 }
1306 apex_key {
1307 name: "myapex.key",
1308 public_key: "testkey.avbpubkey",
1309 private_key: "testkey.pem",
1310 }
1311 android_app_certificate {
1312 name: "myapex.certificate",
1313 certificate: "testkey",
1314 }`)
1315 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1316 expected := "testkey.x509.pem testkey.pk8"
1317 if actual := rule.Args["certificates"]; actual != expected {
1318 t.Errorf("certificates should be %q, not %q", expected, actual)
1319 }
1320 })
1321 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1322 ctx, _ := testApex(t, `
1323 apex {
1324 name: "myapex_keytest",
1325 key: "myapex.key",
1326 file_contexts: ":myapex-file_contexts",
1327 certificate: ":myapex.certificate",
1328 }
1329 apex_key {
1330 name: "myapex.key",
1331 public_key: "testkey.avbpubkey",
1332 private_key: "testkey.pem",
1333 }
1334 android_app_certificate {
1335 name: "myapex.certificate.override",
1336 certificate: "testkey.override",
1337 }`)
1338 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1339 expected := "testkey.override.x509.pem testkey.override.pk8"
1340 if actual := rule.Args["certificates"]; actual != expected {
1341 t.Errorf("certificates should be %q, not %q", expected, actual)
1342 }
1343 })
1344 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1345 ctx, _ := testApex(t, `
1346 apex {
1347 name: "myapex",
1348 key: "myapex.key",
1349 certificate: "testkey",
1350 }
1351 apex_key {
1352 name: "myapex.key",
1353 public_key: "testkey.avbpubkey",
1354 private_key: "testkey.pem",
1355 }`)
1356 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1357 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1358 if actual := rule.Args["certificates"]; actual != expected {
1359 t.Errorf("certificates should be %q, not %q", expected, actual)
1360 }
1361 })
1362 t.Run("override when specified as <name>", func(t *testing.T) {
1363 ctx, _ := testApex(t, `
1364 apex {
1365 name: "myapex_keytest",
1366 key: "myapex.key",
1367 file_contexts: ":myapex-file_contexts",
1368 certificate: "testkey",
1369 }
1370 apex_key {
1371 name: "myapex.key",
1372 public_key: "testkey.avbpubkey",
1373 private_key: "testkey.pem",
1374 }
1375 android_app_certificate {
1376 name: "myapex.certificate.override",
1377 certificate: "testkey.override",
1378 }`)
1379 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1380 expected := "testkey.override.x509.pem testkey.override.pk8"
1381 if actual := rule.Args["certificates"]; actual != expected {
1382 t.Errorf("certificates should be %q, not %q", expected, actual)
1383 }
1384 })
1385}
1386
Jiyong Park58e364a2019-01-19 19:24:06 +09001387func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001388 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001389 apex {
1390 name: "myapex",
1391 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001392 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001393 }
1394
1395 apex {
1396 name: "otherapex",
1397 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001398 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001399 }
1400
1401 apex_key {
1402 name: "myapex.key",
1403 public_key: "testkey.avbpubkey",
1404 private_key: "testkey.pem",
1405 }
1406
1407 cc_library {
1408 name: "mylib",
1409 srcs: ["mylib.cpp"],
1410 system_shared_libs: [],
1411 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001412 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001413 "myapex",
1414 "otherapex",
1415 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001416 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09001417 cc_library {
1418 name: "mylib2",
1419 srcs: ["mylib.cpp"],
1420 system_shared_libs: [],
1421 stl: "none",
1422 apex_available: [
1423 "myapex",
1424 "otherapex",
1425 ],
1426 use_apex_name_macro: true,
1427 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001428 `)
1429
Jooyung Hanc87a0592020-03-02 17:44:33 +09001430 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001431 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001432 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001433
1434 // APEX variant has __ANDROID_APEX__ defined
1435 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1436 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001437 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001438
1439 // APEX variant has __ANDROID_APEX__ defined
1440 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1441 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001442 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001443
Jooyung Hanc87a0592020-03-02 17:44:33 +09001444 // When cc_library sets use_apex_name_macro: true
1445 // apex variants define additional macro to distinguish which apex variant it is built for
1446
1447 // non-APEX variant does not have __ANDROID_APEX__ defined
1448 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1449 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1450
1451 // APEX variant has __ANDROID_APEX__ defined
1452 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001453 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001454 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1455 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001456
Jooyung Hanc87a0592020-03-02 17:44:33 +09001457 // APEX variant has __ANDROID_APEX__ defined
1458 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001459 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001460 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1461 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001462}
Jiyong Park7e636d02019-01-28 16:16:54 +09001463
1464func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001465 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001466 apex {
1467 name: "myapex",
1468 key: "myapex.key",
1469 native_shared_libs: ["mylib"],
1470 }
1471
1472 apex_key {
1473 name: "myapex.key",
1474 public_key: "testkey.avbpubkey",
1475 private_key: "testkey.pem",
1476 }
1477
1478 cc_library_headers {
1479 name: "mylib_headers",
1480 export_include_dirs: ["my_include"],
1481 system_shared_libs: [],
1482 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001483 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001484 }
1485
1486 cc_library {
1487 name: "mylib",
1488 srcs: ["mylib.cpp"],
1489 system_shared_libs: [],
1490 stl: "none",
1491 header_libs: ["mylib_headers"],
1492 export_header_lib_headers: ["mylib_headers"],
1493 stubs: {
1494 versions: ["1", "2", "3"],
1495 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001496 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001497 }
1498
1499 cc_library {
1500 name: "otherlib",
1501 srcs: ["mylib.cpp"],
1502 system_shared_libs: [],
1503 stl: "none",
1504 shared_libs: ["mylib"],
1505 }
1506 `)
1507
Colin Cross7113d202019-11-20 16:39:12 -08001508 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001509
1510 // Ensure that the include path of the header lib is exported to 'otherlib'
1511 ensureContains(t, cFlags, "-Imy_include")
1512}
Alex Light9670d332019-01-29 18:07:33 -08001513
Jiyong Park7cd10e32020-01-14 09:22:18 +09001514type fileInApex struct {
1515 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001516 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001517 isLink bool
1518}
1519
Jooyung Hana57af4a2020-01-23 05:36:59 +00001520func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001521 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001522 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001523 copyCmds := apexRule.Args["copy_commands"]
1524 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001525 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001526 for _, cmd := range strings.Split(copyCmds, "&&") {
1527 cmd = strings.TrimSpace(cmd)
1528 if cmd == "" {
1529 continue
1530 }
1531 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001532 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001533 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001534 switch terms[0] {
1535 case "mkdir":
1536 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001537 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001538 t.Fatal("copyCmds contains invalid cp command", cmd)
1539 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001540 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001541 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001542 isLink = false
1543 case "ln":
1544 if len(terms) != 3 && len(terms) != 4 {
1545 // ln LINK TARGET or ln -s LINK TARGET
1546 t.Fatal("copyCmds contains invalid ln command", cmd)
1547 }
1548 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001549 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001550 isLink = true
1551 default:
1552 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1553 }
1554 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001555 index := strings.Index(dst, imageApexDir)
1556 if index == -1 {
1557 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1558 }
1559 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001560 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001561 }
1562 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001563 return ret
1564}
1565
Jooyung Hana57af4a2020-01-23 05:36:59 +00001566func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1567 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001568 var failed bool
1569 var surplus []string
1570 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001571 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001572 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001573 for _, expected := range files {
1574 if matched, _ := path.Match(expected, file.path); matched {
1575 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001576 mactchFound = true
1577 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001578 }
1579 }
Jooyung Hane6436d72020-02-27 13:31:56 +09001580 if !mactchFound {
1581 surplus = append(surplus, file.path)
1582 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001583 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001584
Jooyung Han31c470b2019-10-18 16:26:59 +09001585 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001586 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001587 t.Log("surplus files", surplus)
1588 failed = true
1589 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001590
1591 if len(files) > len(filesMatched) {
1592 var missing []string
1593 for _, expected := range files {
1594 if !filesMatched[expected] {
1595 missing = append(missing, expected)
1596 }
1597 }
1598 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001599 t.Log("missing files", missing)
1600 failed = true
1601 }
1602 if failed {
1603 t.Fail()
1604 }
1605}
1606
Jooyung Han344d5432019-08-23 11:17:39 +09001607func TestVndkApexCurrent(t *testing.T) {
1608 ctx, _ := testApex(t, `
1609 apex_vndk {
1610 name: "myapex",
1611 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001612 }
1613
1614 apex_key {
1615 name: "myapex.key",
1616 public_key: "testkey.avbpubkey",
1617 private_key: "testkey.pem",
1618 }
1619
1620 cc_library {
1621 name: "libvndk",
1622 srcs: ["mylib.cpp"],
1623 vendor_available: true,
1624 vndk: {
1625 enabled: true,
1626 },
1627 system_shared_libs: [],
1628 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001629 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001630 }
1631
1632 cc_library {
1633 name: "libvndksp",
1634 srcs: ["mylib.cpp"],
1635 vendor_available: true,
1636 vndk: {
1637 enabled: true,
1638 support_system_process: true,
1639 },
1640 system_shared_libs: [],
1641 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001642 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001643 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001644 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001645
Jooyung Hana57af4a2020-01-23 05:36:59 +00001646 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001647 "lib/libvndk.so",
1648 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001649 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09001650 "lib64/libvndk.so",
1651 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001652 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001653 "etc/llndk.libraries.VER.txt",
1654 "etc/vndkcore.libraries.VER.txt",
1655 "etc/vndksp.libraries.VER.txt",
1656 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001657 })
Jooyung Han344d5432019-08-23 11:17:39 +09001658}
1659
1660func TestVndkApexWithPrebuilt(t *testing.T) {
1661 ctx, _ := testApex(t, `
1662 apex_vndk {
1663 name: "myapex",
1664 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001665 }
1666
1667 apex_key {
1668 name: "myapex.key",
1669 public_key: "testkey.avbpubkey",
1670 private_key: "testkey.pem",
1671 }
1672
1673 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001674 name: "libvndk",
1675 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001676 vendor_available: true,
1677 vndk: {
1678 enabled: true,
1679 },
1680 system_shared_libs: [],
1681 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001682 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001683 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001684
1685 cc_prebuilt_library_shared {
1686 name: "libvndk.arm",
1687 srcs: ["libvndk.arm.so"],
1688 vendor_available: true,
1689 vndk: {
1690 enabled: true,
1691 },
1692 enabled: false,
1693 arch: {
1694 arm: {
1695 enabled: true,
1696 },
1697 },
1698 system_shared_libs: [],
1699 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001700 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001701 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001702 `+vndkLibrariesTxtFiles("current"),
1703 withFiles(map[string][]byte{
1704 "libvndk.so": nil,
1705 "libvndk.arm.so": nil,
1706 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001707
Jooyung Hana57af4a2020-01-23 05:36:59 +00001708 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001709 "lib/libvndk.so",
1710 "lib/libvndk.arm.so",
1711 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001712 "lib/libc++.so",
1713 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001714 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001715 })
Jooyung Han344d5432019-08-23 11:17:39 +09001716}
1717
Jooyung Han39edb6c2019-11-06 16:53:07 +09001718func vndkLibrariesTxtFiles(vers ...string) (result string) {
1719 for _, v := range vers {
1720 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001721 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001722 result += `
1723 vndk_libraries_txt {
1724 name: "` + txt + `.libraries.txt",
1725 }
1726 `
1727 }
1728 } else {
1729 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1730 result += `
1731 prebuilt_etc {
1732 name: "` + txt + `.libraries.` + v + `.txt",
1733 src: "dummy.txt",
1734 }
1735 `
1736 }
1737 }
1738 }
1739 return
1740}
1741
Jooyung Han344d5432019-08-23 11:17:39 +09001742func TestVndkApexVersion(t *testing.T) {
1743 ctx, _ := testApex(t, `
1744 apex_vndk {
1745 name: "myapex_v27",
1746 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001747 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001748 vndk_version: "27",
1749 }
1750
1751 apex_key {
1752 name: "myapex.key",
1753 public_key: "testkey.avbpubkey",
1754 private_key: "testkey.pem",
1755 }
1756
Jooyung Han31c470b2019-10-18 16:26:59 +09001757 vndk_prebuilt_shared {
1758 name: "libvndk27",
1759 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001760 vendor_available: true,
1761 vndk: {
1762 enabled: true,
1763 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001764 target_arch: "arm64",
1765 arch: {
1766 arm: {
1767 srcs: ["libvndk27_arm.so"],
1768 },
1769 arm64: {
1770 srcs: ["libvndk27_arm64.so"],
1771 },
1772 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001773 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001774 }
1775
1776 vndk_prebuilt_shared {
1777 name: "libvndk27",
1778 version: "27",
1779 vendor_available: true,
1780 vndk: {
1781 enabled: true,
1782 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001783 target_arch: "x86_64",
1784 arch: {
1785 x86: {
1786 srcs: ["libvndk27_x86.so"],
1787 },
1788 x86_64: {
1789 srcs: ["libvndk27_x86_64.so"],
1790 },
1791 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001792 }
1793 `+vndkLibrariesTxtFiles("27"),
1794 withFiles(map[string][]byte{
1795 "libvndk27_arm.so": nil,
1796 "libvndk27_arm64.so": nil,
1797 "libvndk27_x86.so": nil,
1798 "libvndk27_x86_64.so": nil,
1799 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001800
Jooyung Hana57af4a2020-01-23 05:36:59 +00001801 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001802 "lib/libvndk27_arm.so",
1803 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001804 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001805 })
Jooyung Han344d5432019-08-23 11:17:39 +09001806}
1807
1808func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1809 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1810 apex_vndk {
1811 name: "myapex_v27",
1812 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001813 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001814 vndk_version: "27",
1815 }
1816 apex_vndk {
1817 name: "myapex_v27_other",
1818 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001819 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001820 vndk_version: "27",
1821 }
1822
1823 apex_key {
1824 name: "myapex.key",
1825 public_key: "testkey.avbpubkey",
1826 private_key: "testkey.pem",
1827 }
1828
1829 cc_library {
1830 name: "libvndk",
1831 srcs: ["mylib.cpp"],
1832 vendor_available: true,
1833 vndk: {
1834 enabled: true,
1835 },
1836 system_shared_libs: [],
1837 stl: "none",
1838 }
1839
1840 vndk_prebuilt_shared {
1841 name: "libvndk",
1842 version: "27",
1843 vendor_available: true,
1844 vndk: {
1845 enabled: true,
1846 },
1847 srcs: ["libvndk.so"],
1848 }
1849 `, withFiles(map[string][]byte{
1850 "libvndk.so": nil,
1851 }))
1852}
1853
Jooyung Han90eee022019-10-01 20:02:42 +09001854func TestVndkApexNameRule(t *testing.T) {
1855 ctx, _ := testApex(t, `
1856 apex_vndk {
1857 name: "myapex",
1858 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001859 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001860 }
1861 apex_vndk {
1862 name: "myapex_v28",
1863 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001864 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001865 vndk_version: "28",
1866 }
1867 apex_key {
1868 name: "myapex.key",
1869 public_key: "testkey.avbpubkey",
1870 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001871 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001872
1873 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001874 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001875 actual := proptools.String(bundle.properties.Apex_name)
1876 if !reflect.DeepEqual(actual, expected) {
1877 t.Errorf("Got '%v', expected '%v'", actual, expected)
1878 }
1879 }
1880
1881 assertApexName("com.android.vndk.vVER", "myapex")
1882 assertApexName("com.android.vndk.v28", "myapex_v28")
1883}
1884
Jooyung Han344d5432019-08-23 11:17:39 +09001885func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1886 ctx, _ := testApex(t, `
1887 apex_vndk {
1888 name: "myapex",
1889 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001890 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001891 }
1892
1893 apex_key {
1894 name: "myapex.key",
1895 public_key: "testkey.avbpubkey",
1896 private_key: "testkey.pem",
1897 }
1898
1899 cc_library {
1900 name: "libvndk",
1901 srcs: ["mylib.cpp"],
1902 vendor_available: true,
1903 native_bridge_supported: true,
1904 host_supported: true,
1905 vndk: {
1906 enabled: true,
1907 },
1908 system_shared_libs: [],
1909 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001910 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001911 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001912 `+vndkLibrariesTxtFiles("current"),
1913 withTargets(map[android.OsType][]android.Target{
1914 android.Android: []android.Target{
1915 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1916 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1917 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1918 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1919 },
1920 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001921
Jooyung Hana57af4a2020-01-23 05:36:59 +00001922 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001923 "lib/libvndk.so",
1924 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001925 "lib/libc++.so",
1926 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001927 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001928 })
Jooyung Han344d5432019-08-23 11:17:39 +09001929}
1930
1931func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1932 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1933 apex_vndk {
1934 name: "myapex",
1935 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001936 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001937 native_bridge_supported: true,
1938 }
1939
1940 apex_key {
1941 name: "myapex.key",
1942 public_key: "testkey.avbpubkey",
1943 private_key: "testkey.pem",
1944 }
1945
1946 cc_library {
1947 name: "libvndk",
1948 srcs: ["mylib.cpp"],
1949 vendor_available: true,
1950 native_bridge_supported: true,
1951 host_supported: true,
1952 vndk: {
1953 enabled: true,
1954 },
1955 system_shared_libs: [],
1956 stl: "none",
1957 }
1958 `)
1959}
1960
Jooyung Han31c470b2019-10-18 16:26:59 +09001961func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001962 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001963 apex_vndk {
1964 name: "myapex_v27",
1965 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001966 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001967 vndk_version: "27",
1968 }
1969
1970 apex_key {
1971 name: "myapex.key",
1972 public_key: "testkey.avbpubkey",
1973 private_key: "testkey.pem",
1974 }
1975
1976 vndk_prebuilt_shared {
1977 name: "libvndk27",
1978 version: "27",
1979 target_arch: "arm",
1980 vendor_available: true,
1981 vndk: {
1982 enabled: true,
1983 },
1984 arch: {
1985 arm: {
1986 srcs: ["libvndk27.so"],
1987 }
1988 },
1989 }
1990
1991 vndk_prebuilt_shared {
1992 name: "libvndk27",
1993 version: "27",
1994 target_arch: "arm",
1995 binder32bit: true,
1996 vendor_available: true,
1997 vndk: {
1998 enabled: true,
1999 },
2000 arch: {
2001 arm: {
2002 srcs: ["libvndk27binder32.so"],
2003 }
2004 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002005 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002006 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002007 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002008 withFiles(map[string][]byte{
2009 "libvndk27.so": nil,
2010 "libvndk27binder32.so": nil,
2011 }),
2012 withBinder32bit,
2013 withTargets(map[android.OsType][]android.Target{
2014 android.Android: []android.Target{
2015 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2016 },
2017 }),
2018 )
2019
Jooyung Hana57af4a2020-01-23 05:36:59 +00002020 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002021 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002022 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002023 })
2024}
2025
Jooyung Hane1633032019-08-01 17:41:43 +09002026func TestDependenciesInApexManifest(t *testing.T) {
2027 ctx, _ := testApex(t, `
2028 apex {
2029 name: "myapex_nodep",
2030 key: "myapex.key",
2031 native_shared_libs: ["lib_nodep"],
2032 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002033 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002034 }
2035
2036 apex {
2037 name: "myapex_dep",
2038 key: "myapex.key",
2039 native_shared_libs: ["lib_dep"],
2040 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002041 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002042 }
2043
2044 apex {
2045 name: "myapex_provider",
2046 key: "myapex.key",
2047 native_shared_libs: ["libfoo"],
2048 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002049 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002050 }
2051
2052 apex {
2053 name: "myapex_selfcontained",
2054 key: "myapex.key",
2055 native_shared_libs: ["lib_dep", "libfoo"],
2056 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002057 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002058 }
2059
2060 apex_key {
2061 name: "myapex.key",
2062 public_key: "testkey.avbpubkey",
2063 private_key: "testkey.pem",
2064 }
2065
2066 cc_library {
2067 name: "lib_nodep",
2068 srcs: ["mylib.cpp"],
2069 system_shared_libs: [],
2070 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002071 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002072 }
2073
2074 cc_library {
2075 name: "lib_dep",
2076 srcs: ["mylib.cpp"],
2077 shared_libs: ["libfoo"],
2078 system_shared_libs: [],
2079 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002080 apex_available: [
2081 "myapex_dep",
2082 "myapex_provider",
2083 "myapex_selfcontained",
2084 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002085 }
2086
2087 cc_library {
2088 name: "libfoo",
2089 srcs: ["mytest.cpp"],
2090 stubs: {
2091 versions: ["1"],
2092 },
2093 system_shared_libs: [],
2094 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002095 apex_available: [
2096 "myapex_provider",
2097 "myapex_selfcontained",
2098 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002099 }
2100 `)
2101
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002102 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002103 var provideNativeLibs, requireNativeLibs []string
2104
Sundong Ahnabb64432019-10-22 13:58:29 +09002105 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002106 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2107 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002108 ensureListEmpty(t, provideNativeLibs)
2109 ensureListEmpty(t, requireNativeLibs)
2110
Sundong Ahnabb64432019-10-22 13:58:29 +09002111 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002112 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2113 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002114 ensureListEmpty(t, provideNativeLibs)
2115 ensureListContains(t, requireNativeLibs, "libfoo.so")
2116
Sundong Ahnabb64432019-10-22 13:58:29 +09002117 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002118 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2119 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002120 ensureListContains(t, provideNativeLibs, "libfoo.so")
2121 ensureListEmpty(t, requireNativeLibs)
2122
Sundong Ahnabb64432019-10-22 13:58:29 +09002123 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002124 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2125 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002126 ensureListContains(t, provideNativeLibs, "libfoo.so")
2127 ensureListEmpty(t, requireNativeLibs)
2128}
2129
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002130func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002131 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002132 apex {
2133 name: "myapex",
2134 key: "myapex.key",
2135 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002136 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002137 }
2138
2139 apex_key {
2140 name: "myapex.key",
2141 public_key: "testkey.avbpubkey",
2142 private_key: "testkey.pem",
2143 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002144
2145 cc_library {
2146 name: "mylib",
2147 srcs: ["mylib.cpp"],
2148 system_shared_libs: [],
2149 stl: "none",
2150 apex_available: [
2151 "//apex_available:platform",
2152 "myapex",
2153 ],
2154 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002155 `)
2156
Sundong Ahnabb64432019-10-22 13:58:29 +09002157 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002158 apexManifestRule := module.Rule("apexManifestRule")
2159 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2160 apexRule := module.Rule("apexRule")
2161 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002162
2163 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2164 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2165 name := apexBundle.BaseModuleName()
2166 prefix := "TARGET_"
2167 var builder strings.Builder
2168 data.Custom(&builder, name, prefix, "", data)
2169 androidMk := builder.String()
2170 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2171 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002172}
2173
Alex Light0851b882019-02-07 13:20:53 -08002174func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002175 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002176 apex {
2177 name: "myapex",
2178 key: "myapex.key",
2179 native_shared_libs: ["mylib_common"],
2180 }
2181
2182 apex_key {
2183 name: "myapex.key",
2184 public_key: "testkey.avbpubkey",
2185 private_key: "testkey.pem",
2186 }
2187
2188 cc_library {
2189 name: "mylib_common",
2190 srcs: ["mylib.cpp"],
2191 system_shared_libs: [],
2192 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002193 apex_available: [
2194 "//apex_available:platform",
2195 "myapex",
2196 ],
Alex Light0851b882019-02-07 13:20:53 -08002197 }
2198 `)
2199
Sundong Ahnabb64432019-10-22 13:58:29 +09002200 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002201 apexRule := module.Rule("apexRule")
2202 copyCmds := apexRule.Args["copy_commands"]
2203
2204 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2205 t.Log("Apex was a test apex!")
2206 t.Fail()
2207 }
2208 // Ensure that main rule creates an output
2209 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2210
2211 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002212 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002213
2214 // Ensure that both direct and indirect deps are copied into apex
2215 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2216
Colin Cross7113d202019-11-20 16:39:12 -08002217 // Ensure that the platform variant ends with _shared
2218 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002219
2220 if !android.InAnyApex("mylib_common") {
2221 t.Log("Found mylib_common not in any apex!")
2222 t.Fail()
2223 }
2224}
2225
2226func TestTestApex(t *testing.T) {
2227 if android.InAnyApex("mylib_common_test") {
2228 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!")
2229 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002230 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002231 apex_test {
2232 name: "myapex",
2233 key: "myapex.key",
2234 native_shared_libs: ["mylib_common_test"],
2235 }
2236
2237 apex_key {
2238 name: "myapex.key",
2239 public_key: "testkey.avbpubkey",
2240 private_key: "testkey.pem",
2241 }
2242
2243 cc_library {
2244 name: "mylib_common_test",
2245 srcs: ["mylib.cpp"],
2246 system_shared_libs: [],
2247 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002248 // TODO: remove //apex_available:platform
2249 apex_available: [
2250 "//apex_available:platform",
2251 "myapex",
2252 ],
Alex Light0851b882019-02-07 13:20:53 -08002253 }
2254 `)
2255
Sundong Ahnabb64432019-10-22 13:58:29 +09002256 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002257 apexRule := module.Rule("apexRule")
2258 copyCmds := apexRule.Args["copy_commands"]
2259
2260 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2261 t.Log("Apex was not a test apex!")
2262 t.Fail()
2263 }
2264 // Ensure that main rule creates an output
2265 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2266
2267 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002268 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002269
2270 // Ensure that both direct and indirect deps are copied into apex
2271 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2272
Colin Cross7113d202019-11-20 16:39:12 -08002273 // Ensure that the platform variant ends with _shared
2274 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002275}
2276
Alex Light9670d332019-01-29 18:07:33 -08002277func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002278 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002279 apex {
2280 name: "myapex",
2281 key: "myapex.key",
2282 multilib: {
2283 first: {
2284 native_shared_libs: ["mylib_common"],
2285 }
2286 },
2287 target: {
2288 android: {
2289 multilib: {
2290 first: {
2291 native_shared_libs: ["mylib"],
2292 }
2293 }
2294 },
2295 host: {
2296 multilib: {
2297 first: {
2298 native_shared_libs: ["mylib2"],
2299 }
2300 }
2301 }
2302 }
2303 }
2304
2305 apex_key {
2306 name: "myapex.key",
2307 public_key: "testkey.avbpubkey",
2308 private_key: "testkey.pem",
2309 }
2310
2311 cc_library {
2312 name: "mylib",
2313 srcs: ["mylib.cpp"],
2314 system_shared_libs: [],
2315 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002316 // TODO: remove //apex_available:platform
2317 apex_available: [
2318 "//apex_available:platform",
2319 "myapex",
2320 ],
Alex Light9670d332019-01-29 18:07:33 -08002321 }
2322
2323 cc_library {
2324 name: "mylib_common",
2325 srcs: ["mylib.cpp"],
2326 system_shared_libs: [],
2327 stl: "none",
2328 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002329 // TODO: remove //apex_available:platform
2330 apex_available: [
2331 "//apex_available:platform",
2332 "myapex",
2333 ],
Alex Light9670d332019-01-29 18:07:33 -08002334 }
2335
2336 cc_library {
2337 name: "mylib2",
2338 srcs: ["mylib.cpp"],
2339 system_shared_libs: [],
2340 stl: "none",
2341 compile_multilib: "first",
2342 }
2343 `)
2344
Sundong Ahnabb64432019-10-22 13:58:29 +09002345 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002346 copyCmds := apexRule.Args["copy_commands"]
2347
2348 // Ensure that main rule creates an output
2349 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2350
2351 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002352 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2353 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2354 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002355
2356 // Ensure that both direct and indirect deps are copied into apex
2357 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2358 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2359 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2360
Colin Cross7113d202019-11-20 16:39:12 -08002361 // Ensure that the platform variant ends with _shared
2362 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2363 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2364 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002365}
Jiyong Park04480cf2019-02-06 00:16:29 +09002366
2367func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002368 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002369 apex {
2370 name: "myapex",
2371 key: "myapex.key",
2372 binaries: ["myscript"],
2373 }
2374
2375 apex_key {
2376 name: "myapex.key",
2377 public_key: "testkey.avbpubkey",
2378 private_key: "testkey.pem",
2379 }
2380
2381 sh_binary {
2382 name: "myscript",
2383 src: "mylib.cpp",
2384 filename: "myscript.sh",
2385 sub_dir: "script",
2386 }
2387 `)
2388
Sundong Ahnabb64432019-10-22 13:58:29 +09002389 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002390 copyCmds := apexRule.Args["copy_commands"]
2391
2392 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2393}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002394
Jooyung Han91df2082019-11-20 01:49:42 +09002395func TestApexInVariousPartition(t *testing.T) {
2396 testcases := []struct {
2397 propName, parition, flattenedPartition string
2398 }{
2399 {"", "system", "system_ext"},
2400 {"product_specific: true", "product", "product"},
2401 {"soc_specific: true", "vendor", "vendor"},
2402 {"proprietary: true", "vendor", "vendor"},
2403 {"vendor: true", "vendor", "vendor"},
2404 {"system_ext_specific: true", "system_ext", "system_ext"},
2405 }
2406 for _, tc := range testcases {
2407 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2408 ctx, _ := testApex(t, `
2409 apex {
2410 name: "myapex",
2411 key: "myapex.key",
2412 `+tc.propName+`
2413 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002414
Jooyung Han91df2082019-11-20 01:49:42 +09002415 apex_key {
2416 name: "myapex.key",
2417 public_key: "testkey.avbpubkey",
2418 private_key: "testkey.pem",
2419 }
2420 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002421
Jooyung Han91df2082019-11-20 01:49:42 +09002422 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2423 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2424 actual := apex.installDir.String()
2425 if actual != expected {
2426 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2427 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002428
Jooyung Han91df2082019-11-20 01:49:42 +09002429 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2430 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2431 actual = flattened.installDir.String()
2432 if actual != expected {
2433 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2434 }
2435 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002436 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002437}
Jiyong Park67882562019-03-21 01:11:21 +09002438
Jooyung Han54aca7b2019-11-20 02:26:02 +09002439func TestFileContexts(t *testing.T) {
2440 ctx, _ := testApex(t, `
2441 apex {
2442 name: "myapex",
2443 key: "myapex.key",
2444 }
2445
2446 apex_key {
2447 name: "myapex.key",
2448 public_key: "testkey.avbpubkey",
2449 private_key: "testkey.pem",
2450 }
2451 `)
2452 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2453 apexRule := module.Rule("apexRule")
2454 actual := apexRule.Args["file_contexts"]
2455 expected := "system/sepolicy/apex/myapex-file_contexts"
2456 if actual != expected {
2457 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2458 }
2459
2460 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2461 apex {
2462 name: "myapex",
2463 key: "myapex.key",
2464 file_contexts: "my_own_file_contexts",
2465 }
2466
2467 apex_key {
2468 name: "myapex.key",
2469 public_key: "testkey.avbpubkey",
2470 private_key: "testkey.pem",
2471 }
2472 `, withFiles(map[string][]byte{
2473 "my_own_file_contexts": nil,
2474 }))
2475
2476 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2477 apex {
2478 name: "myapex",
2479 key: "myapex.key",
2480 product_specific: true,
2481 file_contexts: "product_specific_file_contexts",
2482 }
2483
2484 apex_key {
2485 name: "myapex.key",
2486 public_key: "testkey.avbpubkey",
2487 private_key: "testkey.pem",
2488 }
2489 `)
2490
2491 ctx, _ = testApex(t, `
2492 apex {
2493 name: "myapex",
2494 key: "myapex.key",
2495 product_specific: true,
2496 file_contexts: "product_specific_file_contexts",
2497 }
2498
2499 apex_key {
2500 name: "myapex.key",
2501 public_key: "testkey.avbpubkey",
2502 private_key: "testkey.pem",
2503 }
2504 `, withFiles(map[string][]byte{
2505 "product_specific_file_contexts": nil,
2506 }))
2507 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2508 apexRule = module.Rule("apexRule")
2509 actual = apexRule.Args["file_contexts"]
2510 expected = "product_specific_file_contexts"
2511 if actual != expected {
2512 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2513 }
2514
2515 ctx, _ = testApex(t, `
2516 apex {
2517 name: "myapex",
2518 key: "myapex.key",
2519 product_specific: true,
2520 file_contexts: ":my-file-contexts",
2521 }
2522
2523 apex_key {
2524 name: "myapex.key",
2525 public_key: "testkey.avbpubkey",
2526 private_key: "testkey.pem",
2527 }
2528
2529 filegroup {
2530 name: "my-file-contexts",
2531 srcs: ["product_specific_file_contexts"],
2532 }
2533 `, withFiles(map[string][]byte{
2534 "product_specific_file_contexts": nil,
2535 }))
2536 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2537 apexRule = module.Rule("apexRule")
2538 actual = apexRule.Args["file_contexts"]
2539 expected = "product_specific_file_contexts"
2540 if actual != expected {
2541 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2542 }
2543}
2544
Jiyong Park67882562019-03-21 01:11:21 +09002545func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002546 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002547 apex_key {
2548 name: "myapex.key",
2549 public_key: ":my.avbpubkey",
2550 private_key: ":my.pem",
2551 product_specific: true,
2552 }
2553
2554 filegroup {
2555 name: "my.avbpubkey",
2556 srcs: ["testkey2.avbpubkey"],
2557 }
2558
2559 filegroup {
2560 name: "my.pem",
2561 srcs: ["testkey2.pem"],
2562 }
2563 `)
2564
2565 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2566 expected_pubkey := "testkey2.avbpubkey"
2567 actual_pubkey := apex_key.public_key_file.String()
2568 if actual_pubkey != expected_pubkey {
2569 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2570 }
2571 expected_privkey := "testkey2.pem"
2572 actual_privkey := apex_key.private_key_file.String()
2573 if actual_privkey != expected_privkey {
2574 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2575 }
2576}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002577
2578func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002579 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002580 prebuilt_apex {
2581 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002582 arch: {
2583 arm64: {
2584 src: "myapex-arm64.apex",
2585 },
2586 arm: {
2587 src: "myapex-arm.apex",
2588 },
2589 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002590 }
2591 `)
2592
2593 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2594
Jiyong Parkc95714e2019-03-29 14:23:10 +09002595 expectedInput := "myapex-arm64.apex"
2596 if prebuilt.inputApex.String() != expectedInput {
2597 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2598 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002599}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002600
2601func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002602 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002603 prebuilt_apex {
2604 name: "myapex",
2605 src: "myapex-arm.apex",
2606 filename: "notmyapex.apex",
2607 }
2608 `)
2609
2610 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2611
2612 expected := "notmyapex.apex"
2613 if p.installFilename != expected {
2614 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2615 }
2616}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002617
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002618func TestPrebuiltOverrides(t *testing.T) {
2619 ctx, config := testApex(t, `
2620 prebuilt_apex {
2621 name: "myapex.prebuilt",
2622 src: "myapex-arm.apex",
2623 overrides: [
2624 "myapex",
2625 ],
2626 }
2627 `)
2628
2629 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2630
2631 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002632 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002633 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002634 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002635 }
2636}
2637
Roland Levillain630846d2019-06-26 12:48:34 +01002638func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002639 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002640 apex_test {
2641 name: "myapex",
2642 key: "myapex.key",
2643 tests: [
2644 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002645 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002646 ],
2647 }
2648
2649 apex_key {
2650 name: "myapex.key",
2651 public_key: "testkey.avbpubkey",
2652 private_key: "testkey.pem",
2653 }
2654
2655 cc_test {
2656 name: "mytest",
2657 gtest: false,
2658 srcs: ["mytest.cpp"],
2659 relative_install_path: "test",
2660 system_shared_libs: [],
2661 static_executable: true,
2662 stl: "none",
2663 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002664
2665 cc_test {
2666 name: "mytests",
2667 gtest: false,
2668 srcs: [
2669 "mytest1.cpp",
2670 "mytest2.cpp",
2671 "mytest3.cpp",
2672 ],
2673 test_per_src: true,
2674 relative_install_path: "test",
2675 system_shared_libs: [],
2676 static_executable: true,
2677 stl: "none",
2678 }
Roland Levillain630846d2019-06-26 12:48:34 +01002679 `)
2680
Sundong Ahnabb64432019-10-22 13:58:29 +09002681 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002682 copyCmds := apexRule.Args["copy_commands"]
2683
2684 // Ensure that test dep is copied into apex.
2685 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002686
2687 // Ensure that test deps built with `test_per_src` are copied into apex.
2688 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2689 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2690 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002691
2692 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002693 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002694 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2695 name := apexBundle.BaseModuleName()
2696 prefix := "TARGET_"
2697 var builder strings.Builder
2698 data.Custom(&builder, name, prefix, "", data)
2699 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002700 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2701 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2702 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2703 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002704 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002705 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002706 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002707}
2708
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002709func TestInstallExtraFlattenedApexes(t *testing.T) {
2710 ctx, config := testApex(t, `
2711 apex {
2712 name: "myapex",
2713 key: "myapex.key",
2714 }
2715 apex_key {
2716 name: "myapex.key",
2717 public_key: "testkey.avbpubkey",
2718 private_key: "testkey.pem",
2719 }
2720 `, func(fs map[string][]byte, config android.Config) {
2721 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2722 })
2723 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002724 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002725 mk := android.AndroidMkDataForTest(t, config, "", ab)
2726 var builder strings.Builder
2727 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2728 androidMk := builder.String()
2729 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2730}
2731
Jooyung Han5c998b92019-06-27 11:30:33 +09002732func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002733 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002734 apex {
2735 name: "myapex",
2736 key: "myapex.key",
2737 native_shared_libs: ["mylib"],
2738 uses: ["commonapex"],
2739 }
2740
2741 apex {
2742 name: "commonapex",
2743 key: "myapex.key",
2744 native_shared_libs: ["libcommon"],
2745 provide_cpp_shared_libs: true,
2746 }
2747
2748 apex_key {
2749 name: "myapex.key",
2750 public_key: "testkey.avbpubkey",
2751 private_key: "testkey.pem",
2752 }
2753
2754 cc_library {
2755 name: "mylib",
2756 srcs: ["mylib.cpp"],
2757 shared_libs: ["libcommon"],
2758 system_shared_libs: [],
2759 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002760 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002761 }
2762
2763 cc_library {
2764 name: "libcommon",
2765 srcs: ["mylib_common.cpp"],
2766 system_shared_libs: [],
2767 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002768 // TODO: remove //apex_available:platform
2769 apex_available: [
2770 "//apex_available:platform",
2771 "commonapex",
2772 "myapex",
2773 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002774 }
2775 `)
2776
Sundong Ahnabb64432019-10-22 13:58:29 +09002777 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002778 apexRule1 := module1.Rule("apexRule")
2779 copyCmds1 := apexRule1.Args["copy_commands"]
2780
Sundong Ahnabb64432019-10-22 13:58:29 +09002781 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002782 apexRule2 := module2.Rule("apexRule")
2783 copyCmds2 := apexRule2.Args["copy_commands"]
2784
Colin Cross7113d202019-11-20 16:39:12 -08002785 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2786 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002787 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2788 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2789 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2790}
2791
2792func TestApexUsesFailsIfNotProvided(t *testing.T) {
2793 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2794 apex {
2795 name: "myapex",
2796 key: "myapex.key",
2797 uses: ["commonapex"],
2798 }
2799
2800 apex {
2801 name: "commonapex",
2802 key: "myapex.key",
2803 }
2804
2805 apex_key {
2806 name: "myapex.key",
2807 public_key: "testkey.avbpubkey",
2808 private_key: "testkey.pem",
2809 }
2810 `)
2811 testApexError(t, `uses: "commonapex" is not a provider`, `
2812 apex {
2813 name: "myapex",
2814 key: "myapex.key",
2815 uses: ["commonapex"],
2816 }
2817
2818 cc_library {
2819 name: "commonapex",
2820 system_shared_libs: [],
2821 stl: "none",
2822 }
2823
2824 apex_key {
2825 name: "myapex.key",
2826 public_key: "testkey.avbpubkey",
2827 private_key: "testkey.pem",
2828 }
2829 `)
2830}
2831
2832func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2833 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2834 apex {
2835 name: "myapex",
2836 key: "myapex.key",
2837 use_vendor: true,
2838 uses: ["commonapex"],
2839 }
2840
2841 apex {
2842 name: "commonapex",
2843 key: "myapex.key",
2844 provide_cpp_shared_libs: true,
2845 }
2846
2847 apex_key {
2848 name: "myapex.key",
2849 public_key: "testkey.avbpubkey",
2850 private_key: "testkey.pem",
2851 }
Jooyung Handc782442019-11-01 03:14:38 +09002852 `, func(fs map[string][]byte, config android.Config) {
2853 setUseVendorWhitelistForTest(config, []string{"myapex"})
2854 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002855}
2856
Jooyung Hand48f3c32019-08-23 11:18:57 +09002857func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2858 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2859 apex {
2860 name: "myapex",
2861 key: "myapex.key",
2862 native_shared_libs: ["libfoo"],
2863 }
2864
2865 apex_key {
2866 name: "myapex.key",
2867 public_key: "testkey.avbpubkey",
2868 private_key: "testkey.pem",
2869 }
2870
2871 cc_library {
2872 name: "libfoo",
2873 stl: "none",
2874 system_shared_libs: [],
2875 enabled: false,
2876 }
2877 `)
2878 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2879 apex {
2880 name: "myapex",
2881 key: "myapex.key",
2882 java_libs: ["myjar"],
2883 }
2884
2885 apex_key {
2886 name: "myapex.key",
2887 public_key: "testkey.avbpubkey",
2888 private_key: "testkey.pem",
2889 }
2890
2891 java_library {
2892 name: "myjar",
2893 srcs: ["foo/bar/MyClass.java"],
2894 sdk_version: "none",
2895 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09002896 enabled: false,
2897 }
2898 `)
2899}
2900
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002901func TestApexWithApps(t *testing.T) {
2902 ctx, _ := testApex(t, `
2903 apex {
2904 name: "myapex",
2905 key: "myapex.key",
2906 apps: [
2907 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002908 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002909 ],
2910 }
2911
2912 apex_key {
2913 name: "myapex.key",
2914 public_key: "testkey.avbpubkey",
2915 private_key: "testkey.pem",
2916 }
2917
2918 android_app {
2919 name: "AppFoo",
2920 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08002921 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002922 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002923 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08002924 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002925 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002926 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002927
2928 android_app {
2929 name: "AppFooPriv",
2930 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08002931 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09002932 system_modules: "none",
2933 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08002934 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002935 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09002936 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002937
2938 cc_library_shared {
2939 name: "libjni",
2940 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002941 shared_libs: ["libfoo"],
2942 stl: "none",
2943 system_shared_libs: [],
2944 apex_available: [ "myapex" ],
2945 sdk_version: "current",
2946 }
2947
2948 cc_library_shared {
2949 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09002950 stl: "none",
2951 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09002952 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08002953 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09002954 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002955 `)
2956
Sundong Ahnabb64432019-10-22 13:58:29 +09002957 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002958 apexRule := module.Rule("apexRule")
2959 copyCmds := apexRule.Args["copy_commands"]
2960
2961 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002962 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002963
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002964 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
2965 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09002966 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002967 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002968 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002969 // JNI libraries including transitive deps are
2970 for _, jni := range []string{"libjni", "libfoo"} {
2971 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
2972 // ... embedded inside APK (jnilibs.zip)
2973 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
2974 // ... and not directly inside the APEX
2975 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
2976 }
Dario Frenicde2a032019-10-27 00:29:22 +01002977}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002978
Dario Frenicde2a032019-10-27 00:29:22 +01002979func TestApexWithAppImports(t *testing.T) {
2980 ctx, _ := testApex(t, `
2981 apex {
2982 name: "myapex",
2983 key: "myapex.key",
2984 apps: [
2985 "AppFooPrebuilt",
2986 "AppFooPrivPrebuilt",
2987 ],
2988 }
2989
2990 apex_key {
2991 name: "myapex.key",
2992 public_key: "testkey.avbpubkey",
2993 private_key: "testkey.pem",
2994 }
2995
2996 android_app_import {
2997 name: "AppFooPrebuilt",
2998 apk: "PrebuiltAppFoo.apk",
2999 presigned: true,
3000 dex_preopt: {
3001 enabled: false,
3002 },
3003 }
3004
3005 android_app_import {
3006 name: "AppFooPrivPrebuilt",
3007 apk: "PrebuiltAppFooPriv.apk",
3008 privileged: true,
3009 presigned: true,
3010 dex_preopt: {
3011 enabled: false,
3012 },
3013 }
3014 `)
3015
Sundong Ahnabb64432019-10-22 13:58:29 +09003016 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003017 apexRule := module.Rule("apexRule")
3018 copyCmds := apexRule.Args["copy_commands"]
3019
3020 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3021 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003022}
3023
Dario Freni6f3937c2019-12-20 22:58:03 +00003024func TestApexWithTestHelperApp(t *testing.T) {
3025 ctx, _ := testApex(t, `
3026 apex {
3027 name: "myapex",
3028 key: "myapex.key",
3029 apps: [
3030 "TesterHelpAppFoo",
3031 ],
3032 }
3033
3034 apex_key {
3035 name: "myapex.key",
3036 public_key: "testkey.avbpubkey",
3037 private_key: "testkey.pem",
3038 }
3039
3040 android_test_helper_app {
3041 name: "TesterHelpAppFoo",
3042 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003043 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003044 }
3045
3046 `)
3047
3048 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3049 apexRule := module.Rule("apexRule")
3050 copyCmds := apexRule.Args["copy_commands"]
3051
3052 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3053}
3054
Jooyung Han18020ea2019-11-13 10:50:48 +09003055func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3056 // libfoo's apex_available comes from cc_defaults
3057 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3058 apex {
3059 name: "myapex",
3060 key: "myapex.key",
3061 native_shared_libs: ["libfoo"],
3062 }
3063
3064 apex_key {
3065 name: "myapex.key",
3066 public_key: "testkey.avbpubkey",
3067 private_key: "testkey.pem",
3068 }
3069
3070 apex {
3071 name: "otherapex",
3072 key: "myapex.key",
3073 native_shared_libs: ["libfoo"],
3074 }
3075
3076 cc_defaults {
3077 name: "libfoo-defaults",
3078 apex_available: ["otherapex"],
3079 }
3080
3081 cc_library {
3082 name: "libfoo",
3083 defaults: ["libfoo-defaults"],
3084 stl: "none",
3085 system_shared_libs: [],
3086 }`)
3087}
3088
Jiyong Park127b40b2019-09-30 16:04:35 +09003089func TestApexAvailable(t *testing.T) {
3090 // libfoo is not available to myapex, but only to otherapex
3091 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3092 apex {
3093 name: "myapex",
3094 key: "myapex.key",
3095 native_shared_libs: ["libfoo"],
3096 }
3097
3098 apex_key {
3099 name: "myapex.key",
3100 public_key: "testkey.avbpubkey",
3101 private_key: "testkey.pem",
3102 }
3103
3104 apex {
3105 name: "otherapex",
3106 key: "otherapex.key",
3107 native_shared_libs: ["libfoo"],
3108 }
3109
3110 apex_key {
3111 name: "otherapex.key",
3112 public_key: "testkey.avbpubkey",
3113 private_key: "testkey.pem",
3114 }
3115
3116 cc_library {
3117 name: "libfoo",
3118 stl: "none",
3119 system_shared_libs: [],
3120 apex_available: ["otherapex"],
3121 }`)
3122
3123 // libbar is an indirect dep
3124 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3125 apex {
3126 name: "myapex",
3127 key: "myapex.key",
3128 native_shared_libs: ["libfoo"],
3129 }
3130
3131 apex_key {
3132 name: "myapex.key",
3133 public_key: "testkey.avbpubkey",
3134 private_key: "testkey.pem",
3135 }
3136
3137 apex {
3138 name: "otherapex",
3139 key: "otherapex.key",
3140 native_shared_libs: ["libfoo"],
3141 }
3142
3143 apex_key {
3144 name: "otherapex.key",
3145 public_key: "testkey.avbpubkey",
3146 private_key: "testkey.pem",
3147 }
3148
3149 cc_library {
3150 name: "libfoo",
3151 stl: "none",
3152 shared_libs: ["libbar"],
3153 system_shared_libs: [],
3154 apex_available: ["myapex", "otherapex"],
3155 }
3156
3157 cc_library {
3158 name: "libbar",
3159 stl: "none",
3160 system_shared_libs: [],
3161 apex_available: ["otherapex"],
3162 }`)
3163
3164 testApexError(t, "\"otherapex\" is not a valid module name", `
3165 apex {
3166 name: "myapex",
3167 key: "myapex.key",
3168 native_shared_libs: ["libfoo"],
3169 }
3170
3171 apex_key {
3172 name: "myapex.key",
3173 public_key: "testkey.avbpubkey",
3174 private_key: "testkey.pem",
3175 }
3176
3177 cc_library {
3178 name: "libfoo",
3179 stl: "none",
3180 system_shared_libs: [],
3181 apex_available: ["otherapex"],
3182 }`)
3183
3184 ctx, _ := testApex(t, `
3185 apex {
3186 name: "myapex",
3187 key: "myapex.key",
3188 native_shared_libs: ["libfoo", "libbar"],
3189 }
3190
3191 apex_key {
3192 name: "myapex.key",
3193 public_key: "testkey.avbpubkey",
3194 private_key: "testkey.pem",
3195 }
3196
3197 cc_library {
3198 name: "libfoo",
3199 stl: "none",
3200 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09003201 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003202 apex_available: ["myapex"],
3203 }
3204
3205 cc_library {
3206 name: "libbar",
3207 stl: "none",
3208 system_shared_libs: [],
3209 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09003210 }
3211
3212 cc_library {
3213 name: "libbaz",
3214 stl: "none",
3215 system_shared_libs: [],
3216 stubs: {
3217 versions: ["10", "20", "30"],
3218 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003219 }`)
3220
3221 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003222 // TODO(jiyong) the checks for the platform variant are removed because we now create
3223 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3224 // the platform variants are not used from other platform modules. When that is done,
3225 // these checks will be replaced by expecting a specific error message that will be
3226 // emitted when the platform variant is used.
3227 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3228 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3229 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3230 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003231
3232 ctx, _ = testApex(t, `
3233 apex {
3234 name: "myapex",
3235 key: "myapex.key",
3236 }
3237
3238 apex_key {
3239 name: "myapex.key",
3240 public_key: "testkey.avbpubkey",
3241 private_key: "testkey.pem",
3242 }
3243
3244 cc_library {
3245 name: "libfoo",
3246 stl: "none",
3247 system_shared_libs: [],
3248 apex_available: ["//apex_available:platform"],
3249 }`)
3250
3251 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003252 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3253 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003254
3255 ctx, _ = testApex(t, `
3256 apex {
3257 name: "myapex",
3258 key: "myapex.key",
3259 native_shared_libs: ["libfoo"],
3260 }
3261
3262 apex_key {
3263 name: "myapex.key",
3264 public_key: "testkey.avbpubkey",
3265 private_key: "testkey.pem",
3266 }
3267
3268 cc_library {
3269 name: "libfoo",
3270 stl: "none",
3271 system_shared_libs: [],
3272 apex_available: ["myapex"],
3273 static: {
3274 apex_available: ["//apex_available:platform"],
3275 },
3276 }`)
3277
3278 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003279 // TODO(jiyong) the checks for the platform variant are removed because we now create
3280 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3281 // the platform variants are not used from other platform modules. When that is done,
3282 // these checks will be replaced by expecting a specific error message that will be
3283 // emitted when the platform variant is used.
3284 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3285 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3286 // // but the static variant is available to both myapex and the platform
3287 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3288 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003289}
3290
Jiyong Park5d790c32019-11-15 18:40:32 +09003291func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003292 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003293 apex {
3294 name: "myapex",
3295 key: "myapex.key",
3296 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003297 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003298 }
3299
3300 override_apex {
3301 name: "override_myapex",
3302 base: "myapex",
3303 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003304 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003305 logging_parent: "com.foo.bar",
Jiyong Park5d790c32019-11-15 18:40:32 +09003306 }
3307
3308 apex_key {
3309 name: "myapex.key",
3310 public_key: "testkey.avbpubkey",
3311 private_key: "testkey.pem",
3312 }
3313
3314 android_app {
3315 name: "app",
3316 srcs: ["foo/bar/MyClass.java"],
3317 package_name: "foo",
3318 sdk_version: "none",
3319 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003320 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003321 }
3322
3323 override_android_app {
3324 name: "override_app",
3325 base: "app",
3326 package_name: "bar",
3327 }
Jiyong Park20bacab2020-03-03 11:45:41 +09003328 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003329
Jiyong Park317645e2019-12-05 13:20:58 +09003330 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3331 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3332 if originalVariant.GetOverriddenBy() != "" {
3333 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3334 }
3335 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3336 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3337 }
3338
Jiyong Park5d790c32019-11-15 18:40:32 +09003339 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3340 apexRule := module.Rule("apexRule")
3341 copyCmds := apexRule.Args["copy_commands"]
3342
3343 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3344 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003345
3346 apexBundle := module.Module().(*apexBundle)
3347 name := apexBundle.Name()
3348 if name != "override_myapex" {
3349 t.Errorf("name should be \"override_myapex\", but was %q", name)
3350 }
3351
Baligh Uddin004d7172020-02-19 21:29:28 -08003352 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3353 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3354 }
3355
Jiyong Park20bacab2020-03-03 11:45:41 +09003356 optFlags := apexRule.Args["opt_flags"]
3357 ensureContains(t, optFlags, "--override_apk_package_name com.android.myapex")
3358
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003359 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3360 var builder strings.Builder
3361 data.Custom(&builder, name, "TARGET_", "", data)
3362 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003363 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003364 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3365 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003366 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003367 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003368 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003369 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3370 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003371}
3372
Jooyung Han214bf372019-11-12 13:03:50 +09003373func TestLegacyAndroid10Support(t *testing.T) {
3374 ctx, _ := testApex(t, `
3375 apex {
3376 name: "myapex",
3377 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003378 native_shared_libs: ["mylib"],
Jooyung Han214bf372019-11-12 13:03:50 +09003379 legacy_android10_support: true,
3380 }
3381
3382 apex_key {
3383 name: "myapex.key",
3384 public_key: "testkey.avbpubkey",
3385 private_key: "testkey.pem",
3386 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003387
3388 cc_library {
3389 name: "mylib",
3390 srcs: ["mylib.cpp"],
3391 stl: "libc++",
3392 system_shared_libs: [],
3393 apex_available: [ "myapex" ],
3394 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003395 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003396
3397 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3398 args := module.Rule("apexRule").Args
3399 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003400 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003401
3402 // The copies of the libraries in the apex should have one more dependency than
3403 // the ones outside the apex, namely the unwinder. Ideally we should check
3404 // the dependency names directly here but for some reason the names are blank in
3405 // this test.
3406 for _, lib := range []string{"libc++", "mylib"} {
3407 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3408 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3409 if len(apexImplicits) != len(nonApexImplicits)+1 {
3410 t.Errorf("%q missing unwinder dep", lib)
3411 }
3412 }
Jooyung Han214bf372019-11-12 13:03:50 +09003413}
3414
Jooyung Han58f26ab2019-12-18 15:34:32 +09003415func TestJavaSDKLibrary(t *testing.T) {
3416 ctx, _ := testApex(t, `
3417 apex {
3418 name: "myapex",
3419 key: "myapex.key",
3420 java_libs: ["foo"],
3421 }
3422
3423 apex_key {
3424 name: "myapex.key",
3425 public_key: "testkey.avbpubkey",
3426 private_key: "testkey.pem",
3427 }
3428
3429 java_sdk_library {
3430 name: "foo",
3431 srcs: ["a.java"],
3432 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003433 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003434 }
3435 `, withFiles(map[string][]byte{
3436 "api/current.txt": nil,
3437 "api/removed.txt": nil,
3438 "api/system-current.txt": nil,
3439 "api/system-removed.txt": nil,
3440 "api/test-current.txt": nil,
3441 "api/test-removed.txt": nil,
3442 }))
3443
3444 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003445 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003446 "javalib/foo.jar",
3447 "etc/permissions/foo.xml",
3448 })
3449 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003450 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3451 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003452}
3453
atrost6e126252020-01-27 17:01:16 +00003454func TestCompatConfig(t *testing.T) {
3455 ctx, _ := testApex(t, `
3456 apex {
3457 name: "myapex",
3458 key: "myapex.key",
3459 prebuilts: ["myjar-platform-compat-config"],
3460 java_libs: ["myjar"],
3461 }
3462
3463 apex_key {
3464 name: "myapex.key",
3465 public_key: "testkey.avbpubkey",
3466 private_key: "testkey.pem",
3467 }
3468
3469 platform_compat_config {
3470 name: "myjar-platform-compat-config",
3471 src: ":myjar",
3472 }
3473
3474 java_library {
3475 name: "myjar",
3476 srcs: ["foo/bar/MyClass.java"],
3477 sdk_version: "none",
3478 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003479 apex_available: [ "myapex" ],
3480 }
3481 `)
3482 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3483 "etc/compatconfig/myjar-platform-compat-config.xml",
3484 "javalib/myjar.jar",
3485 })
3486}
3487
Jiyong Park479321d2019-12-16 11:47:12 +09003488func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3489 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3490 apex {
3491 name: "myapex",
3492 key: "myapex.key",
3493 java_libs: ["myjar"],
3494 }
3495
3496 apex_key {
3497 name: "myapex.key",
3498 public_key: "testkey.avbpubkey",
3499 private_key: "testkey.pem",
3500 }
3501
3502 java_library {
3503 name: "myjar",
3504 srcs: ["foo/bar/MyClass.java"],
3505 sdk_version: "none",
3506 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003507 compile_dex: false,
Jiyong Park479321d2019-12-16 11:47:12 +09003508 }
3509 `)
3510}
3511
Jiyong Park7afd1072019-12-30 16:56:33 +09003512func TestCarryRequiredModuleNames(t *testing.T) {
3513 ctx, config := testApex(t, `
3514 apex {
3515 name: "myapex",
3516 key: "myapex.key",
3517 native_shared_libs: ["mylib"],
3518 }
3519
3520 apex_key {
3521 name: "myapex.key",
3522 public_key: "testkey.avbpubkey",
3523 private_key: "testkey.pem",
3524 }
3525
3526 cc_library {
3527 name: "mylib",
3528 srcs: ["mylib.cpp"],
3529 system_shared_libs: [],
3530 stl: "none",
3531 required: ["a", "b"],
3532 host_required: ["c", "d"],
3533 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003534 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003535 }
3536 `)
3537
3538 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3539 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3540 name := apexBundle.BaseModuleName()
3541 prefix := "TARGET_"
3542 var builder strings.Builder
3543 data.Custom(&builder, name, prefix, "", data)
3544 androidMk := builder.String()
3545 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3546 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3547 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3548}
3549
Jiyong Park7cd10e32020-01-14 09:22:18 +09003550func TestSymlinksFromApexToSystem(t *testing.T) {
3551 bp := `
3552 apex {
3553 name: "myapex",
3554 key: "myapex.key",
3555 native_shared_libs: ["mylib"],
3556 java_libs: ["myjar"],
3557 }
3558
Jiyong Park9d677202020-02-19 16:29:35 +09003559 apex {
3560 name: "myapex.updatable",
3561 key: "myapex.key",
3562 native_shared_libs: ["mylib"],
3563 java_libs: ["myjar"],
3564 updatable: true,
3565 }
3566
Jiyong Park7cd10e32020-01-14 09:22:18 +09003567 apex_key {
3568 name: "myapex.key",
3569 public_key: "testkey.avbpubkey",
3570 private_key: "testkey.pem",
3571 }
3572
3573 cc_library {
3574 name: "mylib",
3575 srcs: ["mylib.cpp"],
3576 shared_libs: ["myotherlib"],
3577 system_shared_libs: [],
3578 stl: "none",
3579 apex_available: [
3580 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003581 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003582 "//apex_available:platform",
3583 ],
3584 }
3585
3586 cc_library {
3587 name: "myotherlib",
3588 srcs: ["mylib.cpp"],
3589 system_shared_libs: [],
3590 stl: "none",
3591 apex_available: [
3592 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003593 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003594 "//apex_available:platform",
3595 ],
3596 }
3597
3598 java_library {
3599 name: "myjar",
3600 srcs: ["foo/bar/MyClass.java"],
3601 sdk_version: "none",
3602 system_modules: "none",
3603 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003604 apex_available: [
3605 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003606 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003607 "//apex_available:platform",
3608 ],
3609 }
3610
3611 java_library {
3612 name: "myotherjar",
3613 srcs: ["foo/bar/MyClass.java"],
3614 sdk_version: "none",
3615 system_modules: "none",
3616 apex_available: [
3617 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003618 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003619 "//apex_available:platform",
3620 ],
3621 }
3622 `
3623
3624 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3625 for _, f := range files {
3626 if f.path == file {
3627 if f.isLink {
3628 t.Errorf("%q is not a real file", file)
3629 }
3630 return
3631 }
3632 }
3633 t.Errorf("%q is not found", file)
3634 }
3635
3636 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3637 for _, f := range files {
3638 if f.path == file {
3639 if !f.isLink {
3640 t.Errorf("%q is not a symlink", file)
3641 }
3642 return
3643 }
3644 }
3645 t.Errorf("%q is not found", file)
3646 }
3647
Jiyong Park9d677202020-02-19 16:29:35 +09003648 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3649 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003650 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003651 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003652 ensureRealfileExists(t, files, "javalib/myjar.jar")
3653 ensureRealfileExists(t, files, "lib64/mylib.so")
3654 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3655
Jiyong Park9d677202020-02-19 16:29:35 +09003656 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3657 ensureRealfileExists(t, files, "javalib/myjar.jar")
3658 ensureRealfileExists(t, files, "lib64/mylib.so")
3659 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3660
3661 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003662 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003663 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003664 ensureRealfileExists(t, files, "javalib/myjar.jar")
3665 ensureRealfileExists(t, files, "lib64/mylib.so")
3666 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09003667
3668 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3669 ensureRealfileExists(t, files, "javalib/myjar.jar")
3670 ensureRealfileExists(t, files, "lib64/mylib.so")
3671 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09003672}
3673
Jooyung Han643adc42020-02-27 13:50:06 +09003674func TestApexWithJniLibs(t *testing.T) {
3675 ctx, _ := testApex(t, `
3676 apex {
3677 name: "myapex",
3678 key: "myapex.key",
3679 jni_libs: ["mylib"],
3680 }
3681
3682 apex_key {
3683 name: "myapex.key",
3684 public_key: "testkey.avbpubkey",
3685 private_key: "testkey.pem",
3686 }
3687
3688 cc_library {
3689 name: "mylib",
3690 srcs: ["mylib.cpp"],
3691 shared_libs: ["mylib2"],
3692 system_shared_libs: [],
3693 stl: "none",
3694 apex_available: [ "myapex" ],
3695 }
3696
3697 cc_library {
3698 name: "mylib2",
3699 srcs: ["mylib.cpp"],
3700 system_shared_libs: [],
3701 stl: "none",
3702 apex_available: [ "myapex" ],
3703 }
3704 `)
3705
3706 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
3707 // Notice mylib2.so (transitive dep) is not added as a jni_lib
3708 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
3709 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3710 "lib64/mylib.so",
3711 "lib64/mylib2.so",
3712 })
3713}
3714
3715func TestApexWithJniLibs_Errors(t *testing.T) {
3716 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
3717 apex {
3718 name: "myapex",
3719 key: "myapex.key",
3720 jni_libs: ["xxx"],
3721 }
3722
3723 apex_key {
3724 name: "myapex.key",
3725 public_key: "testkey.avbpubkey",
3726 private_key: "testkey.pem",
3727 }
3728
3729 prebuilt_etc {
3730 name: "xxx",
3731 src: "xxx",
3732 }
3733 `, withFiles(map[string][]byte{
3734 "xxx": nil,
3735 }))
3736}
3737
Jiyong Parkbd159612020-02-28 15:22:21 +09003738func TestAppBundle(t *testing.T) {
3739 ctx, _ := testApex(t, `
3740 apex {
3741 name: "myapex",
3742 key: "myapex.key",
3743 apps: ["AppFoo"],
3744 }
3745
3746 apex_key {
3747 name: "myapex.key",
3748 public_key: "testkey.avbpubkey",
3749 private_key: "testkey.pem",
3750 }
3751
3752 android_app {
3753 name: "AppFoo",
3754 srcs: ["foo/bar/MyClass.java"],
3755 sdk_version: "none",
3756 system_modules: "none",
3757 apex_available: [ "myapex" ],
3758 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09003759 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09003760
3761 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
3762 content := bundleConfigRule.Args["content"]
3763
3764 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09003765 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo/AppFoo.apk"}]}`)
Jiyong Parkbd159612020-02-28 15:22:21 +09003766}
3767
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003768func TestMain(m *testing.M) {
3769 run := func() int {
3770 setUp()
3771 defer tearDown()
3772
3773 return m.Run()
3774 }
3775
3776 os.Exit(run())
3777}