blob: bd53b1546471f53ee22cdb26bed7de8139267d68 [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 },
Jiyong Park0f80c182020-01-31 02:49:53 +0900813 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900814 }
815
816 cc_library {
817 name: "libbar",
818 srcs: ["mylib.cpp"],
819 system_shared_libs: [],
820 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000821 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900822 }
823
824 `)
825
Sundong Ahnabb64432019-10-22 13:58:29 +0900826 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900827 copyCmds := apexRule.Args["copy_commands"]
828
829 // Ensure that direct non-stubs dep is always included
830 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
831
832 // Ensure that indirect stubs dep is not included
833 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
834
835 // Ensure that runtime_libs dep in included
836 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
837
Sundong Ahnabb64432019-10-22 13:58:29 +0900838 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900839 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
840 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900841
842}
843
Jooyung Han9c80bae2019-08-20 17:30:57 +0900844func TestApexDependencyToLLNDK(t *testing.T) {
845 ctx, _ := testApex(t, `
846 apex {
847 name: "myapex",
848 key: "myapex.key",
849 use_vendor: true,
850 native_shared_libs: ["mylib"],
851 }
852
853 apex_key {
854 name: "myapex.key",
855 public_key: "testkey.avbpubkey",
856 private_key: "testkey.pem",
857 }
858
859 cc_library {
860 name: "mylib",
861 srcs: ["mylib.cpp"],
862 vendor_available: true,
863 shared_libs: ["libbar"],
864 system_shared_libs: [],
865 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000866 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900867 }
868
869 cc_library {
870 name: "libbar",
871 srcs: ["mylib.cpp"],
872 system_shared_libs: [],
873 stl: "none",
874 }
875
876 llndk_library {
877 name: "libbar",
878 symbol_file: "",
879 }
Jooyung Handc782442019-11-01 03:14:38 +0900880 `, func(fs map[string][]byte, config android.Config) {
881 setUseVendorWhitelistForTest(config, []string{"myapex"})
882 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900883
Sundong Ahnabb64432019-10-22 13:58:29 +0900884 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900885 copyCmds := apexRule.Args["copy_commands"]
886
887 // Ensure that LLNDK dep is not included
888 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
889
Sundong Ahnabb64432019-10-22 13:58:29 +0900890 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900891 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900892
893 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900894 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900895
896}
897
Jiyong Park25fc6a92018-11-18 18:02:45 +0900898func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700899 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900900 apex {
901 name: "myapex",
902 key: "myapex.key",
903 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
904 }
905
906 apex_key {
907 name: "myapex.key",
908 public_key: "testkey.avbpubkey",
909 private_key: "testkey.pem",
910 }
911
912 cc_library {
913 name: "mylib",
914 srcs: ["mylib.cpp"],
915 shared_libs: ["libdl#27"],
916 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000917 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900918 }
919
920 cc_library_shared {
921 name: "mylib_shared",
922 srcs: ["mylib.cpp"],
923 shared_libs: ["libdl#27"],
924 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000925 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900926 }
927
928 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900929 name: "libBootstrap",
930 srcs: ["mylib.cpp"],
931 stl: "none",
932 bootstrap: true,
933 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900934 `)
935
Sundong Ahnabb64432019-10-22 13:58:29 +0900936 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900937 copyCmds := apexRule.Args["copy_commands"]
938
939 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800940 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900941 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
942 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900943
944 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900945 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900946
Colin Cross7113d202019-11-20 16:39:12 -0800947 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
948 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
949 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900950
951 // For dependency to libc
952 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900953 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900954 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900955 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900956 // ... Cflags from stub is correctly exported to mylib
957 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
958 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
959
960 // For dependency to libm
961 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800962 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900963 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900964 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900965 // ... and is not compiling with the stub
966 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
967 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
968
969 // For dependency to libdl
970 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900971 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900973 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
974 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900975 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800976 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900977 // ... Cflags from stub is correctly exported to mylib
978 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
979 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900980
981 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800982 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
983 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
984 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
985 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900986}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900987
988func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700989 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900990 apex {
991 name: "myapex",
992 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900993 native_shared_libs: ["mylib"],
994 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900995 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900996 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900997 }
998
999 apex_key {
1000 name: "myapex.key",
1001 public_key: "testkey.avbpubkey",
1002 private_key: "testkey.pem",
1003 }
1004
1005 prebuilt_etc {
1006 name: "myetc",
1007 src: "myprebuilt",
1008 sub_dir: "foo/bar",
1009 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001010
1011 cc_library {
1012 name: "mylib",
1013 srcs: ["mylib.cpp"],
1014 relative_install_path: "foo/bar",
1015 system_shared_libs: [],
1016 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001017 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001018 }
1019
1020 cc_binary {
1021 name: "mybin",
1022 srcs: ["mylib.cpp"],
1023 relative_install_path: "foo/bar",
1024 system_shared_libs: [],
1025 static_executable: true,
1026 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001027 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001028 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001029 `)
1030
Sundong Ahnabb64432019-10-22 13:58:29 +09001031 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001032 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1033
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001034 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001035 ensureListContains(t, dirs, "etc")
1036 ensureListContains(t, dirs, "etc/foo")
1037 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001038 ensureListContains(t, dirs, "lib64")
1039 ensureListContains(t, dirs, "lib64/foo")
1040 ensureListContains(t, dirs, "lib64/foo/bar")
1041 ensureListContains(t, dirs, "lib")
1042 ensureListContains(t, dirs, "lib/foo")
1043 ensureListContains(t, dirs, "lib/foo/bar")
1044
Jiyong Parkbd13e442019-03-15 18:10:35 +09001045 ensureListContains(t, dirs, "bin")
1046 ensureListContains(t, dirs, "bin/foo")
1047 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001048}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001049
1050func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001051 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001052 apex {
1053 name: "myapex",
1054 key: "myapex.key",
1055 native_shared_libs: ["mylib"],
1056 use_vendor: true,
1057 }
1058
1059 apex_key {
1060 name: "myapex.key",
1061 public_key: "testkey.avbpubkey",
1062 private_key: "testkey.pem",
1063 }
1064
1065 cc_library {
1066 name: "mylib",
1067 srcs: ["mylib.cpp"],
1068 shared_libs: ["mylib2"],
1069 system_shared_libs: [],
1070 vendor_available: true,
1071 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001072 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001073 }
1074
1075 cc_library {
1076 name: "mylib2",
1077 srcs: ["mylib.cpp"],
1078 system_shared_libs: [],
1079 vendor_available: true,
1080 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001081 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001082 }
Jooyung Handc782442019-11-01 03:14:38 +09001083 `, func(fs map[string][]byte, config android.Config) {
1084 setUseVendorWhitelistForTest(config, []string{"myapex"})
1085 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001086
1087 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001088 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001089 for _, implicit := range i.Implicits {
1090 inputsList = append(inputsList, implicit.String())
1091 }
1092 }
1093 inputsString := strings.Join(inputsList, " ")
1094
1095 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001096 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1097 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001098
1099 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001100 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1101 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001102}
Jiyong Park16e91a02018-12-20 18:18:08 +09001103
Jooyung Handc782442019-11-01 03:14:38 +09001104func TestUseVendorRestriction(t *testing.T) {
1105 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1106 apex {
1107 name: "myapex",
1108 key: "myapex.key",
1109 use_vendor: true,
1110 }
1111 apex_key {
1112 name: "myapex.key",
1113 public_key: "testkey.avbpubkey",
1114 private_key: "testkey.pem",
1115 }
1116 `, func(fs map[string][]byte, config android.Config) {
1117 setUseVendorWhitelistForTest(config, []string{""})
1118 })
1119 // no error with whitelist
1120 testApex(t, `
1121 apex {
1122 name: "myapex",
1123 key: "myapex.key",
1124 use_vendor: true,
1125 }
1126 apex_key {
1127 name: "myapex.key",
1128 public_key: "testkey.avbpubkey",
1129 private_key: "testkey.pem",
1130 }
1131 `, func(fs map[string][]byte, config android.Config) {
1132 setUseVendorWhitelistForTest(config, []string{"myapex"})
1133 })
1134}
1135
Jooyung Han5c998b92019-06-27 11:30:33 +09001136func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1137 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1138 apex {
1139 name: "myapex",
1140 key: "myapex.key",
1141 native_shared_libs: ["mylib"],
1142 use_vendor: true,
1143 }
1144
1145 apex_key {
1146 name: "myapex.key",
1147 public_key: "testkey.avbpubkey",
1148 private_key: "testkey.pem",
1149 }
1150
1151 cc_library {
1152 name: "mylib",
1153 srcs: ["mylib.cpp"],
1154 system_shared_libs: [],
1155 stl: "none",
1156 }
1157 `)
1158}
1159
Jiyong Park16e91a02018-12-20 18:18:08 +09001160func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001161 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001162 apex {
1163 name: "myapex",
1164 key: "myapex.key",
1165 native_shared_libs: ["mylib"],
1166 }
1167
1168 apex_key {
1169 name: "myapex.key",
1170 public_key: "testkey.avbpubkey",
1171 private_key: "testkey.pem",
1172 }
1173
1174 cc_library {
1175 name: "mylib",
1176 srcs: ["mylib.cpp"],
1177 system_shared_libs: [],
1178 stl: "none",
1179 stubs: {
1180 versions: ["1", "2", "3"],
1181 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001182 apex_available: [
1183 "//apex_available:platform",
1184 "myapex",
1185 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001186 }
1187
1188 cc_binary {
1189 name: "not_in_apex",
1190 srcs: ["mylib.cpp"],
1191 static_libs: ["mylib"],
1192 static_executable: true,
1193 system_shared_libs: [],
1194 stl: "none",
1195 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001196 `)
1197
Colin Cross7113d202019-11-20 16:39:12 -08001198 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001199
1200 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001201 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001202}
Jiyong Park9335a262018-12-24 11:31:58 +09001203
1204func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001205 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001206 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001207 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001208 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001209 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001210 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001211 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001212 }
1213
1214 cc_library {
1215 name: "mylib",
1216 srcs: ["mylib.cpp"],
1217 system_shared_libs: [],
1218 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001219 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001220 }
1221
1222 apex_key {
1223 name: "myapex.key",
1224 public_key: "testkey.avbpubkey",
1225 private_key: "testkey.pem",
1226 }
1227
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001228 android_app_certificate {
1229 name: "myapex.certificate",
1230 certificate: "testkey",
1231 }
1232
1233 android_app_certificate {
1234 name: "myapex.certificate.override",
1235 certificate: "testkey.override",
1236 }
1237
Jiyong Park9335a262018-12-24 11:31:58 +09001238 `)
1239
1240 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001241 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001242
1243 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1244 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1245 "vendor/foo/devkeys/testkey.avbpubkey")
1246 }
1247 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1248 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1249 "vendor/foo/devkeys/testkey.pem")
1250 }
1251
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001252 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001253 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001254 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001255 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001256 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001257 }
1258}
Jiyong Park58e364a2019-01-19 19:24:06 +09001259
Jooyung Hanf121a652019-12-17 14:30:11 +09001260func TestCertificate(t *testing.T) {
1261 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1262 ctx, _ := testApex(t, `
1263 apex {
1264 name: "myapex",
1265 key: "myapex.key",
1266 }
1267 apex_key {
1268 name: "myapex.key",
1269 public_key: "testkey.avbpubkey",
1270 private_key: "testkey.pem",
1271 }`)
1272 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1273 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1274 if actual := rule.Args["certificates"]; actual != expected {
1275 t.Errorf("certificates should be %q, not %q", expected, actual)
1276 }
1277 })
1278 t.Run("override when unspecified", func(t *testing.T) {
1279 ctx, _ := testApex(t, `
1280 apex {
1281 name: "myapex_keytest",
1282 key: "myapex.key",
1283 file_contexts: ":myapex-file_contexts",
1284 }
1285 apex_key {
1286 name: "myapex.key",
1287 public_key: "testkey.avbpubkey",
1288 private_key: "testkey.pem",
1289 }
1290 android_app_certificate {
1291 name: "myapex.certificate.override",
1292 certificate: "testkey.override",
1293 }`)
1294 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1295 expected := "testkey.override.x509.pem testkey.override.pk8"
1296 if actual := rule.Args["certificates"]; actual != expected {
1297 t.Errorf("certificates should be %q, not %q", expected, actual)
1298 }
1299 })
1300 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1301 ctx, _ := testApex(t, `
1302 apex {
1303 name: "myapex",
1304 key: "myapex.key",
1305 certificate: ":myapex.certificate",
1306 }
1307 apex_key {
1308 name: "myapex.key",
1309 public_key: "testkey.avbpubkey",
1310 private_key: "testkey.pem",
1311 }
1312 android_app_certificate {
1313 name: "myapex.certificate",
1314 certificate: "testkey",
1315 }`)
1316 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1317 expected := "testkey.x509.pem testkey.pk8"
1318 if actual := rule.Args["certificates"]; actual != expected {
1319 t.Errorf("certificates should be %q, not %q", expected, actual)
1320 }
1321 })
1322 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1323 ctx, _ := testApex(t, `
1324 apex {
1325 name: "myapex_keytest",
1326 key: "myapex.key",
1327 file_contexts: ":myapex-file_contexts",
1328 certificate: ":myapex.certificate",
1329 }
1330 apex_key {
1331 name: "myapex.key",
1332 public_key: "testkey.avbpubkey",
1333 private_key: "testkey.pem",
1334 }
1335 android_app_certificate {
1336 name: "myapex.certificate.override",
1337 certificate: "testkey.override",
1338 }`)
1339 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1340 expected := "testkey.override.x509.pem testkey.override.pk8"
1341 if actual := rule.Args["certificates"]; actual != expected {
1342 t.Errorf("certificates should be %q, not %q", expected, actual)
1343 }
1344 })
1345 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1346 ctx, _ := testApex(t, `
1347 apex {
1348 name: "myapex",
1349 key: "myapex.key",
1350 certificate: "testkey",
1351 }
1352 apex_key {
1353 name: "myapex.key",
1354 public_key: "testkey.avbpubkey",
1355 private_key: "testkey.pem",
1356 }`)
1357 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1358 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1359 if actual := rule.Args["certificates"]; actual != expected {
1360 t.Errorf("certificates should be %q, not %q", expected, actual)
1361 }
1362 })
1363 t.Run("override when specified as <name>", func(t *testing.T) {
1364 ctx, _ := testApex(t, `
1365 apex {
1366 name: "myapex_keytest",
1367 key: "myapex.key",
1368 file_contexts: ":myapex-file_contexts",
1369 certificate: "testkey",
1370 }
1371 apex_key {
1372 name: "myapex.key",
1373 public_key: "testkey.avbpubkey",
1374 private_key: "testkey.pem",
1375 }
1376 android_app_certificate {
1377 name: "myapex.certificate.override",
1378 certificate: "testkey.override",
1379 }`)
1380 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1381 expected := "testkey.override.x509.pem testkey.override.pk8"
1382 if actual := rule.Args["certificates"]; actual != expected {
1383 t.Errorf("certificates should be %q, not %q", expected, actual)
1384 }
1385 })
1386}
1387
Jiyong Park58e364a2019-01-19 19:24:06 +09001388func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001389 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001390 apex {
1391 name: "myapex",
1392 key: "myapex.key",
1393 native_shared_libs: ["mylib"],
1394 }
1395
1396 apex {
1397 name: "otherapex",
1398 key: "myapex.key",
1399 native_shared_libs: ["mylib"],
1400 }
1401
1402 apex_key {
1403 name: "myapex.key",
1404 public_key: "testkey.avbpubkey",
1405 private_key: "testkey.pem",
1406 }
1407
1408 cc_library {
1409 name: "mylib",
1410 srcs: ["mylib.cpp"],
1411 system_shared_libs: [],
1412 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001413 // TODO: remove //apex_available:platform
1414 apex_available: [
1415 "//apex_available:platform",
1416 "myapex",
1417 "otherapex",
1418 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001419 }
1420 `)
1421
Jooyung Han6b8459b2019-10-30 08:29:25 +09001422 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001423 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001424 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001425 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1426 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001427
Jooyung Han6b8459b2019-10-30 08:29:25 +09001428 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001429 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001430 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001431 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1432 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001433
Jooyung Han6b8459b2019-10-30 08:29:25 +09001434 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001435 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001436 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001437 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1438 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001439}
Jiyong Park7e636d02019-01-28 16:16:54 +09001440
1441func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001442 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001443 apex {
1444 name: "myapex",
1445 key: "myapex.key",
1446 native_shared_libs: ["mylib"],
1447 }
1448
1449 apex_key {
1450 name: "myapex.key",
1451 public_key: "testkey.avbpubkey",
1452 private_key: "testkey.pem",
1453 }
1454
1455 cc_library_headers {
1456 name: "mylib_headers",
1457 export_include_dirs: ["my_include"],
1458 system_shared_libs: [],
1459 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001460 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001461 }
1462
1463 cc_library {
1464 name: "mylib",
1465 srcs: ["mylib.cpp"],
1466 system_shared_libs: [],
1467 stl: "none",
1468 header_libs: ["mylib_headers"],
1469 export_header_lib_headers: ["mylib_headers"],
1470 stubs: {
1471 versions: ["1", "2", "3"],
1472 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001473 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001474 }
1475
1476 cc_library {
1477 name: "otherlib",
1478 srcs: ["mylib.cpp"],
1479 system_shared_libs: [],
1480 stl: "none",
1481 shared_libs: ["mylib"],
1482 }
1483 `)
1484
Colin Cross7113d202019-11-20 16:39:12 -08001485 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001486
1487 // Ensure that the include path of the header lib is exported to 'otherlib'
1488 ensureContains(t, cFlags, "-Imy_include")
1489}
Alex Light9670d332019-01-29 18:07:33 -08001490
Jiyong Park7cd10e32020-01-14 09:22:18 +09001491type fileInApex struct {
1492 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001493 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001494 isLink bool
1495}
1496
Jooyung Hana57af4a2020-01-23 05:36:59 +00001497func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001498 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001499 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001500 copyCmds := apexRule.Args["copy_commands"]
1501 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001502 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001503 for _, cmd := range strings.Split(copyCmds, "&&") {
1504 cmd = strings.TrimSpace(cmd)
1505 if cmd == "" {
1506 continue
1507 }
1508 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001509 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001510 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001511 switch terms[0] {
1512 case "mkdir":
1513 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001514 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001515 t.Fatal("copyCmds contains invalid cp command", cmd)
1516 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001517 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001518 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001519 isLink = false
1520 case "ln":
1521 if len(terms) != 3 && len(terms) != 4 {
1522 // ln LINK TARGET or ln -s LINK TARGET
1523 t.Fatal("copyCmds contains invalid ln command", cmd)
1524 }
1525 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001526 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001527 isLink = true
1528 default:
1529 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1530 }
1531 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001532 index := strings.Index(dst, imageApexDir)
1533 if index == -1 {
1534 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1535 }
1536 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001537 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001538 }
1539 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001540 return ret
1541}
1542
Jooyung Hana57af4a2020-01-23 05:36:59 +00001543func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1544 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001545 var failed bool
1546 var surplus []string
1547 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001548 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001549 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001550 for _, expected := range files {
1551 if matched, _ := path.Match(expected, file.path); matched {
1552 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001553 mactchFound = true
1554 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001555 }
1556 }
Jooyung Hane6436d72020-02-27 13:31:56 +09001557 if !mactchFound {
1558 surplus = append(surplus, file.path)
1559 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001560 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001561
Jooyung Han31c470b2019-10-18 16:26:59 +09001562 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001563 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001564 t.Log("surplus files", surplus)
1565 failed = true
1566 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001567
1568 if len(files) > len(filesMatched) {
1569 var missing []string
1570 for _, expected := range files {
1571 if !filesMatched[expected] {
1572 missing = append(missing, expected)
1573 }
1574 }
1575 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001576 t.Log("missing files", missing)
1577 failed = true
1578 }
1579 if failed {
1580 t.Fail()
1581 }
1582}
1583
Jooyung Han344d5432019-08-23 11:17:39 +09001584func TestVndkApexCurrent(t *testing.T) {
1585 ctx, _ := testApex(t, `
1586 apex_vndk {
1587 name: "myapex",
1588 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001589 }
1590
1591 apex_key {
1592 name: "myapex.key",
1593 public_key: "testkey.avbpubkey",
1594 private_key: "testkey.pem",
1595 }
1596
1597 cc_library {
1598 name: "libvndk",
1599 srcs: ["mylib.cpp"],
1600 vendor_available: true,
1601 vndk: {
1602 enabled: true,
1603 },
1604 system_shared_libs: [],
1605 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001606 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001607 }
1608
1609 cc_library {
1610 name: "libvndksp",
1611 srcs: ["mylib.cpp"],
1612 vendor_available: true,
1613 vndk: {
1614 enabled: true,
1615 support_system_process: true,
1616 },
1617 system_shared_libs: [],
1618 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001619 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001620 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001621 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001622
Jooyung Hana57af4a2020-01-23 05:36:59 +00001623 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001624 "lib/libvndk.so",
1625 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001626 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09001627 "lib64/libvndk.so",
1628 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001629 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001630 "etc/llndk.libraries.VER.txt",
1631 "etc/vndkcore.libraries.VER.txt",
1632 "etc/vndksp.libraries.VER.txt",
1633 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001634 })
Jooyung Han344d5432019-08-23 11:17:39 +09001635}
1636
1637func TestVndkApexWithPrebuilt(t *testing.T) {
1638 ctx, _ := testApex(t, `
1639 apex_vndk {
1640 name: "myapex",
1641 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001642 }
1643
1644 apex_key {
1645 name: "myapex.key",
1646 public_key: "testkey.avbpubkey",
1647 private_key: "testkey.pem",
1648 }
1649
1650 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001651 name: "libvndk",
1652 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001653 vendor_available: true,
1654 vndk: {
1655 enabled: true,
1656 },
1657 system_shared_libs: [],
1658 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001659 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001660 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001661
1662 cc_prebuilt_library_shared {
1663 name: "libvndk.arm",
1664 srcs: ["libvndk.arm.so"],
1665 vendor_available: true,
1666 vndk: {
1667 enabled: true,
1668 },
1669 enabled: false,
1670 arch: {
1671 arm: {
1672 enabled: true,
1673 },
1674 },
1675 system_shared_libs: [],
1676 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001677 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001678 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001679 `+vndkLibrariesTxtFiles("current"),
1680 withFiles(map[string][]byte{
1681 "libvndk.so": nil,
1682 "libvndk.arm.so": nil,
1683 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001684
Jooyung Hana57af4a2020-01-23 05:36:59 +00001685 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001686 "lib/libvndk.so",
1687 "lib/libvndk.arm.so",
1688 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001689 "lib/libc++.so",
1690 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001691 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001692 })
Jooyung Han344d5432019-08-23 11:17:39 +09001693}
1694
Jooyung Han39edb6c2019-11-06 16:53:07 +09001695func vndkLibrariesTxtFiles(vers ...string) (result string) {
1696 for _, v := range vers {
1697 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001698 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001699 result += `
1700 vndk_libraries_txt {
1701 name: "` + txt + `.libraries.txt",
1702 }
1703 `
1704 }
1705 } else {
1706 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1707 result += `
1708 prebuilt_etc {
1709 name: "` + txt + `.libraries.` + v + `.txt",
1710 src: "dummy.txt",
1711 }
1712 `
1713 }
1714 }
1715 }
1716 return
1717}
1718
Jooyung Han344d5432019-08-23 11:17:39 +09001719func TestVndkApexVersion(t *testing.T) {
1720 ctx, _ := testApex(t, `
1721 apex_vndk {
1722 name: "myapex_v27",
1723 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001724 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001725 vndk_version: "27",
1726 }
1727
1728 apex_key {
1729 name: "myapex.key",
1730 public_key: "testkey.avbpubkey",
1731 private_key: "testkey.pem",
1732 }
1733
Jooyung Han31c470b2019-10-18 16:26:59 +09001734 vndk_prebuilt_shared {
1735 name: "libvndk27",
1736 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001737 vendor_available: true,
1738 vndk: {
1739 enabled: true,
1740 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001741 target_arch: "arm64",
1742 arch: {
1743 arm: {
1744 srcs: ["libvndk27_arm.so"],
1745 },
1746 arm64: {
1747 srcs: ["libvndk27_arm64.so"],
1748 },
1749 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001750 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001751 }
1752
1753 vndk_prebuilt_shared {
1754 name: "libvndk27",
1755 version: "27",
1756 vendor_available: true,
1757 vndk: {
1758 enabled: true,
1759 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001760 target_arch: "x86_64",
1761 arch: {
1762 x86: {
1763 srcs: ["libvndk27_x86.so"],
1764 },
1765 x86_64: {
1766 srcs: ["libvndk27_x86_64.so"],
1767 },
1768 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001769 }
1770 `+vndkLibrariesTxtFiles("27"),
1771 withFiles(map[string][]byte{
1772 "libvndk27_arm.so": nil,
1773 "libvndk27_arm64.so": nil,
1774 "libvndk27_x86.so": nil,
1775 "libvndk27_x86_64.so": nil,
1776 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001777
Jooyung Hana57af4a2020-01-23 05:36:59 +00001778 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001779 "lib/libvndk27_arm.so",
1780 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001781 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001782 })
Jooyung Han344d5432019-08-23 11:17:39 +09001783}
1784
1785func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1786 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1787 apex_vndk {
1788 name: "myapex_v27",
1789 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001790 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001791 vndk_version: "27",
1792 }
1793 apex_vndk {
1794 name: "myapex_v27_other",
1795 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001796 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001797 vndk_version: "27",
1798 }
1799
1800 apex_key {
1801 name: "myapex.key",
1802 public_key: "testkey.avbpubkey",
1803 private_key: "testkey.pem",
1804 }
1805
1806 cc_library {
1807 name: "libvndk",
1808 srcs: ["mylib.cpp"],
1809 vendor_available: true,
1810 vndk: {
1811 enabled: true,
1812 },
1813 system_shared_libs: [],
1814 stl: "none",
1815 }
1816
1817 vndk_prebuilt_shared {
1818 name: "libvndk",
1819 version: "27",
1820 vendor_available: true,
1821 vndk: {
1822 enabled: true,
1823 },
1824 srcs: ["libvndk.so"],
1825 }
1826 `, withFiles(map[string][]byte{
1827 "libvndk.so": nil,
1828 }))
1829}
1830
Jooyung Han90eee022019-10-01 20:02:42 +09001831func TestVndkApexNameRule(t *testing.T) {
1832 ctx, _ := testApex(t, `
1833 apex_vndk {
1834 name: "myapex",
1835 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001836 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001837 }
1838 apex_vndk {
1839 name: "myapex_v28",
1840 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001841 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001842 vndk_version: "28",
1843 }
1844 apex_key {
1845 name: "myapex.key",
1846 public_key: "testkey.avbpubkey",
1847 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001848 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001849
1850 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001851 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001852 actual := proptools.String(bundle.properties.Apex_name)
1853 if !reflect.DeepEqual(actual, expected) {
1854 t.Errorf("Got '%v', expected '%v'", actual, expected)
1855 }
1856 }
1857
1858 assertApexName("com.android.vndk.vVER", "myapex")
1859 assertApexName("com.android.vndk.v28", "myapex_v28")
1860}
1861
Jooyung Han344d5432019-08-23 11:17:39 +09001862func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1863 ctx, _ := testApex(t, `
1864 apex_vndk {
1865 name: "myapex",
1866 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001867 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001868 }
1869
1870 apex_key {
1871 name: "myapex.key",
1872 public_key: "testkey.avbpubkey",
1873 private_key: "testkey.pem",
1874 }
1875
1876 cc_library {
1877 name: "libvndk",
1878 srcs: ["mylib.cpp"],
1879 vendor_available: true,
1880 native_bridge_supported: true,
1881 host_supported: true,
1882 vndk: {
1883 enabled: true,
1884 },
1885 system_shared_libs: [],
1886 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001887 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001888 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001889 `+vndkLibrariesTxtFiles("current"),
1890 withTargets(map[android.OsType][]android.Target{
1891 android.Android: []android.Target{
1892 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1893 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1894 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1895 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1896 },
1897 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001898
Jooyung Hana57af4a2020-01-23 05:36:59 +00001899 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001900 "lib/libvndk.so",
1901 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001902 "lib/libc++.so",
1903 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001904 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001905 })
Jooyung Han344d5432019-08-23 11:17:39 +09001906}
1907
1908func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1909 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1910 apex_vndk {
1911 name: "myapex",
1912 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001913 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001914 native_bridge_supported: true,
1915 }
1916
1917 apex_key {
1918 name: "myapex.key",
1919 public_key: "testkey.avbpubkey",
1920 private_key: "testkey.pem",
1921 }
1922
1923 cc_library {
1924 name: "libvndk",
1925 srcs: ["mylib.cpp"],
1926 vendor_available: true,
1927 native_bridge_supported: true,
1928 host_supported: true,
1929 vndk: {
1930 enabled: true,
1931 },
1932 system_shared_libs: [],
1933 stl: "none",
1934 }
1935 `)
1936}
1937
Jooyung Han31c470b2019-10-18 16:26:59 +09001938func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001939 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001940 apex_vndk {
1941 name: "myapex_v27",
1942 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001943 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001944 vndk_version: "27",
1945 }
1946
1947 apex_key {
1948 name: "myapex.key",
1949 public_key: "testkey.avbpubkey",
1950 private_key: "testkey.pem",
1951 }
1952
1953 vndk_prebuilt_shared {
1954 name: "libvndk27",
1955 version: "27",
1956 target_arch: "arm",
1957 vendor_available: true,
1958 vndk: {
1959 enabled: true,
1960 },
1961 arch: {
1962 arm: {
1963 srcs: ["libvndk27.so"],
1964 }
1965 },
1966 }
1967
1968 vndk_prebuilt_shared {
1969 name: "libvndk27",
1970 version: "27",
1971 target_arch: "arm",
1972 binder32bit: true,
1973 vendor_available: true,
1974 vndk: {
1975 enabled: true,
1976 },
1977 arch: {
1978 arm: {
1979 srcs: ["libvndk27binder32.so"],
1980 }
1981 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001982 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001983 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001984 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001985 withFiles(map[string][]byte{
1986 "libvndk27.so": nil,
1987 "libvndk27binder32.so": nil,
1988 }),
1989 withBinder32bit,
1990 withTargets(map[android.OsType][]android.Target{
1991 android.Android: []android.Target{
1992 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1993 },
1994 }),
1995 )
1996
Jooyung Hana57af4a2020-01-23 05:36:59 +00001997 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001998 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001999 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002000 })
2001}
2002
Jooyung Hane1633032019-08-01 17:41:43 +09002003func TestDependenciesInApexManifest(t *testing.T) {
2004 ctx, _ := testApex(t, `
2005 apex {
2006 name: "myapex_nodep",
2007 key: "myapex.key",
2008 native_shared_libs: ["lib_nodep"],
2009 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002010 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002011 }
2012
2013 apex {
2014 name: "myapex_dep",
2015 key: "myapex.key",
2016 native_shared_libs: ["lib_dep"],
2017 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002018 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002019 }
2020
2021 apex {
2022 name: "myapex_provider",
2023 key: "myapex.key",
2024 native_shared_libs: ["libfoo"],
2025 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002026 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002027 }
2028
2029 apex {
2030 name: "myapex_selfcontained",
2031 key: "myapex.key",
2032 native_shared_libs: ["lib_dep", "libfoo"],
2033 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002034 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002035 }
2036
2037 apex_key {
2038 name: "myapex.key",
2039 public_key: "testkey.avbpubkey",
2040 private_key: "testkey.pem",
2041 }
2042
2043 cc_library {
2044 name: "lib_nodep",
2045 srcs: ["mylib.cpp"],
2046 system_shared_libs: [],
2047 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002048 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002049 }
2050
2051 cc_library {
2052 name: "lib_dep",
2053 srcs: ["mylib.cpp"],
2054 shared_libs: ["libfoo"],
2055 system_shared_libs: [],
2056 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002057 apex_available: [
2058 "myapex_dep",
2059 "myapex_provider",
2060 "myapex_selfcontained",
2061 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002062 }
2063
2064 cc_library {
2065 name: "libfoo",
2066 srcs: ["mytest.cpp"],
2067 stubs: {
2068 versions: ["1"],
2069 },
2070 system_shared_libs: [],
2071 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002072 apex_available: [
2073 "myapex_provider",
2074 "myapex_selfcontained",
2075 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002076 }
2077 `)
2078
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002079 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002080 var provideNativeLibs, requireNativeLibs []string
2081
Sundong Ahnabb64432019-10-22 13:58:29 +09002082 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002083 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2084 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002085 ensureListEmpty(t, provideNativeLibs)
2086 ensureListEmpty(t, requireNativeLibs)
2087
Sundong Ahnabb64432019-10-22 13:58:29 +09002088 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002089 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2090 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002091 ensureListEmpty(t, provideNativeLibs)
2092 ensureListContains(t, requireNativeLibs, "libfoo.so")
2093
Sundong Ahnabb64432019-10-22 13:58:29 +09002094 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002095 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2096 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002097 ensureListContains(t, provideNativeLibs, "libfoo.so")
2098 ensureListEmpty(t, requireNativeLibs)
2099
Sundong Ahnabb64432019-10-22 13:58:29 +09002100 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002101 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2102 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002103 ensureListContains(t, provideNativeLibs, "libfoo.so")
2104 ensureListEmpty(t, requireNativeLibs)
2105}
2106
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002107func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002108 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002109 apex {
2110 name: "myapex",
2111 key: "myapex.key",
2112 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002113 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002114 }
2115
2116 apex_key {
2117 name: "myapex.key",
2118 public_key: "testkey.avbpubkey",
2119 private_key: "testkey.pem",
2120 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002121
2122 cc_library {
2123 name: "mylib",
2124 srcs: ["mylib.cpp"],
2125 system_shared_libs: [],
2126 stl: "none",
2127 apex_available: [
2128 "//apex_available:platform",
2129 "myapex",
2130 ],
2131 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002132 `)
2133
Sundong Ahnabb64432019-10-22 13:58:29 +09002134 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002135 apexManifestRule := module.Rule("apexManifestRule")
2136 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2137 apexRule := module.Rule("apexRule")
2138 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002139
2140 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2141 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2142 name := apexBundle.BaseModuleName()
2143 prefix := "TARGET_"
2144 var builder strings.Builder
2145 data.Custom(&builder, name, prefix, "", data)
2146 androidMk := builder.String()
2147 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2148 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002149}
2150
Alex Light0851b882019-02-07 13:20:53 -08002151func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002152 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002153 apex {
2154 name: "myapex",
2155 key: "myapex.key",
2156 native_shared_libs: ["mylib_common"],
2157 }
2158
2159 apex_key {
2160 name: "myapex.key",
2161 public_key: "testkey.avbpubkey",
2162 private_key: "testkey.pem",
2163 }
2164
2165 cc_library {
2166 name: "mylib_common",
2167 srcs: ["mylib.cpp"],
2168 system_shared_libs: [],
2169 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002170 apex_available: [
2171 "//apex_available:platform",
2172 "myapex",
2173 ],
Alex Light0851b882019-02-07 13:20:53 -08002174 }
2175 `)
2176
Sundong Ahnabb64432019-10-22 13:58:29 +09002177 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002178 apexRule := module.Rule("apexRule")
2179 copyCmds := apexRule.Args["copy_commands"]
2180
2181 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2182 t.Log("Apex was a test apex!")
2183 t.Fail()
2184 }
2185 // Ensure that main rule creates an output
2186 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2187
2188 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002189 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002190
2191 // Ensure that both direct and indirect deps are copied into apex
2192 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2193
Colin Cross7113d202019-11-20 16:39:12 -08002194 // Ensure that the platform variant ends with _shared
2195 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002196
2197 if !android.InAnyApex("mylib_common") {
2198 t.Log("Found mylib_common not in any apex!")
2199 t.Fail()
2200 }
2201}
2202
2203func TestTestApex(t *testing.T) {
2204 if android.InAnyApex("mylib_common_test") {
2205 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!")
2206 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002207 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002208 apex_test {
2209 name: "myapex",
2210 key: "myapex.key",
2211 native_shared_libs: ["mylib_common_test"],
2212 }
2213
2214 apex_key {
2215 name: "myapex.key",
2216 public_key: "testkey.avbpubkey",
2217 private_key: "testkey.pem",
2218 }
2219
2220 cc_library {
2221 name: "mylib_common_test",
2222 srcs: ["mylib.cpp"],
2223 system_shared_libs: [],
2224 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002225 // TODO: remove //apex_available:platform
2226 apex_available: [
2227 "//apex_available:platform",
2228 "myapex",
2229 ],
Alex Light0851b882019-02-07 13:20:53 -08002230 }
2231 `)
2232
Sundong Ahnabb64432019-10-22 13:58:29 +09002233 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002234 apexRule := module.Rule("apexRule")
2235 copyCmds := apexRule.Args["copy_commands"]
2236
2237 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2238 t.Log("Apex was not a test apex!")
2239 t.Fail()
2240 }
2241 // Ensure that main rule creates an output
2242 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2243
2244 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002245 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002246
2247 // Ensure that both direct and indirect deps are copied into apex
2248 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2249
Colin Cross7113d202019-11-20 16:39:12 -08002250 // Ensure that the platform variant ends with _shared
2251 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002252}
2253
Alex Light9670d332019-01-29 18:07:33 -08002254func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002255 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002256 apex {
2257 name: "myapex",
2258 key: "myapex.key",
2259 multilib: {
2260 first: {
2261 native_shared_libs: ["mylib_common"],
2262 }
2263 },
2264 target: {
2265 android: {
2266 multilib: {
2267 first: {
2268 native_shared_libs: ["mylib"],
2269 }
2270 }
2271 },
2272 host: {
2273 multilib: {
2274 first: {
2275 native_shared_libs: ["mylib2"],
2276 }
2277 }
2278 }
2279 }
2280 }
2281
2282 apex_key {
2283 name: "myapex.key",
2284 public_key: "testkey.avbpubkey",
2285 private_key: "testkey.pem",
2286 }
2287
2288 cc_library {
2289 name: "mylib",
2290 srcs: ["mylib.cpp"],
2291 system_shared_libs: [],
2292 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002293 // TODO: remove //apex_available:platform
2294 apex_available: [
2295 "//apex_available:platform",
2296 "myapex",
2297 ],
Alex Light9670d332019-01-29 18:07:33 -08002298 }
2299
2300 cc_library {
2301 name: "mylib_common",
2302 srcs: ["mylib.cpp"],
2303 system_shared_libs: [],
2304 stl: "none",
2305 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002306 // TODO: remove //apex_available:platform
2307 apex_available: [
2308 "//apex_available:platform",
2309 "myapex",
2310 ],
Alex Light9670d332019-01-29 18:07:33 -08002311 }
2312
2313 cc_library {
2314 name: "mylib2",
2315 srcs: ["mylib.cpp"],
2316 system_shared_libs: [],
2317 stl: "none",
2318 compile_multilib: "first",
2319 }
2320 `)
2321
Sundong Ahnabb64432019-10-22 13:58:29 +09002322 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002323 copyCmds := apexRule.Args["copy_commands"]
2324
2325 // Ensure that main rule creates an output
2326 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2327
2328 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002329 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2330 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2331 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002332
2333 // Ensure that both direct and indirect deps are copied into apex
2334 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2335 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2336 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2337
Colin Cross7113d202019-11-20 16:39:12 -08002338 // Ensure that the platform variant ends with _shared
2339 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2340 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2341 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002342}
Jiyong Park04480cf2019-02-06 00:16:29 +09002343
2344func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002345 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002346 apex {
2347 name: "myapex",
2348 key: "myapex.key",
2349 binaries: ["myscript"],
2350 }
2351
2352 apex_key {
2353 name: "myapex.key",
2354 public_key: "testkey.avbpubkey",
2355 private_key: "testkey.pem",
2356 }
2357
2358 sh_binary {
2359 name: "myscript",
2360 src: "mylib.cpp",
2361 filename: "myscript.sh",
2362 sub_dir: "script",
2363 }
2364 `)
2365
Sundong Ahnabb64432019-10-22 13:58:29 +09002366 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002367 copyCmds := apexRule.Args["copy_commands"]
2368
2369 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2370}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002371
Jooyung Han91df2082019-11-20 01:49:42 +09002372func TestApexInVariousPartition(t *testing.T) {
2373 testcases := []struct {
2374 propName, parition, flattenedPartition string
2375 }{
2376 {"", "system", "system_ext"},
2377 {"product_specific: true", "product", "product"},
2378 {"soc_specific: true", "vendor", "vendor"},
2379 {"proprietary: true", "vendor", "vendor"},
2380 {"vendor: true", "vendor", "vendor"},
2381 {"system_ext_specific: true", "system_ext", "system_ext"},
2382 }
2383 for _, tc := range testcases {
2384 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2385 ctx, _ := testApex(t, `
2386 apex {
2387 name: "myapex",
2388 key: "myapex.key",
2389 `+tc.propName+`
2390 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002391
Jooyung Han91df2082019-11-20 01:49:42 +09002392 apex_key {
2393 name: "myapex.key",
2394 public_key: "testkey.avbpubkey",
2395 private_key: "testkey.pem",
2396 }
2397 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002398
Jooyung Han91df2082019-11-20 01:49:42 +09002399 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2400 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2401 actual := apex.installDir.String()
2402 if actual != expected {
2403 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2404 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002405
Jooyung Han91df2082019-11-20 01:49:42 +09002406 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2407 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2408 actual = flattened.installDir.String()
2409 if actual != expected {
2410 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2411 }
2412 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002413 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002414}
Jiyong Park67882562019-03-21 01:11:21 +09002415
Jooyung Han54aca7b2019-11-20 02:26:02 +09002416func TestFileContexts(t *testing.T) {
2417 ctx, _ := testApex(t, `
2418 apex {
2419 name: "myapex",
2420 key: "myapex.key",
2421 }
2422
2423 apex_key {
2424 name: "myapex.key",
2425 public_key: "testkey.avbpubkey",
2426 private_key: "testkey.pem",
2427 }
2428 `)
2429 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2430 apexRule := module.Rule("apexRule")
2431 actual := apexRule.Args["file_contexts"]
2432 expected := "system/sepolicy/apex/myapex-file_contexts"
2433 if actual != expected {
2434 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2435 }
2436
2437 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2438 apex {
2439 name: "myapex",
2440 key: "myapex.key",
2441 file_contexts: "my_own_file_contexts",
2442 }
2443
2444 apex_key {
2445 name: "myapex.key",
2446 public_key: "testkey.avbpubkey",
2447 private_key: "testkey.pem",
2448 }
2449 `, withFiles(map[string][]byte{
2450 "my_own_file_contexts": nil,
2451 }))
2452
2453 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2454 apex {
2455 name: "myapex",
2456 key: "myapex.key",
2457 product_specific: true,
2458 file_contexts: "product_specific_file_contexts",
2459 }
2460
2461 apex_key {
2462 name: "myapex.key",
2463 public_key: "testkey.avbpubkey",
2464 private_key: "testkey.pem",
2465 }
2466 `)
2467
2468 ctx, _ = testApex(t, `
2469 apex {
2470 name: "myapex",
2471 key: "myapex.key",
2472 product_specific: true,
2473 file_contexts: "product_specific_file_contexts",
2474 }
2475
2476 apex_key {
2477 name: "myapex.key",
2478 public_key: "testkey.avbpubkey",
2479 private_key: "testkey.pem",
2480 }
2481 `, withFiles(map[string][]byte{
2482 "product_specific_file_contexts": nil,
2483 }))
2484 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2485 apexRule = module.Rule("apexRule")
2486 actual = apexRule.Args["file_contexts"]
2487 expected = "product_specific_file_contexts"
2488 if actual != expected {
2489 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2490 }
2491
2492 ctx, _ = testApex(t, `
2493 apex {
2494 name: "myapex",
2495 key: "myapex.key",
2496 product_specific: true,
2497 file_contexts: ":my-file-contexts",
2498 }
2499
2500 apex_key {
2501 name: "myapex.key",
2502 public_key: "testkey.avbpubkey",
2503 private_key: "testkey.pem",
2504 }
2505
2506 filegroup {
2507 name: "my-file-contexts",
2508 srcs: ["product_specific_file_contexts"],
2509 }
2510 `, withFiles(map[string][]byte{
2511 "product_specific_file_contexts": nil,
2512 }))
2513 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2514 apexRule = module.Rule("apexRule")
2515 actual = apexRule.Args["file_contexts"]
2516 expected = "product_specific_file_contexts"
2517 if actual != expected {
2518 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2519 }
2520}
2521
Jiyong Park67882562019-03-21 01:11:21 +09002522func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002523 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002524 apex_key {
2525 name: "myapex.key",
2526 public_key: ":my.avbpubkey",
2527 private_key: ":my.pem",
2528 product_specific: true,
2529 }
2530
2531 filegroup {
2532 name: "my.avbpubkey",
2533 srcs: ["testkey2.avbpubkey"],
2534 }
2535
2536 filegroup {
2537 name: "my.pem",
2538 srcs: ["testkey2.pem"],
2539 }
2540 `)
2541
2542 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2543 expected_pubkey := "testkey2.avbpubkey"
2544 actual_pubkey := apex_key.public_key_file.String()
2545 if actual_pubkey != expected_pubkey {
2546 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2547 }
2548 expected_privkey := "testkey2.pem"
2549 actual_privkey := apex_key.private_key_file.String()
2550 if actual_privkey != expected_privkey {
2551 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2552 }
2553}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002554
2555func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002556 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002557 prebuilt_apex {
2558 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002559 arch: {
2560 arm64: {
2561 src: "myapex-arm64.apex",
2562 },
2563 arm: {
2564 src: "myapex-arm.apex",
2565 },
2566 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002567 }
2568 `)
2569
2570 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2571
Jiyong Parkc95714e2019-03-29 14:23:10 +09002572 expectedInput := "myapex-arm64.apex"
2573 if prebuilt.inputApex.String() != expectedInput {
2574 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2575 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002576}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002577
2578func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002579 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002580 prebuilt_apex {
2581 name: "myapex",
2582 src: "myapex-arm.apex",
2583 filename: "notmyapex.apex",
2584 }
2585 `)
2586
2587 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2588
2589 expected := "notmyapex.apex"
2590 if p.installFilename != expected {
2591 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2592 }
2593}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002594
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002595func TestPrebuiltOverrides(t *testing.T) {
2596 ctx, config := testApex(t, `
2597 prebuilt_apex {
2598 name: "myapex.prebuilt",
2599 src: "myapex-arm.apex",
2600 overrides: [
2601 "myapex",
2602 ],
2603 }
2604 `)
2605
2606 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2607
2608 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002609 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002610 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002611 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002612 }
2613}
2614
Roland Levillain630846d2019-06-26 12:48:34 +01002615func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002616 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002617 apex_test {
2618 name: "myapex",
2619 key: "myapex.key",
2620 tests: [
2621 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002622 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002623 ],
2624 }
2625
2626 apex_key {
2627 name: "myapex.key",
2628 public_key: "testkey.avbpubkey",
2629 private_key: "testkey.pem",
2630 }
2631
2632 cc_test {
2633 name: "mytest",
2634 gtest: false,
2635 srcs: ["mytest.cpp"],
2636 relative_install_path: "test",
2637 system_shared_libs: [],
2638 static_executable: true,
2639 stl: "none",
2640 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002641
2642 cc_test {
2643 name: "mytests",
2644 gtest: false,
2645 srcs: [
2646 "mytest1.cpp",
2647 "mytest2.cpp",
2648 "mytest3.cpp",
2649 ],
2650 test_per_src: true,
2651 relative_install_path: "test",
2652 system_shared_libs: [],
2653 static_executable: true,
2654 stl: "none",
2655 }
Roland Levillain630846d2019-06-26 12:48:34 +01002656 `)
2657
Sundong Ahnabb64432019-10-22 13:58:29 +09002658 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002659 copyCmds := apexRule.Args["copy_commands"]
2660
2661 // Ensure that test dep is copied into apex.
2662 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002663
2664 // Ensure that test deps built with `test_per_src` are copied into apex.
2665 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2666 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2667 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002668
2669 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002670 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002671 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2672 name := apexBundle.BaseModuleName()
2673 prefix := "TARGET_"
2674 var builder strings.Builder
2675 data.Custom(&builder, name, prefix, "", data)
2676 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002677 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2678 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2679 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2680 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002681 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002682 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002683 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002684}
2685
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002686func TestInstallExtraFlattenedApexes(t *testing.T) {
2687 ctx, config := testApex(t, `
2688 apex {
2689 name: "myapex",
2690 key: "myapex.key",
2691 }
2692 apex_key {
2693 name: "myapex.key",
2694 public_key: "testkey.avbpubkey",
2695 private_key: "testkey.pem",
2696 }
2697 `, func(fs map[string][]byte, config android.Config) {
2698 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2699 })
2700 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002701 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002702 mk := android.AndroidMkDataForTest(t, config, "", ab)
2703 var builder strings.Builder
2704 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2705 androidMk := builder.String()
2706 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2707}
2708
Jooyung Han5c998b92019-06-27 11:30:33 +09002709func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002710 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002711 apex {
2712 name: "myapex",
2713 key: "myapex.key",
2714 native_shared_libs: ["mylib"],
2715 uses: ["commonapex"],
2716 }
2717
2718 apex {
2719 name: "commonapex",
2720 key: "myapex.key",
2721 native_shared_libs: ["libcommon"],
2722 provide_cpp_shared_libs: true,
2723 }
2724
2725 apex_key {
2726 name: "myapex.key",
2727 public_key: "testkey.avbpubkey",
2728 private_key: "testkey.pem",
2729 }
2730
2731 cc_library {
2732 name: "mylib",
2733 srcs: ["mylib.cpp"],
2734 shared_libs: ["libcommon"],
2735 system_shared_libs: [],
2736 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002737 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002738 }
2739
2740 cc_library {
2741 name: "libcommon",
2742 srcs: ["mylib_common.cpp"],
2743 system_shared_libs: [],
2744 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002745 // TODO: remove //apex_available:platform
2746 apex_available: [
2747 "//apex_available:platform",
2748 "commonapex",
2749 "myapex",
2750 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002751 }
2752 `)
2753
Sundong Ahnabb64432019-10-22 13:58:29 +09002754 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002755 apexRule1 := module1.Rule("apexRule")
2756 copyCmds1 := apexRule1.Args["copy_commands"]
2757
Sundong Ahnabb64432019-10-22 13:58:29 +09002758 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002759 apexRule2 := module2.Rule("apexRule")
2760 copyCmds2 := apexRule2.Args["copy_commands"]
2761
Colin Cross7113d202019-11-20 16:39:12 -08002762 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2763 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002764 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2765 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2766 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2767}
2768
2769func TestApexUsesFailsIfNotProvided(t *testing.T) {
2770 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2771 apex {
2772 name: "myapex",
2773 key: "myapex.key",
2774 uses: ["commonapex"],
2775 }
2776
2777 apex {
2778 name: "commonapex",
2779 key: "myapex.key",
2780 }
2781
2782 apex_key {
2783 name: "myapex.key",
2784 public_key: "testkey.avbpubkey",
2785 private_key: "testkey.pem",
2786 }
2787 `)
2788 testApexError(t, `uses: "commonapex" is not a provider`, `
2789 apex {
2790 name: "myapex",
2791 key: "myapex.key",
2792 uses: ["commonapex"],
2793 }
2794
2795 cc_library {
2796 name: "commonapex",
2797 system_shared_libs: [],
2798 stl: "none",
2799 }
2800
2801 apex_key {
2802 name: "myapex.key",
2803 public_key: "testkey.avbpubkey",
2804 private_key: "testkey.pem",
2805 }
2806 `)
2807}
2808
2809func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2810 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2811 apex {
2812 name: "myapex",
2813 key: "myapex.key",
2814 use_vendor: true,
2815 uses: ["commonapex"],
2816 }
2817
2818 apex {
2819 name: "commonapex",
2820 key: "myapex.key",
2821 provide_cpp_shared_libs: true,
2822 }
2823
2824 apex_key {
2825 name: "myapex.key",
2826 public_key: "testkey.avbpubkey",
2827 private_key: "testkey.pem",
2828 }
Jooyung Handc782442019-11-01 03:14:38 +09002829 `, func(fs map[string][]byte, config android.Config) {
2830 setUseVendorWhitelistForTest(config, []string{"myapex"})
2831 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002832}
2833
Jooyung Hand48f3c32019-08-23 11:18:57 +09002834func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2835 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2836 apex {
2837 name: "myapex",
2838 key: "myapex.key",
2839 native_shared_libs: ["libfoo"],
2840 }
2841
2842 apex_key {
2843 name: "myapex.key",
2844 public_key: "testkey.avbpubkey",
2845 private_key: "testkey.pem",
2846 }
2847
2848 cc_library {
2849 name: "libfoo",
2850 stl: "none",
2851 system_shared_libs: [],
2852 enabled: false,
2853 }
2854 `)
2855 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2856 apex {
2857 name: "myapex",
2858 key: "myapex.key",
2859 java_libs: ["myjar"],
2860 }
2861
2862 apex_key {
2863 name: "myapex.key",
2864 public_key: "testkey.avbpubkey",
2865 private_key: "testkey.pem",
2866 }
2867
2868 java_library {
2869 name: "myjar",
2870 srcs: ["foo/bar/MyClass.java"],
2871 sdk_version: "none",
2872 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09002873 enabled: false,
2874 }
2875 `)
2876}
2877
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002878func TestApexWithApps(t *testing.T) {
2879 ctx, _ := testApex(t, `
2880 apex {
2881 name: "myapex",
2882 key: "myapex.key",
2883 apps: [
2884 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002885 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002886 ],
2887 }
2888
2889 apex_key {
2890 name: "myapex.key",
2891 public_key: "testkey.avbpubkey",
2892 private_key: "testkey.pem",
2893 }
2894
2895 android_app {
2896 name: "AppFoo",
2897 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08002898 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002899 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002900 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08002901 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002902 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002903 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002904
2905 android_app {
2906 name: "AppFooPriv",
2907 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08002908 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09002909 system_modules: "none",
2910 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08002911 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002912 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09002913 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002914
2915 cc_library_shared {
2916 name: "libjni",
2917 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002918 shared_libs: ["libfoo"],
2919 stl: "none",
2920 system_shared_libs: [],
2921 apex_available: [ "myapex" ],
2922 sdk_version: "current",
2923 }
2924
2925 cc_library_shared {
2926 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09002927 stl: "none",
2928 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09002929 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08002930 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09002931 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002932 `)
2933
Sundong Ahnabb64432019-10-22 13:58:29 +09002934 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002935 apexRule := module.Rule("apexRule")
2936 copyCmds := apexRule.Args["copy_commands"]
2937
2938 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002939 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002940
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002941 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
2942 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09002943 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002944 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002945 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002946 // JNI libraries including transitive deps are
2947 for _, jni := range []string{"libjni", "libfoo"} {
2948 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
2949 // ... embedded inside APK (jnilibs.zip)
2950 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
2951 // ... and not directly inside the APEX
2952 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
2953 }
Dario Frenicde2a032019-10-27 00:29:22 +01002954}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002955
Dario Frenicde2a032019-10-27 00:29:22 +01002956func TestApexWithAppImports(t *testing.T) {
2957 ctx, _ := testApex(t, `
2958 apex {
2959 name: "myapex",
2960 key: "myapex.key",
2961 apps: [
2962 "AppFooPrebuilt",
2963 "AppFooPrivPrebuilt",
2964 ],
2965 }
2966
2967 apex_key {
2968 name: "myapex.key",
2969 public_key: "testkey.avbpubkey",
2970 private_key: "testkey.pem",
2971 }
2972
2973 android_app_import {
2974 name: "AppFooPrebuilt",
2975 apk: "PrebuiltAppFoo.apk",
2976 presigned: true,
2977 dex_preopt: {
2978 enabled: false,
2979 },
2980 }
2981
2982 android_app_import {
2983 name: "AppFooPrivPrebuilt",
2984 apk: "PrebuiltAppFooPriv.apk",
2985 privileged: true,
2986 presigned: true,
2987 dex_preopt: {
2988 enabled: false,
2989 },
2990 }
2991 `)
2992
Sundong Ahnabb64432019-10-22 13:58:29 +09002993 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002994 apexRule := module.Rule("apexRule")
2995 copyCmds := apexRule.Args["copy_commands"]
2996
2997 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2998 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002999}
3000
Dario Freni6f3937c2019-12-20 22:58:03 +00003001func TestApexWithTestHelperApp(t *testing.T) {
3002 ctx, _ := testApex(t, `
3003 apex {
3004 name: "myapex",
3005 key: "myapex.key",
3006 apps: [
3007 "TesterHelpAppFoo",
3008 ],
3009 }
3010
3011 apex_key {
3012 name: "myapex.key",
3013 public_key: "testkey.avbpubkey",
3014 private_key: "testkey.pem",
3015 }
3016
3017 android_test_helper_app {
3018 name: "TesterHelpAppFoo",
3019 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003020 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003021 }
3022
3023 `)
3024
3025 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3026 apexRule := module.Rule("apexRule")
3027 copyCmds := apexRule.Args["copy_commands"]
3028
3029 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3030}
3031
Jooyung Han18020ea2019-11-13 10:50:48 +09003032func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3033 // libfoo's apex_available comes from cc_defaults
3034 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3035 apex {
3036 name: "myapex",
3037 key: "myapex.key",
3038 native_shared_libs: ["libfoo"],
3039 }
3040
3041 apex_key {
3042 name: "myapex.key",
3043 public_key: "testkey.avbpubkey",
3044 private_key: "testkey.pem",
3045 }
3046
3047 apex {
3048 name: "otherapex",
3049 key: "myapex.key",
3050 native_shared_libs: ["libfoo"],
3051 }
3052
3053 cc_defaults {
3054 name: "libfoo-defaults",
3055 apex_available: ["otherapex"],
3056 }
3057
3058 cc_library {
3059 name: "libfoo",
3060 defaults: ["libfoo-defaults"],
3061 stl: "none",
3062 system_shared_libs: [],
3063 }`)
3064}
3065
Jiyong Park127b40b2019-09-30 16:04:35 +09003066func TestApexAvailable(t *testing.T) {
3067 // libfoo is not available to myapex, but only to otherapex
3068 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3069 apex {
3070 name: "myapex",
3071 key: "myapex.key",
3072 native_shared_libs: ["libfoo"],
3073 }
3074
3075 apex_key {
3076 name: "myapex.key",
3077 public_key: "testkey.avbpubkey",
3078 private_key: "testkey.pem",
3079 }
3080
3081 apex {
3082 name: "otherapex",
3083 key: "otherapex.key",
3084 native_shared_libs: ["libfoo"],
3085 }
3086
3087 apex_key {
3088 name: "otherapex.key",
3089 public_key: "testkey.avbpubkey",
3090 private_key: "testkey.pem",
3091 }
3092
3093 cc_library {
3094 name: "libfoo",
3095 stl: "none",
3096 system_shared_libs: [],
3097 apex_available: ["otherapex"],
3098 }`)
3099
3100 // libbar is an indirect dep
3101 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3102 apex {
3103 name: "myapex",
3104 key: "myapex.key",
3105 native_shared_libs: ["libfoo"],
3106 }
3107
3108 apex_key {
3109 name: "myapex.key",
3110 public_key: "testkey.avbpubkey",
3111 private_key: "testkey.pem",
3112 }
3113
3114 apex {
3115 name: "otherapex",
3116 key: "otherapex.key",
3117 native_shared_libs: ["libfoo"],
3118 }
3119
3120 apex_key {
3121 name: "otherapex.key",
3122 public_key: "testkey.avbpubkey",
3123 private_key: "testkey.pem",
3124 }
3125
3126 cc_library {
3127 name: "libfoo",
3128 stl: "none",
3129 shared_libs: ["libbar"],
3130 system_shared_libs: [],
3131 apex_available: ["myapex", "otherapex"],
3132 }
3133
3134 cc_library {
3135 name: "libbar",
3136 stl: "none",
3137 system_shared_libs: [],
3138 apex_available: ["otherapex"],
3139 }`)
3140
3141 testApexError(t, "\"otherapex\" is not a valid module name", `
3142 apex {
3143 name: "myapex",
3144 key: "myapex.key",
3145 native_shared_libs: ["libfoo"],
3146 }
3147
3148 apex_key {
3149 name: "myapex.key",
3150 public_key: "testkey.avbpubkey",
3151 private_key: "testkey.pem",
3152 }
3153
3154 cc_library {
3155 name: "libfoo",
3156 stl: "none",
3157 system_shared_libs: [],
3158 apex_available: ["otherapex"],
3159 }`)
3160
3161 ctx, _ := testApex(t, `
3162 apex {
3163 name: "myapex",
3164 key: "myapex.key",
3165 native_shared_libs: ["libfoo", "libbar"],
3166 }
3167
3168 apex_key {
3169 name: "myapex.key",
3170 public_key: "testkey.avbpubkey",
3171 private_key: "testkey.pem",
3172 }
3173
3174 cc_library {
3175 name: "libfoo",
3176 stl: "none",
3177 system_shared_libs: [],
3178 apex_available: ["myapex"],
3179 }
3180
3181 cc_library {
3182 name: "libbar",
3183 stl: "none",
3184 system_shared_libs: [],
3185 apex_available: ["//apex_available:anyapex"],
3186 }`)
3187
3188 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003189 // TODO(jiyong) the checks for the platform variant are removed because we now create
3190 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3191 // the platform variants are not used from other platform modules. When that is done,
3192 // these checks will be replaced by expecting a specific error message that will be
3193 // emitted when the platform variant is used.
3194 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3195 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3196 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3197 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003198
3199 ctx, _ = testApex(t, `
3200 apex {
3201 name: "myapex",
3202 key: "myapex.key",
3203 }
3204
3205 apex_key {
3206 name: "myapex.key",
3207 public_key: "testkey.avbpubkey",
3208 private_key: "testkey.pem",
3209 }
3210
3211 cc_library {
3212 name: "libfoo",
3213 stl: "none",
3214 system_shared_libs: [],
3215 apex_available: ["//apex_available:platform"],
3216 }`)
3217
3218 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003219 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3220 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003221
3222 ctx, _ = testApex(t, `
3223 apex {
3224 name: "myapex",
3225 key: "myapex.key",
3226 native_shared_libs: ["libfoo"],
3227 }
3228
3229 apex_key {
3230 name: "myapex.key",
3231 public_key: "testkey.avbpubkey",
3232 private_key: "testkey.pem",
3233 }
3234
3235 cc_library {
3236 name: "libfoo",
3237 stl: "none",
3238 system_shared_libs: [],
3239 apex_available: ["myapex"],
3240 static: {
3241 apex_available: ["//apex_available:platform"],
3242 },
3243 }`)
3244
3245 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003246 // TODO(jiyong) the checks for the platform variant are removed because we now create
3247 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3248 // the platform variants are not used from other platform modules. When that is done,
3249 // these checks will be replaced by expecting a specific error message that will be
3250 // emitted when the platform variant is used.
3251 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3252 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3253 // // but the static variant is available to both myapex and the platform
3254 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3255 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003256}
3257
Jiyong Park5d790c32019-11-15 18:40:32 +09003258func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003259 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003260 apex {
3261 name: "myapex",
3262 key: "myapex.key",
3263 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003264 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003265 }
3266
3267 override_apex {
3268 name: "override_myapex",
3269 base: "myapex",
3270 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003271 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003272 logging_parent: "com.foo.bar",
Jiyong Park5d790c32019-11-15 18:40:32 +09003273 }
3274
3275 apex_key {
3276 name: "myapex.key",
3277 public_key: "testkey.avbpubkey",
3278 private_key: "testkey.pem",
3279 }
3280
3281 android_app {
3282 name: "app",
3283 srcs: ["foo/bar/MyClass.java"],
3284 package_name: "foo",
3285 sdk_version: "none",
3286 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003287 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003288 }
3289
3290 override_android_app {
3291 name: "override_app",
3292 base: "app",
3293 package_name: "bar",
3294 }
3295 `)
3296
Jiyong Park317645e2019-12-05 13:20:58 +09003297 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3298 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3299 if originalVariant.GetOverriddenBy() != "" {
3300 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3301 }
3302 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3303 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3304 }
3305
Jiyong Park5d790c32019-11-15 18:40:32 +09003306 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3307 apexRule := module.Rule("apexRule")
3308 copyCmds := apexRule.Args["copy_commands"]
3309
3310 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3311 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003312
3313 apexBundle := module.Module().(*apexBundle)
3314 name := apexBundle.Name()
3315 if name != "override_myapex" {
3316 t.Errorf("name should be \"override_myapex\", but was %q", name)
3317 }
3318
Baligh Uddin004d7172020-02-19 21:29:28 -08003319 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3320 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3321 }
3322
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003323 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3324 var builder strings.Builder
3325 data.Custom(&builder, name, "TARGET_", "", data)
3326 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003327 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003328 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3329 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003330 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003331 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003332 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003333 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3334 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003335}
3336
Jooyung Han214bf372019-11-12 13:03:50 +09003337func TestLegacyAndroid10Support(t *testing.T) {
3338 ctx, _ := testApex(t, `
3339 apex {
3340 name: "myapex",
3341 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003342 native_shared_libs: ["mylib"],
Jooyung Han214bf372019-11-12 13:03:50 +09003343 legacy_android10_support: true,
3344 }
3345
3346 apex_key {
3347 name: "myapex.key",
3348 public_key: "testkey.avbpubkey",
3349 private_key: "testkey.pem",
3350 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003351
3352 cc_library {
3353 name: "mylib",
3354 srcs: ["mylib.cpp"],
3355 stl: "libc++",
3356 system_shared_libs: [],
3357 apex_available: [ "myapex" ],
3358 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003359 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003360
3361 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3362 args := module.Rule("apexRule").Args
3363 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003364 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003365
3366 // The copies of the libraries in the apex should have one more dependency than
3367 // the ones outside the apex, namely the unwinder. Ideally we should check
3368 // the dependency names directly here but for some reason the names are blank in
3369 // this test.
3370 for _, lib := range []string{"libc++", "mylib"} {
3371 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3372 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3373 if len(apexImplicits) != len(nonApexImplicits)+1 {
3374 t.Errorf("%q missing unwinder dep", lib)
3375 }
3376 }
Jooyung Han214bf372019-11-12 13:03:50 +09003377}
3378
Jooyung Han58f26ab2019-12-18 15:34:32 +09003379func TestJavaSDKLibrary(t *testing.T) {
3380 ctx, _ := testApex(t, `
3381 apex {
3382 name: "myapex",
3383 key: "myapex.key",
3384 java_libs: ["foo"],
3385 }
3386
3387 apex_key {
3388 name: "myapex.key",
3389 public_key: "testkey.avbpubkey",
3390 private_key: "testkey.pem",
3391 }
3392
3393 java_sdk_library {
3394 name: "foo",
3395 srcs: ["a.java"],
3396 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003397 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003398 }
3399 `, withFiles(map[string][]byte{
3400 "api/current.txt": nil,
3401 "api/removed.txt": nil,
3402 "api/system-current.txt": nil,
3403 "api/system-removed.txt": nil,
3404 "api/test-current.txt": nil,
3405 "api/test-removed.txt": nil,
3406 }))
3407
3408 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003409 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003410 "javalib/foo.jar",
3411 "etc/permissions/foo.xml",
3412 })
3413 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003414 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3415 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003416}
3417
atrost6e126252020-01-27 17:01:16 +00003418func TestCompatConfig(t *testing.T) {
3419 ctx, _ := testApex(t, `
3420 apex {
3421 name: "myapex",
3422 key: "myapex.key",
3423 prebuilts: ["myjar-platform-compat-config"],
3424 java_libs: ["myjar"],
3425 }
3426
3427 apex_key {
3428 name: "myapex.key",
3429 public_key: "testkey.avbpubkey",
3430 private_key: "testkey.pem",
3431 }
3432
3433 platform_compat_config {
3434 name: "myjar-platform-compat-config",
3435 src: ":myjar",
3436 }
3437
3438 java_library {
3439 name: "myjar",
3440 srcs: ["foo/bar/MyClass.java"],
3441 sdk_version: "none",
3442 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003443 apex_available: [ "myapex" ],
3444 }
3445 `)
3446 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3447 "etc/compatconfig/myjar-platform-compat-config.xml",
3448 "javalib/myjar.jar",
3449 })
3450}
3451
Jiyong Park479321d2019-12-16 11:47:12 +09003452func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3453 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3454 apex {
3455 name: "myapex",
3456 key: "myapex.key",
3457 java_libs: ["myjar"],
3458 }
3459
3460 apex_key {
3461 name: "myapex.key",
3462 public_key: "testkey.avbpubkey",
3463 private_key: "testkey.pem",
3464 }
3465
3466 java_library {
3467 name: "myjar",
3468 srcs: ["foo/bar/MyClass.java"],
3469 sdk_version: "none",
3470 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003471 compile_dex: false,
Jiyong Park479321d2019-12-16 11:47:12 +09003472 }
3473 `)
3474}
3475
Jiyong Park7afd1072019-12-30 16:56:33 +09003476func TestCarryRequiredModuleNames(t *testing.T) {
3477 ctx, config := testApex(t, `
3478 apex {
3479 name: "myapex",
3480 key: "myapex.key",
3481 native_shared_libs: ["mylib"],
3482 }
3483
3484 apex_key {
3485 name: "myapex.key",
3486 public_key: "testkey.avbpubkey",
3487 private_key: "testkey.pem",
3488 }
3489
3490 cc_library {
3491 name: "mylib",
3492 srcs: ["mylib.cpp"],
3493 system_shared_libs: [],
3494 stl: "none",
3495 required: ["a", "b"],
3496 host_required: ["c", "d"],
3497 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003498 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003499 }
3500 `)
3501
3502 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3503 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3504 name := apexBundle.BaseModuleName()
3505 prefix := "TARGET_"
3506 var builder strings.Builder
3507 data.Custom(&builder, name, prefix, "", data)
3508 androidMk := builder.String()
3509 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3510 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3511 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3512}
3513
Jiyong Park7cd10e32020-01-14 09:22:18 +09003514func TestSymlinksFromApexToSystem(t *testing.T) {
3515 bp := `
3516 apex {
3517 name: "myapex",
3518 key: "myapex.key",
3519 native_shared_libs: ["mylib"],
3520 java_libs: ["myjar"],
3521 }
3522
Jiyong Park9d677202020-02-19 16:29:35 +09003523 apex {
3524 name: "myapex.updatable",
3525 key: "myapex.key",
3526 native_shared_libs: ["mylib"],
3527 java_libs: ["myjar"],
3528 updatable: true,
3529 }
3530
Jiyong Park7cd10e32020-01-14 09:22:18 +09003531 apex_key {
3532 name: "myapex.key",
3533 public_key: "testkey.avbpubkey",
3534 private_key: "testkey.pem",
3535 }
3536
3537 cc_library {
3538 name: "mylib",
3539 srcs: ["mylib.cpp"],
3540 shared_libs: ["myotherlib"],
3541 system_shared_libs: [],
3542 stl: "none",
3543 apex_available: [
3544 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003545 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003546 "//apex_available:platform",
3547 ],
3548 }
3549
3550 cc_library {
3551 name: "myotherlib",
3552 srcs: ["mylib.cpp"],
3553 system_shared_libs: [],
3554 stl: "none",
3555 apex_available: [
3556 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003557 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003558 "//apex_available:platform",
3559 ],
3560 }
3561
3562 java_library {
3563 name: "myjar",
3564 srcs: ["foo/bar/MyClass.java"],
3565 sdk_version: "none",
3566 system_modules: "none",
3567 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003568 apex_available: [
3569 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003570 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003571 "//apex_available:platform",
3572 ],
3573 }
3574
3575 java_library {
3576 name: "myotherjar",
3577 srcs: ["foo/bar/MyClass.java"],
3578 sdk_version: "none",
3579 system_modules: "none",
3580 apex_available: [
3581 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003582 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003583 "//apex_available:platform",
3584 ],
3585 }
3586 `
3587
3588 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3589 for _, f := range files {
3590 if f.path == file {
3591 if f.isLink {
3592 t.Errorf("%q is not a real file", file)
3593 }
3594 return
3595 }
3596 }
3597 t.Errorf("%q is not found", file)
3598 }
3599
3600 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3601 for _, f := range files {
3602 if f.path == file {
3603 if !f.isLink {
3604 t.Errorf("%q is not a symlink", file)
3605 }
3606 return
3607 }
3608 }
3609 t.Errorf("%q is not found", file)
3610 }
3611
Jiyong Park9d677202020-02-19 16:29:35 +09003612 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3613 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003614 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003615 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003616 ensureRealfileExists(t, files, "javalib/myjar.jar")
3617 ensureRealfileExists(t, files, "lib64/mylib.so")
3618 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3619
Jiyong Park9d677202020-02-19 16:29:35 +09003620 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3621 ensureRealfileExists(t, files, "javalib/myjar.jar")
3622 ensureRealfileExists(t, files, "lib64/mylib.so")
3623 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3624
3625 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003626 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003627 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003628 ensureRealfileExists(t, files, "javalib/myjar.jar")
3629 ensureRealfileExists(t, files, "lib64/mylib.so")
3630 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09003631
3632 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3633 ensureRealfileExists(t, files, "javalib/myjar.jar")
3634 ensureRealfileExists(t, files, "lib64/mylib.so")
3635 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09003636}
3637
Jooyung Han643adc42020-02-27 13:50:06 +09003638func TestApexWithJniLibs(t *testing.T) {
3639 ctx, _ := testApex(t, `
3640 apex {
3641 name: "myapex",
3642 key: "myapex.key",
3643 jni_libs: ["mylib"],
3644 }
3645
3646 apex_key {
3647 name: "myapex.key",
3648 public_key: "testkey.avbpubkey",
3649 private_key: "testkey.pem",
3650 }
3651
3652 cc_library {
3653 name: "mylib",
3654 srcs: ["mylib.cpp"],
3655 shared_libs: ["mylib2"],
3656 system_shared_libs: [],
3657 stl: "none",
3658 apex_available: [ "myapex" ],
3659 }
3660
3661 cc_library {
3662 name: "mylib2",
3663 srcs: ["mylib.cpp"],
3664 system_shared_libs: [],
3665 stl: "none",
3666 apex_available: [ "myapex" ],
3667 }
3668 `)
3669
3670 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
3671 // Notice mylib2.so (transitive dep) is not added as a jni_lib
3672 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
3673 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3674 "lib64/mylib.so",
3675 "lib64/mylib2.so",
3676 })
3677}
3678
3679func TestApexWithJniLibs_Errors(t *testing.T) {
3680 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
3681 apex {
3682 name: "myapex",
3683 key: "myapex.key",
3684 jni_libs: ["xxx"],
3685 }
3686
3687 apex_key {
3688 name: "myapex.key",
3689 public_key: "testkey.avbpubkey",
3690 private_key: "testkey.pem",
3691 }
3692
3693 prebuilt_etc {
3694 name: "xxx",
3695 src: "xxx",
3696 }
3697 `, withFiles(map[string][]byte{
3698 "xxx": nil,
3699 }))
3700}
3701
Jiyong Parkbd159612020-02-28 15:22:21 +09003702func TestAppBundle(t *testing.T) {
3703 ctx, _ := testApex(t, `
3704 apex {
3705 name: "myapex",
3706 key: "myapex.key",
3707 apps: ["AppFoo"],
3708 }
3709
3710 apex_key {
3711 name: "myapex.key",
3712 public_key: "testkey.avbpubkey",
3713 private_key: "testkey.pem",
3714 }
3715
3716 android_app {
3717 name: "AppFoo",
3718 srcs: ["foo/bar/MyClass.java"],
3719 sdk_version: "none",
3720 system_modules: "none",
3721 apex_available: [ "myapex" ],
3722 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09003723 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09003724
3725 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
3726 content := bundleConfigRule.Args["content"]
3727
3728 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09003729 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 +09003730}
3731
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003732func TestMain(m *testing.M) {
3733 run := func() int {
3734 setUp()
3735 defer tearDown()
3736
3737 return m.Run()
3738 }
3739
3740 os.Exit(run())
3741}