blob: 6d9ad268655de01b91ba2c13ade9c65c790368e7 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jiyong Parkcfaa1642020-02-28 16:51:07 +090090func withManifestPackageNameOverrides(specs []string) testCustomizer {
91 return func(fs map[string][]byte, config android.Config) {
92 config.TestProductVariables.ManifestPackageNameOverrides = specs
93 }
94}
95
Jooyung Han31c470b2019-10-18 16:26:59 +090096func withBinder32bit(fs map[string][]byte, config android.Config) {
97 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
98}
99
Jiyong Park7cd10e32020-01-14 09:22:18 +0900100func withUnbundledBuild(fs map[string][]byte, config android.Config) {
101 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
102}
103
Jooyung Han344d5432019-08-23 11:17:39 +0900104func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +0900105 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900106
107 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900108 filegroup {
109 name: "myapex-file_contexts",
110 srcs: [
111 "system/sepolicy/apex/myapex-file_contexts",
112 ],
113 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900114 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800115
Colin Crossf9aabd72020-02-15 11:29:50 -0800116 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
117
Dario Frenicde2a032019-10-27 00:29:22 +0100118 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900119
Jooyung Han344d5432019-08-23 11:17:39 +0900120 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900121 "a.java": nil,
122 "PrebuiltAppFoo.apk": nil,
123 "PrebuiltAppFooPriv.apk": nil,
124 "build/make/target/product/security": nil,
125 "apex_manifest.json": nil,
126 "AndroidManifest.xml": nil,
127 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900128 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900129 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900130 "system/sepolicy/apex/otherapex-file_contexts": nil,
131 "system/sepolicy/apex/commonapex-file_contexts": nil,
132 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800133 "mylib.cpp": nil,
134 "mylib_common.cpp": nil,
135 "mytest.cpp": nil,
136 "mytest1.cpp": nil,
137 "mytest2.cpp": nil,
138 "mytest3.cpp": nil,
139 "myprebuilt": nil,
140 "my_include": nil,
141 "foo/bar/MyClass.java": nil,
142 "prebuilt.jar": nil,
143 "vendor/foo/devkeys/test.x509.pem": nil,
144 "vendor/foo/devkeys/test.pk8": nil,
145 "testkey.x509.pem": nil,
146 "testkey.pk8": nil,
147 "testkey.override.x509.pem": nil,
148 "testkey.override.pk8": nil,
149 "vendor/foo/devkeys/testkey.avbpubkey": nil,
150 "vendor/foo/devkeys/testkey.pem": nil,
151 "NOTICE": nil,
152 "custom_notice": nil,
153 "testkey2.avbpubkey": nil,
154 "testkey2.pem": nil,
155 "myapex-arm64.apex": nil,
156 "myapex-arm.apex": nil,
157 "frameworks/base/api/current.txt": nil,
158 "framework/aidl/a.aidl": nil,
159 "build/make/core/proguard.flags": nil,
160 "build/make/core/proguard_basic_keeps.flags": nil,
161 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900162 }
163
Colin Crossf9aabd72020-02-15 11:29:50 -0800164 cc.GatherRequiredFilesForTest(fs)
165
Jooyung Han344d5432019-08-23 11:17:39 +0900166 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800167 // The fs now needs to be populated before creating the config, call handlers twice
168 // for now, once to get any fs changes, and later after the config was created to
169 // set product variables or targets.
170 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
171 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900172 }
173
Colin Cross98be1bb2019-12-13 20:41:13 -0800174 config := android.TestArchConfig(buildDir, nil, bp, fs)
175 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
176 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
177 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
178 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
179 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
180 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
181
182 for _, handler := range handlers {
183 // The fs now needs to be populated before creating the config, call handlers twice
184 // for now, earlier to get any fs changes, and now after the config was created to
185 // set product variables or targets.
186 tempFS := map[string][]byte{}
187 handler(tempFS, config)
188 }
189
190 ctx := android.NewTestArchContext()
191 ctx.RegisterModuleType("apex", BundleFactory)
192 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
193 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
194 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
195 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
196 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
197 ctx.RegisterModuleType("override_apex", overrideApexFactory)
198
Jooyung Hana57af4a2020-01-23 05:36:59 +0000199 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
200 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
201
Paul Duffin77980a82019-12-19 16:01:36 +0000202 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800203 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800204 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
205 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800206 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000207 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800208 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800209 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000210 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000211 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000212 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900213 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800214
Colin Cross98be1bb2019-12-13 20:41:13 -0800215 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800216 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800217
218 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900219
Jooyung Han5c998b92019-06-27 11:30:33 +0900220 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900221}
222
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700223func setUp() {
224 var err error
225 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900226 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700227 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900228 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229}
230
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700231func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900232 os.RemoveAll(buildDir)
233}
234
Jooyung Han643adc42020-02-27 13:50:06 +0900235// ensure that 'result' equals 'expected'
236func ensureEquals(t *testing.T, result string, expected string) {
237 t.Helper()
238 if result != expected {
239 t.Errorf("%q != %q", expected, result)
240 }
241}
242
Jiyong Park25fc6a92018-11-18 18:02:45 +0900243// ensure that 'result' contains 'expected'
244func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900245 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900246 if !strings.Contains(result, expected) {
247 t.Errorf("%q is not found in %q", expected, result)
248 }
249}
250
251// ensures that 'result' does not contain 'notExpected'
252func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900253 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900254 if strings.Contains(result, notExpected) {
255 t.Errorf("%q is found in %q", notExpected, result)
256 }
257}
258
259func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900260 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261 if !android.InList(expected, result) {
262 t.Errorf("%q is not found in %v", expected, result)
263 }
264}
265
266func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900267 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900268 if android.InList(notExpected, result) {
269 t.Errorf("%q is found in %v", notExpected, result)
270 }
271}
272
Jooyung Hane1633032019-08-01 17:41:43 +0900273func ensureListEmpty(t *testing.T, result []string) {
274 t.Helper()
275 if len(result) > 0 {
276 t.Errorf("%q is expected to be empty", result)
277 }
278}
279
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280// Minimal test
281func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700282 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900283 apex_defaults {
284 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900285 manifest: ":myapex.manifest",
286 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 key: "myapex.key",
288 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800289 multilib: {
290 both: {
291 binaries: ["foo",],
292 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900293 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900294 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900295 }
296
Jiyong Park30ca9372019-02-07 16:27:23 +0900297 apex {
298 name: "myapex",
299 defaults: ["myapex-defaults"],
300 }
301
Jiyong Park25fc6a92018-11-18 18:02:45 +0900302 apex_key {
303 name: "myapex.key",
304 public_key: "testkey.avbpubkey",
305 private_key: "testkey.pem",
306 }
307
Jiyong Park809bb722019-02-13 21:33:49 +0900308 filegroup {
309 name: "myapex.manifest",
310 srcs: ["apex_manifest.json"],
311 }
312
313 filegroup {
314 name: "myapex.androidmanifest",
315 srcs: ["AndroidManifest.xml"],
316 }
317
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318 cc_library {
319 name: "mylib",
320 srcs: ["mylib.cpp"],
321 shared_libs: ["mylib2"],
322 system_shared_libs: [],
323 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000324 // TODO: remove //apex_available:platform
325 apex_available: [
326 "//apex_available:platform",
327 "myapex",
328 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900329 }
330
Alex Light3d673592019-01-18 14:37:31 -0800331 cc_binary {
332 name: "foo",
333 srcs: ["mylib.cpp"],
334 compile_multilib: "both",
335 multilib: {
336 lib32: {
337 suffix: "32",
338 },
339 lib64: {
340 suffix: "64",
341 },
342 },
343 symlinks: ["foo_link_"],
344 symlink_preferred_arch: true,
345 system_shared_libs: [],
346 static_executable: true,
347 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000348 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800349 }
350
Jiyong Park25fc6a92018-11-18 18:02:45 +0900351 cc_library {
352 name: "mylib2",
353 srcs: ["mylib.cpp"],
354 system_shared_libs: [],
355 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900356 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000357 // TODO: remove //apex_available:platform
358 apex_available: [
359 "//apex_available:platform",
360 "myapex",
361 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900363
364 java_library {
365 name: "myjar",
366 srcs: ["foo/bar/MyClass.java"],
367 sdk_version: "none",
368 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900369 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900370 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000371 // TODO: remove //apex_available:platform
372 apex_available: [
373 "//apex_available:platform",
374 "myapex",
375 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900376 }
377
378 java_library {
379 name: "myotherjar",
380 srcs: ["foo/bar/MyClass.java"],
381 sdk_version: "none",
382 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900383 // TODO: remove //apex_available:platform
384 apex_available: [
385 "//apex_available:platform",
386 "myapex",
387 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900388 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900389
390 java_library {
391 name: "mysharedjar",
392 srcs: ["foo/bar/MyClass.java"],
393 sdk_version: "none",
394 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900395 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900396 `)
397
Sundong Ahnabb64432019-10-22 13:58:29 +0900398 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900399
400 optFlags := apexRule.Args["opt_flags"]
401 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700402 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900403 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900404
Jiyong Park25fc6a92018-11-18 18:02:45 +0900405 copyCmds := apexRule.Args["copy_commands"]
406
407 // Ensure that main rule creates an output
408 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
409
410 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800411 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900412 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900413
414 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800415 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900416 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900417
418 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800419 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
420 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900421 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
422 // .. but not for java libs
423 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900424 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800425
Colin Cross7113d202019-11-20 16:39:12 -0800426 // Ensure that the platform variant ends with _shared or _common
427 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
428 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900429 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
430 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900431 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
432
433 // Ensure that dynamic dependency to java libs are not included
434 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800435
436 // Ensure that all symlinks are present.
437 found_foo_link_64 := false
438 found_foo := false
439 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900440 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800441 if strings.HasSuffix(cmd, "bin/foo") {
442 found_foo = true
443 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
444 found_foo_link_64 = true
445 }
446 }
447 }
448 good := found_foo && found_foo_link_64
449 if !good {
450 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
451 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900452
Sundong Ahnabb64432019-10-22 13:58:29 +0900453 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700454 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700455 if len(noticeInputs) != 2 {
456 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900457 }
458 ensureListContains(t, noticeInputs, "NOTICE")
459 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900460
461 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900462 ensureListContains(t, depsInfo, "myjar <- myapex")
463 ensureListContains(t, depsInfo, "mylib <- myapex")
464 ensureListContains(t, depsInfo, "mylib2 <- mylib")
465 ensureListContains(t, depsInfo, "myotherjar <- myjar")
466 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800467}
468
Jooyung Hanf21c7972019-12-16 22:32:06 +0900469func TestDefaults(t *testing.T) {
470 ctx, _ := testApex(t, `
471 apex_defaults {
472 name: "myapex-defaults",
473 key: "myapex.key",
474 prebuilts: ["myetc"],
475 native_shared_libs: ["mylib"],
476 java_libs: ["myjar"],
477 apps: ["AppFoo"],
478 }
479
480 prebuilt_etc {
481 name: "myetc",
482 src: "myprebuilt",
483 }
484
485 apex {
486 name: "myapex",
487 defaults: ["myapex-defaults"],
488 }
489
490 apex_key {
491 name: "myapex.key",
492 public_key: "testkey.avbpubkey",
493 private_key: "testkey.pem",
494 }
495
496 cc_library {
497 name: "mylib",
498 system_shared_libs: [],
499 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000500 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900501 }
502
503 java_library {
504 name: "myjar",
505 srcs: ["foo/bar/MyClass.java"],
506 sdk_version: "none",
507 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000508 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900509 }
510
511 android_app {
512 name: "AppFoo",
513 srcs: ["foo/bar/MyClass.java"],
514 sdk_version: "none",
515 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000516 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900517 }
518 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000519 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900520 "etc/myetc",
521 "javalib/myjar.jar",
522 "lib64/mylib.so",
523 "app/AppFoo/AppFoo.apk",
524 })
525}
526
Jooyung Han01a3ee22019-11-02 02:52:25 +0900527func TestApexManifest(t *testing.T) {
528 ctx, _ := testApex(t, `
529 apex {
530 name: "myapex",
531 key: "myapex.key",
532 }
533
534 apex_key {
535 name: "myapex.key",
536 public_key: "testkey.avbpubkey",
537 private_key: "testkey.pem",
538 }
539 `)
540
541 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900542 args := module.Rule("apexRule").Args
543 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
544 t.Error("manifest should be apex_manifest.pb, but " + manifest)
545 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900546}
547
Alex Light5098a612018-11-29 17:12:15 -0800548func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700549 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800550 apex {
551 name: "myapex",
552 key: "myapex.key",
553 payload_type: "zip",
554 native_shared_libs: ["mylib"],
555 }
556
557 apex_key {
558 name: "myapex.key",
559 public_key: "testkey.avbpubkey",
560 private_key: "testkey.pem",
561 }
562
563 cc_library {
564 name: "mylib",
565 srcs: ["mylib.cpp"],
566 shared_libs: ["mylib2"],
567 system_shared_libs: [],
568 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000569 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800570 }
571
572 cc_library {
573 name: "mylib2",
574 srcs: ["mylib.cpp"],
575 system_shared_libs: [],
576 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000577 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800578 }
579 `)
580
Sundong Ahnabb64432019-10-22 13:58:29 +0900581 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800582 copyCmds := zipApexRule.Args["copy_commands"]
583
584 // Ensure that main rule creates an output
585 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
586
587 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800588 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800589
590 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800591 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800592
593 // Ensure that both direct and indirect deps are copied into apex
594 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
595 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900596}
597
598func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700599 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900600 apex {
601 name: "myapex",
602 key: "myapex.key",
603 native_shared_libs: ["mylib", "mylib3"],
604 }
605
606 apex_key {
607 name: "myapex.key",
608 public_key: "testkey.avbpubkey",
609 private_key: "testkey.pem",
610 }
611
612 cc_library {
613 name: "mylib",
614 srcs: ["mylib.cpp"],
615 shared_libs: ["mylib2", "mylib3"],
616 system_shared_libs: [],
617 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000618 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900619 }
620
621 cc_library {
622 name: "mylib2",
623 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900624 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900625 system_shared_libs: [],
626 stl: "none",
627 stubs: {
628 versions: ["1", "2", "3"],
629 },
630 }
631
632 cc_library {
633 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900634 srcs: ["mylib.cpp"],
635 shared_libs: ["mylib4"],
636 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900637 stl: "none",
638 stubs: {
639 versions: ["10", "11", "12"],
640 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000641 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900642 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900643
644 cc_library {
645 name: "mylib4",
646 srcs: ["mylib.cpp"],
647 system_shared_libs: [],
648 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900650 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900651 `)
652
Sundong Ahnabb64432019-10-22 13:58:29 +0900653 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900654 copyCmds := apexRule.Args["copy_commands"]
655
656 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800657 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900658
659 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800660 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900661
662 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800663 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900664
Colin Cross7113d202019-11-20 16:39:12 -0800665 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900666
667 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900668 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900670 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900671
672 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Cross7113d202019-11-20 16:39:12 -0800673 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900674 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800675 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900676
677 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900678 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900679 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900680
681 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900682 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900683
Jooyung Hana57af4a2020-01-23 05:36:59 +0000684 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900685 "lib64/mylib.so",
686 "lib64/mylib3.so",
687 "lib64/mylib4.so",
688 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900689}
690
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900691func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700692 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900693 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900694 name: "myapex2",
695 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900696 native_shared_libs: ["mylib"],
697 }
698
699 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900700 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900701 public_key: "testkey.avbpubkey",
702 private_key: "testkey.pem",
703 }
704
705 cc_library {
706 name: "mylib",
707 srcs: ["mylib.cpp"],
708 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900709 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900710 system_shared_libs: [],
711 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000712 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900713 }
714
715 cc_library {
716 name: "libfoo",
717 srcs: ["mylib.cpp"],
718 shared_libs: ["libbar"],
719 system_shared_libs: [],
720 stl: "none",
721 stubs: {
722 versions: ["10", "20", "30"],
723 },
724 }
725
726 cc_library {
727 name: "libbar",
728 srcs: ["mylib.cpp"],
729 system_shared_libs: [],
730 stl: "none",
731 }
732
Jiyong Park678c8812020-02-07 17:25:49 +0900733 cc_library_static {
734 name: "libbaz",
735 srcs: ["mylib.cpp"],
736 system_shared_libs: [],
737 stl: "none",
738 apex_available: [ "myapex2" ],
739 }
740
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900741 `)
742
Jiyong Park83dc74b2020-01-14 18:38:44 +0900743 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900744 copyCmds := apexRule.Args["copy_commands"]
745
746 // Ensure that direct non-stubs dep is always included
747 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
748
749 // Ensure that indirect stubs dep is not included
750 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
751
752 // Ensure that dependency of stubs is not included
753 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
754
Jiyong Park83dc74b2020-01-14 18:38:44 +0900755 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900756
757 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900758 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900759 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900760 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900761
Jiyong Park3ff16992019-12-27 14:11:47 +0900762 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900763
764 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
765 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900766
767 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900768
769 ensureListContains(t, depsInfo, "mylib <- myapex2")
770 ensureListContains(t, depsInfo, "libbaz <- mylib")
771 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900772}
773
Jooyung Hand3639552019-08-09 12:57:43 +0900774func TestApexWithRuntimeLibsDependency(t *testing.T) {
775 /*
776 myapex
777 |
778 v (runtime_libs)
779 mylib ------+------> libfoo [provides stub]
780 |
781 `------> libbar
782 */
783 ctx, _ := testApex(t, `
784 apex {
785 name: "myapex",
786 key: "myapex.key",
787 native_shared_libs: ["mylib"],
788 }
789
790 apex_key {
791 name: "myapex.key",
792 public_key: "testkey.avbpubkey",
793 private_key: "testkey.pem",
794 }
795
796 cc_library {
797 name: "mylib",
798 srcs: ["mylib.cpp"],
799 runtime_libs: ["libfoo", "libbar"],
800 system_shared_libs: [],
801 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000802 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900803 }
804
805 cc_library {
806 name: "libfoo",
807 srcs: ["mylib.cpp"],
808 system_shared_libs: [],
809 stl: "none",
810 stubs: {
811 versions: ["10", "20", "30"],
812 },
813 }
814
815 cc_library {
816 name: "libbar",
817 srcs: ["mylib.cpp"],
818 system_shared_libs: [],
819 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000820 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900821 }
822
823 `)
824
Sundong Ahnabb64432019-10-22 13:58:29 +0900825 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900826 copyCmds := apexRule.Args["copy_commands"]
827
828 // Ensure that direct non-stubs dep is always included
829 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
830
831 // Ensure that indirect stubs dep is not included
832 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
833
834 // Ensure that runtime_libs dep in included
835 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
836
Sundong Ahnabb64432019-10-22 13:58:29 +0900837 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900838 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
839 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900840
841}
842
Jooyung Han9c80bae2019-08-20 17:30:57 +0900843func TestApexDependencyToLLNDK(t *testing.T) {
844 ctx, _ := testApex(t, `
845 apex {
846 name: "myapex",
847 key: "myapex.key",
848 use_vendor: true,
849 native_shared_libs: ["mylib"],
850 }
851
852 apex_key {
853 name: "myapex.key",
854 public_key: "testkey.avbpubkey",
855 private_key: "testkey.pem",
856 }
857
858 cc_library {
859 name: "mylib",
860 srcs: ["mylib.cpp"],
861 vendor_available: true,
862 shared_libs: ["libbar"],
863 system_shared_libs: [],
864 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000865 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900866 }
867
868 cc_library {
869 name: "libbar",
870 srcs: ["mylib.cpp"],
871 system_shared_libs: [],
872 stl: "none",
873 }
874
875 llndk_library {
876 name: "libbar",
877 symbol_file: "",
878 }
Jooyung Handc782442019-11-01 03:14:38 +0900879 `, func(fs map[string][]byte, config android.Config) {
880 setUseVendorWhitelistForTest(config, []string{"myapex"})
881 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900882
Sundong Ahnabb64432019-10-22 13:58:29 +0900883 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900884 copyCmds := apexRule.Args["copy_commands"]
885
886 // Ensure that LLNDK dep is not included
887 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
888
Sundong Ahnabb64432019-10-22 13:58:29 +0900889 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900890 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900891
892 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900893 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900894
895}
896
Jiyong Park25fc6a92018-11-18 18:02:45 +0900897func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700898 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900899 apex {
900 name: "myapex",
901 key: "myapex.key",
902 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
903 }
904
905 apex_key {
906 name: "myapex.key",
907 public_key: "testkey.avbpubkey",
908 private_key: "testkey.pem",
909 }
910
911 cc_library {
912 name: "mylib",
913 srcs: ["mylib.cpp"],
914 shared_libs: ["libdl#27"],
915 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000916 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900917 }
918
919 cc_library_shared {
920 name: "mylib_shared",
921 srcs: ["mylib.cpp"],
922 shared_libs: ["libdl#27"],
923 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000924 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925 }
926
927 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900928 name: "libBootstrap",
929 srcs: ["mylib.cpp"],
930 stl: "none",
931 bootstrap: true,
932 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900933 `)
934
Sundong Ahnabb64432019-10-22 13:58:29 +0900935 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900936 copyCmds := apexRule.Args["copy_commands"]
937
938 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800939 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900940 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
941 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900942
943 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900944 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900945
Colin Cross7113d202019-11-20 16:39:12 -0800946 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
947 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
948 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900949
950 // For dependency to libc
951 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900952 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900953 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900954 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900955 // ... Cflags from stub is correctly exported to mylib
956 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
957 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
958
959 // For dependency to libm
960 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800961 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900963 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964 // ... and is not compiling with the stub
965 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
966 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
967
968 // For dependency to libdl
969 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900970 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900971 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900972 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
973 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900974 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800975 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900976 // ... Cflags from stub is correctly exported to mylib
977 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
978 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900979
980 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800981 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
982 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
983 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
984 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900985}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900986
987func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700988 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900989 apex {
990 name: "myapex",
991 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900992 native_shared_libs: ["mylib"],
993 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900994 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900995 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900996 }
997
998 apex_key {
999 name: "myapex.key",
1000 public_key: "testkey.avbpubkey",
1001 private_key: "testkey.pem",
1002 }
1003
1004 prebuilt_etc {
1005 name: "myetc",
1006 src: "myprebuilt",
1007 sub_dir: "foo/bar",
1008 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001009
1010 cc_library {
1011 name: "mylib",
1012 srcs: ["mylib.cpp"],
1013 relative_install_path: "foo/bar",
1014 system_shared_libs: [],
1015 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001016 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001017 }
1018
1019 cc_binary {
1020 name: "mybin",
1021 srcs: ["mylib.cpp"],
1022 relative_install_path: "foo/bar",
1023 system_shared_libs: [],
1024 static_executable: true,
1025 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001026 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001027 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001028 `)
1029
Sundong Ahnabb64432019-10-22 13:58:29 +09001030 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001031 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1032
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001033 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001034 ensureListContains(t, dirs, "etc")
1035 ensureListContains(t, dirs, "etc/foo")
1036 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001037 ensureListContains(t, dirs, "lib64")
1038 ensureListContains(t, dirs, "lib64/foo")
1039 ensureListContains(t, dirs, "lib64/foo/bar")
1040 ensureListContains(t, dirs, "lib")
1041 ensureListContains(t, dirs, "lib/foo")
1042 ensureListContains(t, dirs, "lib/foo/bar")
1043
Jiyong Parkbd13e442019-03-15 18:10:35 +09001044 ensureListContains(t, dirs, "bin")
1045 ensureListContains(t, dirs, "bin/foo")
1046 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001047}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001048
1049func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001050 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001051 apex {
1052 name: "myapex",
1053 key: "myapex.key",
1054 native_shared_libs: ["mylib"],
1055 use_vendor: true,
1056 }
1057
1058 apex_key {
1059 name: "myapex.key",
1060 public_key: "testkey.avbpubkey",
1061 private_key: "testkey.pem",
1062 }
1063
1064 cc_library {
1065 name: "mylib",
1066 srcs: ["mylib.cpp"],
1067 shared_libs: ["mylib2"],
1068 system_shared_libs: [],
1069 vendor_available: true,
1070 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001071 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001072 }
1073
1074 cc_library {
1075 name: "mylib2",
1076 srcs: ["mylib.cpp"],
1077 system_shared_libs: [],
1078 vendor_available: true,
1079 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001080 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001081 }
Jooyung Handc782442019-11-01 03:14:38 +09001082 `, func(fs map[string][]byte, config android.Config) {
1083 setUseVendorWhitelistForTest(config, []string{"myapex"})
1084 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001085
1086 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001087 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001088 for _, implicit := range i.Implicits {
1089 inputsList = append(inputsList, implicit.String())
1090 }
1091 }
1092 inputsString := strings.Join(inputsList, " ")
1093
1094 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001095 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1096 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001097
1098 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001099 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1100 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001101}
Jiyong Park16e91a02018-12-20 18:18:08 +09001102
Jooyung Handc782442019-11-01 03:14:38 +09001103func TestUseVendorRestriction(t *testing.T) {
1104 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1105 apex {
1106 name: "myapex",
1107 key: "myapex.key",
1108 use_vendor: true,
1109 }
1110 apex_key {
1111 name: "myapex.key",
1112 public_key: "testkey.avbpubkey",
1113 private_key: "testkey.pem",
1114 }
1115 `, func(fs map[string][]byte, config android.Config) {
1116 setUseVendorWhitelistForTest(config, []string{""})
1117 })
1118 // no error with whitelist
1119 testApex(t, `
1120 apex {
1121 name: "myapex",
1122 key: "myapex.key",
1123 use_vendor: true,
1124 }
1125 apex_key {
1126 name: "myapex.key",
1127 public_key: "testkey.avbpubkey",
1128 private_key: "testkey.pem",
1129 }
1130 `, func(fs map[string][]byte, config android.Config) {
1131 setUseVendorWhitelistForTest(config, []string{"myapex"})
1132 })
1133}
1134
Jooyung Han5c998b92019-06-27 11:30:33 +09001135func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1136 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1137 apex {
1138 name: "myapex",
1139 key: "myapex.key",
1140 native_shared_libs: ["mylib"],
1141 use_vendor: true,
1142 }
1143
1144 apex_key {
1145 name: "myapex.key",
1146 public_key: "testkey.avbpubkey",
1147 private_key: "testkey.pem",
1148 }
1149
1150 cc_library {
1151 name: "mylib",
1152 srcs: ["mylib.cpp"],
1153 system_shared_libs: [],
1154 stl: "none",
1155 }
1156 `)
1157}
1158
Jiyong Park16e91a02018-12-20 18:18:08 +09001159func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001160 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001161 apex {
1162 name: "myapex",
1163 key: "myapex.key",
1164 native_shared_libs: ["mylib"],
1165 }
1166
1167 apex_key {
1168 name: "myapex.key",
1169 public_key: "testkey.avbpubkey",
1170 private_key: "testkey.pem",
1171 }
1172
1173 cc_library {
1174 name: "mylib",
1175 srcs: ["mylib.cpp"],
1176 system_shared_libs: [],
1177 stl: "none",
1178 stubs: {
1179 versions: ["1", "2", "3"],
1180 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001181 apex_available: [
1182 "//apex_available:platform",
1183 "myapex",
1184 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001185 }
1186
1187 cc_binary {
1188 name: "not_in_apex",
1189 srcs: ["mylib.cpp"],
1190 static_libs: ["mylib"],
1191 static_executable: true,
1192 system_shared_libs: [],
1193 stl: "none",
1194 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001195 `)
1196
Colin Cross7113d202019-11-20 16:39:12 -08001197 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001198
1199 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001200 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001201}
Jiyong Park9335a262018-12-24 11:31:58 +09001202
1203func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001204 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001205 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001206 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001207 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001208 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001209 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001210 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001211 }
1212
1213 cc_library {
1214 name: "mylib",
1215 srcs: ["mylib.cpp"],
1216 system_shared_libs: [],
1217 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001218 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001219 }
1220
1221 apex_key {
1222 name: "myapex.key",
1223 public_key: "testkey.avbpubkey",
1224 private_key: "testkey.pem",
1225 }
1226
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001227 android_app_certificate {
1228 name: "myapex.certificate",
1229 certificate: "testkey",
1230 }
1231
1232 android_app_certificate {
1233 name: "myapex.certificate.override",
1234 certificate: "testkey.override",
1235 }
1236
Jiyong Park9335a262018-12-24 11:31:58 +09001237 `)
1238
1239 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001240 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001241
1242 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1243 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1244 "vendor/foo/devkeys/testkey.avbpubkey")
1245 }
1246 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1247 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1248 "vendor/foo/devkeys/testkey.pem")
1249 }
1250
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001251 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001252 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001253 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001254 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001255 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001256 }
1257}
Jiyong Park58e364a2019-01-19 19:24:06 +09001258
Jooyung Hanf121a652019-12-17 14:30:11 +09001259func TestCertificate(t *testing.T) {
1260 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1261 ctx, _ := testApex(t, `
1262 apex {
1263 name: "myapex",
1264 key: "myapex.key",
1265 }
1266 apex_key {
1267 name: "myapex.key",
1268 public_key: "testkey.avbpubkey",
1269 private_key: "testkey.pem",
1270 }`)
1271 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1272 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1273 if actual := rule.Args["certificates"]; actual != expected {
1274 t.Errorf("certificates should be %q, not %q", expected, actual)
1275 }
1276 })
1277 t.Run("override when unspecified", func(t *testing.T) {
1278 ctx, _ := testApex(t, `
1279 apex {
1280 name: "myapex_keytest",
1281 key: "myapex.key",
1282 file_contexts: ":myapex-file_contexts",
1283 }
1284 apex_key {
1285 name: "myapex.key",
1286 public_key: "testkey.avbpubkey",
1287 private_key: "testkey.pem",
1288 }
1289 android_app_certificate {
1290 name: "myapex.certificate.override",
1291 certificate: "testkey.override",
1292 }`)
1293 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1294 expected := "testkey.override.x509.pem testkey.override.pk8"
1295 if actual := rule.Args["certificates"]; actual != expected {
1296 t.Errorf("certificates should be %q, not %q", expected, actual)
1297 }
1298 })
1299 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1300 ctx, _ := testApex(t, `
1301 apex {
1302 name: "myapex",
1303 key: "myapex.key",
1304 certificate: ":myapex.certificate",
1305 }
1306 apex_key {
1307 name: "myapex.key",
1308 public_key: "testkey.avbpubkey",
1309 private_key: "testkey.pem",
1310 }
1311 android_app_certificate {
1312 name: "myapex.certificate",
1313 certificate: "testkey",
1314 }`)
1315 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1316 expected := "testkey.x509.pem testkey.pk8"
1317 if actual := rule.Args["certificates"]; actual != expected {
1318 t.Errorf("certificates should be %q, not %q", expected, actual)
1319 }
1320 })
1321 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1322 ctx, _ := testApex(t, `
1323 apex {
1324 name: "myapex_keytest",
1325 key: "myapex.key",
1326 file_contexts: ":myapex-file_contexts",
1327 certificate: ":myapex.certificate",
1328 }
1329 apex_key {
1330 name: "myapex.key",
1331 public_key: "testkey.avbpubkey",
1332 private_key: "testkey.pem",
1333 }
1334 android_app_certificate {
1335 name: "myapex.certificate.override",
1336 certificate: "testkey.override",
1337 }`)
1338 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1339 expected := "testkey.override.x509.pem testkey.override.pk8"
1340 if actual := rule.Args["certificates"]; actual != expected {
1341 t.Errorf("certificates should be %q, not %q", expected, actual)
1342 }
1343 })
1344 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1345 ctx, _ := testApex(t, `
1346 apex {
1347 name: "myapex",
1348 key: "myapex.key",
1349 certificate: "testkey",
1350 }
1351 apex_key {
1352 name: "myapex.key",
1353 public_key: "testkey.avbpubkey",
1354 private_key: "testkey.pem",
1355 }`)
1356 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1357 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1358 if actual := rule.Args["certificates"]; actual != expected {
1359 t.Errorf("certificates should be %q, not %q", expected, actual)
1360 }
1361 })
1362 t.Run("override when specified as <name>", func(t *testing.T) {
1363 ctx, _ := testApex(t, `
1364 apex {
1365 name: "myapex_keytest",
1366 key: "myapex.key",
1367 file_contexts: ":myapex-file_contexts",
1368 certificate: "testkey",
1369 }
1370 apex_key {
1371 name: "myapex.key",
1372 public_key: "testkey.avbpubkey",
1373 private_key: "testkey.pem",
1374 }
1375 android_app_certificate {
1376 name: "myapex.certificate.override",
1377 certificate: "testkey.override",
1378 }`)
1379 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1380 expected := "testkey.override.x509.pem testkey.override.pk8"
1381 if actual := rule.Args["certificates"]; actual != expected {
1382 t.Errorf("certificates should be %q, not %q", expected, actual)
1383 }
1384 })
1385}
1386
Jiyong Park58e364a2019-01-19 19:24:06 +09001387func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001388 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001389 apex {
1390 name: "myapex",
1391 key: "myapex.key",
1392 native_shared_libs: ["mylib"],
1393 }
1394
1395 apex {
1396 name: "otherapex",
1397 key: "myapex.key",
1398 native_shared_libs: ["mylib"],
1399 }
1400
1401 apex_key {
1402 name: "myapex.key",
1403 public_key: "testkey.avbpubkey",
1404 private_key: "testkey.pem",
1405 }
1406
1407 cc_library {
1408 name: "mylib",
1409 srcs: ["mylib.cpp"],
1410 system_shared_libs: [],
1411 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001412 // TODO: remove //apex_available:platform
1413 apex_available: [
1414 "//apex_available:platform",
1415 "myapex",
1416 "otherapex",
1417 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001418 }
1419 `)
1420
Jooyung Han6b8459b2019-10-30 08:29:25 +09001421 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001422 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001423 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001424 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1425 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001426
Jooyung Han6b8459b2019-10-30 08:29:25 +09001427 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001428 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001429 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001430 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1431 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001432
Jooyung Han6b8459b2019-10-30 08:29:25 +09001433 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001434 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001435 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001436 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1437 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001438}
Jiyong Park7e636d02019-01-28 16:16:54 +09001439
1440func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001441 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001442 apex {
1443 name: "myapex",
1444 key: "myapex.key",
1445 native_shared_libs: ["mylib"],
1446 }
1447
1448 apex_key {
1449 name: "myapex.key",
1450 public_key: "testkey.avbpubkey",
1451 private_key: "testkey.pem",
1452 }
1453
1454 cc_library_headers {
1455 name: "mylib_headers",
1456 export_include_dirs: ["my_include"],
1457 system_shared_libs: [],
1458 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001459 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001460 }
1461
1462 cc_library {
1463 name: "mylib",
1464 srcs: ["mylib.cpp"],
1465 system_shared_libs: [],
1466 stl: "none",
1467 header_libs: ["mylib_headers"],
1468 export_header_lib_headers: ["mylib_headers"],
1469 stubs: {
1470 versions: ["1", "2", "3"],
1471 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001472 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001473 }
1474
1475 cc_library {
1476 name: "otherlib",
1477 srcs: ["mylib.cpp"],
1478 system_shared_libs: [],
1479 stl: "none",
1480 shared_libs: ["mylib"],
1481 }
1482 `)
1483
Colin Cross7113d202019-11-20 16:39:12 -08001484 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001485
1486 // Ensure that the include path of the header lib is exported to 'otherlib'
1487 ensureContains(t, cFlags, "-Imy_include")
1488}
Alex Light9670d332019-01-29 18:07:33 -08001489
Jiyong Park7cd10e32020-01-14 09:22:18 +09001490type fileInApex struct {
1491 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001492 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001493 isLink bool
1494}
1495
Jooyung Hana57af4a2020-01-23 05:36:59 +00001496func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001497 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001498 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001499 copyCmds := apexRule.Args["copy_commands"]
1500 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001501 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001502 for _, cmd := range strings.Split(copyCmds, "&&") {
1503 cmd = strings.TrimSpace(cmd)
1504 if cmd == "" {
1505 continue
1506 }
1507 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001508 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001509 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001510 switch terms[0] {
1511 case "mkdir":
1512 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001513 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001514 t.Fatal("copyCmds contains invalid cp command", cmd)
1515 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001516 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001517 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001518 isLink = false
1519 case "ln":
1520 if len(terms) != 3 && len(terms) != 4 {
1521 // ln LINK TARGET or ln -s LINK TARGET
1522 t.Fatal("copyCmds contains invalid ln command", cmd)
1523 }
1524 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001525 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001526 isLink = true
1527 default:
1528 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1529 }
1530 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001531 index := strings.Index(dst, imageApexDir)
1532 if index == -1 {
1533 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1534 }
1535 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001536 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001537 }
1538 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001539 return ret
1540}
1541
Jooyung Hana57af4a2020-01-23 05:36:59 +00001542func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1543 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001544 var failed bool
1545 var surplus []string
1546 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001547 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001548 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001549 for _, expected := range files {
1550 if matched, _ := path.Match(expected, file.path); matched {
1551 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001552 mactchFound = true
1553 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001554 }
1555 }
Jooyung Hane6436d72020-02-27 13:31:56 +09001556 if !mactchFound {
1557 surplus = append(surplus, file.path)
1558 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001559 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001560
Jooyung Han31c470b2019-10-18 16:26:59 +09001561 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001562 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001563 t.Log("surplus files", surplus)
1564 failed = true
1565 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001566
1567 if len(files) > len(filesMatched) {
1568 var missing []string
1569 for _, expected := range files {
1570 if !filesMatched[expected] {
1571 missing = append(missing, expected)
1572 }
1573 }
1574 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001575 t.Log("missing files", missing)
1576 failed = true
1577 }
1578 if failed {
1579 t.Fail()
1580 }
1581}
1582
Jooyung Han344d5432019-08-23 11:17:39 +09001583func TestVndkApexCurrent(t *testing.T) {
1584 ctx, _ := testApex(t, `
1585 apex_vndk {
1586 name: "myapex",
1587 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001588 }
1589
1590 apex_key {
1591 name: "myapex.key",
1592 public_key: "testkey.avbpubkey",
1593 private_key: "testkey.pem",
1594 }
1595
1596 cc_library {
1597 name: "libvndk",
1598 srcs: ["mylib.cpp"],
1599 vendor_available: true,
1600 vndk: {
1601 enabled: 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 }
1607
1608 cc_library {
1609 name: "libvndksp",
1610 srcs: ["mylib.cpp"],
1611 vendor_available: true,
1612 vndk: {
1613 enabled: true,
1614 support_system_process: true,
1615 },
1616 system_shared_libs: [],
1617 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001618 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001619 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001620 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001621
Jooyung Hana57af4a2020-01-23 05:36:59 +00001622 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001623 "lib/libvndk.so",
1624 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001625 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09001626 "lib64/libvndk.so",
1627 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001628 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001629 "etc/llndk.libraries.VER.txt",
1630 "etc/vndkcore.libraries.VER.txt",
1631 "etc/vndksp.libraries.VER.txt",
1632 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001633 })
Jooyung Han344d5432019-08-23 11:17:39 +09001634}
1635
1636func TestVndkApexWithPrebuilt(t *testing.T) {
1637 ctx, _ := testApex(t, `
1638 apex_vndk {
1639 name: "myapex",
1640 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001641 }
1642
1643 apex_key {
1644 name: "myapex.key",
1645 public_key: "testkey.avbpubkey",
1646 private_key: "testkey.pem",
1647 }
1648
1649 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001650 name: "libvndk",
1651 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001652 vendor_available: true,
1653 vndk: {
1654 enabled: true,
1655 },
1656 system_shared_libs: [],
1657 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001658 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001659 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001660
1661 cc_prebuilt_library_shared {
1662 name: "libvndk.arm",
1663 srcs: ["libvndk.arm.so"],
1664 vendor_available: true,
1665 vndk: {
1666 enabled: true,
1667 },
1668 enabled: false,
1669 arch: {
1670 arm: {
1671 enabled: true,
1672 },
1673 },
1674 system_shared_libs: [],
1675 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001676 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001677 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001678 `+vndkLibrariesTxtFiles("current"),
1679 withFiles(map[string][]byte{
1680 "libvndk.so": nil,
1681 "libvndk.arm.so": nil,
1682 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001683
Jooyung Hana57af4a2020-01-23 05:36:59 +00001684 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001685 "lib/libvndk.so",
1686 "lib/libvndk.arm.so",
1687 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001688 "lib/libc++.so",
1689 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001690 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001691 })
Jooyung Han344d5432019-08-23 11:17:39 +09001692}
1693
Jooyung Han39edb6c2019-11-06 16:53:07 +09001694func vndkLibrariesTxtFiles(vers ...string) (result string) {
1695 for _, v := range vers {
1696 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001697 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001698 result += `
1699 vndk_libraries_txt {
1700 name: "` + txt + `.libraries.txt",
1701 }
1702 `
1703 }
1704 } else {
1705 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1706 result += `
1707 prebuilt_etc {
1708 name: "` + txt + `.libraries.` + v + `.txt",
1709 src: "dummy.txt",
1710 }
1711 `
1712 }
1713 }
1714 }
1715 return
1716}
1717
Jooyung Han344d5432019-08-23 11:17:39 +09001718func TestVndkApexVersion(t *testing.T) {
1719 ctx, _ := testApex(t, `
1720 apex_vndk {
1721 name: "myapex_v27",
1722 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001723 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001724 vndk_version: "27",
1725 }
1726
1727 apex_key {
1728 name: "myapex.key",
1729 public_key: "testkey.avbpubkey",
1730 private_key: "testkey.pem",
1731 }
1732
Jooyung Han31c470b2019-10-18 16:26:59 +09001733 vndk_prebuilt_shared {
1734 name: "libvndk27",
1735 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001736 vendor_available: true,
1737 vndk: {
1738 enabled: true,
1739 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001740 target_arch: "arm64",
1741 arch: {
1742 arm: {
1743 srcs: ["libvndk27_arm.so"],
1744 },
1745 arm64: {
1746 srcs: ["libvndk27_arm64.so"],
1747 },
1748 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001749 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001750 }
1751
1752 vndk_prebuilt_shared {
1753 name: "libvndk27",
1754 version: "27",
1755 vendor_available: true,
1756 vndk: {
1757 enabled: true,
1758 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001759 target_arch: "x86_64",
1760 arch: {
1761 x86: {
1762 srcs: ["libvndk27_x86.so"],
1763 },
1764 x86_64: {
1765 srcs: ["libvndk27_x86_64.so"],
1766 },
1767 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001768 }
1769 `+vndkLibrariesTxtFiles("27"),
1770 withFiles(map[string][]byte{
1771 "libvndk27_arm.so": nil,
1772 "libvndk27_arm64.so": nil,
1773 "libvndk27_x86.so": nil,
1774 "libvndk27_x86_64.so": nil,
1775 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001776
Jooyung Hana57af4a2020-01-23 05:36:59 +00001777 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001778 "lib/libvndk27_arm.so",
1779 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001780 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001781 })
Jooyung Han344d5432019-08-23 11:17:39 +09001782}
1783
1784func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1785 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1786 apex_vndk {
1787 name: "myapex_v27",
1788 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001789 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001790 vndk_version: "27",
1791 }
1792 apex_vndk {
1793 name: "myapex_v27_other",
1794 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001795 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001796 vndk_version: "27",
1797 }
1798
1799 apex_key {
1800 name: "myapex.key",
1801 public_key: "testkey.avbpubkey",
1802 private_key: "testkey.pem",
1803 }
1804
1805 cc_library {
1806 name: "libvndk",
1807 srcs: ["mylib.cpp"],
1808 vendor_available: true,
1809 vndk: {
1810 enabled: true,
1811 },
1812 system_shared_libs: [],
1813 stl: "none",
1814 }
1815
1816 vndk_prebuilt_shared {
1817 name: "libvndk",
1818 version: "27",
1819 vendor_available: true,
1820 vndk: {
1821 enabled: true,
1822 },
1823 srcs: ["libvndk.so"],
1824 }
1825 `, withFiles(map[string][]byte{
1826 "libvndk.so": nil,
1827 }))
1828}
1829
Jooyung Han90eee022019-10-01 20:02:42 +09001830func TestVndkApexNameRule(t *testing.T) {
1831 ctx, _ := testApex(t, `
1832 apex_vndk {
1833 name: "myapex",
1834 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001835 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001836 }
1837 apex_vndk {
1838 name: "myapex_v28",
1839 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001840 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001841 vndk_version: "28",
1842 }
1843 apex_key {
1844 name: "myapex.key",
1845 public_key: "testkey.avbpubkey",
1846 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001847 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001848
1849 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001850 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001851 actual := proptools.String(bundle.properties.Apex_name)
1852 if !reflect.DeepEqual(actual, expected) {
1853 t.Errorf("Got '%v', expected '%v'", actual, expected)
1854 }
1855 }
1856
1857 assertApexName("com.android.vndk.vVER", "myapex")
1858 assertApexName("com.android.vndk.v28", "myapex_v28")
1859}
1860
Jooyung Han344d5432019-08-23 11:17:39 +09001861func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1862 ctx, _ := testApex(t, `
1863 apex_vndk {
1864 name: "myapex",
1865 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001866 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001867 }
1868
1869 apex_key {
1870 name: "myapex.key",
1871 public_key: "testkey.avbpubkey",
1872 private_key: "testkey.pem",
1873 }
1874
1875 cc_library {
1876 name: "libvndk",
1877 srcs: ["mylib.cpp"],
1878 vendor_available: true,
1879 native_bridge_supported: true,
1880 host_supported: true,
1881 vndk: {
1882 enabled: true,
1883 },
1884 system_shared_libs: [],
1885 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001886 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001887 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001888 `+vndkLibrariesTxtFiles("current"),
1889 withTargets(map[android.OsType][]android.Target{
1890 android.Android: []android.Target{
1891 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1892 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1893 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1894 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1895 },
1896 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001897
Jooyung Hana57af4a2020-01-23 05:36:59 +00001898 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001899 "lib/libvndk.so",
1900 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001901 "lib/libc++.so",
1902 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001903 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001904 })
Jooyung Han344d5432019-08-23 11:17:39 +09001905}
1906
1907func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1908 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1909 apex_vndk {
1910 name: "myapex",
1911 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001912 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001913 native_bridge_supported: true,
1914 }
1915
1916 apex_key {
1917 name: "myapex.key",
1918 public_key: "testkey.avbpubkey",
1919 private_key: "testkey.pem",
1920 }
1921
1922 cc_library {
1923 name: "libvndk",
1924 srcs: ["mylib.cpp"],
1925 vendor_available: true,
1926 native_bridge_supported: true,
1927 host_supported: true,
1928 vndk: {
1929 enabled: true,
1930 },
1931 system_shared_libs: [],
1932 stl: "none",
1933 }
1934 `)
1935}
1936
Jooyung Han31c470b2019-10-18 16:26:59 +09001937func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001938 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001939 apex_vndk {
1940 name: "myapex_v27",
1941 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001942 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001943 vndk_version: "27",
1944 }
1945
1946 apex_key {
1947 name: "myapex.key",
1948 public_key: "testkey.avbpubkey",
1949 private_key: "testkey.pem",
1950 }
1951
1952 vndk_prebuilt_shared {
1953 name: "libvndk27",
1954 version: "27",
1955 target_arch: "arm",
1956 vendor_available: true,
1957 vndk: {
1958 enabled: true,
1959 },
1960 arch: {
1961 arm: {
1962 srcs: ["libvndk27.so"],
1963 }
1964 },
1965 }
1966
1967 vndk_prebuilt_shared {
1968 name: "libvndk27",
1969 version: "27",
1970 target_arch: "arm",
1971 binder32bit: true,
1972 vendor_available: true,
1973 vndk: {
1974 enabled: true,
1975 },
1976 arch: {
1977 arm: {
1978 srcs: ["libvndk27binder32.so"],
1979 }
1980 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001981 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001982 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001983 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001984 withFiles(map[string][]byte{
1985 "libvndk27.so": nil,
1986 "libvndk27binder32.so": nil,
1987 }),
1988 withBinder32bit,
1989 withTargets(map[android.OsType][]android.Target{
1990 android.Android: []android.Target{
1991 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1992 },
1993 }),
1994 )
1995
Jooyung Hana57af4a2020-01-23 05:36:59 +00001996 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001997 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001998 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001999 })
2000}
2001
Jooyung Hane1633032019-08-01 17:41:43 +09002002func TestDependenciesInApexManifest(t *testing.T) {
2003 ctx, _ := testApex(t, `
2004 apex {
2005 name: "myapex_nodep",
2006 key: "myapex.key",
2007 native_shared_libs: ["lib_nodep"],
2008 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002009 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002010 }
2011
2012 apex {
2013 name: "myapex_dep",
2014 key: "myapex.key",
2015 native_shared_libs: ["lib_dep"],
2016 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002017 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002018 }
2019
2020 apex {
2021 name: "myapex_provider",
2022 key: "myapex.key",
2023 native_shared_libs: ["libfoo"],
2024 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002025 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002026 }
2027
2028 apex {
2029 name: "myapex_selfcontained",
2030 key: "myapex.key",
2031 native_shared_libs: ["lib_dep", "libfoo"],
2032 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002033 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002034 }
2035
2036 apex_key {
2037 name: "myapex.key",
2038 public_key: "testkey.avbpubkey",
2039 private_key: "testkey.pem",
2040 }
2041
2042 cc_library {
2043 name: "lib_nodep",
2044 srcs: ["mylib.cpp"],
2045 system_shared_libs: [],
2046 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002047 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002048 }
2049
2050 cc_library {
2051 name: "lib_dep",
2052 srcs: ["mylib.cpp"],
2053 shared_libs: ["libfoo"],
2054 system_shared_libs: [],
2055 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002056 apex_available: [
2057 "myapex_dep",
2058 "myapex_provider",
2059 "myapex_selfcontained",
2060 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002061 }
2062
2063 cc_library {
2064 name: "libfoo",
2065 srcs: ["mytest.cpp"],
2066 stubs: {
2067 versions: ["1"],
2068 },
2069 system_shared_libs: [],
2070 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002071 apex_available: [
2072 "myapex_provider",
2073 "myapex_selfcontained",
2074 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002075 }
2076 `)
2077
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002078 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002079 var provideNativeLibs, requireNativeLibs []string
2080
Sundong Ahnabb64432019-10-22 13:58:29 +09002081 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002082 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2083 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002084 ensureListEmpty(t, provideNativeLibs)
2085 ensureListEmpty(t, requireNativeLibs)
2086
Sundong Ahnabb64432019-10-22 13:58:29 +09002087 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002088 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2089 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002090 ensureListEmpty(t, provideNativeLibs)
2091 ensureListContains(t, requireNativeLibs, "libfoo.so")
2092
Sundong Ahnabb64432019-10-22 13:58:29 +09002093 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002094 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2095 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002096 ensureListContains(t, provideNativeLibs, "libfoo.so")
2097 ensureListEmpty(t, requireNativeLibs)
2098
Sundong Ahnabb64432019-10-22 13:58:29 +09002099 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002100 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2101 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002102 ensureListContains(t, provideNativeLibs, "libfoo.so")
2103 ensureListEmpty(t, requireNativeLibs)
2104}
2105
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002106func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002107 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002108 apex {
2109 name: "myapex",
2110 key: "myapex.key",
2111 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002112 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002113 }
2114
2115 apex_key {
2116 name: "myapex.key",
2117 public_key: "testkey.avbpubkey",
2118 private_key: "testkey.pem",
2119 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002120
2121 cc_library {
2122 name: "mylib",
2123 srcs: ["mylib.cpp"],
2124 system_shared_libs: [],
2125 stl: "none",
2126 apex_available: [
2127 "//apex_available:platform",
2128 "myapex",
2129 ],
2130 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002131 `)
2132
Sundong Ahnabb64432019-10-22 13:58:29 +09002133 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002134 apexManifestRule := module.Rule("apexManifestRule")
2135 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2136 apexRule := module.Rule("apexRule")
2137 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002138
2139 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2140 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2141 name := apexBundle.BaseModuleName()
2142 prefix := "TARGET_"
2143 var builder strings.Builder
2144 data.Custom(&builder, name, prefix, "", data)
2145 androidMk := builder.String()
2146 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2147 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002148}
2149
Alex Light0851b882019-02-07 13:20:53 -08002150func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002151 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002152 apex {
2153 name: "myapex",
2154 key: "myapex.key",
2155 native_shared_libs: ["mylib_common"],
2156 }
2157
2158 apex_key {
2159 name: "myapex.key",
2160 public_key: "testkey.avbpubkey",
2161 private_key: "testkey.pem",
2162 }
2163
2164 cc_library {
2165 name: "mylib_common",
2166 srcs: ["mylib.cpp"],
2167 system_shared_libs: [],
2168 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002169 apex_available: [
2170 "//apex_available:platform",
2171 "myapex",
2172 ],
Alex Light0851b882019-02-07 13:20:53 -08002173 }
2174 `)
2175
Sundong Ahnabb64432019-10-22 13:58:29 +09002176 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002177 apexRule := module.Rule("apexRule")
2178 copyCmds := apexRule.Args["copy_commands"]
2179
2180 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2181 t.Log("Apex was a test apex!")
2182 t.Fail()
2183 }
2184 // Ensure that main rule creates an output
2185 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2186
2187 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002188 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002189
2190 // Ensure that both direct and indirect deps are copied into apex
2191 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2192
Colin Cross7113d202019-11-20 16:39:12 -08002193 // Ensure that the platform variant ends with _shared
2194 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002195
2196 if !android.InAnyApex("mylib_common") {
2197 t.Log("Found mylib_common not in any apex!")
2198 t.Fail()
2199 }
2200}
2201
2202func TestTestApex(t *testing.T) {
2203 if android.InAnyApex("mylib_common_test") {
2204 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!")
2205 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002206 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002207 apex_test {
2208 name: "myapex",
2209 key: "myapex.key",
2210 native_shared_libs: ["mylib_common_test"],
2211 }
2212
2213 apex_key {
2214 name: "myapex.key",
2215 public_key: "testkey.avbpubkey",
2216 private_key: "testkey.pem",
2217 }
2218
2219 cc_library {
2220 name: "mylib_common_test",
2221 srcs: ["mylib.cpp"],
2222 system_shared_libs: [],
2223 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002224 // TODO: remove //apex_available:platform
2225 apex_available: [
2226 "//apex_available:platform",
2227 "myapex",
2228 ],
Alex Light0851b882019-02-07 13:20:53 -08002229 }
2230 `)
2231
Sundong Ahnabb64432019-10-22 13:58:29 +09002232 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002233 apexRule := module.Rule("apexRule")
2234 copyCmds := apexRule.Args["copy_commands"]
2235
2236 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2237 t.Log("Apex was not a test apex!")
2238 t.Fail()
2239 }
2240 // Ensure that main rule creates an output
2241 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2242
2243 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002244 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002245
2246 // Ensure that both direct and indirect deps are copied into apex
2247 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2248
Colin Cross7113d202019-11-20 16:39:12 -08002249 // Ensure that the platform variant ends with _shared
2250 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002251}
2252
Alex Light9670d332019-01-29 18:07:33 -08002253func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002254 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002255 apex {
2256 name: "myapex",
2257 key: "myapex.key",
2258 multilib: {
2259 first: {
2260 native_shared_libs: ["mylib_common"],
2261 }
2262 },
2263 target: {
2264 android: {
2265 multilib: {
2266 first: {
2267 native_shared_libs: ["mylib"],
2268 }
2269 }
2270 },
2271 host: {
2272 multilib: {
2273 first: {
2274 native_shared_libs: ["mylib2"],
2275 }
2276 }
2277 }
2278 }
2279 }
2280
2281 apex_key {
2282 name: "myapex.key",
2283 public_key: "testkey.avbpubkey",
2284 private_key: "testkey.pem",
2285 }
2286
2287 cc_library {
2288 name: "mylib",
2289 srcs: ["mylib.cpp"],
2290 system_shared_libs: [],
2291 stl: "none",
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: "mylib_common",
2301 srcs: ["mylib.cpp"],
2302 system_shared_libs: [],
2303 stl: "none",
2304 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002305 // TODO: remove //apex_available:platform
2306 apex_available: [
2307 "//apex_available:platform",
2308 "myapex",
2309 ],
Alex Light9670d332019-01-29 18:07:33 -08002310 }
2311
2312 cc_library {
2313 name: "mylib2",
2314 srcs: ["mylib.cpp"],
2315 system_shared_libs: [],
2316 stl: "none",
2317 compile_multilib: "first",
2318 }
2319 `)
2320
Sundong Ahnabb64432019-10-22 13:58:29 +09002321 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002322 copyCmds := apexRule.Args["copy_commands"]
2323
2324 // Ensure that main rule creates an output
2325 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2326
2327 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002328 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2329 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2330 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002331
2332 // Ensure that both direct and indirect deps are copied into apex
2333 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2334 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2335 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2336
Colin Cross7113d202019-11-20 16:39:12 -08002337 // Ensure that the platform variant ends with _shared
2338 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2339 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2340 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002341}
Jiyong Park04480cf2019-02-06 00:16:29 +09002342
2343func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002344 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002345 apex {
2346 name: "myapex",
2347 key: "myapex.key",
2348 binaries: ["myscript"],
2349 }
2350
2351 apex_key {
2352 name: "myapex.key",
2353 public_key: "testkey.avbpubkey",
2354 private_key: "testkey.pem",
2355 }
2356
2357 sh_binary {
2358 name: "myscript",
2359 src: "mylib.cpp",
2360 filename: "myscript.sh",
2361 sub_dir: "script",
2362 }
2363 `)
2364
Sundong Ahnabb64432019-10-22 13:58:29 +09002365 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002366 copyCmds := apexRule.Args["copy_commands"]
2367
2368 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2369}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002370
Jooyung Han91df2082019-11-20 01:49:42 +09002371func TestApexInVariousPartition(t *testing.T) {
2372 testcases := []struct {
2373 propName, parition, flattenedPartition string
2374 }{
2375 {"", "system", "system_ext"},
2376 {"product_specific: true", "product", "product"},
2377 {"soc_specific: true", "vendor", "vendor"},
2378 {"proprietary: true", "vendor", "vendor"},
2379 {"vendor: true", "vendor", "vendor"},
2380 {"system_ext_specific: true", "system_ext", "system_ext"},
2381 }
2382 for _, tc := range testcases {
2383 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2384 ctx, _ := testApex(t, `
2385 apex {
2386 name: "myapex",
2387 key: "myapex.key",
2388 `+tc.propName+`
2389 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002390
Jooyung Han91df2082019-11-20 01:49:42 +09002391 apex_key {
2392 name: "myapex.key",
2393 public_key: "testkey.avbpubkey",
2394 private_key: "testkey.pem",
2395 }
2396 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002397
Jooyung Han91df2082019-11-20 01:49:42 +09002398 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2399 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2400 actual := apex.installDir.String()
2401 if actual != expected {
2402 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2403 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002404
Jooyung Han91df2082019-11-20 01:49:42 +09002405 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2406 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2407 actual = flattened.installDir.String()
2408 if actual != expected {
2409 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2410 }
2411 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002412 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002413}
Jiyong Park67882562019-03-21 01:11:21 +09002414
Jooyung Han54aca7b2019-11-20 02:26:02 +09002415func TestFileContexts(t *testing.T) {
2416 ctx, _ := testApex(t, `
2417 apex {
2418 name: "myapex",
2419 key: "myapex.key",
2420 }
2421
2422 apex_key {
2423 name: "myapex.key",
2424 public_key: "testkey.avbpubkey",
2425 private_key: "testkey.pem",
2426 }
2427 `)
2428 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2429 apexRule := module.Rule("apexRule")
2430 actual := apexRule.Args["file_contexts"]
2431 expected := "system/sepolicy/apex/myapex-file_contexts"
2432 if actual != expected {
2433 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2434 }
2435
2436 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2437 apex {
2438 name: "myapex",
2439 key: "myapex.key",
2440 file_contexts: "my_own_file_contexts",
2441 }
2442
2443 apex_key {
2444 name: "myapex.key",
2445 public_key: "testkey.avbpubkey",
2446 private_key: "testkey.pem",
2447 }
2448 `, withFiles(map[string][]byte{
2449 "my_own_file_contexts": nil,
2450 }))
2451
2452 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2453 apex {
2454 name: "myapex",
2455 key: "myapex.key",
2456 product_specific: true,
2457 file_contexts: "product_specific_file_contexts",
2458 }
2459
2460 apex_key {
2461 name: "myapex.key",
2462 public_key: "testkey.avbpubkey",
2463 private_key: "testkey.pem",
2464 }
2465 `)
2466
2467 ctx, _ = testApex(t, `
2468 apex {
2469 name: "myapex",
2470 key: "myapex.key",
2471 product_specific: true,
2472 file_contexts: "product_specific_file_contexts",
2473 }
2474
2475 apex_key {
2476 name: "myapex.key",
2477 public_key: "testkey.avbpubkey",
2478 private_key: "testkey.pem",
2479 }
2480 `, withFiles(map[string][]byte{
2481 "product_specific_file_contexts": nil,
2482 }))
2483 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2484 apexRule = module.Rule("apexRule")
2485 actual = apexRule.Args["file_contexts"]
2486 expected = "product_specific_file_contexts"
2487 if actual != expected {
2488 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2489 }
2490
2491 ctx, _ = testApex(t, `
2492 apex {
2493 name: "myapex",
2494 key: "myapex.key",
2495 product_specific: true,
2496 file_contexts: ":my-file-contexts",
2497 }
2498
2499 apex_key {
2500 name: "myapex.key",
2501 public_key: "testkey.avbpubkey",
2502 private_key: "testkey.pem",
2503 }
2504
2505 filegroup {
2506 name: "my-file-contexts",
2507 srcs: ["product_specific_file_contexts"],
2508 }
2509 `, withFiles(map[string][]byte{
2510 "product_specific_file_contexts": nil,
2511 }))
2512 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2513 apexRule = module.Rule("apexRule")
2514 actual = apexRule.Args["file_contexts"]
2515 expected = "product_specific_file_contexts"
2516 if actual != expected {
2517 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2518 }
2519}
2520
Jiyong Park67882562019-03-21 01:11:21 +09002521func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002522 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002523 apex_key {
2524 name: "myapex.key",
2525 public_key: ":my.avbpubkey",
2526 private_key: ":my.pem",
2527 product_specific: true,
2528 }
2529
2530 filegroup {
2531 name: "my.avbpubkey",
2532 srcs: ["testkey2.avbpubkey"],
2533 }
2534
2535 filegroup {
2536 name: "my.pem",
2537 srcs: ["testkey2.pem"],
2538 }
2539 `)
2540
2541 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2542 expected_pubkey := "testkey2.avbpubkey"
2543 actual_pubkey := apex_key.public_key_file.String()
2544 if actual_pubkey != expected_pubkey {
2545 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2546 }
2547 expected_privkey := "testkey2.pem"
2548 actual_privkey := apex_key.private_key_file.String()
2549 if actual_privkey != expected_privkey {
2550 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2551 }
2552}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002553
2554func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002555 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002556 prebuilt_apex {
2557 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002558 arch: {
2559 arm64: {
2560 src: "myapex-arm64.apex",
2561 },
2562 arm: {
2563 src: "myapex-arm.apex",
2564 },
2565 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002566 }
2567 `)
2568
2569 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2570
Jiyong Parkc95714e2019-03-29 14:23:10 +09002571 expectedInput := "myapex-arm64.apex"
2572 if prebuilt.inputApex.String() != expectedInput {
2573 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2574 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002575}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002576
2577func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002578 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002579 prebuilt_apex {
2580 name: "myapex",
2581 src: "myapex-arm.apex",
2582 filename: "notmyapex.apex",
2583 }
2584 `)
2585
2586 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2587
2588 expected := "notmyapex.apex"
2589 if p.installFilename != expected {
2590 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2591 }
2592}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002593
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002594func TestPrebuiltOverrides(t *testing.T) {
2595 ctx, config := testApex(t, `
2596 prebuilt_apex {
2597 name: "myapex.prebuilt",
2598 src: "myapex-arm.apex",
2599 overrides: [
2600 "myapex",
2601 ],
2602 }
2603 `)
2604
2605 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2606
2607 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002608 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002609 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002610 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002611 }
2612}
2613
Roland Levillain630846d2019-06-26 12:48:34 +01002614func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002615 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002616 apex_test {
2617 name: "myapex",
2618 key: "myapex.key",
2619 tests: [
2620 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002621 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002622 ],
2623 }
2624
2625 apex_key {
2626 name: "myapex.key",
2627 public_key: "testkey.avbpubkey",
2628 private_key: "testkey.pem",
2629 }
2630
2631 cc_test {
2632 name: "mytest",
2633 gtest: false,
2634 srcs: ["mytest.cpp"],
2635 relative_install_path: "test",
2636 system_shared_libs: [],
2637 static_executable: true,
2638 stl: "none",
2639 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002640
2641 cc_test {
2642 name: "mytests",
2643 gtest: false,
2644 srcs: [
2645 "mytest1.cpp",
2646 "mytest2.cpp",
2647 "mytest3.cpp",
2648 ],
2649 test_per_src: true,
2650 relative_install_path: "test",
2651 system_shared_libs: [],
2652 static_executable: true,
2653 stl: "none",
2654 }
Roland Levillain630846d2019-06-26 12:48:34 +01002655 `)
2656
Sundong Ahnabb64432019-10-22 13:58:29 +09002657 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002658 copyCmds := apexRule.Args["copy_commands"]
2659
2660 // Ensure that test dep is copied into apex.
2661 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002662
2663 // Ensure that test deps built with `test_per_src` are copied into apex.
2664 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2665 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2666 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002667
2668 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002669 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002670 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2671 name := apexBundle.BaseModuleName()
2672 prefix := "TARGET_"
2673 var builder strings.Builder
2674 data.Custom(&builder, name, prefix, "", data)
2675 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002676 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2677 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2678 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2679 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002680 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002681 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002682 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002683}
2684
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002685func TestInstallExtraFlattenedApexes(t *testing.T) {
2686 ctx, config := testApex(t, `
2687 apex {
2688 name: "myapex",
2689 key: "myapex.key",
2690 }
2691 apex_key {
2692 name: "myapex.key",
2693 public_key: "testkey.avbpubkey",
2694 private_key: "testkey.pem",
2695 }
2696 `, func(fs map[string][]byte, config android.Config) {
2697 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2698 })
2699 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002700 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002701 mk := android.AndroidMkDataForTest(t, config, "", ab)
2702 var builder strings.Builder
2703 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2704 androidMk := builder.String()
2705 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2706}
2707
Jooyung Han5c998b92019-06-27 11:30:33 +09002708func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002709 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002710 apex {
2711 name: "myapex",
2712 key: "myapex.key",
2713 native_shared_libs: ["mylib"],
2714 uses: ["commonapex"],
2715 }
2716
2717 apex {
2718 name: "commonapex",
2719 key: "myapex.key",
2720 native_shared_libs: ["libcommon"],
2721 provide_cpp_shared_libs: true,
2722 }
2723
2724 apex_key {
2725 name: "myapex.key",
2726 public_key: "testkey.avbpubkey",
2727 private_key: "testkey.pem",
2728 }
2729
2730 cc_library {
2731 name: "mylib",
2732 srcs: ["mylib.cpp"],
2733 shared_libs: ["libcommon"],
2734 system_shared_libs: [],
2735 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002736 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002737 }
2738
2739 cc_library {
2740 name: "libcommon",
2741 srcs: ["mylib_common.cpp"],
2742 system_shared_libs: [],
2743 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002744 // TODO: remove //apex_available:platform
2745 apex_available: [
2746 "//apex_available:platform",
2747 "commonapex",
2748 "myapex",
2749 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002750 }
2751 `)
2752
Sundong Ahnabb64432019-10-22 13:58:29 +09002753 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002754 apexRule1 := module1.Rule("apexRule")
2755 copyCmds1 := apexRule1.Args["copy_commands"]
2756
Sundong Ahnabb64432019-10-22 13:58:29 +09002757 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002758 apexRule2 := module2.Rule("apexRule")
2759 copyCmds2 := apexRule2.Args["copy_commands"]
2760
Colin Cross7113d202019-11-20 16:39:12 -08002761 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2762 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002763 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2764 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2765 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2766}
2767
2768func TestApexUsesFailsIfNotProvided(t *testing.T) {
2769 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2770 apex {
2771 name: "myapex",
2772 key: "myapex.key",
2773 uses: ["commonapex"],
2774 }
2775
2776 apex {
2777 name: "commonapex",
2778 key: "myapex.key",
2779 }
2780
2781 apex_key {
2782 name: "myapex.key",
2783 public_key: "testkey.avbpubkey",
2784 private_key: "testkey.pem",
2785 }
2786 `)
2787 testApexError(t, `uses: "commonapex" is not a provider`, `
2788 apex {
2789 name: "myapex",
2790 key: "myapex.key",
2791 uses: ["commonapex"],
2792 }
2793
2794 cc_library {
2795 name: "commonapex",
2796 system_shared_libs: [],
2797 stl: "none",
2798 }
2799
2800 apex_key {
2801 name: "myapex.key",
2802 public_key: "testkey.avbpubkey",
2803 private_key: "testkey.pem",
2804 }
2805 `)
2806}
2807
2808func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2809 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2810 apex {
2811 name: "myapex",
2812 key: "myapex.key",
2813 use_vendor: true,
2814 uses: ["commonapex"],
2815 }
2816
2817 apex {
2818 name: "commonapex",
2819 key: "myapex.key",
2820 provide_cpp_shared_libs: true,
2821 }
2822
2823 apex_key {
2824 name: "myapex.key",
2825 public_key: "testkey.avbpubkey",
2826 private_key: "testkey.pem",
2827 }
Jooyung Handc782442019-11-01 03:14:38 +09002828 `, func(fs map[string][]byte, config android.Config) {
2829 setUseVendorWhitelistForTest(config, []string{"myapex"})
2830 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002831}
2832
Jooyung Hand48f3c32019-08-23 11:18:57 +09002833func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2834 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2835 apex {
2836 name: "myapex",
2837 key: "myapex.key",
2838 native_shared_libs: ["libfoo"],
2839 }
2840
2841 apex_key {
2842 name: "myapex.key",
2843 public_key: "testkey.avbpubkey",
2844 private_key: "testkey.pem",
2845 }
2846
2847 cc_library {
2848 name: "libfoo",
2849 stl: "none",
2850 system_shared_libs: [],
2851 enabled: false,
2852 }
2853 `)
2854 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2855 apex {
2856 name: "myapex",
2857 key: "myapex.key",
2858 java_libs: ["myjar"],
2859 }
2860
2861 apex_key {
2862 name: "myapex.key",
2863 public_key: "testkey.avbpubkey",
2864 private_key: "testkey.pem",
2865 }
2866
2867 java_library {
2868 name: "myjar",
2869 srcs: ["foo/bar/MyClass.java"],
2870 sdk_version: "none",
2871 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09002872 enabled: false,
2873 }
2874 `)
2875}
2876
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002877func TestApexWithApps(t *testing.T) {
2878 ctx, _ := testApex(t, `
2879 apex {
2880 name: "myapex",
2881 key: "myapex.key",
2882 apps: [
2883 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002884 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002885 ],
2886 }
2887
2888 apex_key {
2889 name: "myapex.key",
2890 public_key: "testkey.avbpubkey",
2891 private_key: "testkey.pem",
2892 }
2893
2894 android_app {
2895 name: "AppFoo",
2896 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08002897 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002898 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002899 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08002900 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002901 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002902 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002903
2904 android_app {
2905 name: "AppFooPriv",
2906 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08002907 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09002908 system_modules: "none",
2909 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08002910 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002911 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09002912 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002913
2914 cc_library_shared {
2915 name: "libjni",
2916 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002917 shared_libs: ["libfoo"],
2918 stl: "none",
2919 system_shared_libs: [],
2920 apex_available: [ "myapex" ],
2921 sdk_version: "current",
2922 }
2923
2924 cc_library_shared {
2925 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09002926 stl: "none",
2927 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09002928 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08002929 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09002930 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002931 `)
2932
Sundong Ahnabb64432019-10-22 13:58:29 +09002933 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002934 apexRule := module.Rule("apexRule")
2935 copyCmds := apexRule.Args["copy_commands"]
2936
2937 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002938 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002939
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002940 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
2941 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09002942 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002943 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002944 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002945 // JNI libraries including transitive deps are
2946 for _, jni := range []string{"libjni", "libfoo"} {
2947 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
2948 // ... embedded inside APK (jnilibs.zip)
2949 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
2950 // ... and not directly inside the APEX
2951 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
2952 }
Dario Frenicde2a032019-10-27 00:29:22 +01002953}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002954
Dario Frenicde2a032019-10-27 00:29:22 +01002955func TestApexWithAppImports(t *testing.T) {
2956 ctx, _ := testApex(t, `
2957 apex {
2958 name: "myapex",
2959 key: "myapex.key",
2960 apps: [
2961 "AppFooPrebuilt",
2962 "AppFooPrivPrebuilt",
2963 ],
2964 }
2965
2966 apex_key {
2967 name: "myapex.key",
2968 public_key: "testkey.avbpubkey",
2969 private_key: "testkey.pem",
2970 }
2971
2972 android_app_import {
2973 name: "AppFooPrebuilt",
2974 apk: "PrebuiltAppFoo.apk",
2975 presigned: true,
2976 dex_preopt: {
2977 enabled: false,
2978 },
2979 }
2980
2981 android_app_import {
2982 name: "AppFooPrivPrebuilt",
2983 apk: "PrebuiltAppFooPriv.apk",
2984 privileged: true,
2985 presigned: true,
2986 dex_preopt: {
2987 enabled: false,
2988 },
2989 }
2990 `)
2991
Sundong Ahnabb64432019-10-22 13:58:29 +09002992 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002993 apexRule := module.Rule("apexRule")
2994 copyCmds := apexRule.Args["copy_commands"]
2995
2996 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2997 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002998}
2999
Dario Freni6f3937c2019-12-20 22:58:03 +00003000func TestApexWithTestHelperApp(t *testing.T) {
3001 ctx, _ := testApex(t, `
3002 apex {
3003 name: "myapex",
3004 key: "myapex.key",
3005 apps: [
3006 "TesterHelpAppFoo",
3007 ],
3008 }
3009
3010 apex_key {
3011 name: "myapex.key",
3012 public_key: "testkey.avbpubkey",
3013 private_key: "testkey.pem",
3014 }
3015
3016 android_test_helper_app {
3017 name: "TesterHelpAppFoo",
3018 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003019 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003020 }
3021
3022 `)
3023
3024 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3025 apexRule := module.Rule("apexRule")
3026 copyCmds := apexRule.Args["copy_commands"]
3027
3028 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3029}
3030
Jooyung Han18020ea2019-11-13 10:50:48 +09003031func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3032 // libfoo's apex_available comes from cc_defaults
3033 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3034 apex {
3035 name: "myapex",
3036 key: "myapex.key",
3037 native_shared_libs: ["libfoo"],
3038 }
3039
3040 apex_key {
3041 name: "myapex.key",
3042 public_key: "testkey.avbpubkey",
3043 private_key: "testkey.pem",
3044 }
3045
3046 apex {
3047 name: "otherapex",
3048 key: "myapex.key",
3049 native_shared_libs: ["libfoo"],
3050 }
3051
3052 cc_defaults {
3053 name: "libfoo-defaults",
3054 apex_available: ["otherapex"],
3055 }
3056
3057 cc_library {
3058 name: "libfoo",
3059 defaults: ["libfoo-defaults"],
3060 stl: "none",
3061 system_shared_libs: [],
3062 }`)
3063}
3064
Jiyong Park127b40b2019-09-30 16:04:35 +09003065func TestApexAvailable(t *testing.T) {
3066 // libfoo is not available to myapex, but only to otherapex
3067 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3068 apex {
3069 name: "myapex",
3070 key: "myapex.key",
3071 native_shared_libs: ["libfoo"],
3072 }
3073
3074 apex_key {
3075 name: "myapex.key",
3076 public_key: "testkey.avbpubkey",
3077 private_key: "testkey.pem",
3078 }
3079
3080 apex {
3081 name: "otherapex",
3082 key: "otherapex.key",
3083 native_shared_libs: ["libfoo"],
3084 }
3085
3086 apex_key {
3087 name: "otherapex.key",
3088 public_key: "testkey.avbpubkey",
3089 private_key: "testkey.pem",
3090 }
3091
3092 cc_library {
3093 name: "libfoo",
3094 stl: "none",
3095 system_shared_libs: [],
3096 apex_available: ["otherapex"],
3097 }`)
3098
3099 // libbar is an indirect dep
3100 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3101 apex {
3102 name: "myapex",
3103 key: "myapex.key",
3104 native_shared_libs: ["libfoo"],
3105 }
3106
3107 apex_key {
3108 name: "myapex.key",
3109 public_key: "testkey.avbpubkey",
3110 private_key: "testkey.pem",
3111 }
3112
3113 apex {
3114 name: "otherapex",
3115 key: "otherapex.key",
3116 native_shared_libs: ["libfoo"],
3117 }
3118
3119 apex_key {
3120 name: "otherapex.key",
3121 public_key: "testkey.avbpubkey",
3122 private_key: "testkey.pem",
3123 }
3124
3125 cc_library {
3126 name: "libfoo",
3127 stl: "none",
3128 shared_libs: ["libbar"],
3129 system_shared_libs: [],
3130 apex_available: ["myapex", "otherapex"],
3131 }
3132
3133 cc_library {
3134 name: "libbar",
3135 stl: "none",
3136 system_shared_libs: [],
3137 apex_available: ["otherapex"],
3138 }`)
3139
3140 testApexError(t, "\"otherapex\" is not a valid module name", `
3141 apex {
3142 name: "myapex",
3143 key: "myapex.key",
3144 native_shared_libs: ["libfoo"],
3145 }
3146
3147 apex_key {
3148 name: "myapex.key",
3149 public_key: "testkey.avbpubkey",
3150 private_key: "testkey.pem",
3151 }
3152
3153 cc_library {
3154 name: "libfoo",
3155 stl: "none",
3156 system_shared_libs: [],
3157 apex_available: ["otherapex"],
3158 }`)
3159
3160 ctx, _ := testApex(t, `
3161 apex {
3162 name: "myapex",
3163 key: "myapex.key",
3164 native_shared_libs: ["libfoo", "libbar"],
3165 }
3166
3167 apex_key {
3168 name: "myapex.key",
3169 public_key: "testkey.avbpubkey",
3170 private_key: "testkey.pem",
3171 }
3172
3173 cc_library {
3174 name: "libfoo",
3175 stl: "none",
3176 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09003177 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003178 apex_available: ["myapex"],
3179 }
3180
3181 cc_library {
3182 name: "libbar",
3183 stl: "none",
3184 system_shared_libs: [],
3185 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09003186 }
3187
3188 cc_library {
3189 name: "libbaz",
3190 stl: "none",
3191 system_shared_libs: [],
3192 stubs: {
3193 versions: ["10", "20", "30"],
3194 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003195 }`)
3196
3197 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003198 // TODO(jiyong) the checks for the platform variant are removed because we now create
3199 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3200 // the platform variants are not used from other platform modules. When that is done,
3201 // these checks will be replaced by expecting a specific error message that will be
3202 // emitted when the platform variant is used.
3203 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3204 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3205 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3206 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003207
3208 ctx, _ = testApex(t, `
3209 apex {
3210 name: "myapex",
3211 key: "myapex.key",
3212 }
3213
3214 apex_key {
3215 name: "myapex.key",
3216 public_key: "testkey.avbpubkey",
3217 private_key: "testkey.pem",
3218 }
3219
3220 cc_library {
3221 name: "libfoo",
3222 stl: "none",
3223 system_shared_libs: [],
3224 apex_available: ["//apex_available:platform"],
3225 }`)
3226
3227 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003228 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3229 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003230
3231 ctx, _ = testApex(t, `
3232 apex {
3233 name: "myapex",
3234 key: "myapex.key",
3235 native_shared_libs: ["libfoo"],
3236 }
3237
3238 apex_key {
3239 name: "myapex.key",
3240 public_key: "testkey.avbpubkey",
3241 private_key: "testkey.pem",
3242 }
3243
3244 cc_library {
3245 name: "libfoo",
3246 stl: "none",
3247 system_shared_libs: [],
3248 apex_available: ["myapex"],
3249 static: {
3250 apex_available: ["//apex_available:platform"],
3251 },
3252 }`)
3253
3254 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003255 // TODO(jiyong) the checks for the platform variant are removed because we now create
3256 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3257 // the platform variants are not used from other platform modules. When that is done,
3258 // these checks will be replaced by expecting a specific error message that will be
3259 // emitted when the platform variant is used.
3260 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3261 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3262 // // but the static variant is available to both myapex and the platform
3263 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3264 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003265}
3266
Jiyong Park5d790c32019-11-15 18:40:32 +09003267func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003268 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003269 apex {
3270 name: "myapex",
3271 key: "myapex.key",
3272 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003273 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003274 }
3275
3276 override_apex {
3277 name: "override_myapex",
3278 base: "myapex",
3279 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003280 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003281 logging_parent: "com.foo.bar",
Jiyong Park5d790c32019-11-15 18:40:32 +09003282 }
3283
3284 apex_key {
3285 name: "myapex.key",
3286 public_key: "testkey.avbpubkey",
3287 private_key: "testkey.pem",
3288 }
3289
3290 android_app {
3291 name: "app",
3292 srcs: ["foo/bar/MyClass.java"],
3293 package_name: "foo",
3294 sdk_version: "none",
3295 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003296 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003297 }
3298
3299 override_android_app {
3300 name: "override_app",
3301 base: "app",
3302 package_name: "bar",
3303 }
3304 `)
3305
Jiyong Park317645e2019-12-05 13:20:58 +09003306 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3307 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3308 if originalVariant.GetOverriddenBy() != "" {
3309 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3310 }
3311 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3312 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3313 }
3314
Jiyong Park5d790c32019-11-15 18:40:32 +09003315 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3316 apexRule := module.Rule("apexRule")
3317 copyCmds := apexRule.Args["copy_commands"]
3318
3319 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3320 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003321
3322 apexBundle := module.Module().(*apexBundle)
3323 name := apexBundle.Name()
3324 if name != "override_myapex" {
3325 t.Errorf("name should be \"override_myapex\", but was %q", name)
3326 }
3327
Baligh Uddin004d7172020-02-19 21:29:28 -08003328 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3329 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3330 }
3331
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003332 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3333 var builder strings.Builder
3334 data.Custom(&builder, name, "TARGET_", "", data)
3335 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003336 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003337 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3338 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003339 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003340 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003341 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003342 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3343 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003344}
3345
Jooyung Han214bf372019-11-12 13:03:50 +09003346func TestLegacyAndroid10Support(t *testing.T) {
3347 ctx, _ := testApex(t, `
3348 apex {
3349 name: "myapex",
3350 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003351 native_shared_libs: ["mylib"],
Jooyung Han214bf372019-11-12 13:03:50 +09003352 legacy_android10_support: true,
3353 }
3354
3355 apex_key {
3356 name: "myapex.key",
3357 public_key: "testkey.avbpubkey",
3358 private_key: "testkey.pem",
3359 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003360
3361 cc_library {
3362 name: "mylib",
3363 srcs: ["mylib.cpp"],
3364 stl: "libc++",
3365 system_shared_libs: [],
3366 apex_available: [ "myapex" ],
3367 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003368 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003369
3370 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3371 args := module.Rule("apexRule").Args
3372 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003373 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003374
3375 // The copies of the libraries in the apex should have one more dependency than
3376 // the ones outside the apex, namely the unwinder. Ideally we should check
3377 // the dependency names directly here but for some reason the names are blank in
3378 // this test.
3379 for _, lib := range []string{"libc++", "mylib"} {
3380 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3381 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3382 if len(apexImplicits) != len(nonApexImplicits)+1 {
3383 t.Errorf("%q missing unwinder dep", lib)
3384 }
3385 }
Jooyung Han214bf372019-11-12 13:03:50 +09003386}
3387
Jooyung Han58f26ab2019-12-18 15:34:32 +09003388func TestJavaSDKLibrary(t *testing.T) {
3389 ctx, _ := testApex(t, `
3390 apex {
3391 name: "myapex",
3392 key: "myapex.key",
3393 java_libs: ["foo"],
3394 }
3395
3396 apex_key {
3397 name: "myapex.key",
3398 public_key: "testkey.avbpubkey",
3399 private_key: "testkey.pem",
3400 }
3401
3402 java_sdk_library {
3403 name: "foo",
3404 srcs: ["a.java"],
3405 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003406 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003407 }
3408 `, withFiles(map[string][]byte{
3409 "api/current.txt": nil,
3410 "api/removed.txt": nil,
3411 "api/system-current.txt": nil,
3412 "api/system-removed.txt": nil,
3413 "api/test-current.txt": nil,
3414 "api/test-removed.txt": nil,
3415 }))
3416
3417 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003418 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003419 "javalib/foo.jar",
3420 "etc/permissions/foo.xml",
3421 })
3422 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003423 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3424 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003425}
3426
atrost6e126252020-01-27 17:01:16 +00003427func TestCompatConfig(t *testing.T) {
3428 ctx, _ := testApex(t, `
3429 apex {
3430 name: "myapex",
3431 key: "myapex.key",
3432 prebuilts: ["myjar-platform-compat-config"],
3433 java_libs: ["myjar"],
3434 }
3435
3436 apex_key {
3437 name: "myapex.key",
3438 public_key: "testkey.avbpubkey",
3439 private_key: "testkey.pem",
3440 }
3441
3442 platform_compat_config {
3443 name: "myjar-platform-compat-config",
3444 src: ":myjar",
3445 }
3446
3447 java_library {
3448 name: "myjar",
3449 srcs: ["foo/bar/MyClass.java"],
3450 sdk_version: "none",
3451 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003452 apex_available: [ "myapex" ],
3453 }
3454 `)
3455 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3456 "etc/compatconfig/myjar-platform-compat-config.xml",
3457 "javalib/myjar.jar",
3458 })
3459}
3460
Jiyong Park479321d2019-12-16 11:47:12 +09003461func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3462 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3463 apex {
3464 name: "myapex",
3465 key: "myapex.key",
3466 java_libs: ["myjar"],
3467 }
3468
3469 apex_key {
3470 name: "myapex.key",
3471 public_key: "testkey.avbpubkey",
3472 private_key: "testkey.pem",
3473 }
3474
3475 java_library {
3476 name: "myjar",
3477 srcs: ["foo/bar/MyClass.java"],
3478 sdk_version: "none",
3479 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003480 compile_dex: false,
Jiyong Park479321d2019-12-16 11:47:12 +09003481 }
3482 `)
3483}
3484
Jiyong Park7afd1072019-12-30 16:56:33 +09003485func TestCarryRequiredModuleNames(t *testing.T) {
3486 ctx, config := testApex(t, `
3487 apex {
3488 name: "myapex",
3489 key: "myapex.key",
3490 native_shared_libs: ["mylib"],
3491 }
3492
3493 apex_key {
3494 name: "myapex.key",
3495 public_key: "testkey.avbpubkey",
3496 private_key: "testkey.pem",
3497 }
3498
3499 cc_library {
3500 name: "mylib",
3501 srcs: ["mylib.cpp"],
3502 system_shared_libs: [],
3503 stl: "none",
3504 required: ["a", "b"],
3505 host_required: ["c", "d"],
3506 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003507 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003508 }
3509 `)
3510
3511 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3512 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3513 name := apexBundle.BaseModuleName()
3514 prefix := "TARGET_"
3515 var builder strings.Builder
3516 data.Custom(&builder, name, prefix, "", data)
3517 androidMk := builder.String()
3518 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3519 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3520 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3521}
3522
Jiyong Park7cd10e32020-01-14 09:22:18 +09003523func TestSymlinksFromApexToSystem(t *testing.T) {
3524 bp := `
3525 apex {
3526 name: "myapex",
3527 key: "myapex.key",
3528 native_shared_libs: ["mylib"],
3529 java_libs: ["myjar"],
3530 }
3531
Jiyong Park9d677202020-02-19 16:29:35 +09003532 apex {
3533 name: "myapex.updatable",
3534 key: "myapex.key",
3535 native_shared_libs: ["mylib"],
3536 java_libs: ["myjar"],
3537 updatable: true,
3538 }
3539
Jiyong Park7cd10e32020-01-14 09:22:18 +09003540 apex_key {
3541 name: "myapex.key",
3542 public_key: "testkey.avbpubkey",
3543 private_key: "testkey.pem",
3544 }
3545
3546 cc_library {
3547 name: "mylib",
3548 srcs: ["mylib.cpp"],
3549 shared_libs: ["myotherlib"],
3550 system_shared_libs: [],
3551 stl: "none",
3552 apex_available: [
3553 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003554 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003555 "//apex_available:platform",
3556 ],
3557 }
3558
3559 cc_library {
3560 name: "myotherlib",
3561 srcs: ["mylib.cpp"],
3562 system_shared_libs: [],
3563 stl: "none",
3564 apex_available: [
3565 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003566 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003567 "//apex_available:platform",
3568 ],
3569 }
3570
3571 java_library {
3572 name: "myjar",
3573 srcs: ["foo/bar/MyClass.java"],
3574 sdk_version: "none",
3575 system_modules: "none",
3576 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003577 apex_available: [
3578 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003579 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003580 "//apex_available:platform",
3581 ],
3582 }
3583
3584 java_library {
3585 name: "myotherjar",
3586 srcs: ["foo/bar/MyClass.java"],
3587 sdk_version: "none",
3588 system_modules: "none",
3589 apex_available: [
3590 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003591 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003592 "//apex_available:platform",
3593 ],
3594 }
3595 `
3596
3597 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3598 for _, f := range files {
3599 if f.path == file {
3600 if f.isLink {
3601 t.Errorf("%q is not a real file", file)
3602 }
3603 return
3604 }
3605 }
3606 t.Errorf("%q is not found", file)
3607 }
3608
3609 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3610 for _, f := range files {
3611 if f.path == file {
3612 if !f.isLink {
3613 t.Errorf("%q is not a symlink", file)
3614 }
3615 return
3616 }
3617 }
3618 t.Errorf("%q is not found", file)
3619 }
3620
Jiyong Park9d677202020-02-19 16:29:35 +09003621 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3622 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003623 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003624 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003625 ensureRealfileExists(t, files, "javalib/myjar.jar")
3626 ensureRealfileExists(t, files, "lib64/mylib.so")
3627 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3628
Jiyong Park9d677202020-02-19 16:29:35 +09003629 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3630 ensureRealfileExists(t, files, "javalib/myjar.jar")
3631 ensureRealfileExists(t, files, "lib64/mylib.so")
3632 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3633
3634 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003635 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003636 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003637 ensureRealfileExists(t, files, "javalib/myjar.jar")
3638 ensureRealfileExists(t, files, "lib64/mylib.so")
3639 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09003640
3641 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3642 ensureRealfileExists(t, files, "javalib/myjar.jar")
3643 ensureRealfileExists(t, files, "lib64/mylib.so")
3644 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09003645}
3646
Jooyung Han643adc42020-02-27 13:50:06 +09003647func TestApexWithJniLibs(t *testing.T) {
3648 ctx, _ := testApex(t, `
3649 apex {
3650 name: "myapex",
3651 key: "myapex.key",
3652 jni_libs: ["mylib"],
3653 }
3654
3655 apex_key {
3656 name: "myapex.key",
3657 public_key: "testkey.avbpubkey",
3658 private_key: "testkey.pem",
3659 }
3660
3661 cc_library {
3662 name: "mylib",
3663 srcs: ["mylib.cpp"],
3664 shared_libs: ["mylib2"],
3665 system_shared_libs: [],
3666 stl: "none",
3667 apex_available: [ "myapex" ],
3668 }
3669
3670 cc_library {
3671 name: "mylib2",
3672 srcs: ["mylib.cpp"],
3673 system_shared_libs: [],
3674 stl: "none",
3675 apex_available: [ "myapex" ],
3676 }
3677 `)
3678
3679 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
3680 // Notice mylib2.so (transitive dep) is not added as a jni_lib
3681 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
3682 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3683 "lib64/mylib.so",
3684 "lib64/mylib2.so",
3685 })
3686}
3687
3688func TestApexWithJniLibs_Errors(t *testing.T) {
3689 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
3690 apex {
3691 name: "myapex",
3692 key: "myapex.key",
3693 jni_libs: ["xxx"],
3694 }
3695
3696 apex_key {
3697 name: "myapex.key",
3698 public_key: "testkey.avbpubkey",
3699 private_key: "testkey.pem",
3700 }
3701
3702 prebuilt_etc {
3703 name: "xxx",
3704 src: "xxx",
3705 }
3706 `, withFiles(map[string][]byte{
3707 "xxx": nil,
3708 }))
3709}
3710
Jiyong Parkbd159612020-02-28 15:22:21 +09003711func TestAppBundle(t *testing.T) {
3712 ctx, _ := testApex(t, `
3713 apex {
3714 name: "myapex",
3715 key: "myapex.key",
3716 apps: ["AppFoo"],
3717 }
3718
3719 apex_key {
3720 name: "myapex.key",
3721 public_key: "testkey.avbpubkey",
3722 private_key: "testkey.pem",
3723 }
3724
3725 android_app {
3726 name: "AppFoo",
3727 srcs: ["foo/bar/MyClass.java"],
3728 sdk_version: "none",
3729 system_modules: "none",
3730 apex_available: [ "myapex" ],
3731 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09003732 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09003733
3734 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
3735 content := bundleConfigRule.Args["content"]
3736
3737 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09003738 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo/AppFoo.apk"}]}`)
Jiyong Parkbd159612020-02-28 15:22:21 +09003739}
3740
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003741func TestMain(m *testing.M) {
3742 run := func() int {
3743 setUp()
3744 defer tearDown()
3745
3746 return m.Run()
3747 }
3748
3749 os.Exit(run())
3750}