blob: 7361fc6d38f877131a125f36c3124b1c1f0ec8e6 [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
Jooyung Han31c470b2019-10-18 16:26:59 +090090func withBinder32bit(fs map[string][]byte, config android.Config) {
91 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
92}
93
Jiyong Park7cd10e32020-01-14 09:22:18 +090094func withUnbundledBuild(fs map[string][]byte, config android.Config) {
95 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
96}
97
Jooyung Han344d5432019-08-23 11:17:39 +090098func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090099 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900100
101 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900102 filegroup {
103 name: "myapex-file_contexts",
104 srcs: [
105 "system/sepolicy/apex/myapex-file_contexts",
106 ],
107 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900108 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800109
Colin Crossf9aabd72020-02-15 11:29:50 -0800110 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
111
Dario Frenicde2a032019-10-27 00:29:22 +0100112 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900113
Jooyung Han344d5432019-08-23 11:17:39 +0900114 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900115 "a.java": nil,
116 "PrebuiltAppFoo.apk": nil,
117 "PrebuiltAppFooPriv.apk": nil,
118 "build/make/target/product/security": nil,
119 "apex_manifest.json": nil,
120 "AndroidManifest.xml": nil,
121 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900122 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900123 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900124 "system/sepolicy/apex/otherapex-file_contexts": nil,
125 "system/sepolicy/apex/commonapex-file_contexts": nil,
126 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800127 "mylib.cpp": nil,
128 "mylib_common.cpp": nil,
129 "mytest.cpp": nil,
130 "mytest1.cpp": nil,
131 "mytest2.cpp": nil,
132 "mytest3.cpp": nil,
133 "myprebuilt": nil,
134 "my_include": nil,
135 "foo/bar/MyClass.java": nil,
136 "prebuilt.jar": nil,
137 "vendor/foo/devkeys/test.x509.pem": nil,
138 "vendor/foo/devkeys/test.pk8": nil,
139 "testkey.x509.pem": nil,
140 "testkey.pk8": nil,
141 "testkey.override.x509.pem": nil,
142 "testkey.override.pk8": nil,
143 "vendor/foo/devkeys/testkey.avbpubkey": nil,
144 "vendor/foo/devkeys/testkey.pem": nil,
145 "NOTICE": nil,
146 "custom_notice": nil,
147 "testkey2.avbpubkey": nil,
148 "testkey2.pem": nil,
149 "myapex-arm64.apex": nil,
150 "myapex-arm.apex": nil,
151 "frameworks/base/api/current.txt": nil,
152 "framework/aidl/a.aidl": nil,
153 "build/make/core/proguard.flags": nil,
154 "build/make/core/proguard_basic_keeps.flags": nil,
155 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900156 }
157
Colin Crossf9aabd72020-02-15 11:29:50 -0800158 cc.GatherRequiredFilesForTest(fs)
159
Jooyung Han344d5432019-08-23 11:17:39 +0900160 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800161 // The fs now needs to be populated before creating the config, call handlers twice
162 // for now, once to get any fs changes, and later after the config was created to
163 // set product variables or targets.
164 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
165 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900166 }
167
Colin Cross98be1bb2019-12-13 20:41:13 -0800168 config := android.TestArchConfig(buildDir, nil, bp, fs)
169 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
170 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
171 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
172 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
173 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
174 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
175
176 for _, handler := range handlers {
177 // The fs now needs to be populated before creating the config, call handlers twice
178 // for now, earlier to get any fs changes, and now after the config was created to
179 // set product variables or targets.
180 tempFS := map[string][]byte{}
181 handler(tempFS, config)
182 }
183
184 ctx := android.NewTestArchContext()
185 ctx.RegisterModuleType("apex", BundleFactory)
186 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
187 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
188 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
189 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
190 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
191 ctx.RegisterModuleType("override_apex", overrideApexFactory)
192
Jooyung Hana57af4a2020-01-23 05:36:59 +0000193 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
194 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
195
Paul Duffin77980a82019-12-19 16:01:36 +0000196 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800197 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800198 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
199 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800200 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000201 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800202 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800203 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000204 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000205 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000206 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900207 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800208
Colin Cross98be1bb2019-12-13 20:41:13 -0800209 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800210 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800211
212 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213
Jooyung Han5c998b92019-06-27 11:30:33 +0900214 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900215}
216
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700217func setUp() {
218 var err error
219 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900220 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700221 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900222 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900223}
224
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700225func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900226 os.RemoveAll(buildDir)
227}
228
229// ensure that 'result' contains 'expected'
230func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900231 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900232 if !strings.Contains(result, expected) {
233 t.Errorf("%q is not found in %q", expected, result)
234 }
235}
236
237// ensures that 'result' does not contain 'notExpected'
238func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900239 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900240 if strings.Contains(result, notExpected) {
241 t.Errorf("%q is found in %q", notExpected, result)
242 }
243}
244
245func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900246 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 if !android.InList(expected, result) {
248 t.Errorf("%q is not found in %v", expected, result)
249 }
250}
251
252func ensureListNotContains(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 android.InList(notExpected, result) {
255 t.Errorf("%q is found in %v", notExpected, result)
256 }
257}
258
Jooyung Hane1633032019-08-01 17:41:43 +0900259func ensureListEmpty(t *testing.T, result []string) {
260 t.Helper()
261 if len(result) > 0 {
262 t.Errorf("%q is expected to be empty", result)
263 }
264}
265
Jiyong Park25fc6a92018-11-18 18:02:45 +0900266// Minimal test
267func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700268 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900269 apex_defaults {
270 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900271 manifest: ":myapex.manifest",
272 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 key: "myapex.key",
274 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800275 multilib: {
276 both: {
277 binaries: ["foo",],
278 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900279 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900280 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281 }
282
Jiyong Park30ca9372019-02-07 16:27:23 +0900283 apex {
284 name: "myapex",
285 defaults: ["myapex-defaults"],
286 }
287
Jiyong Park25fc6a92018-11-18 18:02:45 +0900288 apex_key {
289 name: "myapex.key",
290 public_key: "testkey.avbpubkey",
291 private_key: "testkey.pem",
292 }
293
Jiyong Park809bb722019-02-13 21:33:49 +0900294 filegroup {
295 name: "myapex.manifest",
296 srcs: ["apex_manifest.json"],
297 }
298
299 filegroup {
300 name: "myapex.androidmanifest",
301 srcs: ["AndroidManifest.xml"],
302 }
303
Jiyong Park25fc6a92018-11-18 18:02:45 +0900304 cc_library {
305 name: "mylib",
306 srcs: ["mylib.cpp"],
307 shared_libs: ["mylib2"],
308 system_shared_libs: [],
309 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000310 // TODO: remove //apex_available:platform
311 apex_available: [
312 "//apex_available:platform",
313 "myapex",
314 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 }
316
Alex Light3d673592019-01-18 14:37:31 -0800317 cc_binary {
318 name: "foo",
319 srcs: ["mylib.cpp"],
320 compile_multilib: "both",
321 multilib: {
322 lib32: {
323 suffix: "32",
324 },
325 lib64: {
326 suffix: "64",
327 },
328 },
329 symlinks: ["foo_link_"],
330 symlink_preferred_arch: true,
331 system_shared_libs: [],
332 static_executable: true,
333 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000334 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800335 }
336
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 cc_library {
338 name: "mylib2",
339 srcs: ["mylib.cpp"],
340 system_shared_libs: [],
341 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900342 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000343 // TODO: remove //apex_available:platform
344 apex_available: [
345 "//apex_available:platform",
346 "myapex",
347 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900349
350 java_library {
351 name: "myjar",
352 srcs: ["foo/bar/MyClass.java"],
353 sdk_version: "none",
354 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900355 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900356 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000357 // TODO: remove //apex_available:platform
358 apex_available: [
359 "//apex_available:platform",
360 "myapex",
361 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900362 }
363
364 java_library {
365 name: "myotherjar",
366 srcs: ["foo/bar/MyClass.java"],
367 sdk_version: "none",
368 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900369 // TODO: remove //apex_available:platform
370 apex_available: [
371 "//apex_available:platform",
372 "myapex",
373 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900374 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900375
376 java_library {
377 name: "mysharedjar",
378 srcs: ["foo/bar/MyClass.java"],
379 sdk_version: "none",
380 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900381 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900382 `)
383
Sundong Ahnabb64432019-10-22 13:58:29 +0900384 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900385
386 optFlags := apexRule.Args["opt_flags"]
387 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700388 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900389 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900390
Jiyong Park25fc6a92018-11-18 18:02:45 +0900391 copyCmds := apexRule.Args["copy_commands"]
392
393 // Ensure that main rule creates an output
394 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
395
396 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800397 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900398 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399
400 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800401 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900402 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900403
404 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800405 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
406 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900407 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
408 // .. but not for java libs
409 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900410 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800411
Colin Cross7113d202019-11-20 16:39:12 -0800412 // Ensure that the platform variant ends with _shared or _common
413 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
414 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900415 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
416 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900417 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
418
419 // Ensure that dynamic dependency to java libs are not included
420 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800421
422 // Ensure that all symlinks are present.
423 found_foo_link_64 := false
424 found_foo := false
425 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900426 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800427 if strings.HasSuffix(cmd, "bin/foo") {
428 found_foo = true
429 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
430 found_foo_link_64 = true
431 }
432 }
433 }
434 good := found_foo && found_foo_link_64
435 if !good {
436 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
437 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900438
Sundong Ahnabb64432019-10-22 13:58:29 +0900439 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700440 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700441 if len(noticeInputs) != 2 {
442 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900443 }
444 ensureListContains(t, noticeInputs, "NOTICE")
445 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900446
447 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 +0900448 ensureListContains(t, depsInfo, "myjar <- myapex")
449 ensureListContains(t, depsInfo, "mylib <- myapex")
450 ensureListContains(t, depsInfo, "mylib2 <- mylib")
451 ensureListContains(t, depsInfo, "myotherjar <- myjar")
452 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800453}
454
Jooyung Hanf21c7972019-12-16 22:32:06 +0900455func TestDefaults(t *testing.T) {
456 ctx, _ := testApex(t, `
457 apex_defaults {
458 name: "myapex-defaults",
459 key: "myapex.key",
460 prebuilts: ["myetc"],
461 native_shared_libs: ["mylib"],
462 java_libs: ["myjar"],
463 apps: ["AppFoo"],
464 }
465
466 prebuilt_etc {
467 name: "myetc",
468 src: "myprebuilt",
469 }
470
471 apex {
472 name: "myapex",
473 defaults: ["myapex-defaults"],
474 }
475
476 apex_key {
477 name: "myapex.key",
478 public_key: "testkey.avbpubkey",
479 private_key: "testkey.pem",
480 }
481
482 cc_library {
483 name: "mylib",
484 system_shared_libs: [],
485 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000486 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900487 }
488
489 java_library {
490 name: "myjar",
491 srcs: ["foo/bar/MyClass.java"],
492 sdk_version: "none",
493 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000494 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900495 }
496
497 android_app {
498 name: "AppFoo",
499 srcs: ["foo/bar/MyClass.java"],
500 sdk_version: "none",
501 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000502 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900503 }
504 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000505 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900506 "etc/myetc",
507 "javalib/myjar.jar",
508 "lib64/mylib.so",
509 "app/AppFoo/AppFoo.apk",
510 })
511}
512
Jooyung Han01a3ee22019-11-02 02:52:25 +0900513func TestApexManifest(t *testing.T) {
514 ctx, _ := testApex(t, `
515 apex {
516 name: "myapex",
517 key: "myapex.key",
518 }
519
520 apex_key {
521 name: "myapex.key",
522 public_key: "testkey.avbpubkey",
523 private_key: "testkey.pem",
524 }
525 `)
526
527 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900528 args := module.Rule("apexRule").Args
529 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
530 t.Error("manifest should be apex_manifest.pb, but " + manifest)
531 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900532}
533
Alex Light5098a612018-11-29 17:12:15 -0800534func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700535 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800536 apex {
537 name: "myapex",
538 key: "myapex.key",
539 payload_type: "zip",
540 native_shared_libs: ["mylib"],
541 }
542
543 apex_key {
544 name: "myapex.key",
545 public_key: "testkey.avbpubkey",
546 private_key: "testkey.pem",
547 }
548
549 cc_library {
550 name: "mylib",
551 srcs: ["mylib.cpp"],
552 shared_libs: ["mylib2"],
553 system_shared_libs: [],
554 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000555 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800556 }
557
558 cc_library {
559 name: "mylib2",
560 srcs: ["mylib.cpp"],
561 system_shared_libs: [],
562 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000563 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800564 }
565 `)
566
Sundong Ahnabb64432019-10-22 13:58:29 +0900567 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800568 copyCmds := zipApexRule.Args["copy_commands"]
569
570 // Ensure that main rule creates an output
571 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
572
573 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800574 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800575
576 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800577 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800578
579 // Ensure that both direct and indirect deps are copied into apex
580 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
581 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900582}
583
584func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700585 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900586 apex {
587 name: "myapex",
588 key: "myapex.key",
589 native_shared_libs: ["mylib", "mylib3"],
590 }
591
592 apex_key {
593 name: "myapex.key",
594 public_key: "testkey.avbpubkey",
595 private_key: "testkey.pem",
596 }
597
598 cc_library {
599 name: "mylib",
600 srcs: ["mylib.cpp"],
601 shared_libs: ["mylib2", "mylib3"],
602 system_shared_libs: [],
603 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000604 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900605 }
606
607 cc_library {
608 name: "mylib2",
609 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900610 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900611 system_shared_libs: [],
612 stl: "none",
613 stubs: {
614 versions: ["1", "2", "3"],
615 },
616 }
617
618 cc_library {
619 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900620 srcs: ["mylib.cpp"],
621 shared_libs: ["mylib4"],
622 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900623 stl: "none",
624 stubs: {
625 versions: ["10", "11", "12"],
626 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000627 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900628 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900629
630 cc_library {
631 name: "mylib4",
632 srcs: ["mylib.cpp"],
633 system_shared_libs: [],
634 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000635 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900636 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900637 `)
638
Sundong Ahnabb64432019-10-22 13:58:29 +0900639 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900640 copyCmds := apexRule.Args["copy_commands"]
641
642 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800643 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900644
645 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800646 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900647
648 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800649 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900650
Colin Cross7113d202019-11-20 16:39:12 -0800651 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900652
653 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900654 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900655 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900656 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900657
658 // 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 -0800659 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900660 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800661 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900662
663 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900664 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900665 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900666
667 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900668 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900669
Jooyung Hana57af4a2020-01-23 05:36:59 +0000670 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900671 "lib64/mylib.so",
672 "lib64/mylib3.so",
673 "lib64/mylib4.so",
674 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900675}
676
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900677func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700678 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900679 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900680 name: "myapex2",
681 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900682 native_shared_libs: ["mylib"],
683 }
684
685 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900686 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900687 public_key: "testkey.avbpubkey",
688 private_key: "testkey.pem",
689 }
690
691 cc_library {
692 name: "mylib",
693 srcs: ["mylib.cpp"],
694 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900695 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900696 system_shared_libs: [],
697 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000698 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900699 }
700
701 cc_library {
702 name: "libfoo",
703 srcs: ["mylib.cpp"],
704 shared_libs: ["libbar"],
705 system_shared_libs: [],
706 stl: "none",
707 stubs: {
708 versions: ["10", "20", "30"],
709 },
710 }
711
712 cc_library {
713 name: "libbar",
714 srcs: ["mylib.cpp"],
715 system_shared_libs: [],
716 stl: "none",
717 }
718
Jiyong Park678c8812020-02-07 17:25:49 +0900719 cc_library_static {
720 name: "libbaz",
721 srcs: ["mylib.cpp"],
722 system_shared_libs: [],
723 stl: "none",
724 apex_available: [ "myapex2" ],
725 }
726
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900727 `)
728
Jiyong Park83dc74b2020-01-14 18:38:44 +0900729 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900730 copyCmds := apexRule.Args["copy_commands"]
731
732 // Ensure that direct non-stubs dep is always included
733 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
734
735 // Ensure that indirect stubs dep is not included
736 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
737
738 // Ensure that dependency of stubs is not included
739 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
740
Jiyong Park83dc74b2020-01-14 18:38:44 +0900741 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900742
743 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900744 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900745 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900746 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900747
Jiyong Park3ff16992019-12-27 14:11:47 +0900748 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900749
750 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
751 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900752
753 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 +0900754
755 ensureListContains(t, depsInfo, "mylib <- myapex2")
756 ensureListContains(t, depsInfo, "libbaz <- mylib")
757 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900758}
759
Jooyung Hand3639552019-08-09 12:57:43 +0900760func TestApexWithRuntimeLibsDependency(t *testing.T) {
761 /*
762 myapex
763 |
764 v (runtime_libs)
765 mylib ------+------> libfoo [provides stub]
766 |
767 `------> libbar
768 */
769 ctx, _ := testApex(t, `
770 apex {
771 name: "myapex",
772 key: "myapex.key",
773 native_shared_libs: ["mylib"],
774 }
775
776 apex_key {
777 name: "myapex.key",
778 public_key: "testkey.avbpubkey",
779 private_key: "testkey.pem",
780 }
781
782 cc_library {
783 name: "mylib",
784 srcs: ["mylib.cpp"],
785 runtime_libs: ["libfoo", "libbar"],
786 system_shared_libs: [],
787 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000788 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900789 }
790
791 cc_library {
792 name: "libfoo",
793 srcs: ["mylib.cpp"],
794 system_shared_libs: [],
795 stl: "none",
796 stubs: {
797 versions: ["10", "20", "30"],
798 },
Jiyong Park0f80c182020-01-31 02:49:53 +0900799 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900800 }
801
802 cc_library {
803 name: "libbar",
804 srcs: ["mylib.cpp"],
805 system_shared_libs: [],
806 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000807 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900808 }
809
810 `)
811
Sundong Ahnabb64432019-10-22 13:58:29 +0900812 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900813 copyCmds := apexRule.Args["copy_commands"]
814
815 // Ensure that direct non-stubs dep is always included
816 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
817
818 // Ensure that indirect stubs dep is not included
819 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
820
821 // Ensure that runtime_libs dep in included
822 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
823
Sundong Ahnabb64432019-10-22 13:58:29 +0900824 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900825 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
826 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900827
828}
829
Jooyung Han9c80bae2019-08-20 17:30:57 +0900830func TestApexDependencyToLLNDK(t *testing.T) {
831 ctx, _ := testApex(t, `
832 apex {
833 name: "myapex",
834 key: "myapex.key",
835 use_vendor: true,
836 native_shared_libs: ["mylib"],
837 }
838
839 apex_key {
840 name: "myapex.key",
841 public_key: "testkey.avbpubkey",
842 private_key: "testkey.pem",
843 }
844
845 cc_library {
846 name: "mylib",
847 srcs: ["mylib.cpp"],
848 vendor_available: true,
849 shared_libs: ["libbar"],
850 system_shared_libs: [],
851 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000852 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900853 }
854
855 cc_library {
856 name: "libbar",
857 srcs: ["mylib.cpp"],
858 system_shared_libs: [],
859 stl: "none",
860 }
861
862 llndk_library {
863 name: "libbar",
864 symbol_file: "",
865 }
Jooyung Handc782442019-11-01 03:14:38 +0900866 `, func(fs map[string][]byte, config android.Config) {
867 setUseVendorWhitelistForTest(config, []string{"myapex"})
868 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900869
Sundong Ahnabb64432019-10-22 13:58:29 +0900870 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900871 copyCmds := apexRule.Args["copy_commands"]
872
873 // Ensure that LLNDK dep is not included
874 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
875
Sundong Ahnabb64432019-10-22 13:58:29 +0900876 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900877 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900878
879 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900880 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900881
882}
883
Jiyong Park25fc6a92018-11-18 18:02:45 +0900884func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700885 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900886 apex {
887 name: "myapex",
888 key: "myapex.key",
889 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
890 }
891
892 apex_key {
893 name: "myapex.key",
894 public_key: "testkey.avbpubkey",
895 private_key: "testkey.pem",
896 }
897
898 cc_library {
899 name: "mylib",
900 srcs: ["mylib.cpp"],
901 shared_libs: ["libdl#27"],
902 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000903 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900904 }
905
906 cc_library_shared {
907 name: "mylib_shared",
908 srcs: ["mylib.cpp"],
909 shared_libs: ["libdl#27"],
910 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000911 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900912 }
913
914 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900915 name: "libBootstrap",
916 srcs: ["mylib.cpp"],
917 stl: "none",
918 bootstrap: true,
919 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900920 `)
921
Sundong Ahnabb64432019-10-22 13:58:29 +0900922 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900923 copyCmds := apexRule.Args["copy_commands"]
924
925 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800926 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900927 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
928 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900929
930 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900931 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900932
Colin Cross7113d202019-11-20 16:39:12 -0800933 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
934 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
935 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900936
937 // For dependency to libc
938 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900939 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900940 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900941 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900942 // ... Cflags from stub is correctly exported to mylib
943 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
944 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
945
946 // For dependency to libm
947 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800948 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900949 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900950 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900951 // ... and is not compiling with the stub
952 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
953 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
954
955 // For dependency to libdl
956 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900957 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900958 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900959 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
960 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900961 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800962 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900963 // ... Cflags from stub is correctly exported to mylib
964 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
965 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900966
967 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800968 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
969 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
970 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
971 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900973
974func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700975 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900976 apex {
977 name: "myapex",
978 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900979 native_shared_libs: ["mylib"],
980 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900981 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900982 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900983 }
984
985 apex_key {
986 name: "myapex.key",
987 public_key: "testkey.avbpubkey",
988 private_key: "testkey.pem",
989 }
990
991 prebuilt_etc {
992 name: "myetc",
993 src: "myprebuilt",
994 sub_dir: "foo/bar",
995 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900996
997 cc_library {
998 name: "mylib",
999 srcs: ["mylib.cpp"],
1000 relative_install_path: "foo/bar",
1001 system_shared_libs: [],
1002 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001003 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001004 }
1005
1006 cc_binary {
1007 name: "mybin",
1008 srcs: ["mylib.cpp"],
1009 relative_install_path: "foo/bar",
1010 system_shared_libs: [],
1011 static_executable: true,
1012 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001013 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001014 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001015 `)
1016
Sundong Ahnabb64432019-10-22 13:58:29 +09001017 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001018 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1019
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001020 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001021 ensureListContains(t, dirs, "etc")
1022 ensureListContains(t, dirs, "etc/foo")
1023 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001024 ensureListContains(t, dirs, "lib64")
1025 ensureListContains(t, dirs, "lib64/foo")
1026 ensureListContains(t, dirs, "lib64/foo/bar")
1027 ensureListContains(t, dirs, "lib")
1028 ensureListContains(t, dirs, "lib/foo")
1029 ensureListContains(t, dirs, "lib/foo/bar")
1030
Jiyong Parkbd13e442019-03-15 18:10:35 +09001031 ensureListContains(t, dirs, "bin")
1032 ensureListContains(t, dirs, "bin/foo")
1033 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001034}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001035
1036func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001037 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001038 apex {
1039 name: "myapex",
1040 key: "myapex.key",
1041 native_shared_libs: ["mylib"],
1042 use_vendor: true,
1043 }
1044
1045 apex_key {
1046 name: "myapex.key",
1047 public_key: "testkey.avbpubkey",
1048 private_key: "testkey.pem",
1049 }
1050
1051 cc_library {
1052 name: "mylib",
1053 srcs: ["mylib.cpp"],
1054 shared_libs: ["mylib2"],
1055 system_shared_libs: [],
1056 vendor_available: true,
1057 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001058 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001059 }
1060
1061 cc_library {
1062 name: "mylib2",
1063 srcs: ["mylib.cpp"],
1064 system_shared_libs: [],
1065 vendor_available: true,
1066 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001067 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001068 }
Jooyung Handc782442019-11-01 03:14:38 +09001069 `, func(fs map[string][]byte, config android.Config) {
1070 setUseVendorWhitelistForTest(config, []string{"myapex"})
1071 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001072
1073 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001074 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001075 for _, implicit := range i.Implicits {
1076 inputsList = append(inputsList, implicit.String())
1077 }
1078 }
1079 inputsString := strings.Join(inputsList, " ")
1080
1081 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001082 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1083 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001084
1085 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001086 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1087 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001088}
Jiyong Park16e91a02018-12-20 18:18:08 +09001089
Jooyung Handc782442019-11-01 03:14:38 +09001090func TestUseVendorRestriction(t *testing.T) {
1091 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1092 apex {
1093 name: "myapex",
1094 key: "myapex.key",
1095 use_vendor: true,
1096 }
1097 apex_key {
1098 name: "myapex.key",
1099 public_key: "testkey.avbpubkey",
1100 private_key: "testkey.pem",
1101 }
1102 `, func(fs map[string][]byte, config android.Config) {
1103 setUseVendorWhitelistForTest(config, []string{""})
1104 })
1105 // no error with whitelist
1106 testApex(t, `
1107 apex {
1108 name: "myapex",
1109 key: "myapex.key",
1110 use_vendor: true,
1111 }
1112 apex_key {
1113 name: "myapex.key",
1114 public_key: "testkey.avbpubkey",
1115 private_key: "testkey.pem",
1116 }
1117 `, func(fs map[string][]byte, config android.Config) {
1118 setUseVendorWhitelistForTest(config, []string{"myapex"})
1119 })
1120}
1121
Jooyung Han5c998b92019-06-27 11:30:33 +09001122func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1123 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1124 apex {
1125 name: "myapex",
1126 key: "myapex.key",
1127 native_shared_libs: ["mylib"],
1128 use_vendor: true,
1129 }
1130
1131 apex_key {
1132 name: "myapex.key",
1133 public_key: "testkey.avbpubkey",
1134 private_key: "testkey.pem",
1135 }
1136
1137 cc_library {
1138 name: "mylib",
1139 srcs: ["mylib.cpp"],
1140 system_shared_libs: [],
1141 stl: "none",
1142 }
1143 `)
1144}
1145
Jiyong Park16e91a02018-12-20 18:18:08 +09001146func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001147 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001148 apex {
1149 name: "myapex",
1150 key: "myapex.key",
1151 native_shared_libs: ["mylib"],
1152 }
1153
1154 apex_key {
1155 name: "myapex.key",
1156 public_key: "testkey.avbpubkey",
1157 private_key: "testkey.pem",
1158 }
1159
1160 cc_library {
1161 name: "mylib",
1162 srcs: ["mylib.cpp"],
1163 system_shared_libs: [],
1164 stl: "none",
1165 stubs: {
1166 versions: ["1", "2", "3"],
1167 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001168 apex_available: [
1169 "//apex_available:platform",
1170 "myapex",
1171 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001172 }
1173
1174 cc_binary {
1175 name: "not_in_apex",
1176 srcs: ["mylib.cpp"],
1177 static_libs: ["mylib"],
1178 static_executable: true,
1179 system_shared_libs: [],
1180 stl: "none",
1181 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001182 `)
1183
Colin Cross7113d202019-11-20 16:39:12 -08001184 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001185
1186 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001187 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001188}
Jiyong Park9335a262018-12-24 11:31:58 +09001189
1190func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001191 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001192 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001193 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001194 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001195 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001196 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001197 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001198 }
1199
1200 cc_library {
1201 name: "mylib",
1202 srcs: ["mylib.cpp"],
1203 system_shared_libs: [],
1204 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001205 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001206 }
1207
1208 apex_key {
1209 name: "myapex.key",
1210 public_key: "testkey.avbpubkey",
1211 private_key: "testkey.pem",
1212 }
1213
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001214 android_app_certificate {
1215 name: "myapex.certificate",
1216 certificate: "testkey",
1217 }
1218
1219 android_app_certificate {
1220 name: "myapex.certificate.override",
1221 certificate: "testkey.override",
1222 }
1223
Jiyong Park9335a262018-12-24 11:31:58 +09001224 `)
1225
1226 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001227 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001228
1229 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1230 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1231 "vendor/foo/devkeys/testkey.avbpubkey")
1232 }
1233 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1234 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1235 "vendor/foo/devkeys/testkey.pem")
1236 }
1237
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001238 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001239 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001240 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001241 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001242 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001243 }
1244}
Jiyong Park58e364a2019-01-19 19:24:06 +09001245
Jooyung Hanf121a652019-12-17 14:30:11 +09001246func TestCertificate(t *testing.T) {
1247 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1248 ctx, _ := testApex(t, `
1249 apex {
1250 name: "myapex",
1251 key: "myapex.key",
1252 }
1253 apex_key {
1254 name: "myapex.key",
1255 public_key: "testkey.avbpubkey",
1256 private_key: "testkey.pem",
1257 }`)
1258 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1259 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1260 if actual := rule.Args["certificates"]; actual != expected {
1261 t.Errorf("certificates should be %q, not %q", expected, actual)
1262 }
1263 })
1264 t.Run("override when unspecified", func(t *testing.T) {
1265 ctx, _ := testApex(t, `
1266 apex {
1267 name: "myapex_keytest",
1268 key: "myapex.key",
1269 file_contexts: ":myapex-file_contexts",
1270 }
1271 apex_key {
1272 name: "myapex.key",
1273 public_key: "testkey.avbpubkey",
1274 private_key: "testkey.pem",
1275 }
1276 android_app_certificate {
1277 name: "myapex.certificate.override",
1278 certificate: "testkey.override",
1279 }`)
1280 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1281 expected := "testkey.override.x509.pem testkey.override.pk8"
1282 if actual := rule.Args["certificates"]; actual != expected {
1283 t.Errorf("certificates should be %q, not %q", expected, actual)
1284 }
1285 })
1286 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1287 ctx, _ := testApex(t, `
1288 apex {
1289 name: "myapex",
1290 key: "myapex.key",
1291 certificate: ":myapex.certificate",
1292 }
1293 apex_key {
1294 name: "myapex.key",
1295 public_key: "testkey.avbpubkey",
1296 private_key: "testkey.pem",
1297 }
1298 android_app_certificate {
1299 name: "myapex.certificate",
1300 certificate: "testkey",
1301 }`)
1302 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1303 expected := "testkey.x509.pem testkey.pk8"
1304 if actual := rule.Args["certificates"]; actual != expected {
1305 t.Errorf("certificates should be %q, not %q", expected, actual)
1306 }
1307 })
1308 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1309 ctx, _ := testApex(t, `
1310 apex {
1311 name: "myapex_keytest",
1312 key: "myapex.key",
1313 file_contexts: ":myapex-file_contexts",
1314 certificate: ":myapex.certificate",
1315 }
1316 apex_key {
1317 name: "myapex.key",
1318 public_key: "testkey.avbpubkey",
1319 private_key: "testkey.pem",
1320 }
1321 android_app_certificate {
1322 name: "myapex.certificate.override",
1323 certificate: "testkey.override",
1324 }`)
1325 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1326 expected := "testkey.override.x509.pem testkey.override.pk8"
1327 if actual := rule.Args["certificates"]; actual != expected {
1328 t.Errorf("certificates should be %q, not %q", expected, actual)
1329 }
1330 })
1331 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1332 ctx, _ := testApex(t, `
1333 apex {
1334 name: "myapex",
1335 key: "myapex.key",
1336 certificate: "testkey",
1337 }
1338 apex_key {
1339 name: "myapex.key",
1340 public_key: "testkey.avbpubkey",
1341 private_key: "testkey.pem",
1342 }`)
1343 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1344 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1345 if actual := rule.Args["certificates"]; actual != expected {
1346 t.Errorf("certificates should be %q, not %q", expected, actual)
1347 }
1348 })
1349 t.Run("override when specified as <name>", func(t *testing.T) {
1350 ctx, _ := testApex(t, `
1351 apex {
1352 name: "myapex_keytest",
1353 key: "myapex.key",
1354 file_contexts: ":myapex-file_contexts",
1355 certificate: "testkey",
1356 }
1357 apex_key {
1358 name: "myapex.key",
1359 public_key: "testkey.avbpubkey",
1360 private_key: "testkey.pem",
1361 }
1362 android_app_certificate {
1363 name: "myapex.certificate.override",
1364 certificate: "testkey.override",
1365 }`)
1366 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1367 expected := "testkey.override.x509.pem testkey.override.pk8"
1368 if actual := rule.Args["certificates"]; actual != expected {
1369 t.Errorf("certificates should be %q, not %q", expected, actual)
1370 }
1371 })
1372}
1373
Jiyong Park58e364a2019-01-19 19:24:06 +09001374func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001375 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001376 apex {
1377 name: "myapex",
1378 key: "myapex.key",
1379 native_shared_libs: ["mylib"],
1380 }
1381
1382 apex {
1383 name: "otherapex",
1384 key: "myapex.key",
1385 native_shared_libs: ["mylib"],
1386 }
1387
1388 apex_key {
1389 name: "myapex.key",
1390 public_key: "testkey.avbpubkey",
1391 private_key: "testkey.pem",
1392 }
1393
1394 cc_library {
1395 name: "mylib",
1396 srcs: ["mylib.cpp"],
1397 system_shared_libs: [],
1398 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001399 // TODO: remove //apex_available:platform
1400 apex_available: [
1401 "//apex_available:platform",
1402 "myapex",
1403 "otherapex",
1404 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001405 }
1406 `)
1407
Jooyung Han6b8459b2019-10-30 08:29:25 +09001408 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001409 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001410 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001411 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1412 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001413
Jooyung Han6b8459b2019-10-30 08:29:25 +09001414 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001415 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001416 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001417 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1418 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001419
Jooyung Han6b8459b2019-10-30 08:29:25 +09001420 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001421 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001422 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001423 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1424 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001425}
Jiyong Park7e636d02019-01-28 16:16:54 +09001426
1427func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001428 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001429 apex {
1430 name: "myapex",
1431 key: "myapex.key",
1432 native_shared_libs: ["mylib"],
1433 }
1434
1435 apex_key {
1436 name: "myapex.key",
1437 public_key: "testkey.avbpubkey",
1438 private_key: "testkey.pem",
1439 }
1440
1441 cc_library_headers {
1442 name: "mylib_headers",
1443 export_include_dirs: ["my_include"],
1444 system_shared_libs: [],
1445 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001446 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001447 }
1448
1449 cc_library {
1450 name: "mylib",
1451 srcs: ["mylib.cpp"],
1452 system_shared_libs: [],
1453 stl: "none",
1454 header_libs: ["mylib_headers"],
1455 export_header_lib_headers: ["mylib_headers"],
1456 stubs: {
1457 versions: ["1", "2", "3"],
1458 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001459 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001460 }
1461
1462 cc_library {
1463 name: "otherlib",
1464 srcs: ["mylib.cpp"],
1465 system_shared_libs: [],
1466 stl: "none",
1467 shared_libs: ["mylib"],
1468 }
1469 `)
1470
Colin Cross7113d202019-11-20 16:39:12 -08001471 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001472
1473 // Ensure that the include path of the header lib is exported to 'otherlib'
1474 ensureContains(t, cFlags, "-Imy_include")
1475}
Alex Light9670d332019-01-29 18:07:33 -08001476
Jiyong Park7cd10e32020-01-14 09:22:18 +09001477type fileInApex struct {
1478 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001479 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001480 isLink bool
1481}
1482
Jooyung Hana57af4a2020-01-23 05:36:59 +00001483func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001484 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001485 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001486 copyCmds := apexRule.Args["copy_commands"]
1487 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001488 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001489 for _, cmd := range strings.Split(copyCmds, "&&") {
1490 cmd = strings.TrimSpace(cmd)
1491 if cmd == "" {
1492 continue
1493 }
1494 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001495 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001496 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001497 switch terms[0] {
1498 case "mkdir":
1499 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001500 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001501 t.Fatal("copyCmds contains invalid cp command", cmd)
1502 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001503 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001504 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001505 isLink = false
1506 case "ln":
1507 if len(terms) != 3 && len(terms) != 4 {
1508 // ln LINK TARGET or ln -s LINK TARGET
1509 t.Fatal("copyCmds contains invalid ln command", cmd)
1510 }
1511 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001512 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001513 isLink = true
1514 default:
1515 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1516 }
1517 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001518 index := strings.Index(dst, imageApexDir)
1519 if index == -1 {
1520 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1521 }
1522 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001523 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001524 }
1525 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001526 return ret
1527}
1528
Jooyung Hana57af4a2020-01-23 05:36:59 +00001529func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1530 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001531 var failed bool
1532 var surplus []string
1533 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001534 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09001535 for _, expected := range files {
1536 if matched, _ := path.Match(expected, file.path); matched {
1537 filesMatched[expected] = true
1538 return
1539 }
1540 }
1541 surplus = append(surplus, file.path)
1542 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001543
Jooyung Han31c470b2019-10-18 16:26:59 +09001544 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001545 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001546 t.Log("surplus files", surplus)
1547 failed = true
1548 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001549
1550 if len(files) > len(filesMatched) {
1551 var missing []string
1552 for _, expected := range files {
1553 if !filesMatched[expected] {
1554 missing = append(missing, expected)
1555 }
1556 }
1557 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001558 t.Log("missing files", missing)
1559 failed = true
1560 }
1561 if failed {
1562 t.Fail()
1563 }
1564}
1565
Jooyung Han344d5432019-08-23 11:17:39 +09001566func TestVndkApexCurrent(t *testing.T) {
1567 ctx, _ := testApex(t, `
1568 apex_vndk {
1569 name: "myapex",
1570 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001571 }
1572
1573 apex_key {
1574 name: "myapex.key",
1575 public_key: "testkey.avbpubkey",
1576 private_key: "testkey.pem",
1577 }
1578
1579 cc_library {
1580 name: "libvndk",
1581 srcs: ["mylib.cpp"],
1582 vendor_available: true,
1583 vndk: {
1584 enabled: true,
1585 },
1586 system_shared_libs: [],
1587 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001588 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001589 }
1590
1591 cc_library {
1592 name: "libvndksp",
1593 srcs: ["mylib.cpp"],
1594 vendor_available: true,
1595 vndk: {
1596 enabled: true,
1597 support_system_process: true,
1598 },
1599 system_shared_libs: [],
1600 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001601 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001602 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001603 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001604
Jooyung Hana57af4a2020-01-23 05:36:59 +00001605 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001606 "lib/libvndk.so",
1607 "lib/libvndksp.so",
1608 "lib64/libvndk.so",
1609 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001610 "etc/llndk.libraries.VER.txt",
1611 "etc/vndkcore.libraries.VER.txt",
1612 "etc/vndksp.libraries.VER.txt",
1613 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001614 })
Jooyung Han344d5432019-08-23 11:17:39 +09001615}
1616
1617func TestVndkApexWithPrebuilt(t *testing.T) {
1618 ctx, _ := testApex(t, `
1619 apex_vndk {
1620 name: "myapex",
1621 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001622 }
1623
1624 apex_key {
1625 name: "myapex.key",
1626 public_key: "testkey.avbpubkey",
1627 private_key: "testkey.pem",
1628 }
1629
1630 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001631 name: "libvndk",
1632 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001633 vendor_available: true,
1634 vndk: {
1635 enabled: true,
1636 },
1637 system_shared_libs: [],
1638 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001639 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001640 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001641
1642 cc_prebuilt_library_shared {
1643 name: "libvndk.arm",
1644 srcs: ["libvndk.arm.so"],
1645 vendor_available: true,
1646 vndk: {
1647 enabled: true,
1648 },
1649 enabled: false,
1650 arch: {
1651 arm: {
1652 enabled: true,
1653 },
1654 },
1655 system_shared_libs: [],
1656 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001657 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001658 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001659 `+vndkLibrariesTxtFiles("current"),
1660 withFiles(map[string][]byte{
1661 "libvndk.so": nil,
1662 "libvndk.arm.so": nil,
1663 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001664
Jooyung Hana57af4a2020-01-23 05:36:59 +00001665 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001666 "lib/libvndk.so",
1667 "lib/libvndk.arm.so",
1668 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001669 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001670 })
Jooyung Han344d5432019-08-23 11:17:39 +09001671}
1672
Jooyung Han39edb6c2019-11-06 16:53:07 +09001673func vndkLibrariesTxtFiles(vers ...string) (result string) {
1674 for _, v := range vers {
1675 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001676 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001677 result += `
1678 vndk_libraries_txt {
1679 name: "` + txt + `.libraries.txt",
1680 }
1681 `
1682 }
1683 } else {
1684 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1685 result += `
1686 prebuilt_etc {
1687 name: "` + txt + `.libraries.` + v + `.txt",
1688 src: "dummy.txt",
1689 }
1690 `
1691 }
1692 }
1693 }
1694 return
1695}
1696
Jooyung Han344d5432019-08-23 11:17:39 +09001697func TestVndkApexVersion(t *testing.T) {
1698 ctx, _ := testApex(t, `
1699 apex_vndk {
1700 name: "myapex_v27",
1701 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001702 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001703 vndk_version: "27",
1704 }
1705
1706 apex_key {
1707 name: "myapex.key",
1708 public_key: "testkey.avbpubkey",
1709 private_key: "testkey.pem",
1710 }
1711
Jooyung Han31c470b2019-10-18 16:26:59 +09001712 vndk_prebuilt_shared {
1713 name: "libvndk27",
1714 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001715 vendor_available: true,
1716 vndk: {
1717 enabled: true,
1718 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001719 target_arch: "arm64",
1720 arch: {
1721 arm: {
1722 srcs: ["libvndk27_arm.so"],
1723 },
1724 arm64: {
1725 srcs: ["libvndk27_arm64.so"],
1726 },
1727 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001728 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001729 }
1730
1731 vndk_prebuilt_shared {
1732 name: "libvndk27",
1733 version: "27",
1734 vendor_available: true,
1735 vndk: {
1736 enabled: true,
1737 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001738 target_arch: "x86_64",
1739 arch: {
1740 x86: {
1741 srcs: ["libvndk27_x86.so"],
1742 },
1743 x86_64: {
1744 srcs: ["libvndk27_x86_64.so"],
1745 },
1746 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001747 }
1748 `+vndkLibrariesTxtFiles("27"),
1749 withFiles(map[string][]byte{
1750 "libvndk27_arm.so": nil,
1751 "libvndk27_arm64.so": nil,
1752 "libvndk27_x86.so": nil,
1753 "libvndk27_x86_64.so": nil,
1754 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001755
Jooyung Hana57af4a2020-01-23 05:36:59 +00001756 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001757 "lib/libvndk27_arm.so",
1758 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001759 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001760 })
Jooyung Han344d5432019-08-23 11:17:39 +09001761}
1762
1763func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1764 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1765 apex_vndk {
1766 name: "myapex_v27",
1767 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001768 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001769 vndk_version: "27",
1770 }
1771 apex_vndk {
1772 name: "myapex_v27_other",
1773 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001774 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001775 vndk_version: "27",
1776 }
1777
1778 apex_key {
1779 name: "myapex.key",
1780 public_key: "testkey.avbpubkey",
1781 private_key: "testkey.pem",
1782 }
1783
1784 cc_library {
1785 name: "libvndk",
1786 srcs: ["mylib.cpp"],
1787 vendor_available: true,
1788 vndk: {
1789 enabled: true,
1790 },
1791 system_shared_libs: [],
1792 stl: "none",
1793 }
1794
1795 vndk_prebuilt_shared {
1796 name: "libvndk",
1797 version: "27",
1798 vendor_available: true,
1799 vndk: {
1800 enabled: true,
1801 },
1802 srcs: ["libvndk.so"],
1803 }
1804 `, withFiles(map[string][]byte{
1805 "libvndk.so": nil,
1806 }))
1807}
1808
Jooyung Han90eee022019-10-01 20:02:42 +09001809func TestVndkApexNameRule(t *testing.T) {
1810 ctx, _ := testApex(t, `
1811 apex_vndk {
1812 name: "myapex",
1813 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001814 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001815 }
1816 apex_vndk {
1817 name: "myapex_v28",
1818 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001819 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001820 vndk_version: "28",
1821 }
1822 apex_key {
1823 name: "myapex.key",
1824 public_key: "testkey.avbpubkey",
1825 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001826 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001827
1828 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001829 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001830 actual := proptools.String(bundle.properties.Apex_name)
1831 if !reflect.DeepEqual(actual, expected) {
1832 t.Errorf("Got '%v', expected '%v'", actual, expected)
1833 }
1834 }
1835
1836 assertApexName("com.android.vndk.vVER", "myapex")
1837 assertApexName("com.android.vndk.v28", "myapex_v28")
1838}
1839
Jooyung Han344d5432019-08-23 11:17:39 +09001840func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1841 ctx, _ := testApex(t, `
1842 apex_vndk {
1843 name: "myapex",
1844 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001845 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001846 }
1847
1848 apex_key {
1849 name: "myapex.key",
1850 public_key: "testkey.avbpubkey",
1851 private_key: "testkey.pem",
1852 }
1853
1854 cc_library {
1855 name: "libvndk",
1856 srcs: ["mylib.cpp"],
1857 vendor_available: true,
1858 native_bridge_supported: true,
1859 host_supported: true,
1860 vndk: {
1861 enabled: true,
1862 },
1863 system_shared_libs: [],
1864 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001865 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001866 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001867 `+vndkLibrariesTxtFiles("current"),
1868 withTargets(map[android.OsType][]android.Target{
1869 android.Android: []android.Target{
1870 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1871 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1872 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1873 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1874 },
1875 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001876
Jooyung Hana57af4a2020-01-23 05:36:59 +00001877 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001878 "lib/libvndk.so",
1879 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001880 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001881 })
Jooyung Han344d5432019-08-23 11:17:39 +09001882}
1883
1884func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1885 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1886 apex_vndk {
1887 name: "myapex",
1888 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001889 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001890 native_bridge_supported: true,
1891 }
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",
1910 }
1911 `)
1912}
1913
Jooyung Han31c470b2019-10-18 16:26:59 +09001914func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001915 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001916 apex_vndk {
1917 name: "myapex_v27",
1918 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001919 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001920 vndk_version: "27",
1921 }
1922
1923 apex_key {
1924 name: "myapex.key",
1925 public_key: "testkey.avbpubkey",
1926 private_key: "testkey.pem",
1927 }
1928
1929 vndk_prebuilt_shared {
1930 name: "libvndk27",
1931 version: "27",
1932 target_arch: "arm",
1933 vendor_available: true,
1934 vndk: {
1935 enabled: true,
1936 },
1937 arch: {
1938 arm: {
1939 srcs: ["libvndk27.so"],
1940 }
1941 },
1942 }
1943
1944 vndk_prebuilt_shared {
1945 name: "libvndk27",
1946 version: "27",
1947 target_arch: "arm",
1948 binder32bit: true,
1949 vendor_available: true,
1950 vndk: {
1951 enabled: true,
1952 },
1953 arch: {
1954 arm: {
1955 srcs: ["libvndk27binder32.so"],
1956 }
1957 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001958 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001959 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001960 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001961 withFiles(map[string][]byte{
1962 "libvndk27.so": nil,
1963 "libvndk27binder32.so": nil,
1964 }),
1965 withBinder32bit,
1966 withTargets(map[android.OsType][]android.Target{
1967 android.Android: []android.Target{
1968 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1969 },
1970 }),
1971 )
1972
Jooyung Hana57af4a2020-01-23 05:36:59 +00001973 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001974 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001975 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001976 })
1977}
1978
Jooyung Hane1633032019-08-01 17:41:43 +09001979func TestDependenciesInApexManifest(t *testing.T) {
1980 ctx, _ := testApex(t, `
1981 apex {
1982 name: "myapex_nodep",
1983 key: "myapex.key",
1984 native_shared_libs: ["lib_nodep"],
1985 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001986 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001987 }
1988
1989 apex {
1990 name: "myapex_dep",
1991 key: "myapex.key",
1992 native_shared_libs: ["lib_dep"],
1993 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001994 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001995 }
1996
1997 apex {
1998 name: "myapex_provider",
1999 key: "myapex.key",
2000 native_shared_libs: ["libfoo"],
2001 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002002 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002003 }
2004
2005 apex {
2006 name: "myapex_selfcontained",
2007 key: "myapex.key",
2008 native_shared_libs: ["lib_dep", "libfoo"],
2009 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002010 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002011 }
2012
2013 apex_key {
2014 name: "myapex.key",
2015 public_key: "testkey.avbpubkey",
2016 private_key: "testkey.pem",
2017 }
2018
2019 cc_library {
2020 name: "lib_nodep",
2021 srcs: ["mylib.cpp"],
2022 system_shared_libs: [],
2023 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002024 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002025 }
2026
2027 cc_library {
2028 name: "lib_dep",
2029 srcs: ["mylib.cpp"],
2030 shared_libs: ["libfoo"],
2031 system_shared_libs: [],
2032 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002033 apex_available: [
2034 "myapex_dep",
2035 "myapex_provider",
2036 "myapex_selfcontained",
2037 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002038 }
2039
2040 cc_library {
2041 name: "libfoo",
2042 srcs: ["mytest.cpp"],
2043 stubs: {
2044 versions: ["1"],
2045 },
2046 system_shared_libs: [],
2047 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002048 apex_available: [
2049 "myapex_provider",
2050 "myapex_selfcontained",
2051 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002052 }
2053 `)
2054
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002055 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002056 var provideNativeLibs, requireNativeLibs []string
2057
Sundong Ahnabb64432019-10-22 13:58:29 +09002058 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002059 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2060 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002061 ensureListEmpty(t, provideNativeLibs)
2062 ensureListEmpty(t, requireNativeLibs)
2063
Sundong Ahnabb64432019-10-22 13:58:29 +09002064 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002065 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2066 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002067 ensureListEmpty(t, provideNativeLibs)
2068 ensureListContains(t, requireNativeLibs, "libfoo.so")
2069
Sundong Ahnabb64432019-10-22 13:58:29 +09002070 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002071 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2072 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002073 ensureListContains(t, provideNativeLibs, "libfoo.so")
2074 ensureListEmpty(t, requireNativeLibs)
2075
Sundong Ahnabb64432019-10-22 13:58:29 +09002076 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002077 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2078 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002079 ensureListContains(t, provideNativeLibs, "libfoo.so")
2080 ensureListEmpty(t, requireNativeLibs)
2081}
2082
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002083func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002084 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002085 apex {
2086 name: "myapex",
2087 key: "myapex.key",
2088 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002089 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002090 }
2091
2092 apex_key {
2093 name: "myapex.key",
2094 public_key: "testkey.avbpubkey",
2095 private_key: "testkey.pem",
2096 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002097
2098 cc_library {
2099 name: "mylib",
2100 srcs: ["mylib.cpp"],
2101 system_shared_libs: [],
2102 stl: "none",
2103 apex_available: [
2104 "//apex_available:platform",
2105 "myapex",
2106 ],
2107 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002108 `)
2109
Sundong Ahnabb64432019-10-22 13:58:29 +09002110 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002111 apexManifestRule := module.Rule("apexManifestRule")
2112 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2113 apexRule := module.Rule("apexRule")
2114 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002115
2116 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2117 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2118 name := apexBundle.BaseModuleName()
2119 prefix := "TARGET_"
2120 var builder strings.Builder
2121 data.Custom(&builder, name, prefix, "", data)
2122 androidMk := builder.String()
2123 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2124 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002125}
2126
Alex Light0851b882019-02-07 13:20:53 -08002127func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002128 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002129 apex {
2130 name: "myapex",
2131 key: "myapex.key",
2132 native_shared_libs: ["mylib_common"],
2133 }
2134
2135 apex_key {
2136 name: "myapex.key",
2137 public_key: "testkey.avbpubkey",
2138 private_key: "testkey.pem",
2139 }
2140
2141 cc_library {
2142 name: "mylib_common",
2143 srcs: ["mylib.cpp"],
2144 system_shared_libs: [],
2145 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002146 apex_available: [
2147 "//apex_available:platform",
2148 "myapex",
2149 ],
Alex Light0851b882019-02-07 13:20:53 -08002150 }
2151 `)
2152
Sundong Ahnabb64432019-10-22 13:58:29 +09002153 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002154 apexRule := module.Rule("apexRule")
2155 copyCmds := apexRule.Args["copy_commands"]
2156
2157 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2158 t.Log("Apex was a test apex!")
2159 t.Fail()
2160 }
2161 // Ensure that main rule creates an output
2162 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2163
2164 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002165 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002166
2167 // Ensure that both direct and indirect deps are copied into apex
2168 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2169
Colin Cross7113d202019-11-20 16:39:12 -08002170 // Ensure that the platform variant ends with _shared
2171 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002172
2173 if !android.InAnyApex("mylib_common") {
2174 t.Log("Found mylib_common not in any apex!")
2175 t.Fail()
2176 }
2177}
2178
2179func TestTestApex(t *testing.T) {
2180 if android.InAnyApex("mylib_common_test") {
2181 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!")
2182 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002183 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002184 apex_test {
2185 name: "myapex",
2186 key: "myapex.key",
2187 native_shared_libs: ["mylib_common_test"],
2188 }
2189
2190 apex_key {
2191 name: "myapex.key",
2192 public_key: "testkey.avbpubkey",
2193 private_key: "testkey.pem",
2194 }
2195
2196 cc_library {
2197 name: "mylib_common_test",
2198 srcs: ["mylib.cpp"],
2199 system_shared_libs: [],
2200 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002201 // TODO: remove //apex_available:platform
2202 apex_available: [
2203 "//apex_available:platform",
2204 "myapex",
2205 ],
Alex Light0851b882019-02-07 13:20:53 -08002206 }
2207 `)
2208
Sundong Ahnabb64432019-10-22 13:58:29 +09002209 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002210 apexRule := module.Rule("apexRule")
2211 copyCmds := apexRule.Args["copy_commands"]
2212
2213 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2214 t.Log("Apex was not a test apex!")
2215 t.Fail()
2216 }
2217 // Ensure that main rule creates an output
2218 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2219
2220 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002221 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002222
2223 // Ensure that both direct and indirect deps are copied into apex
2224 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2225
Colin Cross7113d202019-11-20 16:39:12 -08002226 // Ensure that the platform variant ends with _shared
2227 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002228}
2229
Alex Light9670d332019-01-29 18:07:33 -08002230func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002231 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002232 apex {
2233 name: "myapex",
2234 key: "myapex.key",
2235 multilib: {
2236 first: {
2237 native_shared_libs: ["mylib_common"],
2238 }
2239 },
2240 target: {
2241 android: {
2242 multilib: {
2243 first: {
2244 native_shared_libs: ["mylib"],
2245 }
2246 }
2247 },
2248 host: {
2249 multilib: {
2250 first: {
2251 native_shared_libs: ["mylib2"],
2252 }
2253 }
2254 }
2255 }
2256 }
2257
2258 apex_key {
2259 name: "myapex.key",
2260 public_key: "testkey.avbpubkey",
2261 private_key: "testkey.pem",
2262 }
2263
2264 cc_library {
2265 name: "mylib",
2266 srcs: ["mylib.cpp"],
2267 system_shared_libs: [],
2268 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002269 // TODO: remove //apex_available:platform
2270 apex_available: [
2271 "//apex_available:platform",
2272 "myapex",
2273 ],
Alex Light9670d332019-01-29 18:07:33 -08002274 }
2275
2276 cc_library {
2277 name: "mylib_common",
2278 srcs: ["mylib.cpp"],
2279 system_shared_libs: [],
2280 stl: "none",
2281 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002282 // TODO: remove //apex_available:platform
2283 apex_available: [
2284 "//apex_available:platform",
2285 "myapex",
2286 ],
Alex Light9670d332019-01-29 18:07:33 -08002287 }
2288
2289 cc_library {
2290 name: "mylib2",
2291 srcs: ["mylib.cpp"],
2292 system_shared_libs: [],
2293 stl: "none",
2294 compile_multilib: "first",
2295 }
2296 `)
2297
Sundong Ahnabb64432019-10-22 13:58:29 +09002298 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002299 copyCmds := apexRule.Args["copy_commands"]
2300
2301 // Ensure that main rule creates an output
2302 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2303
2304 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002305 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2306 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2307 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002308
2309 // Ensure that both direct and indirect deps are copied into apex
2310 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2311 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2312 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2313
Colin Cross7113d202019-11-20 16:39:12 -08002314 // Ensure that the platform variant ends with _shared
2315 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2316 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2317 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002318}
Jiyong Park04480cf2019-02-06 00:16:29 +09002319
2320func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002321 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002322 apex {
2323 name: "myapex",
2324 key: "myapex.key",
2325 binaries: ["myscript"],
2326 }
2327
2328 apex_key {
2329 name: "myapex.key",
2330 public_key: "testkey.avbpubkey",
2331 private_key: "testkey.pem",
2332 }
2333
2334 sh_binary {
2335 name: "myscript",
2336 src: "mylib.cpp",
2337 filename: "myscript.sh",
2338 sub_dir: "script",
2339 }
2340 `)
2341
Sundong Ahnabb64432019-10-22 13:58:29 +09002342 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002343 copyCmds := apexRule.Args["copy_commands"]
2344
2345 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2346}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002347
Jooyung Han91df2082019-11-20 01:49:42 +09002348func TestApexInVariousPartition(t *testing.T) {
2349 testcases := []struct {
2350 propName, parition, flattenedPartition string
2351 }{
2352 {"", "system", "system_ext"},
2353 {"product_specific: true", "product", "product"},
2354 {"soc_specific: true", "vendor", "vendor"},
2355 {"proprietary: true", "vendor", "vendor"},
2356 {"vendor: true", "vendor", "vendor"},
2357 {"system_ext_specific: true", "system_ext", "system_ext"},
2358 }
2359 for _, tc := range testcases {
2360 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2361 ctx, _ := testApex(t, `
2362 apex {
2363 name: "myapex",
2364 key: "myapex.key",
2365 `+tc.propName+`
2366 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002367
Jooyung Han91df2082019-11-20 01:49:42 +09002368 apex_key {
2369 name: "myapex.key",
2370 public_key: "testkey.avbpubkey",
2371 private_key: "testkey.pem",
2372 }
2373 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002374
Jooyung Han91df2082019-11-20 01:49:42 +09002375 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2376 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2377 actual := apex.installDir.String()
2378 if actual != expected {
2379 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2380 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002381
Jooyung Han91df2082019-11-20 01:49:42 +09002382 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2383 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2384 actual = flattened.installDir.String()
2385 if actual != expected {
2386 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2387 }
2388 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002389 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002390}
Jiyong Park67882562019-03-21 01:11:21 +09002391
Jooyung Han54aca7b2019-11-20 02:26:02 +09002392func TestFileContexts(t *testing.T) {
2393 ctx, _ := testApex(t, `
2394 apex {
2395 name: "myapex",
2396 key: "myapex.key",
2397 }
2398
2399 apex_key {
2400 name: "myapex.key",
2401 public_key: "testkey.avbpubkey",
2402 private_key: "testkey.pem",
2403 }
2404 `)
2405 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2406 apexRule := module.Rule("apexRule")
2407 actual := apexRule.Args["file_contexts"]
2408 expected := "system/sepolicy/apex/myapex-file_contexts"
2409 if actual != expected {
2410 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2411 }
2412
2413 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2414 apex {
2415 name: "myapex",
2416 key: "myapex.key",
2417 file_contexts: "my_own_file_contexts",
2418 }
2419
2420 apex_key {
2421 name: "myapex.key",
2422 public_key: "testkey.avbpubkey",
2423 private_key: "testkey.pem",
2424 }
2425 `, withFiles(map[string][]byte{
2426 "my_own_file_contexts": nil,
2427 }))
2428
2429 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2430 apex {
2431 name: "myapex",
2432 key: "myapex.key",
2433 product_specific: true,
2434 file_contexts: "product_specific_file_contexts",
2435 }
2436
2437 apex_key {
2438 name: "myapex.key",
2439 public_key: "testkey.avbpubkey",
2440 private_key: "testkey.pem",
2441 }
2442 `)
2443
2444 ctx, _ = testApex(t, `
2445 apex {
2446 name: "myapex",
2447 key: "myapex.key",
2448 product_specific: true,
2449 file_contexts: "product_specific_file_contexts",
2450 }
2451
2452 apex_key {
2453 name: "myapex.key",
2454 public_key: "testkey.avbpubkey",
2455 private_key: "testkey.pem",
2456 }
2457 `, withFiles(map[string][]byte{
2458 "product_specific_file_contexts": nil,
2459 }))
2460 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2461 apexRule = module.Rule("apexRule")
2462 actual = apexRule.Args["file_contexts"]
2463 expected = "product_specific_file_contexts"
2464 if actual != expected {
2465 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2466 }
2467
2468 ctx, _ = testApex(t, `
2469 apex {
2470 name: "myapex",
2471 key: "myapex.key",
2472 product_specific: true,
2473 file_contexts: ":my-file-contexts",
2474 }
2475
2476 apex_key {
2477 name: "myapex.key",
2478 public_key: "testkey.avbpubkey",
2479 private_key: "testkey.pem",
2480 }
2481
2482 filegroup {
2483 name: "my-file-contexts",
2484 srcs: ["product_specific_file_contexts"],
2485 }
2486 `, withFiles(map[string][]byte{
2487 "product_specific_file_contexts": nil,
2488 }))
2489 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2490 apexRule = module.Rule("apexRule")
2491 actual = apexRule.Args["file_contexts"]
2492 expected = "product_specific_file_contexts"
2493 if actual != expected {
2494 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2495 }
2496}
2497
Jiyong Park67882562019-03-21 01:11:21 +09002498func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002499 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002500 apex_key {
2501 name: "myapex.key",
2502 public_key: ":my.avbpubkey",
2503 private_key: ":my.pem",
2504 product_specific: true,
2505 }
2506
2507 filegroup {
2508 name: "my.avbpubkey",
2509 srcs: ["testkey2.avbpubkey"],
2510 }
2511
2512 filegroup {
2513 name: "my.pem",
2514 srcs: ["testkey2.pem"],
2515 }
2516 `)
2517
2518 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2519 expected_pubkey := "testkey2.avbpubkey"
2520 actual_pubkey := apex_key.public_key_file.String()
2521 if actual_pubkey != expected_pubkey {
2522 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2523 }
2524 expected_privkey := "testkey2.pem"
2525 actual_privkey := apex_key.private_key_file.String()
2526 if actual_privkey != expected_privkey {
2527 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2528 }
2529}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002530
2531func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002532 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002533 prebuilt_apex {
2534 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002535 arch: {
2536 arm64: {
2537 src: "myapex-arm64.apex",
2538 },
2539 arm: {
2540 src: "myapex-arm.apex",
2541 },
2542 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002543 }
2544 `)
2545
2546 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2547
Jiyong Parkc95714e2019-03-29 14:23:10 +09002548 expectedInput := "myapex-arm64.apex"
2549 if prebuilt.inputApex.String() != expectedInput {
2550 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2551 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002552}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002553
2554func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002555 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002556 prebuilt_apex {
2557 name: "myapex",
2558 src: "myapex-arm.apex",
2559 filename: "notmyapex.apex",
2560 }
2561 `)
2562
2563 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2564
2565 expected := "notmyapex.apex"
2566 if p.installFilename != expected {
2567 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2568 }
2569}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002570
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002571func TestPrebuiltOverrides(t *testing.T) {
2572 ctx, config := testApex(t, `
2573 prebuilt_apex {
2574 name: "myapex.prebuilt",
2575 src: "myapex-arm.apex",
2576 overrides: [
2577 "myapex",
2578 ],
2579 }
2580 `)
2581
2582 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2583
2584 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002585 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002586 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002587 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002588 }
2589}
2590
Roland Levillain630846d2019-06-26 12:48:34 +01002591func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002592 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002593 apex_test {
2594 name: "myapex",
2595 key: "myapex.key",
2596 tests: [
2597 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002598 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002599 ],
2600 }
2601
2602 apex_key {
2603 name: "myapex.key",
2604 public_key: "testkey.avbpubkey",
2605 private_key: "testkey.pem",
2606 }
2607
2608 cc_test {
2609 name: "mytest",
2610 gtest: false,
2611 srcs: ["mytest.cpp"],
2612 relative_install_path: "test",
2613 system_shared_libs: [],
2614 static_executable: true,
2615 stl: "none",
2616 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002617
2618 cc_test {
2619 name: "mytests",
2620 gtest: false,
2621 srcs: [
2622 "mytest1.cpp",
2623 "mytest2.cpp",
2624 "mytest3.cpp",
2625 ],
2626 test_per_src: true,
2627 relative_install_path: "test",
2628 system_shared_libs: [],
2629 static_executable: true,
2630 stl: "none",
2631 }
Roland Levillain630846d2019-06-26 12:48:34 +01002632 `)
2633
Sundong Ahnabb64432019-10-22 13:58:29 +09002634 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002635 copyCmds := apexRule.Args["copy_commands"]
2636
2637 // Ensure that test dep is copied into apex.
2638 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002639
2640 // Ensure that test deps built with `test_per_src` are copied into apex.
2641 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2642 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2643 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002644
2645 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002646 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002647 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2648 name := apexBundle.BaseModuleName()
2649 prefix := "TARGET_"
2650 var builder strings.Builder
2651 data.Custom(&builder, name, prefix, "", data)
2652 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002653 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2654 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2655 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2656 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002657 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002658 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002659 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002660}
2661
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002662func TestInstallExtraFlattenedApexes(t *testing.T) {
2663 ctx, config := testApex(t, `
2664 apex {
2665 name: "myapex",
2666 key: "myapex.key",
2667 }
2668 apex_key {
2669 name: "myapex.key",
2670 public_key: "testkey.avbpubkey",
2671 private_key: "testkey.pem",
2672 }
2673 `, func(fs map[string][]byte, config android.Config) {
2674 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2675 })
2676 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002677 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002678 mk := android.AndroidMkDataForTest(t, config, "", ab)
2679 var builder strings.Builder
2680 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2681 androidMk := builder.String()
2682 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2683}
2684
Jooyung Han5c998b92019-06-27 11:30:33 +09002685func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002686 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002687 apex {
2688 name: "myapex",
2689 key: "myapex.key",
2690 native_shared_libs: ["mylib"],
2691 uses: ["commonapex"],
2692 }
2693
2694 apex {
2695 name: "commonapex",
2696 key: "myapex.key",
2697 native_shared_libs: ["libcommon"],
2698 provide_cpp_shared_libs: true,
2699 }
2700
2701 apex_key {
2702 name: "myapex.key",
2703 public_key: "testkey.avbpubkey",
2704 private_key: "testkey.pem",
2705 }
2706
2707 cc_library {
2708 name: "mylib",
2709 srcs: ["mylib.cpp"],
2710 shared_libs: ["libcommon"],
2711 system_shared_libs: [],
2712 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002713 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002714 }
2715
2716 cc_library {
2717 name: "libcommon",
2718 srcs: ["mylib_common.cpp"],
2719 system_shared_libs: [],
2720 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002721 // TODO: remove //apex_available:platform
2722 apex_available: [
2723 "//apex_available:platform",
2724 "commonapex",
2725 "myapex",
2726 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002727 }
2728 `)
2729
Sundong Ahnabb64432019-10-22 13:58:29 +09002730 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002731 apexRule1 := module1.Rule("apexRule")
2732 copyCmds1 := apexRule1.Args["copy_commands"]
2733
Sundong Ahnabb64432019-10-22 13:58:29 +09002734 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002735 apexRule2 := module2.Rule("apexRule")
2736 copyCmds2 := apexRule2.Args["copy_commands"]
2737
Colin Cross7113d202019-11-20 16:39:12 -08002738 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2739 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002740 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2741 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2742 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2743}
2744
2745func TestApexUsesFailsIfNotProvided(t *testing.T) {
2746 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2747 apex {
2748 name: "myapex",
2749 key: "myapex.key",
2750 uses: ["commonapex"],
2751 }
2752
2753 apex {
2754 name: "commonapex",
2755 key: "myapex.key",
2756 }
2757
2758 apex_key {
2759 name: "myapex.key",
2760 public_key: "testkey.avbpubkey",
2761 private_key: "testkey.pem",
2762 }
2763 `)
2764 testApexError(t, `uses: "commonapex" is not a provider`, `
2765 apex {
2766 name: "myapex",
2767 key: "myapex.key",
2768 uses: ["commonapex"],
2769 }
2770
2771 cc_library {
2772 name: "commonapex",
2773 system_shared_libs: [],
2774 stl: "none",
2775 }
2776
2777 apex_key {
2778 name: "myapex.key",
2779 public_key: "testkey.avbpubkey",
2780 private_key: "testkey.pem",
2781 }
2782 `)
2783}
2784
2785func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2786 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2787 apex {
2788 name: "myapex",
2789 key: "myapex.key",
2790 use_vendor: true,
2791 uses: ["commonapex"],
2792 }
2793
2794 apex {
2795 name: "commonapex",
2796 key: "myapex.key",
2797 provide_cpp_shared_libs: true,
2798 }
2799
2800 apex_key {
2801 name: "myapex.key",
2802 public_key: "testkey.avbpubkey",
2803 private_key: "testkey.pem",
2804 }
Jooyung Handc782442019-11-01 03:14:38 +09002805 `, func(fs map[string][]byte, config android.Config) {
2806 setUseVendorWhitelistForTest(config, []string{"myapex"})
2807 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002808}
2809
Jooyung Hand48f3c32019-08-23 11:18:57 +09002810func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2811 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2812 apex {
2813 name: "myapex",
2814 key: "myapex.key",
2815 native_shared_libs: ["libfoo"],
2816 }
2817
2818 apex_key {
2819 name: "myapex.key",
2820 public_key: "testkey.avbpubkey",
2821 private_key: "testkey.pem",
2822 }
2823
2824 cc_library {
2825 name: "libfoo",
2826 stl: "none",
2827 system_shared_libs: [],
2828 enabled: false,
2829 }
2830 `)
2831 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2832 apex {
2833 name: "myapex",
2834 key: "myapex.key",
2835 java_libs: ["myjar"],
2836 }
2837
2838 apex_key {
2839 name: "myapex.key",
2840 public_key: "testkey.avbpubkey",
2841 private_key: "testkey.pem",
2842 }
2843
2844 java_library {
2845 name: "myjar",
2846 srcs: ["foo/bar/MyClass.java"],
2847 sdk_version: "none",
2848 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09002849 enabled: false,
2850 }
2851 `)
2852}
2853
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002854func TestApexWithApps(t *testing.T) {
2855 ctx, _ := testApex(t, `
2856 apex {
2857 name: "myapex",
2858 key: "myapex.key",
2859 apps: [
2860 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002861 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002862 ],
2863 }
2864
2865 apex_key {
2866 name: "myapex.key",
2867 public_key: "testkey.avbpubkey",
2868 private_key: "testkey.pem",
2869 }
2870
2871 android_app {
2872 name: "AppFoo",
2873 srcs: ["foo/bar/MyClass.java"],
2874 sdk_version: "none",
2875 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002876 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002877 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002878 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002879
2880 android_app {
2881 name: "AppFooPriv",
2882 srcs: ["foo/bar/MyClass.java"],
2883 sdk_version: "none",
2884 system_modules: "none",
2885 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002886 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09002887 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002888
2889 cc_library_shared {
2890 name: "libjni",
2891 srcs: ["mylib.cpp"],
2892 stl: "none",
2893 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09002894 apex_available: [ "myapex" ],
Jiyong Park8be103b2019-11-08 15:53:48 +09002895 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002896 `)
2897
Sundong Ahnabb64432019-10-22 13:58:29 +09002898 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002899 apexRule := module.Rule("apexRule")
2900 copyCmds := apexRule.Args["copy_commands"]
2901
2902 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002903 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002904
2905 // JNI libraries are embedded inside APK
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00002906 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni lib")
Colin Cross7113d202019-11-20 16:39:12 -08002907 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002908 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2909 // ... uncompressed
2910 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2911 t.Errorf("jni lib is not uncompressed for AppFoo")
2912 }
2913 // ... and not directly inside the APEX
2914 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002915}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002916
Dario Frenicde2a032019-10-27 00:29:22 +01002917func TestApexWithAppImports(t *testing.T) {
2918 ctx, _ := testApex(t, `
2919 apex {
2920 name: "myapex",
2921 key: "myapex.key",
2922 apps: [
2923 "AppFooPrebuilt",
2924 "AppFooPrivPrebuilt",
2925 ],
2926 }
2927
2928 apex_key {
2929 name: "myapex.key",
2930 public_key: "testkey.avbpubkey",
2931 private_key: "testkey.pem",
2932 }
2933
2934 android_app_import {
2935 name: "AppFooPrebuilt",
2936 apk: "PrebuiltAppFoo.apk",
2937 presigned: true,
2938 dex_preopt: {
2939 enabled: false,
2940 },
2941 }
2942
2943 android_app_import {
2944 name: "AppFooPrivPrebuilt",
2945 apk: "PrebuiltAppFooPriv.apk",
2946 privileged: true,
2947 presigned: true,
2948 dex_preopt: {
2949 enabled: false,
2950 },
2951 }
2952 `)
2953
Sundong Ahnabb64432019-10-22 13:58:29 +09002954 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002955 apexRule := module.Rule("apexRule")
2956 copyCmds := apexRule.Args["copy_commands"]
2957
2958 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2959 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002960}
2961
Dario Freni6f3937c2019-12-20 22:58:03 +00002962func TestApexWithTestHelperApp(t *testing.T) {
2963 ctx, _ := testApex(t, `
2964 apex {
2965 name: "myapex",
2966 key: "myapex.key",
2967 apps: [
2968 "TesterHelpAppFoo",
2969 ],
2970 }
2971
2972 apex_key {
2973 name: "myapex.key",
2974 public_key: "testkey.avbpubkey",
2975 private_key: "testkey.pem",
2976 }
2977
2978 android_test_helper_app {
2979 name: "TesterHelpAppFoo",
2980 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002981 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00002982 }
2983
2984 `)
2985
2986 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2987 apexRule := module.Rule("apexRule")
2988 copyCmds := apexRule.Args["copy_commands"]
2989
2990 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
2991}
2992
Jooyung Han18020ea2019-11-13 10:50:48 +09002993func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2994 // libfoo's apex_available comes from cc_defaults
2995 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2996 apex {
2997 name: "myapex",
2998 key: "myapex.key",
2999 native_shared_libs: ["libfoo"],
3000 }
3001
3002 apex_key {
3003 name: "myapex.key",
3004 public_key: "testkey.avbpubkey",
3005 private_key: "testkey.pem",
3006 }
3007
3008 apex {
3009 name: "otherapex",
3010 key: "myapex.key",
3011 native_shared_libs: ["libfoo"],
3012 }
3013
3014 cc_defaults {
3015 name: "libfoo-defaults",
3016 apex_available: ["otherapex"],
3017 }
3018
3019 cc_library {
3020 name: "libfoo",
3021 defaults: ["libfoo-defaults"],
3022 stl: "none",
3023 system_shared_libs: [],
3024 }`)
3025}
3026
Jiyong Park127b40b2019-09-30 16:04:35 +09003027func TestApexAvailable(t *testing.T) {
3028 // libfoo is not available to myapex, but only to otherapex
3029 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3030 apex {
3031 name: "myapex",
3032 key: "myapex.key",
3033 native_shared_libs: ["libfoo"],
3034 }
3035
3036 apex_key {
3037 name: "myapex.key",
3038 public_key: "testkey.avbpubkey",
3039 private_key: "testkey.pem",
3040 }
3041
3042 apex {
3043 name: "otherapex",
3044 key: "otherapex.key",
3045 native_shared_libs: ["libfoo"],
3046 }
3047
3048 apex_key {
3049 name: "otherapex.key",
3050 public_key: "testkey.avbpubkey",
3051 private_key: "testkey.pem",
3052 }
3053
3054 cc_library {
3055 name: "libfoo",
3056 stl: "none",
3057 system_shared_libs: [],
3058 apex_available: ["otherapex"],
3059 }`)
3060
3061 // libbar is an indirect dep
3062 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3063 apex {
3064 name: "myapex",
3065 key: "myapex.key",
3066 native_shared_libs: ["libfoo"],
3067 }
3068
3069 apex_key {
3070 name: "myapex.key",
3071 public_key: "testkey.avbpubkey",
3072 private_key: "testkey.pem",
3073 }
3074
3075 apex {
3076 name: "otherapex",
3077 key: "otherapex.key",
3078 native_shared_libs: ["libfoo"],
3079 }
3080
3081 apex_key {
3082 name: "otherapex.key",
3083 public_key: "testkey.avbpubkey",
3084 private_key: "testkey.pem",
3085 }
3086
3087 cc_library {
3088 name: "libfoo",
3089 stl: "none",
3090 shared_libs: ["libbar"],
3091 system_shared_libs: [],
3092 apex_available: ["myapex", "otherapex"],
3093 }
3094
3095 cc_library {
3096 name: "libbar",
3097 stl: "none",
3098 system_shared_libs: [],
3099 apex_available: ["otherapex"],
3100 }`)
3101
3102 testApexError(t, "\"otherapex\" is not a valid module name", `
3103 apex {
3104 name: "myapex",
3105 key: "myapex.key",
3106 native_shared_libs: ["libfoo"],
3107 }
3108
3109 apex_key {
3110 name: "myapex.key",
3111 public_key: "testkey.avbpubkey",
3112 private_key: "testkey.pem",
3113 }
3114
3115 cc_library {
3116 name: "libfoo",
3117 stl: "none",
3118 system_shared_libs: [],
3119 apex_available: ["otherapex"],
3120 }`)
3121
3122 ctx, _ := testApex(t, `
3123 apex {
3124 name: "myapex",
3125 key: "myapex.key",
3126 native_shared_libs: ["libfoo", "libbar"],
3127 }
3128
3129 apex_key {
3130 name: "myapex.key",
3131 public_key: "testkey.avbpubkey",
3132 private_key: "testkey.pem",
3133 }
3134
3135 cc_library {
3136 name: "libfoo",
3137 stl: "none",
3138 system_shared_libs: [],
3139 apex_available: ["myapex"],
3140 }
3141
3142 cc_library {
3143 name: "libbar",
3144 stl: "none",
3145 system_shared_libs: [],
3146 apex_available: ["//apex_available:anyapex"],
3147 }`)
3148
3149 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003150 // TODO(jiyong) the checks for the platform variant are removed because we now create
3151 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3152 // the platform variants are not used from other platform modules. When that is done,
3153 // these checks will be replaced by expecting a specific error message that will be
3154 // emitted when the platform variant is used.
3155 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3156 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3157 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3158 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003159
3160 ctx, _ = testApex(t, `
3161 apex {
3162 name: "myapex",
3163 key: "myapex.key",
3164 }
3165
3166 apex_key {
3167 name: "myapex.key",
3168 public_key: "testkey.avbpubkey",
3169 private_key: "testkey.pem",
3170 }
3171
3172 cc_library {
3173 name: "libfoo",
3174 stl: "none",
3175 system_shared_libs: [],
3176 apex_available: ["//apex_available:platform"],
3177 }`)
3178
3179 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003180 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3181 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003182
3183 ctx, _ = testApex(t, `
3184 apex {
3185 name: "myapex",
3186 key: "myapex.key",
3187 native_shared_libs: ["libfoo"],
3188 }
3189
3190 apex_key {
3191 name: "myapex.key",
3192 public_key: "testkey.avbpubkey",
3193 private_key: "testkey.pem",
3194 }
3195
3196 cc_library {
3197 name: "libfoo",
3198 stl: "none",
3199 system_shared_libs: [],
3200 apex_available: ["myapex"],
3201 static: {
3202 apex_available: ["//apex_available:platform"],
3203 },
3204 }`)
3205
3206 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003207 // TODO(jiyong) the checks for the platform variant are removed because we now create
3208 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3209 // the platform variants are not used from other platform modules. When that is done,
3210 // these checks will be replaced by expecting a specific error message that will be
3211 // emitted when the platform variant is used.
3212 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3213 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3214 // // but the static variant is available to both myapex and the platform
3215 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3216 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003217}
3218
Jiyong Park5d790c32019-11-15 18:40:32 +09003219func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003220 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003221 apex {
3222 name: "myapex",
3223 key: "myapex.key",
3224 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003225 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003226 }
3227
3228 override_apex {
3229 name: "override_myapex",
3230 base: "myapex",
3231 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003232 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003233 }
3234
3235 apex_key {
3236 name: "myapex.key",
3237 public_key: "testkey.avbpubkey",
3238 private_key: "testkey.pem",
3239 }
3240
3241 android_app {
3242 name: "app",
3243 srcs: ["foo/bar/MyClass.java"],
3244 package_name: "foo",
3245 sdk_version: "none",
3246 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003247 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003248 }
3249
3250 override_android_app {
3251 name: "override_app",
3252 base: "app",
3253 package_name: "bar",
3254 }
3255 `)
3256
Jiyong Park317645e2019-12-05 13:20:58 +09003257 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3258 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3259 if originalVariant.GetOverriddenBy() != "" {
3260 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3261 }
3262 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3263 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3264 }
3265
Jiyong Park5d790c32019-11-15 18:40:32 +09003266 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3267 apexRule := module.Rule("apexRule")
3268 copyCmds := apexRule.Args["copy_commands"]
3269
3270 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3271 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003272
3273 apexBundle := module.Module().(*apexBundle)
3274 name := apexBundle.Name()
3275 if name != "override_myapex" {
3276 t.Errorf("name should be \"override_myapex\", but was %q", name)
3277 }
3278
3279 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3280 var builder strings.Builder
3281 data.Custom(&builder, name, "TARGET_", "", data)
3282 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003283 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003284 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3285 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003286 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003287 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003288 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003289 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3290 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003291}
3292
Jooyung Han214bf372019-11-12 13:03:50 +09003293func TestLegacyAndroid10Support(t *testing.T) {
3294 ctx, _ := testApex(t, `
3295 apex {
3296 name: "myapex",
3297 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003298 native_shared_libs: ["mylib"],
Jooyung Han214bf372019-11-12 13:03:50 +09003299 legacy_android10_support: true,
3300 }
3301
3302 apex_key {
3303 name: "myapex.key",
3304 public_key: "testkey.avbpubkey",
3305 private_key: "testkey.pem",
3306 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003307
3308 cc_library {
3309 name: "mylib",
3310 srcs: ["mylib.cpp"],
3311 stl: "libc++",
3312 system_shared_libs: [],
3313 apex_available: [ "myapex" ],
3314 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003315 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003316
3317 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3318 args := module.Rule("apexRule").Args
3319 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003320 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003321
3322 // The copies of the libraries in the apex should have one more dependency than
3323 // the ones outside the apex, namely the unwinder. Ideally we should check
3324 // the dependency names directly here but for some reason the names are blank in
3325 // this test.
3326 for _, lib := range []string{"libc++", "mylib"} {
3327 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3328 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3329 if len(apexImplicits) != len(nonApexImplicits)+1 {
3330 t.Errorf("%q missing unwinder dep", lib)
3331 }
3332 }
Jooyung Han214bf372019-11-12 13:03:50 +09003333}
3334
Jooyung Han58f26ab2019-12-18 15:34:32 +09003335func TestJavaSDKLibrary(t *testing.T) {
3336 ctx, _ := testApex(t, `
3337 apex {
3338 name: "myapex",
3339 key: "myapex.key",
3340 java_libs: ["foo"],
3341 }
3342
3343 apex_key {
3344 name: "myapex.key",
3345 public_key: "testkey.avbpubkey",
3346 private_key: "testkey.pem",
3347 }
3348
3349 java_sdk_library {
3350 name: "foo",
3351 srcs: ["a.java"],
3352 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003353 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003354 }
3355 `, withFiles(map[string][]byte{
3356 "api/current.txt": nil,
3357 "api/removed.txt": nil,
3358 "api/system-current.txt": nil,
3359 "api/system-removed.txt": nil,
3360 "api/test-current.txt": nil,
3361 "api/test-removed.txt": nil,
3362 }))
3363
3364 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003365 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003366 "javalib/foo.jar",
3367 "etc/permissions/foo.xml",
3368 })
3369 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003370 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3371 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003372}
3373
atrost6e126252020-01-27 17:01:16 +00003374func TestCompatConfig(t *testing.T) {
3375 ctx, _ := testApex(t, `
3376 apex {
3377 name: "myapex",
3378 key: "myapex.key",
3379 prebuilts: ["myjar-platform-compat-config"],
3380 java_libs: ["myjar"],
3381 }
3382
3383 apex_key {
3384 name: "myapex.key",
3385 public_key: "testkey.avbpubkey",
3386 private_key: "testkey.pem",
3387 }
3388
3389 platform_compat_config {
3390 name: "myjar-platform-compat-config",
3391 src: ":myjar",
3392 }
3393
3394 java_library {
3395 name: "myjar",
3396 srcs: ["foo/bar/MyClass.java"],
3397 sdk_version: "none",
3398 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003399 apex_available: [ "myapex" ],
3400 }
3401 `)
3402 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3403 "etc/compatconfig/myjar-platform-compat-config.xml",
3404 "javalib/myjar.jar",
3405 })
3406}
3407
Jiyong Park479321d2019-12-16 11:47:12 +09003408func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3409 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3410 apex {
3411 name: "myapex",
3412 key: "myapex.key",
3413 java_libs: ["myjar"],
3414 }
3415
3416 apex_key {
3417 name: "myapex.key",
3418 public_key: "testkey.avbpubkey",
3419 private_key: "testkey.pem",
3420 }
3421
3422 java_library {
3423 name: "myjar",
3424 srcs: ["foo/bar/MyClass.java"],
3425 sdk_version: "none",
3426 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003427 compile_dex: false,
Jiyong Park479321d2019-12-16 11:47:12 +09003428 }
3429 `)
3430}
3431
Jiyong Park7afd1072019-12-30 16:56:33 +09003432func TestCarryRequiredModuleNames(t *testing.T) {
3433 ctx, config := testApex(t, `
3434 apex {
3435 name: "myapex",
3436 key: "myapex.key",
3437 native_shared_libs: ["mylib"],
3438 }
3439
3440 apex_key {
3441 name: "myapex.key",
3442 public_key: "testkey.avbpubkey",
3443 private_key: "testkey.pem",
3444 }
3445
3446 cc_library {
3447 name: "mylib",
3448 srcs: ["mylib.cpp"],
3449 system_shared_libs: [],
3450 stl: "none",
3451 required: ["a", "b"],
3452 host_required: ["c", "d"],
3453 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003454 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003455 }
3456 `)
3457
3458 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3459 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3460 name := apexBundle.BaseModuleName()
3461 prefix := "TARGET_"
3462 var builder strings.Builder
3463 data.Custom(&builder, name, prefix, "", data)
3464 androidMk := builder.String()
3465 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3466 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3467 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3468}
3469
Jiyong Park7cd10e32020-01-14 09:22:18 +09003470func TestSymlinksFromApexToSystem(t *testing.T) {
3471 bp := `
3472 apex {
3473 name: "myapex",
3474 key: "myapex.key",
3475 native_shared_libs: ["mylib"],
3476 java_libs: ["myjar"],
3477 }
3478
Jiyong Park9d677202020-02-19 16:29:35 +09003479 apex {
3480 name: "myapex.updatable",
3481 key: "myapex.key",
3482 native_shared_libs: ["mylib"],
3483 java_libs: ["myjar"],
3484 updatable: true,
3485 }
3486
Jiyong Park7cd10e32020-01-14 09:22:18 +09003487 apex_key {
3488 name: "myapex.key",
3489 public_key: "testkey.avbpubkey",
3490 private_key: "testkey.pem",
3491 }
3492
3493 cc_library {
3494 name: "mylib",
3495 srcs: ["mylib.cpp"],
3496 shared_libs: ["myotherlib"],
3497 system_shared_libs: [],
3498 stl: "none",
3499 apex_available: [
3500 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003501 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003502 "//apex_available:platform",
3503 ],
3504 }
3505
3506 cc_library {
3507 name: "myotherlib",
3508 srcs: ["mylib.cpp"],
3509 system_shared_libs: [],
3510 stl: "none",
3511 apex_available: [
3512 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003513 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003514 "//apex_available:platform",
3515 ],
3516 }
3517
3518 java_library {
3519 name: "myjar",
3520 srcs: ["foo/bar/MyClass.java"],
3521 sdk_version: "none",
3522 system_modules: "none",
3523 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003524 apex_available: [
3525 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003526 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003527 "//apex_available:platform",
3528 ],
3529 }
3530
3531 java_library {
3532 name: "myotherjar",
3533 srcs: ["foo/bar/MyClass.java"],
3534 sdk_version: "none",
3535 system_modules: "none",
3536 apex_available: [
3537 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003538 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003539 "//apex_available:platform",
3540 ],
3541 }
3542 `
3543
3544 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3545 for _, f := range files {
3546 if f.path == file {
3547 if f.isLink {
3548 t.Errorf("%q is not a real file", file)
3549 }
3550 return
3551 }
3552 }
3553 t.Errorf("%q is not found", file)
3554 }
3555
3556 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3557 for _, f := range files {
3558 if f.path == file {
3559 if !f.isLink {
3560 t.Errorf("%q is not a symlink", file)
3561 }
3562 return
3563 }
3564 }
3565 t.Errorf("%q is not found", file)
3566 }
3567
Jiyong Park9d677202020-02-19 16:29:35 +09003568 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3569 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003570 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003571 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003572 ensureRealfileExists(t, files, "javalib/myjar.jar")
3573 ensureRealfileExists(t, files, "lib64/mylib.so")
3574 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3575
Jiyong Park9d677202020-02-19 16:29:35 +09003576 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3577 ensureRealfileExists(t, files, "javalib/myjar.jar")
3578 ensureRealfileExists(t, files, "lib64/mylib.so")
3579 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3580
3581 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003582 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003583 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003584 ensureRealfileExists(t, files, "javalib/myjar.jar")
3585 ensureRealfileExists(t, files, "lib64/mylib.so")
3586 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09003587
3588 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3589 ensureRealfileExists(t, files, "javalib/myjar.jar")
3590 ensureRealfileExists(t, files, "lib64/mylib.so")
3591 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09003592}
3593
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003594func TestMain(m *testing.M) {
3595 run := func() int {
3596 setUp()
3597 defer tearDown()
3598
3599 return m.Run()
3600 }
3601
3602 os.Exit(run())
3603}