blob: d1d8d64919d04cdf68476a699081f1308fa94783 [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) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001535 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001536 for _, expected := range files {
1537 if matched, _ := path.Match(expected, file.path); matched {
1538 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001539 mactchFound = true
1540 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001541 }
1542 }
Jooyung Hane6436d72020-02-27 13:31:56 +09001543 if !mactchFound {
1544 surplus = append(surplus, file.path)
1545 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001546 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001547
Jooyung Han31c470b2019-10-18 16:26:59 +09001548 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001549 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001550 t.Log("surplus files", surplus)
1551 failed = true
1552 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001553
1554 if len(files) > len(filesMatched) {
1555 var missing []string
1556 for _, expected := range files {
1557 if !filesMatched[expected] {
1558 missing = append(missing, expected)
1559 }
1560 }
1561 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001562 t.Log("missing files", missing)
1563 failed = true
1564 }
1565 if failed {
1566 t.Fail()
1567 }
1568}
1569
Jooyung Han344d5432019-08-23 11:17:39 +09001570func TestVndkApexCurrent(t *testing.T) {
1571 ctx, _ := testApex(t, `
1572 apex_vndk {
1573 name: "myapex",
1574 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001575 }
1576
1577 apex_key {
1578 name: "myapex.key",
1579 public_key: "testkey.avbpubkey",
1580 private_key: "testkey.pem",
1581 }
1582
1583 cc_library {
1584 name: "libvndk",
1585 srcs: ["mylib.cpp"],
1586 vendor_available: true,
1587 vndk: {
1588 enabled: true,
1589 },
1590 system_shared_libs: [],
1591 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001592 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001593 }
1594
1595 cc_library {
1596 name: "libvndksp",
1597 srcs: ["mylib.cpp"],
1598 vendor_available: true,
1599 vndk: {
1600 enabled: true,
1601 support_system_process: true,
1602 },
1603 system_shared_libs: [],
1604 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001605 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001606 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001607 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001608
Jooyung Hana57af4a2020-01-23 05:36:59 +00001609 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001610 "lib/libvndk.so",
1611 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001612 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09001613 "lib64/libvndk.so",
1614 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001615 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001616 "etc/llndk.libraries.VER.txt",
1617 "etc/vndkcore.libraries.VER.txt",
1618 "etc/vndksp.libraries.VER.txt",
1619 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001620 })
Jooyung Han344d5432019-08-23 11:17:39 +09001621}
1622
1623func TestVndkApexWithPrebuilt(t *testing.T) {
1624 ctx, _ := testApex(t, `
1625 apex_vndk {
1626 name: "myapex",
1627 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001628 }
1629
1630 apex_key {
1631 name: "myapex.key",
1632 public_key: "testkey.avbpubkey",
1633 private_key: "testkey.pem",
1634 }
1635
1636 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001637 name: "libvndk",
1638 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001639 vendor_available: true,
1640 vndk: {
1641 enabled: true,
1642 },
1643 system_shared_libs: [],
1644 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001645 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001646 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001647
1648 cc_prebuilt_library_shared {
1649 name: "libvndk.arm",
1650 srcs: ["libvndk.arm.so"],
1651 vendor_available: true,
1652 vndk: {
1653 enabled: true,
1654 },
1655 enabled: false,
1656 arch: {
1657 arm: {
1658 enabled: true,
1659 },
1660 },
1661 system_shared_libs: [],
1662 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001663 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001664 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001665 `+vndkLibrariesTxtFiles("current"),
1666 withFiles(map[string][]byte{
1667 "libvndk.so": nil,
1668 "libvndk.arm.so": nil,
1669 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001670
Jooyung Hana57af4a2020-01-23 05:36:59 +00001671 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001672 "lib/libvndk.so",
1673 "lib/libvndk.arm.so",
1674 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001675 "lib/libc++.so",
1676 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001677 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001678 })
Jooyung Han344d5432019-08-23 11:17:39 +09001679}
1680
Jooyung Han39edb6c2019-11-06 16:53:07 +09001681func vndkLibrariesTxtFiles(vers ...string) (result string) {
1682 for _, v := range vers {
1683 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001684 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001685 result += `
1686 vndk_libraries_txt {
1687 name: "` + txt + `.libraries.txt",
1688 }
1689 `
1690 }
1691 } else {
1692 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1693 result += `
1694 prebuilt_etc {
1695 name: "` + txt + `.libraries.` + v + `.txt",
1696 src: "dummy.txt",
1697 }
1698 `
1699 }
1700 }
1701 }
1702 return
1703}
1704
Jooyung Han344d5432019-08-23 11:17:39 +09001705func TestVndkApexVersion(t *testing.T) {
1706 ctx, _ := testApex(t, `
1707 apex_vndk {
1708 name: "myapex_v27",
1709 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001710 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001711 vndk_version: "27",
1712 }
1713
1714 apex_key {
1715 name: "myapex.key",
1716 public_key: "testkey.avbpubkey",
1717 private_key: "testkey.pem",
1718 }
1719
Jooyung Han31c470b2019-10-18 16:26:59 +09001720 vndk_prebuilt_shared {
1721 name: "libvndk27",
1722 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001723 vendor_available: true,
1724 vndk: {
1725 enabled: true,
1726 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001727 target_arch: "arm64",
1728 arch: {
1729 arm: {
1730 srcs: ["libvndk27_arm.so"],
1731 },
1732 arm64: {
1733 srcs: ["libvndk27_arm64.so"],
1734 },
1735 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001736 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001737 }
1738
1739 vndk_prebuilt_shared {
1740 name: "libvndk27",
1741 version: "27",
1742 vendor_available: true,
1743 vndk: {
1744 enabled: true,
1745 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001746 target_arch: "x86_64",
1747 arch: {
1748 x86: {
1749 srcs: ["libvndk27_x86.so"],
1750 },
1751 x86_64: {
1752 srcs: ["libvndk27_x86_64.so"],
1753 },
1754 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001755 }
1756 `+vndkLibrariesTxtFiles("27"),
1757 withFiles(map[string][]byte{
1758 "libvndk27_arm.so": nil,
1759 "libvndk27_arm64.so": nil,
1760 "libvndk27_x86.so": nil,
1761 "libvndk27_x86_64.so": nil,
1762 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001763
Jooyung Hana57af4a2020-01-23 05:36:59 +00001764 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001765 "lib/libvndk27_arm.so",
1766 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001767 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001768 })
Jooyung Han344d5432019-08-23 11:17:39 +09001769}
1770
1771func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1772 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1773 apex_vndk {
1774 name: "myapex_v27",
1775 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001776 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001777 vndk_version: "27",
1778 }
1779 apex_vndk {
1780 name: "myapex_v27_other",
1781 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001782 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001783 vndk_version: "27",
1784 }
1785
1786 apex_key {
1787 name: "myapex.key",
1788 public_key: "testkey.avbpubkey",
1789 private_key: "testkey.pem",
1790 }
1791
1792 cc_library {
1793 name: "libvndk",
1794 srcs: ["mylib.cpp"],
1795 vendor_available: true,
1796 vndk: {
1797 enabled: true,
1798 },
1799 system_shared_libs: [],
1800 stl: "none",
1801 }
1802
1803 vndk_prebuilt_shared {
1804 name: "libvndk",
1805 version: "27",
1806 vendor_available: true,
1807 vndk: {
1808 enabled: true,
1809 },
1810 srcs: ["libvndk.so"],
1811 }
1812 `, withFiles(map[string][]byte{
1813 "libvndk.so": nil,
1814 }))
1815}
1816
Jooyung Han90eee022019-10-01 20:02:42 +09001817func TestVndkApexNameRule(t *testing.T) {
1818 ctx, _ := testApex(t, `
1819 apex_vndk {
1820 name: "myapex",
1821 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001822 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001823 }
1824 apex_vndk {
1825 name: "myapex_v28",
1826 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001827 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001828 vndk_version: "28",
1829 }
1830 apex_key {
1831 name: "myapex.key",
1832 public_key: "testkey.avbpubkey",
1833 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001834 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001835
1836 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001837 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001838 actual := proptools.String(bundle.properties.Apex_name)
1839 if !reflect.DeepEqual(actual, expected) {
1840 t.Errorf("Got '%v', expected '%v'", actual, expected)
1841 }
1842 }
1843
1844 assertApexName("com.android.vndk.vVER", "myapex")
1845 assertApexName("com.android.vndk.v28", "myapex_v28")
1846}
1847
Jooyung Han344d5432019-08-23 11:17:39 +09001848func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1849 ctx, _ := testApex(t, `
1850 apex_vndk {
1851 name: "myapex",
1852 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001853 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001854 }
1855
1856 apex_key {
1857 name: "myapex.key",
1858 public_key: "testkey.avbpubkey",
1859 private_key: "testkey.pem",
1860 }
1861
1862 cc_library {
1863 name: "libvndk",
1864 srcs: ["mylib.cpp"],
1865 vendor_available: true,
1866 native_bridge_supported: true,
1867 host_supported: true,
1868 vndk: {
1869 enabled: true,
1870 },
1871 system_shared_libs: [],
1872 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001873 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001874 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001875 `+vndkLibrariesTxtFiles("current"),
1876 withTargets(map[android.OsType][]android.Target{
1877 android.Android: []android.Target{
1878 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1879 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1880 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1881 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1882 },
1883 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001884
Jooyung Hana57af4a2020-01-23 05:36:59 +00001885 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001886 "lib/libvndk.so",
1887 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001888 "lib/libc++.so",
1889 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001890 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001891 })
Jooyung Han344d5432019-08-23 11:17:39 +09001892}
1893
1894func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1895 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1896 apex_vndk {
1897 name: "myapex",
1898 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001899 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001900 native_bridge_supported: true,
1901 }
1902
1903 apex_key {
1904 name: "myapex.key",
1905 public_key: "testkey.avbpubkey",
1906 private_key: "testkey.pem",
1907 }
1908
1909 cc_library {
1910 name: "libvndk",
1911 srcs: ["mylib.cpp"],
1912 vendor_available: true,
1913 native_bridge_supported: true,
1914 host_supported: true,
1915 vndk: {
1916 enabled: true,
1917 },
1918 system_shared_libs: [],
1919 stl: "none",
1920 }
1921 `)
1922}
1923
Jooyung Han31c470b2019-10-18 16:26:59 +09001924func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001925 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001926 apex_vndk {
1927 name: "myapex_v27",
1928 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001929 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001930 vndk_version: "27",
1931 }
1932
1933 apex_key {
1934 name: "myapex.key",
1935 public_key: "testkey.avbpubkey",
1936 private_key: "testkey.pem",
1937 }
1938
1939 vndk_prebuilt_shared {
1940 name: "libvndk27",
1941 version: "27",
1942 target_arch: "arm",
1943 vendor_available: true,
1944 vndk: {
1945 enabled: true,
1946 },
1947 arch: {
1948 arm: {
1949 srcs: ["libvndk27.so"],
1950 }
1951 },
1952 }
1953
1954 vndk_prebuilt_shared {
1955 name: "libvndk27",
1956 version: "27",
1957 target_arch: "arm",
1958 binder32bit: true,
1959 vendor_available: true,
1960 vndk: {
1961 enabled: true,
1962 },
1963 arch: {
1964 arm: {
1965 srcs: ["libvndk27binder32.so"],
1966 }
1967 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001968 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001969 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001970 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001971 withFiles(map[string][]byte{
1972 "libvndk27.so": nil,
1973 "libvndk27binder32.so": nil,
1974 }),
1975 withBinder32bit,
1976 withTargets(map[android.OsType][]android.Target{
1977 android.Android: []android.Target{
1978 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1979 },
1980 }),
1981 )
1982
Jooyung Hana57af4a2020-01-23 05:36:59 +00001983 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001984 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001985 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001986 })
1987}
1988
Jooyung Hane1633032019-08-01 17:41:43 +09001989func TestDependenciesInApexManifest(t *testing.T) {
1990 ctx, _ := testApex(t, `
1991 apex {
1992 name: "myapex_nodep",
1993 key: "myapex.key",
1994 native_shared_libs: ["lib_nodep"],
1995 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001996 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001997 }
1998
1999 apex {
2000 name: "myapex_dep",
2001 key: "myapex.key",
2002 native_shared_libs: ["lib_dep"],
2003 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002004 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002005 }
2006
2007 apex {
2008 name: "myapex_provider",
2009 key: "myapex.key",
2010 native_shared_libs: ["libfoo"],
2011 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002012 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002013 }
2014
2015 apex {
2016 name: "myapex_selfcontained",
2017 key: "myapex.key",
2018 native_shared_libs: ["lib_dep", "libfoo"],
2019 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002020 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002021 }
2022
2023 apex_key {
2024 name: "myapex.key",
2025 public_key: "testkey.avbpubkey",
2026 private_key: "testkey.pem",
2027 }
2028
2029 cc_library {
2030 name: "lib_nodep",
2031 srcs: ["mylib.cpp"],
2032 system_shared_libs: [],
2033 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002034 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002035 }
2036
2037 cc_library {
2038 name: "lib_dep",
2039 srcs: ["mylib.cpp"],
2040 shared_libs: ["libfoo"],
2041 system_shared_libs: [],
2042 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002043 apex_available: [
2044 "myapex_dep",
2045 "myapex_provider",
2046 "myapex_selfcontained",
2047 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002048 }
2049
2050 cc_library {
2051 name: "libfoo",
2052 srcs: ["mytest.cpp"],
2053 stubs: {
2054 versions: ["1"],
2055 },
2056 system_shared_libs: [],
2057 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002058 apex_available: [
2059 "myapex_provider",
2060 "myapex_selfcontained",
2061 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002062 }
2063 `)
2064
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002065 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002066 var provideNativeLibs, requireNativeLibs []string
2067
Sundong Ahnabb64432019-10-22 13:58:29 +09002068 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002069 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2070 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002071 ensureListEmpty(t, provideNativeLibs)
2072 ensureListEmpty(t, requireNativeLibs)
2073
Sundong Ahnabb64432019-10-22 13:58:29 +09002074 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002075 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2076 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002077 ensureListEmpty(t, provideNativeLibs)
2078 ensureListContains(t, requireNativeLibs, "libfoo.so")
2079
Sundong Ahnabb64432019-10-22 13:58:29 +09002080 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002081 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2082 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002083 ensureListContains(t, provideNativeLibs, "libfoo.so")
2084 ensureListEmpty(t, requireNativeLibs)
2085
Sundong Ahnabb64432019-10-22 13:58:29 +09002086 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002087 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2088 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002089 ensureListContains(t, provideNativeLibs, "libfoo.so")
2090 ensureListEmpty(t, requireNativeLibs)
2091}
2092
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002093func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002094 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002095 apex {
2096 name: "myapex",
2097 key: "myapex.key",
2098 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002099 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002100 }
2101
2102 apex_key {
2103 name: "myapex.key",
2104 public_key: "testkey.avbpubkey",
2105 private_key: "testkey.pem",
2106 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002107
2108 cc_library {
2109 name: "mylib",
2110 srcs: ["mylib.cpp"],
2111 system_shared_libs: [],
2112 stl: "none",
2113 apex_available: [
2114 "//apex_available:platform",
2115 "myapex",
2116 ],
2117 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002118 `)
2119
Sundong Ahnabb64432019-10-22 13:58:29 +09002120 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002121 apexManifestRule := module.Rule("apexManifestRule")
2122 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2123 apexRule := module.Rule("apexRule")
2124 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002125
2126 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2127 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2128 name := apexBundle.BaseModuleName()
2129 prefix := "TARGET_"
2130 var builder strings.Builder
2131 data.Custom(&builder, name, prefix, "", data)
2132 androidMk := builder.String()
2133 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2134 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002135}
2136
Alex Light0851b882019-02-07 13:20:53 -08002137func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002138 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002139 apex {
2140 name: "myapex",
2141 key: "myapex.key",
2142 native_shared_libs: ["mylib_common"],
2143 }
2144
2145 apex_key {
2146 name: "myapex.key",
2147 public_key: "testkey.avbpubkey",
2148 private_key: "testkey.pem",
2149 }
2150
2151 cc_library {
2152 name: "mylib_common",
2153 srcs: ["mylib.cpp"],
2154 system_shared_libs: [],
2155 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002156 apex_available: [
2157 "//apex_available:platform",
2158 "myapex",
2159 ],
Alex Light0851b882019-02-07 13:20:53 -08002160 }
2161 `)
2162
Sundong Ahnabb64432019-10-22 13:58:29 +09002163 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002164 apexRule := module.Rule("apexRule")
2165 copyCmds := apexRule.Args["copy_commands"]
2166
2167 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2168 t.Log("Apex was a test apex!")
2169 t.Fail()
2170 }
2171 // Ensure that main rule creates an output
2172 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2173
2174 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002175 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002176
2177 // Ensure that both direct and indirect deps are copied into apex
2178 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2179
Colin Cross7113d202019-11-20 16:39:12 -08002180 // Ensure that the platform variant ends with _shared
2181 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002182
2183 if !android.InAnyApex("mylib_common") {
2184 t.Log("Found mylib_common not in any apex!")
2185 t.Fail()
2186 }
2187}
2188
2189func TestTestApex(t *testing.T) {
2190 if android.InAnyApex("mylib_common_test") {
2191 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!")
2192 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002193 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002194 apex_test {
2195 name: "myapex",
2196 key: "myapex.key",
2197 native_shared_libs: ["mylib_common_test"],
2198 }
2199
2200 apex_key {
2201 name: "myapex.key",
2202 public_key: "testkey.avbpubkey",
2203 private_key: "testkey.pem",
2204 }
2205
2206 cc_library {
2207 name: "mylib_common_test",
2208 srcs: ["mylib.cpp"],
2209 system_shared_libs: [],
2210 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002211 // TODO: remove //apex_available:platform
2212 apex_available: [
2213 "//apex_available:platform",
2214 "myapex",
2215 ],
Alex Light0851b882019-02-07 13:20:53 -08002216 }
2217 `)
2218
Sundong Ahnabb64432019-10-22 13:58:29 +09002219 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002220 apexRule := module.Rule("apexRule")
2221 copyCmds := apexRule.Args["copy_commands"]
2222
2223 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2224 t.Log("Apex was not a test apex!")
2225 t.Fail()
2226 }
2227 // Ensure that main rule creates an output
2228 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2229
2230 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002231 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002232
2233 // Ensure that both direct and indirect deps are copied into apex
2234 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2235
Colin Cross7113d202019-11-20 16:39:12 -08002236 // Ensure that the platform variant ends with _shared
2237 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002238}
2239
Alex Light9670d332019-01-29 18:07:33 -08002240func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002241 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002242 apex {
2243 name: "myapex",
2244 key: "myapex.key",
2245 multilib: {
2246 first: {
2247 native_shared_libs: ["mylib_common"],
2248 }
2249 },
2250 target: {
2251 android: {
2252 multilib: {
2253 first: {
2254 native_shared_libs: ["mylib"],
2255 }
2256 }
2257 },
2258 host: {
2259 multilib: {
2260 first: {
2261 native_shared_libs: ["mylib2"],
2262 }
2263 }
2264 }
2265 }
2266 }
2267
2268 apex_key {
2269 name: "myapex.key",
2270 public_key: "testkey.avbpubkey",
2271 private_key: "testkey.pem",
2272 }
2273
2274 cc_library {
2275 name: "mylib",
2276 srcs: ["mylib.cpp"],
2277 system_shared_libs: [],
2278 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002279 // TODO: remove //apex_available:platform
2280 apex_available: [
2281 "//apex_available:platform",
2282 "myapex",
2283 ],
Alex Light9670d332019-01-29 18:07:33 -08002284 }
2285
2286 cc_library {
2287 name: "mylib_common",
2288 srcs: ["mylib.cpp"],
2289 system_shared_libs: [],
2290 stl: "none",
2291 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002292 // TODO: remove //apex_available:platform
2293 apex_available: [
2294 "//apex_available:platform",
2295 "myapex",
2296 ],
Alex Light9670d332019-01-29 18:07:33 -08002297 }
2298
2299 cc_library {
2300 name: "mylib2",
2301 srcs: ["mylib.cpp"],
2302 system_shared_libs: [],
2303 stl: "none",
2304 compile_multilib: "first",
2305 }
2306 `)
2307
Sundong Ahnabb64432019-10-22 13:58:29 +09002308 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002309 copyCmds := apexRule.Args["copy_commands"]
2310
2311 // Ensure that main rule creates an output
2312 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2313
2314 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002315 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2316 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2317 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002318
2319 // Ensure that both direct and indirect deps are copied into apex
2320 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2321 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2322 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2323
Colin Cross7113d202019-11-20 16:39:12 -08002324 // Ensure that the platform variant ends with _shared
2325 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2326 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2327 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002328}
Jiyong Park04480cf2019-02-06 00:16:29 +09002329
2330func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002331 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002332 apex {
2333 name: "myapex",
2334 key: "myapex.key",
2335 binaries: ["myscript"],
2336 }
2337
2338 apex_key {
2339 name: "myapex.key",
2340 public_key: "testkey.avbpubkey",
2341 private_key: "testkey.pem",
2342 }
2343
2344 sh_binary {
2345 name: "myscript",
2346 src: "mylib.cpp",
2347 filename: "myscript.sh",
2348 sub_dir: "script",
2349 }
2350 `)
2351
Sundong Ahnabb64432019-10-22 13:58:29 +09002352 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002353 copyCmds := apexRule.Args["copy_commands"]
2354
2355 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2356}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002357
Jooyung Han91df2082019-11-20 01:49:42 +09002358func TestApexInVariousPartition(t *testing.T) {
2359 testcases := []struct {
2360 propName, parition, flattenedPartition string
2361 }{
2362 {"", "system", "system_ext"},
2363 {"product_specific: true", "product", "product"},
2364 {"soc_specific: true", "vendor", "vendor"},
2365 {"proprietary: true", "vendor", "vendor"},
2366 {"vendor: true", "vendor", "vendor"},
2367 {"system_ext_specific: true", "system_ext", "system_ext"},
2368 }
2369 for _, tc := range testcases {
2370 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2371 ctx, _ := testApex(t, `
2372 apex {
2373 name: "myapex",
2374 key: "myapex.key",
2375 `+tc.propName+`
2376 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002377
Jooyung Han91df2082019-11-20 01:49:42 +09002378 apex_key {
2379 name: "myapex.key",
2380 public_key: "testkey.avbpubkey",
2381 private_key: "testkey.pem",
2382 }
2383 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002384
Jooyung Han91df2082019-11-20 01:49:42 +09002385 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2386 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2387 actual := apex.installDir.String()
2388 if actual != expected {
2389 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2390 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002391
Jooyung Han91df2082019-11-20 01:49:42 +09002392 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2393 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2394 actual = flattened.installDir.String()
2395 if actual != expected {
2396 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2397 }
2398 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002399 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002400}
Jiyong Park67882562019-03-21 01:11:21 +09002401
Jooyung Han54aca7b2019-11-20 02:26:02 +09002402func TestFileContexts(t *testing.T) {
2403 ctx, _ := testApex(t, `
2404 apex {
2405 name: "myapex",
2406 key: "myapex.key",
2407 }
2408
2409 apex_key {
2410 name: "myapex.key",
2411 public_key: "testkey.avbpubkey",
2412 private_key: "testkey.pem",
2413 }
2414 `)
2415 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2416 apexRule := module.Rule("apexRule")
2417 actual := apexRule.Args["file_contexts"]
2418 expected := "system/sepolicy/apex/myapex-file_contexts"
2419 if actual != expected {
2420 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2421 }
2422
2423 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2424 apex {
2425 name: "myapex",
2426 key: "myapex.key",
2427 file_contexts: "my_own_file_contexts",
2428 }
2429
2430 apex_key {
2431 name: "myapex.key",
2432 public_key: "testkey.avbpubkey",
2433 private_key: "testkey.pem",
2434 }
2435 `, withFiles(map[string][]byte{
2436 "my_own_file_contexts": nil,
2437 }))
2438
2439 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2440 apex {
2441 name: "myapex",
2442 key: "myapex.key",
2443 product_specific: true,
2444 file_contexts: "product_specific_file_contexts",
2445 }
2446
2447 apex_key {
2448 name: "myapex.key",
2449 public_key: "testkey.avbpubkey",
2450 private_key: "testkey.pem",
2451 }
2452 `)
2453
2454 ctx, _ = testApex(t, `
2455 apex {
2456 name: "myapex",
2457 key: "myapex.key",
2458 product_specific: true,
2459 file_contexts: "product_specific_file_contexts",
2460 }
2461
2462 apex_key {
2463 name: "myapex.key",
2464 public_key: "testkey.avbpubkey",
2465 private_key: "testkey.pem",
2466 }
2467 `, withFiles(map[string][]byte{
2468 "product_specific_file_contexts": nil,
2469 }))
2470 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2471 apexRule = module.Rule("apexRule")
2472 actual = apexRule.Args["file_contexts"]
2473 expected = "product_specific_file_contexts"
2474 if actual != expected {
2475 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2476 }
2477
2478 ctx, _ = testApex(t, `
2479 apex {
2480 name: "myapex",
2481 key: "myapex.key",
2482 product_specific: true,
2483 file_contexts: ":my-file-contexts",
2484 }
2485
2486 apex_key {
2487 name: "myapex.key",
2488 public_key: "testkey.avbpubkey",
2489 private_key: "testkey.pem",
2490 }
2491
2492 filegroup {
2493 name: "my-file-contexts",
2494 srcs: ["product_specific_file_contexts"],
2495 }
2496 `, withFiles(map[string][]byte{
2497 "product_specific_file_contexts": nil,
2498 }))
2499 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2500 apexRule = module.Rule("apexRule")
2501 actual = apexRule.Args["file_contexts"]
2502 expected = "product_specific_file_contexts"
2503 if actual != expected {
2504 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2505 }
2506}
2507
Jiyong Park67882562019-03-21 01:11:21 +09002508func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002509 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002510 apex_key {
2511 name: "myapex.key",
2512 public_key: ":my.avbpubkey",
2513 private_key: ":my.pem",
2514 product_specific: true,
2515 }
2516
2517 filegroup {
2518 name: "my.avbpubkey",
2519 srcs: ["testkey2.avbpubkey"],
2520 }
2521
2522 filegroup {
2523 name: "my.pem",
2524 srcs: ["testkey2.pem"],
2525 }
2526 `)
2527
2528 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2529 expected_pubkey := "testkey2.avbpubkey"
2530 actual_pubkey := apex_key.public_key_file.String()
2531 if actual_pubkey != expected_pubkey {
2532 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2533 }
2534 expected_privkey := "testkey2.pem"
2535 actual_privkey := apex_key.private_key_file.String()
2536 if actual_privkey != expected_privkey {
2537 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2538 }
2539}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002540
2541func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002542 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002543 prebuilt_apex {
2544 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002545 arch: {
2546 arm64: {
2547 src: "myapex-arm64.apex",
2548 },
2549 arm: {
2550 src: "myapex-arm.apex",
2551 },
2552 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002553 }
2554 `)
2555
2556 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2557
Jiyong Parkc95714e2019-03-29 14:23:10 +09002558 expectedInput := "myapex-arm64.apex"
2559 if prebuilt.inputApex.String() != expectedInput {
2560 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2561 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002562}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002563
2564func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002565 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002566 prebuilt_apex {
2567 name: "myapex",
2568 src: "myapex-arm.apex",
2569 filename: "notmyapex.apex",
2570 }
2571 `)
2572
2573 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2574
2575 expected := "notmyapex.apex"
2576 if p.installFilename != expected {
2577 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2578 }
2579}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002580
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002581func TestPrebuiltOverrides(t *testing.T) {
2582 ctx, config := testApex(t, `
2583 prebuilt_apex {
2584 name: "myapex.prebuilt",
2585 src: "myapex-arm.apex",
2586 overrides: [
2587 "myapex",
2588 ],
2589 }
2590 `)
2591
2592 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2593
2594 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002595 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002596 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002597 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002598 }
2599}
2600
Roland Levillain630846d2019-06-26 12:48:34 +01002601func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002602 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002603 apex_test {
2604 name: "myapex",
2605 key: "myapex.key",
2606 tests: [
2607 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002608 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002609 ],
2610 }
2611
2612 apex_key {
2613 name: "myapex.key",
2614 public_key: "testkey.avbpubkey",
2615 private_key: "testkey.pem",
2616 }
2617
2618 cc_test {
2619 name: "mytest",
2620 gtest: false,
2621 srcs: ["mytest.cpp"],
2622 relative_install_path: "test",
2623 system_shared_libs: [],
2624 static_executable: true,
2625 stl: "none",
2626 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002627
2628 cc_test {
2629 name: "mytests",
2630 gtest: false,
2631 srcs: [
2632 "mytest1.cpp",
2633 "mytest2.cpp",
2634 "mytest3.cpp",
2635 ],
2636 test_per_src: true,
2637 relative_install_path: "test",
2638 system_shared_libs: [],
2639 static_executable: true,
2640 stl: "none",
2641 }
Roland Levillain630846d2019-06-26 12:48:34 +01002642 `)
2643
Sundong Ahnabb64432019-10-22 13:58:29 +09002644 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002645 copyCmds := apexRule.Args["copy_commands"]
2646
2647 // Ensure that test dep is copied into apex.
2648 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002649
2650 // Ensure that test deps built with `test_per_src` are copied into apex.
2651 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2652 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2653 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002654
2655 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002656 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002657 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2658 name := apexBundle.BaseModuleName()
2659 prefix := "TARGET_"
2660 var builder strings.Builder
2661 data.Custom(&builder, name, prefix, "", data)
2662 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002663 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2664 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2665 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2666 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002667 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002668 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002669 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002670}
2671
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002672func TestInstallExtraFlattenedApexes(t *testing.T) {
2673 ctx, config := testApex(t, `
2674 apex {
2675 name: "myapex",
2676 key: "myapex.key",
2677 }
2678 apex_key {
2679 name: "myapex.key",
2680 public_key: "testkey.avbpubkey",
2681 private_key: "testkey.pem",
2682 }
2683 `, func(fs map[string][]byte, config android.Config) {
2684 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2685 })
2686 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002687 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002688 mk := android.AndroidMkDataForTest(t, config, "", ab)
2689 var builder strings.Builder
2690 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2691 androidMk := builder.String()
2692 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2693}
2694
Jooyung Han5c998b92019-06-27 11:30:33 +09002695func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002696 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002697 apex {
2698 name: "myapex",
2699 key: "myapex.key",
2700 native_shared_libs: ["mylib"],
2701 uses: ["commonapex"],
2702 }
2703
2704 apex {
2705 name: "commonapex",
2706 key: "myapex.key",
2707 native_shared_libs: ["libcommon"],
2708 provide_cpp_shared_libs: true,
2709 }
2710
2711 apex_key {
2712 name: "myapex.key",
2713 public_key: "testkey.avbpubkey",
2714 private_key: "testkey.pem",
2715 }
2716
2717 cc_library {
2718 name: "mylib",
2719 srcs: ["mylib.cpp"],
2720 shared_libs: ["libcommon"],
2721 system_shared_libs: [],
2722 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002723 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002724 }
2725
2726 cc_library {
2727 name: "libcommon",
2728 srcs: ["mylib_common.cpp"],
2729 system_shared_libs: [],
2730 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002731 // TODO: remove //apex_available:platform
2732 apex_available: [
2733 "//apex_available:platform",
2734 "commonapex",
2735 "myapex",
2736 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002737 }
2738 `)
2739
Sundong Ahnabb64432019-10-22 13:58:29 +09002740 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002741 apexRule1 := module1.Rule("apexRule")
2742 copyCmds1 := apexRule1.Args["copy_commands"]
2743
Sundong Ahnabb64432019-10-22 13:58:29 +09002744 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002745 apexRule2 := module2.Rule("apexRule")
2746 copyCmds2 := apexRule2.Args["copy_commands"]
2747
Colin Cross7113d202019-11-20 16:39:12 -08002748 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2749 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002750 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2751 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2752 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2753}
2754
2755func TestApexUsesFailsIfNotProvided(t *testing.T) {
2756 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2757 apex {
2758 name: "myapex",
2759 key: "myapex.key",
2760 uses: ["commonapex"],
2761 }
2762
2763 apex {
2764 name: "commonapex",
2765 key: "myapex.key",
2766 }
2767
2768 apex_key {
2769 name: "myapex.key",
2770 public_key: "testkey.avbpubkey",
2771 private_key: "testkey.pem",
2772 }
2773 `)
2774 testApexError(t, `uses: "commonapex" is not a provider`, `
2775 apex {
2776 name: "myapex",
2777 key: "myapex.key",
2778 uses: ["commonapex"],
2779 }
2780
2781 cc_library {
2782 name: "commonapex",
2783 system_shared_libs: [],
2784 stl: "none",
2785 }
2786
2787 apex_key {
2788 name: "myapex.key",
2789 public_key: "testkey.avbpubkey",
2790 private_key: "testkey.pem",
2791 }
2792 `)
2793}
2794
2795func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2796 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2797 apex {
2798 name: "myapex",
2799 key: "myapex.key",
2800 use_vendor: true,
2801 uses: ["commonapex"],
2802 }
2803
2804 apex {
2805 name: "commonapex",
2806 key: "myapex.key",
2807 provide_cpp_shared_libs: true,
2808 }
2809
2810 apex_key {
2811 name: "myapex.key",
2812 public_key: "testkey.avbpubkey",
2813 private_key: "testkey.pem",
2814 }
Jooyung Handc782442019-11-01 03:14:38 +09002815 `, func(fs map[string][]byte, config android.Config) {
2816 setUseVendorWhitelistForTest(config, []string{"myapex"})
2817 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002818}
2819
Jooyung Hand48f3c32019-08-23 11:18:57 +09002820func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2821 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2822 apex {
2823 name: "myapex",
2824 key: "myapex.key",
2825 native_shared_libs: ["libfoo"],
2826 }
2827
2828 apex_key {
2829 name: "myapex.key",
2830 public_key: "testkey.avbpubkey",
2831 private_key: "testkey.pem",
2832 }
2833
2834 cc_library {
2835 name: "libfoo",
2836 stl: "none",
2837 system_shared_libs: [],
2838 enabled: false,
2839 }
2840 `)
2841 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2842 apex {
2843 name: "myapex",
2844 key: "myapex.key",
2845 java_libs: ["myjar"],
2846 }
2847
2848 apex_key {
2849 name: "myapex.key",
2850 public_key: "testkey.avbpubkey",
2851 private_key: "testkey.pem",
2852 }
2853
2854 java_library {
2855 name: "myjar",
2856 srcs: ["foo/bar/MyClass.java"],
2857 sdk_version: "none",
2858 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09002859 enabled: false,
2860 }
2861 `)
2862}
2863
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002864func TestApexWithApps(t *testing.T) {
2865 ctx, _ := testApex(t, `
2866 apex {
2867 name: "myapex",
2868 key: "myapex.key",
2869 apps: [
2870 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002871 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002872 ],
2873 }
2874
2875 apex_key {
2876 name: "myapex.key",
2877 public_key: "testkey.avbpubkey",
2878 private_key: "testkey.pem",
2879 }
2880
2881 android_app {
2882 name: "AppFoo",
2883 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08002884 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002885 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002886 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08002887 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002888 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002889 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002890
2891 android_app {
2892 name: "AppFooPriv",
2893 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08002894 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09002895 system_modules: "none",
2896 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08002897 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002898 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09002899 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002900
2901 cc_library_shared {
2902 name: "libjni",
2903 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002904 shared_libs: ["libfoo"],
2905 stl: "none",
2906 system_shared_libs: [],
2907 apex_available: [ "myapex" ],
2908 sdk_version: "current",
2909 }
2910
2911 cc_library_shared {
2912 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09002913 stl: "none",
2914 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09002915 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08002916 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09002917 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002918 `)
2919
Sundong Ahnabb64432019-10-22 13:58:29 +09002920 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002921 apexRule := module.Rule("apexRule")
2922 copyCmds := apexRule.Args["copy_commands"]
2923
2924 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002925 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002926
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002927 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
2928 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09002929 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002930 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002931 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002932 // JNI libraries including transitive deps are
2933 for _, jni := range []string{"libjni", "libfoo"} {
2934 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
2935 // ... embedded inside APK (jnilibs.zip)
2936 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
2937 // ... and not directly inside the APEX
2938 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
2939 }
Dario Frenicde2a032019-10-27 00:29:22 +01002940}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002941
Dario Frenicde2a032019-10-27 00:29:22 +01002942func TestApexWithAppImports(t *testing.T) {
2943 ctx, _ := testApex(t, `
2944 apex {
2945 name: "myapex",
2946 key: "myapex.key",
2947 apps: [
2948 "AppFooPrebuilt",
2949 "AppFooPrivPrebuilt",
2950 ],
2951 }
2952
2953 apex_key {
2954 name: "myapex.key",
2955 public_key: "testkey.avbpubkey",
2956 private_key: "testkey.pem",
2957 }
2958
2959 android_app_import {
2960 name: "AppFooPrebuilt",
2961 apk: "PrebuiltAppFoo.apk",
2962 presigned: true,
2963 dex_preopt: {
2964 enabled: false,
2965 },
2966 }
2967
2968 android_app_import {
2969 name: "AppFooPrivPrebuilt",
2970 apk: "PrebuiltAppFooPriv.apk",
2971 privileged: true,
2972 presigned: true,
2973 dex_preopt: {
2974 enabled: false,
2975 },
2976 }
2977 `)
2978
Sundong Ahnabb64432019-10-22 13:58:29 +09002979 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002980 apexRule := module.Rule("apexRule")
2981 copyCmds := apexRule.Args["copy_commands"]
2982
2983 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2984 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002985}
2986
Dario Freni6f3937c2019-12-20 22:58:03 +00002987func TestApexWithTestHelperApp(t *testing.T) {
2988 ctx, _ := testApex(t, `
2989 apex {
2990 name: "myapex",
2991 key: "myapex.key",
2992 apps: [
2993 "TesterHelpAppFoo",
2994 ],
2995 }
2996
2997 apex_key {
2998 name: "myapex.key",
2999 public_key: "testkey.avbpubkey",
3000 private_key: "testkey.pem",
3001 }
3002
3003 android_test_helper_app {
3004 name: "TesterHelpAppFoo",
3005 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003006 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003007 }
3008
3009 `)
3010
3011 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3012 apexRule := module.Rule("apexRule")
3013 copyCmds := apexRule.Args["copy_commands"]
3014
3015 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3016}
3017
Jooyung Han18020ea2019-11-13 10:50:48 +09003018func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3019 // libfoo's apex_available comes from cc_defaults
3020 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3021 apex {
3022 name: "myapex",
3023 key: "myapex.key",
3024 native_shared_libs: ["libfoo"],
3025 }
3026
3027 apex_key {
3028 name: "myapex.key",
3029 public_key: "testkey.avbpubkey",
3030 private_key: "testkey.pem",
3031 }
3032
3033 apex {
3034 name: "otherapex",
3035 key: "myapex.key",
3036 native_shared_libs: ["libfoo"],
3037 }
3038
3039 cc_defaults {
3040 name: "libfoo-defaults",
3041 apex_available: ["otherapex"],
3042 }
3043
3044 cc_library {
3045 name: "libfoo",
3046 defaults: ["libfoo-defaults"],
3047 stl: "none",
3048 system_shared_libs: [],
3049 }`)
3050}
3051
Jiyong Park127b40b2019-09-30 16:04:35 +09003052func TestApexAvailable(t *testing.T) {
3053 // libfoo is not available to myapex, but only to otherapex
3054 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3055 apex {
3056 name: "myapex",
3057 key: "myapex.key",
3058 native_shared_libs: ["libfoo"],
3059 }
3060
3061 apex_key {
3062 name: "myapex.key",
3063 public_key: "testkey.avbpubkey",
3064 private_key: "testkey.pem",
3065 }
3066
3067 apex {
3068 name: "otherapex",
3069 key: "otherapex.key",
3070 native_shared_libs: ["libfoo"],
3071 }
3072
3073 apex_key {
3074 name: "otherapex.key",
3075 public_key: "testkey.avbpubkey",
3076 private_key: "testkey.pem",
3077 }
3078
3079 cc_library {
3080 name: "libfoo",
3081 stl: "none",
3082 system_shared_libs: [],
3083 apex_available: ["otherapex"],
3084 }`)
3085
3086 // libbar is an indirect dep
3087 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3088 apex {
3089 name: "myapex",
3090 key: "myapex.key",
3091 native_shared_libs: ["libfoo"],
3092 }
3093
3094 apex_key {
3095 name: "myapex.key",
3096 public_key: "testkey.avbpubkey",
3097 private_key: "testkey.pem",
3098 }
3099
3100 apex {
3101 name: "otherapex",
3102 key: "otherapex.key",
3103 native_shared_libs: ["libfoo"],
3104 }
3105
3106 apex_key {
3107 name: "otherapex.key",
3108 public_key: "testkey.avbpubkey",
3109 private_key: "testkey.pem",
3110 }
3111
3112 cc_library {
3113 name: "libfoo",
3114 stl: "none",
3115 shared_libs: ["libbar"],
3116 system_shared_libs: [],
3117 apex_available: ["myapex", "otherapex"],
3118 }
3119
3120 cc_library {
3121 name: "libbar",
3122 stl: "none",
3123 system_shared_libs: [],
3124 apex_available: ["otherapex"],
3125 }`)
3126
3127 testApexError(t, "\"otherapex\" is not a valid module name", `
3128 apex {
3129 name: "myapex",
3130 key: "myapex.key",
3131 native_shared_libs: ["libfoo"],
3132 }
3133
3134 apex_key {
3135 name: "myapex.key",
3136 public_key: "testkey.avbpubkey",
3137 private_key: "testkey.pem",
3138 }
3139
3140 cc_library {
3141 name: "libfoo",
3142 stl: "none",
3143 system_shared_libs: [],
3144 apex_available: ["otherapex"],
3145 }`)
3146
3147 ctx, _ := testApex(t, `
3148 apex {
3149 name: "myapex",
3150 key: "myapex.key",
3151 native_shared_libs: ["libfoo", "libbar"],
3152 }
3153
3154 apex_key {
3155 name: "myapex.key",
3156 public_key: "testkey.avbpubkey",
3157 private_key: "testkey.pem",
3158 }
3159
3160 cc_library {
3161 name: "libfoo",
3162 stl: "none",
3163 system_shared_libs: [],
3164 apex_available: ["myapex"],
3165 }
3166
3167 cc_library {
3168 name: "libbar",
3169 stl: "none",
3170 system_shared_libs: [],
3171 apex_available: ["//apex_available:anyapex"],
3172 }`)
3173
3174 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003175 // TODO(jiyong) the checks for the platform variant are removed because we now create
3176 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3177 // the platform variants are not used from other platform modules. When that is done,
3178 // these checks will be replaced by expecting a specific error message that will be
3179 // emitted when the platform variant is used.
3180 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3181 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3182 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3183 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003184
3185 ctx, _ = testApex(t, `
3186 apex {
3187 name: "myapex",
3188 key: "myapex.key",
3189 }
3190
3191 apex_key {
3192 name: "myapex.key",
3193 public_key: "testkey.avbpubkey",
3194 private_key: "testkey.pem",
3195 }
3196
3197 cc_library {
3198 name: "libfoo",
3199 stl: "none",
3200 system_shared_libs: [],
3201 apex_available: ["//apex_available:platform"],
3202 }`)
3203
3204 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003205 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3206 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003207
3208 ctx, _ = testApex(t, `
3209 apex {
3210 name: "myapex",
3211 key: "myapex.key",
3212 native_shared_libs: ["libfoo"],
3213 }
3214
3215 apex_key {
3216 name: "myapex.key",
3217 public_key: "testkey.avbpubkey",
3218 private_key: "testkey.pem",
3219 }
3220
3221 cc_library {
3222 name: "libfoo",
3223 stl: "none",
3224 system_shared_libs: [],
3225 apex_available: ["myapex"],
3226 static: {
3227 apex_available: ["//apex_available:platform"],
3228 },
3229 }`)
3230
3231 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003232 // TODO(jiyong) the checks for the platform variant are removed because we now create
3233 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3234 // the platform variants are not used from other platform modules. When that is done,
3235 // these checks will be replaced by expecting a specific error message that will be
3236 // emitted when the platform variant is used.
3237 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3238 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3239 // // but the static variant is available to both myapex and the platform
3240 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3241 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003242}
3243
Jiyong Park5d790c32019-11-15 18:40:32 +09003244func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003245 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003246 apex {
3247 name: "myapex",
3248 key: "myapex.key",
3249 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003250 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003251 }
3252
3253 override_apex {
3254 name: "override_myapex",
3255 base: "myapex",
3256 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003257 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003258 logging_parent: "com.foo.bar",
Jiyong Park5d790c32019-11-15 18:40:32 +09003259 }
3260
3261 apex_key {
3262 name: "myapex.key",
3263 public_key: "testkey.avbpubkey",
3264 private_key: "testkey.pem",
3265 }
3266
3267 android_app {
3268 name: "app",
3269 srcs: ["foo/bar/MyClass.java"],
3270 package_name: "foo",
3271 sdk_version: "none",
3272 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003273 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003274 }
3275
3276 override_android_app {
3277 name: "override_app",
3278 base: "app",
3279 package_name: "bar",
3280 }
3281 `)
3282
Jiyong Park317645e2019-12-05 13:20:58 +09003283 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3284 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3285 if originalVariant.GetOverriddenBy() != "" {
3286 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3287 }
3288 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3289 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3290 }
3291
Jiyong Park5d790c32019-11-15 18:40:32 +09003292 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3293 apexRule := module.Rule("apexRule")
3294 copyCmds := apexRule.Args["copy_commands"]
3295
3296 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3297 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003298
3299 apexBundle := module.Module().(*apexBundle)
3300 name := apexBundle.Name()
3301 if name != "override_myapex" {
3302 t.Errorf("name should be \"override_myapex\", but was %q", name)
3303 }
3304
Baligh Uddin004d7172020-02-19 21:29:28 -08003305 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3306 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3307 }
3308
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003309 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3310 var builder strings.Builder
3311 data.Custom(&builder, name, "TARGET_", "", data)
3312 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003313 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003314 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3315 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003316 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003317 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003318 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003319 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3320 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003321}
3322
Jooyung Han214bf372019-11-12 13:03:50 +09003323func TestLegacyAndroid10Support(t *testing.T) {
3324 ctx, _ := testApex(t, `
3325 apex {
3326 name: "myapex",
3327 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003328 native_shared_libs: ["mylib"],
Jooyung Han214bf372019-11-12 13:03:50 +09003329 legacy_android10_support: true,
3330 }
3331
3332 apex_key {
3333 name: "myapex.key",
3334 public_key: "testkey.avbpubkey",
3335 private_key: "testkey.pem",
3336 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003337
3338 cc_library {
3339 name: "mylib",
3340 srcs: ["mylib.cpp"],
3341 stl: "libc++",
3342 system_shared_libs: [],
3343 apex_available: [ "myapex" ],
3344 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003345 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003346
3347 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3348 args := module.Rule("apexRule").Args
3349 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003350 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003351
3352 // The copies of the libraries in the apex should have one more dependency than
3353 // the ones outside the apex, namely the unwinder. Ideally we should check
3354 // the dependency names directly here but for some reason the names are blank in
3355 // this test.
3356 for _, lib := range []string{"libc++", "mylib"} {
3357 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3358 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3359 if len(apexImplicits) != len(nonApexImplicits)+1 {
3360 t.Errorf("%q missing unwinder dep", lib)
3361 }
3362 }
Jooyung Han214bf372019-11-12 13:03:50 +09003363}
3364
Jooyung Han58f26ab2019-12-18 15:34:32 +09003365func TestJavaSDKLibrary(t *testing.T) {
3366 ctx, _ := testApex(t, `
3367 apex {
3368 name: "myapex",
3369 key: "myapex.key",
3370 java_libs: ["foo"],
3371 }
3372
3373 apex_key {
3374 name: "myapex.key",
3375 public_key: "testkey.avbpubkey",
3376 private_key: "testkey.pem",
3377 }
3378
3379 java_sdk_library {
3380 name: "foo",
3381 srcs: ["a.java"],
3382 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003383 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003384 }
3385 `, withFiles(map[string][]byte{
3386 "api/current.txt": nil,
3387 "api/removed.txt": nil,
3388 "api/system-current.txt": nil,
3389 "api/system-removed.txt": nil,
3390 "api/test-current.txt": nil,
3391 "api/test-removed.txt": nil,
3392 }))
3393
3394 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003395 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003396 "javalib/foo.jar",
3397 "etc/permissions/foo.xml",
3398 })
3399 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003400 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3401 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003402}
3403
atrost6e126252020-01-27 17:01:16 +00003404func TestCompatConfig(t *testing.T) {
3405 ctx, _ := testApex(t, `
3406 apex {
3407 name: "myapex",
3408 key: "myapex.key",
3409 prebuilts: ["myjar-platform-compat-config"],
3410 java_libs: ["myjar"],
3411 }
3412
3413 apex_key {
3414 name: "myapex.key",
3415 public_key: "testkey.avbpubkey",
3416 private_key: "testkey.pem",
3417 }
3418
3419 platform_compat_config {
3420 name: "myjar-platform-compat-config",
3421 src: ":myjar",
3422 }
3423
3424 java_library {
3425 name: "myjar",
3426 srcs: ["foo/bar/MyClass.java"],
3427 sdk_version: "none",
3428 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003429 apex_available: [ "myapex" ],
3430 }
3431 `)
3432 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3433 "etc/compatconfig/myjar-platform-compat-config.xml",
3434 "javalib/myjar.jar",
3435 })
3436}
3437
Jiyong Park479321d2019-12-16 11:47:12 +09003438func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3439 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3440 apex {
3441 name: "myapex",
3442 key: "myapex.key",
3443 java_libs: ["myjar"],
3444 }
3445
3446 apex_key {
3447 name: "myapex.key",
3448 public_key: "testkey.avbpubkey",
3449 private_key: "testkey.pem",
3450 }
3451
3452 java_library {
3453 name: "myjar",
3454 srcs: ["foo/bar/MyClass.java"],
3455 sdk_version: "none",
3456 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003457 compile_dex: false,
Jiyong Park479321d2019-12-16 11:47:12 +09003458 }
3459 `)
3460}
3461
Jiyong Park7afd1072019-12-30 16:56:33 +09003462func TestCarryRequiredModuleNames(t *testing.T) {
3463 ctx, config := testApex(t, `
3464 apex {
3465 name: "myapex",
3466 key: "myapex.key",
3467 native_shared_libs: ["mylib"],
3468 }
3469
3470 apex_key {
3471 name: "myapex.key",
3472 public_key: "testkey.avbpubkey",
3473 private_key: "testkey.pem",
3474 }
3475
3476 cc_library {
3477 name: "mylib",
3478 srcs: ["mylib.cpp"],
3479 system_shared_libs: [],
3480 stl: "none",
3481 required: ["a", "b"],
3482 host_required: ["c", "d"],
3483 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003484 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003485 }
3486 `)
3487
3488 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3489 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3490 name := apexBundle.BaseModuleName()
3491 prefix := "TARGET_"
3492 var builder strings.Builder
3493 data.Custom(&builder, name, prefix, "", data)
3494 androidMk := builder.String()
3495 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3496 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3497 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3498}
3499
Jiyong Park7cd10e32020-01-14 09:22:18 +09003500func TestSymlinksFromApexToSystem(t *testing.T) {
3501 bp := `
3502 apex {
3503 name: "myapex",
3504 key: "myapex.key",
3505 native_shared_libs: ["mylib"],
3506 java_libs: ["myjar"],
3507 }
3508
Jiyong Park9d677202020-02-19 16:29:35 +09003509 apex {
3510 name: "myapex.updatable",
3511 key: "myapex.key",
3512 native_shared_libs: ["mylib"],
3513 java_libs: ["myjar"],
3514 updatable: true,
3515 }
3516
Jiyong Park7cd10e32020-01-14 09:22:18 +09003517 apex_key {
3518 name: "myapex.key",
3519 public_key: "testkey.avbpubkey",
3520 private_key: "testkey.pem",
3521 }
3522
3523 cc_library {
3524 name: "mylib",
3525 srcs: ["mylib.cpp"],
3526 shared_libs: ["myotherlib"],
3527 system_shared_libs: [],
3528 stl: "none",
3529 apex_available: [
3530 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003531 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003532 "//apex_available:platform",
3533 ],
3534 }
3535
3536 cc_library {
3537 name: "myotherlib",
3538 srcs: ["mylib.cpp"],
3539 system_shared_libs: [],
3540 stl: "none",
3541 apex_available: [
3542 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003543 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003544 "//apex_available:platform",
3545 ],
3546 }
3547
3548 java_library {
3549 name: "myjar",
3550 srcs: ["foo/bar/MyClass.java"],
3551 sdk_version: "none",
3552 system_modules: "none",
3553 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003554 apex_available: [
3555 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003556 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003557 "//apex_available:platform",
3558 ],
3559 }
3560
3561 java_library {
3562 name: "myotherjar",
3563 srcs: ["foo/bar/MyClass.java"],
3564 sdk_version: "none",
3565 system_modules: "none",
3566 apex_available: [
3567 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003568 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003569 "//apex_available:platform",
3570 ],
3571 }
3572 `
3573
3574 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3575 for _, f := range files {
3576 if f.path == file {
3577 if f.isLink {
3578 t.Errorf("%q is not a real file", file)
3579 }
3580 return
3581 }
3582 }
3583 t.Errorf("%q is not found", file)
3584 }
3585
3586 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3587 for _, f := range files {
3588 if f.path == file {
3589 if !f.isLink {
3590 t.Errorf("%q is not a symlink", file)
3591 }
3592 return
3593 }
3594 }
3595 t.Errorf("%q is not found", file)
3596 }
3597
Jiyong Park9d677202020-02-19 16:29:35 +09003598 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3599 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003600 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003601 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003602 ensureRealfileExists(t, files, "javalib/myjar.jar")
3603 ensureRealfileExists(t, files, "lib64/mylib.so")
3604 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3605
Jiyong Park9d677202020-02-19 16:29:35 +09003606 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3607 ensureRealfileExists(t, files, "javalib/myjar.jar")
3608 ensureRealfileExists(t, files, "lib64/mylib.so")
3609 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3610
3611 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003612 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003613 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003614 ensureRealfileExists(t, files, "javalib/myjar.jar")
3615 ensureRealfileExists(t, files, "lib64/mylib.so")
3616 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09003617
3618 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3619 ensureRealfileExists(t, files, "javalib/myjar.jar")
3620 ensureRealfileExists(t, files, "lib64/mylib.so")
3621 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09003622}
3623
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003624func TestMain(m *testing.M) {
3625 run := func() int {
3626 setUp()
3627 defer tearDown()
3628
3629 return m.Run()
3630 }
3631
3632 os.Exit(run())
3633}