blob: 1eab4da109dd6eff8e7a2b214642ee87ff789a38 [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 Parkaf8998c2020-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
235// ensure that 'result' contains 'expected'
236func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900237 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238 if !strings.Contains(result, expected) {
239 t.Errorf("%q is not found in %q", expected, result)
240 }
241}
242
243// ensures that 'result' does not contain 'notExpected'
244func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900245 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900246 if strings.Contains(result, notExpected) {
247 t.Errorf("%q is found in %q", notExpected, result)
248 }
249}
250
251func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900252 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900253 if !android.InList(expected, result) {
254 t.Errorf("%q is not found in %v", expected, result)
255 }
256}
257
258func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900259 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900260 if android.InList(notExpected, result) {
261 t.Errorf("%q is found in %v", notExpected, result)
262 }
263}
264
Jooyung Hane1633032019-08-01 17:41:43 +0900265func ensureListEmpty(t *testing.T, result []string) {
266 t.Helper()
267 if len(result) > 0 {
268 t.Errorf("%q is expected to be empty", result)
269 }
270}
271
Jiyong Park25fc6a92018-11-18 18:02:45 +0900272// Minimal test
273func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700274 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900275 apex_defaults {
276 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900277 manifest: ":myapex.manifest",
278 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900279 key: "myapex.key",
280 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800281 multilib: {
282 both: {
283 binaries: ["foo",],
284 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900285 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900286 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 }
288
Jiyong Park30ca9372019-02-07 16:27:23 +0900289 apex {
290 name: "myapex",
291 defaults: ["myapex-defaults"],
292 }
293
Jiyong Park25fc6a92018-11-18 18:02:45 +0900294 apex_key {
295 name: "myapex.key",
296 public_key: "testkey.avbpubkey",
297 private_key: "testkey.pem",
298 }
299
Jiyong Park809bb722019-02-13 21:33:49 +0900300 filegroup {
301 name: "myapex.manifest",
302 srcs: ["apex_manifest.json"],
303 }
304
305 filegroup {
306 name: "myapex.androidmanifest",
307 srcs: ["AndroidManifest.xml"],
308 }
309
Jiyong Park25fc6a92018-11-18 18:02:45 +0900310 cc_library {
311 name: "mylib",
312 srcs: ["mylib.cpp"],
313 shared_libs: ["mylib2"],
314 system_shared_libs: [],
315 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000316 // TODO: remove //apex_available:platform
317 apex_available: [
318 "//apex_available:platform",
319 "myapex",
320 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 }
322
Alex Light3d673592019-01-18 14:37:31 -0800323 cc_binary {
324 name: "foo",
325 srcs: ["mylib.cpp"],
326 compile_multilib: "both",
327 multilib: {
328 lib32: {
329 suffix: "32",
330 },
331 lib64: {
332 suffix: "64",
333 },
334 },
335 symlinks: ["foo_link_"],
336 symlink_preferred_arch: true,
337 system_shared_libs: [],
338 static_executable: true,
339 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000340 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800341 }
342
Jiyong Park25fc6a92018-11-18 18:02:45 +0900343 cc_library {
344 name: "mylib2",
345 srcs: ["mylib.cpp"],
346 system_shared_libs: [],
347 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900348 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000349 // TODO: remove //apex_available:platform
350 apex_available: [
351 "//apex_available:platform",
352 "myapex",
353 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900354 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900355
356 java_library {
357 name: "myjar",
358 srcs: ["foo/bar/MyClass.java"],
359 sdk_version: "none",
360 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900361 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900362 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000363 // TODO: remove //apex_available:platform
364 apex_available: [
365 "//apex_available:platform",
366 "myapex",
367 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900368 }
369
370 java_library {
371 name: "myotherjar",
372 srcs: ["foo/bar/MyClass.java"],
373 sdk_version: "none",
374 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900375 // TODO: remove //apex_available:platform
376 apex_available: [
377 "//apex_available:platform",
378 "myapex",
379 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900380 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900381
382 java_library {
383 name: "mysharedjar",
384 srcs: ["foo/bar/MyClass.java"],
385 sdk_version: "none",
386 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900387 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900388 `)
389
Sundong Ahnabb64432019-10-22 13:58:29 +0900390 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900391
392 optFlags := apexRule.Args["opt_flags"]
393 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700394 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900395 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900396
Jiyong Park25fc6a92018-11-18 18:02:45 +0900397 copyCmds := apexRule.Args["copy_commands"]
398
399 // Ensure that main rule creates an output
400 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
401
402 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800403 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900404 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900405
406 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800407 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900408 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900409
410 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800411 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
412 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900413 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
414 // .. but not for java libs
415 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900416 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800417
Colin Cross7113d202019-11-20 16:39:12 -0800418 // Ensure that the platform variant ends with _shared or _common
419 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
420 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900421 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
422 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900423 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
424
425 // Ensure that dynamic dependency to java libs are not included
426 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800427
428 // Ensure that all symlinks are present.
429 found_foo_link_64 := false
430 found_foo := false
431 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900432 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800433 if strings.HasSuffix(cmd, "bin/foo") {
434 found_foo = true
435 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
436 found_foo_link_64 = true
437 }
438 }
439 }
440 good := found_foo && found_foo_link_64
441 if !good {
442 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
443 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900444
Sundong Ahnabb64432019-10-22 13:58:29 +0900445 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700446 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700447 if len(noticeInputs) != 2 {
448 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900449 }
450 ensureListContains(t, noticeInputs, "NOTICE")
451 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900452
453 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 +0900454 ensureListContains(t, depsInfo, "myjar <- myapex")
455 ensureListContains(t, depsInfo, "mylib <- myapex")
456 ensureListContains(t, depsInfo, "mylib2 <- mylib")
457 ensureListContains(t, depsInfo, "myotherjar <- myjar")
458 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800459}
460
Jooyung Hanf21c7972019-12-16 22:32:06 +0900461func TestDefaults(t *testing.T) {
462 ctx, _ := testApex(t, `
463 apex_defaults {
464 name: "myapex-defaults",
465 key: "myapex.key",
466 prebuilts: ["myetc"],
467 native_shared_libs: ["mylib"],
468 java_libs: ["myjar"],
469 apps: ["AppFoo"],
470 }
471
472 prebuilt_etc {
473 name: "myetc",
474 src: "myprebuilt",
475 }
476
477 apex {
478 name: "myapex",
479 defaults: ["myapex-defaults"],
480 }
481
482 apex_key {
483 name: "myapex.key",
484 public_key: "testkey.avbpubkey",
485 private_key: "testkey.pem",
486 }
487
488 cc_library {
489 name: "mylib",
490 system_shared_libs: [],
491 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000492 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900493 }
494
495 java_library {
496 name: "myjar",
497 srcs: ["foo/bar/MyClass.java"],
498 sdk_version: "none",
499 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000500 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900501 }
502
503 android_app {
504 name: "AppFoo",
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 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000511 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900512 "etc/myetc",
513 "javalib/myjar.jar",
514 "lib64/mylib.so",
515 "app/AppFoo/AppFoo.apk",
516 })
517}
518
Jooyung Han01a3ee22019-11-02 02:52:25 +0900519func TestApexManifest(t *testing.T) {
520 ctx, _ := testApex(t, `
521 apex {
522 name: "myapex",
523 key: "myapex.key",
524 }
525
526 apex_key {
527 name: "myapex.key",
528 public_key: "testkey.avbpubkey",
529 private_key: "testkey.pem",
530 }
531 `)
532
533 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900534 args := module.Rule("apexRule").Args
535 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
536 t.Error("manifest should be apex_manifest.pb, but " + manifest)
537 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900538}
539
Alex Light5098a612018-11-29 17:12:15 -0800540func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700541 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800542 apex {
543 name: "myapex",
544 key: "myapex.key",
545 payload_type: "zip",
546 native_shared_libs: ["mylib"],
547 }
548
549 apex_key {
550 name: "myapex.key",
551 public_key: "testkey.avbpubkey",
552 private_key: "testkey.pem",
553 }
554
555 cc_library {
556 name: "mylib",
557 srcs: ["mylib.cpp"],
558 shared_libs: ["mylib2"],
559 system_shared_libs: [],
560 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000561 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800562 }
563
564 cc_library {
565 name: "mylib2",
566 srcs: ["mylib.cpp"],
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
Sundong Ahnabb64432019-10-22 13:58:29 +0900573 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800574 copyCmds := zipApexRule.Args["copy_commands"]
575
576 // Ensure that main rule creates an output
577 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
578
579 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800580 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800581
582 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800583 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800584
585 // Ensure that both direct and indirect deps are copied into apex
586 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
587 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900588}
589
590func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700591 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900592 apex {
593 name: "myapex",
594 key: "myapex.key",
595 native_shared_libs: ["mylib", "mylib3"],
596 }
597
598 apex_key {
599 name: "myapex.key",
600 public_key: "testkey.avbpubkey",
601 private_key: "testkey.pem",
602 }
603
604 cc_library {
605 name: "mylib",
606 srcs: ["mylib.cpp"],
607 shared_libs: ["mylib2", "mylib3"],
608 system_shared_libs: [],
609 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000610 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900611 }
612
613 cc_library {
614 name: "mylib2",
615 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900616 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900617 system_shared_libs: [],
618 stl: "none",
619 stubs: {
620 versions: ["1", "2", "3"],
621 },
622 }
623
624 cc_library {
625 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900626 srcs: ["mylib.cpp"],
627 shared_libs: ["mylib4"],
628 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900629 stl: "none",
630 stubs: {
631 versions: ["10", "11", "12"],
632 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000633 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900634 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900635
636 cc_library {
637 name: "mylib4",
638 srcs: ["mylib.cpp"],
639 system_shared_libs: [],
640 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000641 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900642 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900643 `)
644
Sundong Ahnabb64432019-10-22 13:58:29 +0900645 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900646 copyCmds := apexRule.Args["copy_commands"]
647
648 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800649 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900650
651 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800652 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900653
654 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800655 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900656
Colin Cross7113d202019-11-20 16:39:12 -0800657 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900658
659 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900660 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900661 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900662 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900663
664 // 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 -0800665 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900666 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800667 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900668
669 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900670 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900671 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900672
673 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900674 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900675
Jooyung Hana57af4a2020-01-23 05:36:59 +0000676 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900677 "lib64/mylib.so",
678 "lib64/mylib3.so",
679 "lib64/mylib4.so",
680 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900681}
682
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900683func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700684 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900685 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900686 name: "myapex2",
687 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900688 native_shared_libs: ["mylib"],
689 }
690
691 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900692 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900693 public_key: "testkey.avbpubkey",
694 private_key: "testkey.pem",
695 }
696
697 cc_library {
698 name: "mylib",
699 srcs: ["mylib.cpp"],
700 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900701 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900702 system_shared_libs: [],
703 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000704 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900705 }
706
707 cc_library {
708 name: "libfoo",
709 srcs: ["mylib.cpp"],
710 shared_libs: ["libbar"],
711 system_shared_libs: [],
712 stl: "none",
713 stubs: {
714 versions: ["10", "20", "30"],
715 },
716 }
717
718 cc_library {
719 name: "libbar",
720 srcs: ["mylib.cpp"],
721 system_shared_libs: [],
722 stl: "none",
723 }
724
Jiyong Park678c8812020-02-07 17:25:49 +0900725 cc_library_static {
726 name: "libbaz",
727 srcs: ["mylib.cpp"],
728 system_shared_libs: [],
729 stl: "none",
730 apex_available: [ "myapex2" ],
731 }
732
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900733 `)
734
Jiyong Park83dc74b2020-01-14 18:38:44 +0900735 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900736 copyCmds := apexRule.Args["copy_commands"]
737
738 // Ensure that direct non-stubs dep is always included
739 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
740
741 // Ensure that indirect stubs dep is not included
742 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
743
744 // Ensure that dependency of stubs is not included
745 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
746
Jiyong Park83dc74b2020-01-14 18:38:44 +0900747 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900748
749 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900750 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900751 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900752 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900753
Jiyong Park3ff16992019-12-27 14:11:47 +0900754 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900755
756 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
757 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900758
759 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 +0900760
761 ensureListContains(t, depsInfo, "mylib <- myapex2")
762 ensureListContains(t, depsInfo, "libbaz <- mylib")
763 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900764}
765
Jooyung Hand3639552019-08-09 12:57:43 +0900766func TestApexWithRuntimeLibsDependency(t *testing.T) {
767 /*
768 myapex
769 |
770 v (runtime_libs)
771 mylib ------+------> libfoo [provides stub]
772 |
773 `------> libbar
774 */
775 ctx, _ := testApex(t, `
776 apex {
777 name: "myapex",
778 key: "myapex.key",
779 native_shared_libs: ["mylib"],
780 }
781
782 apex_key {
783 name: "myapex.key",
784 public_key: "testkey.avbpubkey",
785 private_key: "testkey.pem",
786 }
787
788 cc_library {
789 name: "mylib",
790 srcs: ["mylib.cpp"],
791 runtime_libs: ["libfoo", "libbar"],
792 system_shared_libs: [],
793 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000794 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900795 }
796
797 cc_library {
798 name: "libfoo",
799 srcs: ["mylib.cpp"],
800 system_shared_libs: [],
801 stl: "none",
802 stubs: {
803 versions: ["10", "20", "30"],
804 },
805 }
806
807 cc_library {
808 name: "libbar",
809 srcs: ["mylib.cpp"],
810 system_shared_libs: [],
811 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000812 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900813 }
814
815 `)
816
Sundong Ahnabb64432019-10-22 13:58:29 +0900817 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900818 copyCmds := apexRule.Args["copy_commands"]
819
820 // Ensure that direct non-stubs dep is always included
821 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
822
823 // Ensure that indirect stubs dep is not included
824 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
825
826 // Ensure that runtime_libs dep in included
827 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
828
Sundong Ahnabb64432019-10-22 13:58:29 +0900829 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900830 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
831 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900832
833}
834
Jooyung Han9c80bae2019-08-20 17:30:57 +0900835func TestApexDependencyToLLNDK(t *testing.T) {
836 ctx, _ := testApex(t, `
837 apex {
838 name: "myapex",
839 key: "myapex.key",
840 use_vendor: true,
841 native_shared_libs: ["mylib"],
842 }
843
844 apex_key {
845 name: "myapex.key",
846 public_key: "testkey.avbpubkey",
847 private_key: "testkey.pem",
848 }
849
850 cc_library {
851 name: "mylib",
852 srcs: ["mylib.cpp"],
853 vendor_available: true,
854 shared_libs: ["libbar"],
855 system_shared_libs: [],
856 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000857 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900858 }
859
860 cc_library {
861 name: "libbar",
862 srcs: ["mylib.cpp"],
863 system_shared_libs: [],
864 stl: "none",
865 }
866
867 llndk_library {
868 name: "libbar",
869 symbol_file: "",
870 }
Jooyung Handc782442019-11-01 03:14:38 +0900871 `, func(fs map[string][]byte, config android.Config) {
872 setUseVendorWhitelistForTest(config, []string{"myapex"})
873 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900874
Sundong Ahnabb64432019-10-22 13:58:29 +0900875 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900876 copyCmds := apexRule.Args["copy_commands"]
877
878 // Ensure that LLNDK dep is not included
879 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
880
Sundong Ahnabb64432019-10-22 13:58:29 +0900881 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900882 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900883
884 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900885 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900886
887}
888
Jiyong Park25fc6a92018-11-18 18:02:45 +0900889func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700890 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900891 apex {
892 name: "myapex",
893 key: "myapex.key",
894 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
895 }
896
897 apex_key {
898 name: "myapex.key",
899 public_key: "testkey.avbpubkey",
900 private_key: "testkey.pem",
901 }
902
903 cc_library {
904 name: "mylib",
905 srcs: ["mylib.cpp"],
906 shared_libs: ["libdl#27"],
907 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000908 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900909 }
910
911 cc_library_shared {
912 name: "mylib_shared",
913 srcs: ["mylib.cpp"],
914 shared_libs: ["libdl#27"],
915 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000916 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900917 }
918
919 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900920 name: "libBootstrap",
921 srcs: ["mylib.cpp"],
922 stl: "none",
923 bootstrap: true,
924 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925 `)
926
Sundong Ahnabb64432019-10-22 13:58:29 +0900927 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928 copyCmds := apexRule.Args["copy_commands"]
929
930 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800931 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900932 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
933 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900934
935 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900936 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900937
Colin Cross7113d202019-11-20 16:39:12 -0800938 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
939 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
940 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900941
942 // For dependency to libc
943 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900944 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900945 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900946 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900947 // ... Cflags from stub is correctly exported to mylib
948 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
949 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
950
951 // For dependency to libm
952 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800953 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900954 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900955 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900956 // ... and is not compiling with the stub
957 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
958 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
959
960 // For dependency to libdl
961 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900962 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900963 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900964 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
965 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900966 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800967 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900968 // ... Cflags from stub is correctly exported to mylib
969 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
970 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900971
972 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800973 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
974 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
975 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
976 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900977}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900978
979func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700980 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900981 apex {
982 name: "myapex",
983 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900984 native_shared_libs: ["mylib"],
985 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900986 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900987 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900988 }
989
990 apex_key {
991 name: "myapex.key",
992 public_key: "testkey.avbpubkey",
993 private_key: "testkey.pem",
994 }
995
996 prebuilt_etc {
997 name: "myetc",
998 src: "myprebuilt",
999 sub_dir: "foo/bar",
1000 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001001
1002 cc_library {
1003 name: "mylib",
1004 srcs: ["mylib.cpp"],
1005 relative_install_path: "foo/bar",
1006 system_shared_libs: [],
1007 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001008 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001009 }
1010
1011 cc_binary {
1012 name: "mybin",
1013 srcs: ["mylib.cpp"],
1014 relative_install_path: "foo/bar",
1015 system_shared_libs: [],
1016 static_executable: true,
1017 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001018 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001019 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001020 `)
1021
Sundong Ahnabb64432019-10-22 13:58:29 +09001022 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001023 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1024
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001025 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001026 ensureListContains(t, dirs, "etc")
1027 ensureListContains(t, dirs, "etc/foo")
1028 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001029 ensureListContains(t, dirs, "lib64")
1030 ensureListContains(t, dirs, "lib64/foo")
1031 ensureListContains(t, dirs, "lib64/foo/bar")
1032 ensureListContains(t, dirs, "lib")
1033 ensureListContains(t, dirs, "lib/foo")
1034 ensureListContains(t, dirs, "lib/foo/bar")
1035
Jiyong Parkbd13e442019-03-15 18:10:35 +09001036 ensureListContains(t, dirs, "bin")
1037 ensureListContains(t, dirs, "bin/foo")
1038 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001039}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001040
1041func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001042 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001043 apex {
1044 name: "myapex",
1045 key: "myapex.key",
1046 native_shared_libs: ["mylib"],
1047 use_vendor: true,
1048 }
1049
1050 apex_key {
1051 name: "myapex.key",
1052 public_key: "testkey.avbpubkey",
1053 private_key: "testkey.pem",
1054 }
1055
1056 cc_library {
1057 name: "mylib",
1058 srcs: ["mylib.cpp"],
1059 shared_libs: ["mylib2"],
1060 system_shared_libs: [],
1061 vendor_available: true,
1062 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001063 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001064 }
1065
1066 cc_library {
1067 name: "mylib2",
1068 srcs: ["mylib.cpp"],
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 }
Jooyung Handc782442019-11-01 03:14:38 +09001074 `, func(fs map[string][]byte, config android.Config) {
1075 setUseVendorWhitelistForTest(config, []string{"myapex"})
1076 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001077
1078 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001079 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001080 for _, implicit := range i.Implicits {
1081 inputsList = append(inputsList, implicit.String())
1082 }
1083 }
1084 inputsString := strings.Join(inputsList, " ")
1085
1086 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001087 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1088 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001089
1090 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001091 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1092 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001093}
Jiyong Park16e91a02018-12-20 18:18:08 +09001094
Jooyung Handc782442019-11-01 03:14:38 +09001095func TestUseVendorRestriction(t *testing.T) {
1096 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1097 apex {
1098 name: "myapex",
1099 key: "myapex.key",
1100 use_vendor: true,
1101 }
1102 apex_key {
1103 name: "myapex.key",
1104 public_key: "testkey.avbpubkey",
1105 private_key: "testkey.pem",
1106 }
1107 `, func(fs map[string][]byte, config android.Config) {
1108 setUseVendorWhitelistForTest(config, []string{""})
1109 })
1110 // no error with whitelist
1111 testApex(t, `
1112 apex {
1113 name: "myapex",
1114 key: "myapex.key",
1115 use_vendor: true,
1116 }
1117 apex_key {
1118 name: "myapex.key",
1119 public_key: "testkey.avbpubkey",
1120 private_key: "testkey.pem",
1121 }
1122 `, func(fs map[string][]byte, config android.Config) {
1123 setUseVendorWhitelistForTest(config, []string{"myapex"})
1124 })
1125}
1126
Jooyung Han5c998b92019-06-27 11:30:33 +09001127func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1128 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1129 apex {
1130 name: "myapex",
1131 key: "myapex.key",
1132 native_shared_libs: ["mylib"],
1133 use_vendor: true,
1134 }
1135
1136 apex_key {
1137 name: "myapex.key",
1138 public_key: "testkey.avbpubkey",
1139 private_key: "testkey.pem",
1140 }
1141
1142 cc_library {
1143 name: "mylib",
1144 srcs: ["mylib.cpp"],
1145 system_shared_libs: [],
1146 stl: "none",
1147 }
1148 `)
1149}
1150
Jiyong Park16e91a02018-12-20 18:18:08 +09001151func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001152 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001153 apex {
1154 name: "myapex",
1155 key: "myapex.key",
1156 native_shared_libs: ["mylib"],
1157 }
1158
1159 apex_key {
1160 name: "myapex.key",
1161 public_key: "testkey.avbpubkey",
1162 private_key: "testkey.pem",
1163 }
1164
1165 cc_library {
1166 name: "mylib",
1167 srcs: ["mylib.cpp"],
1168 system_shared_libs: [],
1169 stl: "none",
1170 stubs: {
1171 versions: ["1", "2", "3"],
1172 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001173 apex_available: [
1174 "//apex_available:platform",
1175 "myapex",
1176 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001177 }
1178
1179 cc_binary {
1180 name: "not_in_apex",
1181 srcs: ["mylib.cpp"],
1182 static_libs: ["mylib"],
1183 static_executable: true,
1184 system_shared_libs: [],
1185 stl: "none",
1186 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001187 `)
1188
Colin Cross7113d202019-11-20 16:39:12 -08001189 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001190
1191 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001192 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001193}
Jiyong Park9335a262018-12-24 11:31:58 +09001194
1195func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001196 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001197 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001198 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001199 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001200 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001201 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001202 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001203 }
1204
1205 cc_library {
1206 name: "mylib",
1207 srcs: ["mylib.cpp"],
1208 system_shared_libs: [],
1209 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001210 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001211 }
1212
1213 apex_key {
1214 name: "myapex.key",
1215 public_key: "testkey.avbpubkey",
1216 private_key: "testkey.pem",
1217 }
1218
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001219 android_app_certificate {
1220 name: "myapex.certificate",
1221 certificate: "testkey",
1222 }
1223
1224 android_app_certificate {
1225 name: "myapex.certificate.override",
1226 certificate: "testkey.override",
1227 }
1228
Jiyong Park9335a262018-12-24 11:31:58 +09001229 `)
1230
1231 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001232 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001233
1234 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1235 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1236 "vendor/foo/devkeys/testkey.avbpubkey")
1237 }
1238 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1239 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1240 "vendor/foo/devkeys/testkey.pem")
1241 }
1242
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001243 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001244 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001245 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001246 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001247 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001248 }
1249}
Jiyong Park58e364a2019-01-19 19:24:06 +09001250
Jooyung Hanf121a652019-12-17 14:30:11 +09001251func TestCertificate(t *testing.T) {
1252 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1253 ctx, _ := testApex(t, `
1254 apex {
1255 name: "myapex",
1256 key: "myapex.key",
1257 }
1258 apex_key {
1259 name: "myapex.key",
1260 public_key: "testkey.avbpubkey",
1261 private_key: "testkey.pem",
1262 }`)
1263 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1264 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1265 if actual := rule.Args["certificates"]; actual != expected {
1266 t.Errorf("certificates should be %q, not %q", expected, actual)
1267 }
1268 })
1269 t.Run("override when unspecified", func(t *testing.T) {
1270 ctx, _ := testApex(t, `
1271 apex {
1272 name: "myapex_keytest",
1273 key: "myapex.key",
1274 file_contexts: ":myapex-file_contexts",
1275 }
1276 apex_key {
1277 name: "myapex.key",
1278 public_key: "testkey.avbpubkey",
1279 private_key: "testkey.pem",
1280 }
1281 android_app_certificate {
1282 name: "myapex.certificate.override",
1283 certificate: "testkey.override",
1284 }`)
1285 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1286 expected := "testkey.override.x509.pem testkey.override.pk8"
1287 if actual := rule.Args["certificates"]; actual != expected {
1288 t.Errorf("certificates should be %q, not %q", expected, actual)
1289 }
1290 })
1291 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1292 ctx, _ := testApex(t, `
1293 apex {
1294 name: "myapex",
1295 key: "myapex.key",
1296 certificate: ":myapex.certificate",
1297 }
1298 apex_key {
1299 name: "myapex.key",
1300 public_key: "testkey.avbpubkey",
1301 private_key: "testkey.pem",
1302 }
1303 android_app_certificate {
1304 name: "myapex.certificate",
1305 certificate: "testkey",
1306 }`)
1307 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1308 expected := "testkey.x509.pem testkey.pk8"
1309 if actual := rule.Args["certificates"]; actual != expected {
1310 t.Errorf("certificates should be %q, not %q", expected, actual)
1311 }
1312 })
1313 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1314 ctx, _ := testApex(t, `
1315 apex {
1316 name: "myapex_keytest",
1317 key: "myapex.key",
1318 file_contexts: ":myapex-file_contexts",
1319 certificate: ":myapex.certificate",
1320 }
1321 apex_key {
1322 name: "myapex.key",
1323 public_key: "testkey.avbpubkey",
1324 private_key: "testkey.pem",
1325 }
1326 android_app_certificate {
1327 name: "myapex.certificate.override",
1328 certificate: "testkey.override",
1329 }`)
1330 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1331 expected := "testkey.override.x509.pem testkey.override.pk8"
1332 if actual := rule.Args["certificates"]; actual != expected {
1333 t.Errorf("certificates should be %q, not %q", expected, actual)
1334 }
1335 })
1336 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1337 ctx, _ := testApex(t, `
1338 apex {
1339 name: "myapex",
1340 key: "myapex.key",
1341 certificate: "testkey",
1342 }
1343 apex_key {
1344 name: "myapex.key",
1345 public_key: "testkey.avbpubkey",
1346 private_key: "testkey.pem",
1347 }`)
1348 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1349 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1350 if actual := rule.Args["certificates"]; actual != expected {
1351 t.Errorf("certificates should be %q, not %q", expected, actual)
1352 }
1353 })
1354 t.Run("override when specified as <name>", func(t *testing.T) {
1355 ctx, _ := testApex(t, `
1356 apex {
1357 name: "myapex_keytest",
1358 key: "myapex.key",
1359 file_contexts: ":myapex-file_contexts",
1360 certificate: "testkey",
1361 }
1362 apex_key {
1363 name: "myapex.key",
1364 public_key: "testkey.avbpubkey",
1365 private_key: "testkey.pem",
1366 }
1367 android_app_certificate {
1368 name: "myapex.certificate.override",
1369 certificate: "testkey.override",
1370 }`)
1371 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1372 expected := "testkey.override.x509.pem testkey.override.pk8"
1373 if actual := rule.Args["certificates"]; actual != expected {
1374 t.Errorf("certificates should be %q, not %q", expected, actual)
1375 }
1376 })
1377}
1378
Jiyong Park58e364a2019-01-19 19:24:06 +09001379func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001380 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001381 apex {
1382 name: "myapex",
1383 key: "myapex.key",
1384 native_shared_libs: ["mylib"],
1385 }
1386
1387 apex {
1388 name: "otherapex",
1389 key: "myapex.key",
1390 native_shared_libs: ["mylib"],
1391 }
1392
1393 apex_key {
1394 name: "myapex.key",
1395 public_key: "testkey.avbpubkey",
1396 private_key: "testkey.pem",
1397 }
1398
1399 cc_library {
1400 name: "mylib",
1401 srcs: ["mylib.cpp"],
1402 system_shared_libs: [],
1403 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001404 // TODO: remove //apex_available:platform
1405 apex_available: [
1406 "//apex_available:platform",
1407 "myapex",
1408 "otherapex",
1409 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001410 }
1411 `)
1412
Jooyung Han6b8459b2019-10-30 08:29:25 +09001413 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001414 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001415 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001416 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1417 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001418
Jooyung Han6b8459b2019-10-30 08:29:25 +09001419 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001420 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001421 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001422 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1423 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001424
Jooyung Han6b8459b2019-10-30 08:29:25 +09001425 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001426 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001427 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001428 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1429 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001430}
Jiyong Park7e636d02019-01-28 16:16:54 +09001431
1432func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001433 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001434 apex {
1435 name: "myapex",
1436 key: "myapex.key",
1437 native_shared_libs: ["mylib"],
1438 }
1439
1440 apex_key {
1441 name: "myapex.key",
1442 public_key: "testkey.avbpubkey",
1443 private_key: "testkey.pem",
1444 }
1445
1446 cc_library_headers {
1447 name: "mylib_headers",
1448 export_include_dirs: ["my_include"],
1449 system_shared_libs: [],
1450 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001451 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001452 }
1453
1454 cc_library {
1455 name: "mylib",
1456 srcs: ["mylib.cpp"],
1457 system_shared_libs: [],
1458 stl: "none",
1459 header_libs: ["mylib_headers"],
1460 export_header_lib_headers: ["mylib_headers"],
1461 stubs: {
1462 versions: ["1", "2", "3"],
1463 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001464 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001465 }
1466
1467 cc_library {
1468 name: "otherlib",
1469 srcs: ["mylib.cpp"],
1470 system_shared_libs: [],
1471 stl: "none",
1472 shared_libs: ["mylib"],
1473 }
1474 `)
1475
Colin Cross7113d202019-11-20 16:39:12 -08001476 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001477
1478 // Ensure that the include path of the header lib is exported to 'otherlib'
1479 ensureContains(t, cFlags, "-Imy_include")
1480}
Alex Light9670d332019-01-29 18:07:33 -08001481
Jiyong Park7cd10e32020-01-14 09:22:18 +09001482type fileInApex struct {
1483 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001484 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001485 isLink bool
1486}
1487
Jooyung Hana57af4a2020-01-23 05:36:59 +00001488func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001489 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001490 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001491 copyCmds := apexRule.Args["copy_commands"]
1492 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001493 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001494 for _, cmd := range strings.Split(copyCmds, "&&") {
1495 cmd = strings.TrimSpace(cmd)
1496 if cmd == "" {
1497 continue
1498 }
1499 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001500 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001501 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001502 switch terms[0] {
1503 case "mkdir":
1504 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001505 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001506 t.Fatal("copyCmds contains invalid cp command", cmd)
1507 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001508 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001509 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001510 isLink = false
1511 case "ln":
1512 if len(terms) != 3 && len(terms) != 4 {
1513 // ln LINK TARGET or ln -s LINK TARGET
1514 t.Fatal("copyCmds contains invalid ln command", cmd)
1515 }
1516 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001517 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001518 isLink = true
1519 default:
1520 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1521 }
1522 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001523 index := strings.Index(dst, imageApexDir)
1524 if index == -1 {
1525 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1526 }
1527 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001528 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001529 }
1530 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001531 return ret
1532}
1533
Jooyung Hana57af4a2020-01-23 05:36:59 +00001534func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1535 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001536 var failed bool
1537 var surplus []string
1538 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001539 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09001540 for _, expected := range files {
1541 if matched, _ := path.Match(expected, file.path); matched {
1542 filesMatched[expected] = true
1543 return
1544 }
1545 }
1546 surplus = append(surplus, file.path)
1547 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001548
Jooyung Han31c470b2019-10-18 16:26:59 +09001549 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001550 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001551 t.Log("surplus files", surplus)
1552 failed = true
1553 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001554
1555 if len(files) > len(filesMatched) {
1556 var missing []string
1557 for _, expected := range files {
1558 if !filesMatched[expected] {
1559 missing = append(missing, expected)
1560 }
1561 }
1562 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001563 t.Log("missing files", missing)
1564 failed = true
1565 }
1566 if failed {
1567 t.Fail()
1568 }
1569}
1570
Jooyung Han344d5432019-08-23 11:17:39 +09001571func TestVndkApexCurrent(t *testing.T) {
1572 ctx, _ := testApex(t, `
1573 apex_vndk {
1574 name: "myapex",
1575 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001576 }
1577
1578 apex_key {
1579 name: "myapex.key",
1580 public_key: "testkey.avbpubkey",
1581 private_key: "testkey.pem",
1582 }
1583
1584 cc_library {
1585 name: "libvndk",
1586 srcs: ["mylib.cpp"],
1587 vendor_available: true,
1588 vndk: {
1589 enabled: true,
1590 },
1591 system_shared_libs: [],
1592 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001593 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001594 }
1595
1596 cc_library {
1597 name: "libvndksp",
1598 srcs: ["mylib.cpp"],
1599 vendor_available: true,
1600 vndk: {
1601 enabled: true,
1602 support_system_process: 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 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001608 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001609
Jooyung Hana57af4a2020-01-23 05:36:59 +00001610 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001611 "lib/libvndk.so",
1612 "lib/libvndksp.so",
1613 "lib64/libvndk.so",
1614 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001615 "etc/llndk.libraries.VER.txt",
1616 "etc/vndkcore.libraries.VER.txt",
1617 "etc/vndksp.libraries.VER.txt",
1618 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001619 })
Jooyung Han344d5432019-08-23 11:17:39 +09001620}
1621
1622func TestVndkApexWithPrebuilt(t *testing.T) {
1623 ctx, _ := testApex(t, `
1624 apex_vndk {
1625 name: "myapex",
1626 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001627 }
1628
1629 apex_key {
1630 name: "myapex.key",
1631 public_key: "testkey.avbpubkey",
1632 private_key: "testkey.pem",
1633 }
1634
1635 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001636 name: "libvndk",
1637 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001638 vendor_available: true,
1639 vndk: {
1640 enabled: true,
1641 },
1642 system_shared_libs: [],
1643 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001644 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001645 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001646
1647 cc_prebuilt_library_shared {
1648 name: "libvndk.arm",
1649 srcs: ["libvndk.arm.so"],
1650 vendor_available: true,
1651 vndk: {
1652 enabled: true,
1653 },
1654 enabled: false,
1655 arch: {
1656 arm: {
1657 enabled: true,
1658 },
1659 },
1660 system_shared_libs: [],
1661 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001662 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001663 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001664 `+vndkLibrariesTxtFiles("current"),
1665 withFiles(map[string][]byte{
1666 "libvndk.so": nil,
1667 "libvndk.arm.so": nil,
1668 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001669
Jooyung Hana57af4a2020-01-23 05:36:59 +00001670 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001671 "lib/libvndk.so",
1672 "lib/libvndk.arm.so",
1673 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001674 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001675 })
Jooyung Han344d5432019-08-23 11:17:39 +09001676}
1677
Jooyung Han39edb6c2019-11-06 16:53:07 +09001678func vndkLibrariesTxtFiles(vers ...string) (result string) {
1679 for _, v := range vers {
1680 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001681 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001682 result += `
1683 vndk_libraries_txt {
1684 name: "` + txt + `.libraries.txt",
1685 }
1686 `
1687 }
1688 } else {
1689 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1690 result += `
1691 prebuilt_etc {
1692 name: "` + txt + `.libraries.` + v + `.txt",
1693 src: "dummy.txt",
1694 }
1695 `
1696 }
1697 }
1698 }
1699 return
1700}
1701
Jooyung Han344d5432019-08-23 11:17:39 +09001702func TestVndkApexVersion(t *testing.T) {
1703 ctx, _ := testApex(t, `
1704 apex_vndk {
1705 name: "myapex_v27",
1706 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001707 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001708 vndk_version: "27",
1709 }
1710
1711 apex_key {
1712 name: "myapex.key",
1713 public_key: "testkey.avbpubkey",
1714 private_key: "testkey.pem",
1715 }
1716
Jooyung Han31c470b2019-10-18 16:26:59 +09001717 vndk_prebuilt_shared {
1718 name: "libvndk27",
1719 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001720 vendor_available: true,
1721 vndk: {
1722 enabled: true,
1723 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001724 target_arch: "arm64",
1725 arch: {
1726 arm: {
1727 srcs: ["libvndk27_arm.so"],
1728 },
1729 arm64: {
1730 srcs: ["libvndk27_arm64.so"],
1731 },
1732 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001733 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001734 }
1735
1736 vndk_prebuilt_shared {
1737 name: "libvndk27",
1738 version: "27",
1739 vendor_available: true,
1740 vndk: {
1741 enabled: true,
1742 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001743 target_arch: "x86_64",
1744 arch: {
1745 x86: {
1746 srcs: ["libvndk27_x86.so"],
1747 },
1748 x86_64: {
1749 srcs: ["libvndk27_x86_64.so"],
1750 },
1751 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001752 }
1753 `+vndkLibrariesTxtFiles("27"),
1754 withFiles(map[string][]byte{
1755 "libvndk27_arm.so": nil,
1756 "libvndk27_arm64.so": nil,
1757 "libvndk27_x86.so": nil,
1758 "libvndk27_x86_64.so": nil,
1759 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001760
Jooyung Hana57af4a2020-01-23 05:36:59 +00001761 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001762 "lib/libvndk27_arm.so",
1763 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001764 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001765 })
Jooyung Han344d5432019-08-23 11:17:39 +09001766}
1767
1768func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1769 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1770 apex_vndk {
1771 name: "myapex_v27",
1772 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001773 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001774 vndk_version: "27",
1775 }
1776 apex_vndk {
1777 name: "myapex_v27_other",
1778 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001779 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001780 vndk_version: "27",
1781 }
1782
1783 apex_key {
1784 name: "myapex.key",
1785 public_key: "testkey.avbpubkey",
1786 private_key: "testkey.pem",
1787 }
1788
1789 cc_library {
1790 name: "libvndk",
1791 srcs: ["mylib.cpp"],
1792 vendor_available: true,
1793 vndk: {
1794 enabled: true,
1795 },
1796 system_shared_libs: [],
1797 stl: "none",
1798 }
1799
1800 vndk_prebuilt_shared {
1801 name: "libvndk",
1802 version: "27",
1803 vendor_available: true,
1804 vndk: {
1805 enabled: true,
1806 },
1807 srcs: ["libvndk.so"],
1808 }
1809 `, withFiles(map[string][]byte{
1810 "libvndk.so": nil,
1811 }))
1812}
1813
Jooyung Han90eee022019-10-01 20:02:42 +09001814func TestVndkApexNameRule(t *testing.T) {
1815 ctx, _ := testApex(t, `
1816 apex_vndk {
1817 name: "myapex",
1818 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001819 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001820 }
1821 apex_vndk {
1822 name: "myapex_v28",
1823 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001824 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001825 vndk_version: "28",
1826 }
1827 apex_key {
1828 name: "myapex.key",
1829 public_key: "testkey.avbpubkey",
1830 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001831 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001832
1833 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001834 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001835 actual := proptools.String(bundle.properties.Apex_name)
1836 if !reflect.DeepEqual(actual, expected) {
1837 t.Errorf("Got '%v', expected '%v'", actual, expected)
1838 }
1839 }
1840
1841 assertApexName("com.android.vndk.vVER", "myapex")
1842 assertApexName("com.android.vndk.v28", "myapex_v28")
1843}
1844
Jooyung Han344d5432019-08-23 11:17:39 +09001845func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1846 ctx, _ := testApex(t, `
1847 apex_vndk {
1848 name: "myapex",
1849 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001850 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001851 }
1852
1853 apex_key {
1854 name: "myapex.key",
1855 public_key: "testkey.avbpubkey",
1856 private_key: "testkey.pem",
1857 }
1858
1859 cc_library {
1860 name: "libvndk",
1861 srcs: ["mylib.cpp"],
1862 vendor_available: true,
1863 native_bridge_supported: true,
1864 host_supported: true,
1865 vndk: {
1866 enabled: true,
1867 },
1868 system_shared_libs: [],
1869 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001870 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001871 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001872 `+vndkLibrariesTxtFiles("current"),
1873 withTargets(map[android.OsType][]android.Target{
1874 android.Android: []android.Target{
1875 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1876 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1877 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1878 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1879 },
1880 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001881
Jooyung Hana57af4a2020-01-23 05:36:59 +00001882 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001883 "lib/libvndk.so",
1884 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001885 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001886 })
Jooyung Han344d5432019-08-23 11:17:39 +09001887}
1888
1889func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1890 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1891 apex_vndk {
1892 name: "myapex",
1893 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001894 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001895 native_bridge_supported: true,
1896 }
1897
1898 apex_key {
1899 name: "myapex.key",
1900 public_key: "testkey.avbpubkey",
1901 private_key: "testkey.pem",
1902 }
1903
1904 cc_library {
1905 name: "libvndk",
1906 srcs: ["mylib.cpp"],
1907 vendor_available: true,
1908 native_bridge_supported: true,
1909 host_supported: true,
1910 vndk: {
1911 enabled: true,
1912 },
1913 system_shared_libs: [],
1914 stl: "none",
1915 }
1916 `)
1917}
1918
Jooyung Han31c470b2019-10-18 16:26:59 +09001919func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001920 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001921 apex_vndk {
1922 name: "myapex_v27",
1923 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001924 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001925 vndk_version: "27",
1926 }
1927
1928 apex_key {
1929 name: "myapex.key",
1930 public_key: "testkey.avbpubkey",
1931 private_key: "testkey.pem",
1932 }
1933
1934 vndk_prebuilt_shared {
1935 name: "libvndk27",
1936 version: "27",
1937 target_arch: "arm",
1938 vendor_available: true,
1939 vndk: {
1940 enabled: true,
1941 },
1942 arch: {
1943 arm: {
1944 srcs: ["libvndk27.so"],
1945 }
1946 },
1947 }
1948
1949 vndk_prebuilt_shared {
1950 name: "libvndk27",
1951 version: "27",
1952 target_arch: "arm",
1953 binder32bit: true,
1954 vendor_available: true,
1955 vndk: {
1956 enabled: true,
1957 },
1958 arch: {
1959 arm: {
1960 srcs: ["libvndk27binder32.so"],
1961 }
1962 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001963 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001964 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001965 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001966 withFiles(map[string][]byte{
1967 "libvndk27.so": nil,
1968 "libvndk27binder32.so": nil,
1969 }),
1970 withBinder32bit,
1971 withTargets(map[android.OsType][]android.Target{
1972 android.Android: []android.Target{
1973 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1974 },
1975 }),
1976 )
1977
Jooyung Hana57af4a2020-01-23 05:36:59 +00001978 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001979 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001980 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001981 })
1982}
1983
Jooyung Hane1633032019-08-01 17:41:43 +09001984func TestDependenciesInApexManifest(t *testing.T) {
1985 ctx, _ := testApex(t, `
1986 apex {
1987 name: "myapex_nodep",
1988 key: "myapex.key",
1989 native_shared_libs: ["lib_nodep"],
1990 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001991 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001992 }
1993
1994 apex {
1995 name: "myapex_dep",
1996 key: "myapex.key",
1997 native_shared_libs: ["lib_dep"],
1998 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001999 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002000 }
2001
2002 apex {
2003 name: "myapex_provider",
2004 key: "myapex.key",
2005 native_shared_libs: ["libfoo"],
2006 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002007 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002008 }
2009
2010 apex {
2011 name: "myapex_selfcontained",
2012 key: "myapex.key",
2013 native_shared_libs: ["lib_dep", "libfoo"],
2014 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002015 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002016 }
2017
2018 apex_key {
2019 name: "myapex.key",
2020 public_key: "testkey.avbpubkey",
2021 private_key: "testkey.pem",
2022 }
2023
2024 cc_library {
2025 name: "lib_nodep",
2026 srcs: ["mylib.cpp"],
2027 system_shared_libs: [],
2028 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002029 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002030 }
2031
2032 cc_library {
2033 name: "lib_dep",
2034 srcs: ["mylib.cpp"],
2035 shared_libs: ["libfoo"],
2036 system_shared_libs: [],
2037 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002038 apex_available: [
2039 "myapex_dep",
2040 "myapex_provider",
2041 "myapex_selfcontained",
2042 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002043 }
2044
2045 cc_library {
2046 name: "libfoo",
2047 srcs: ["mytest.cpp"],
2048 stubs: {
2049 versions: ["1"],
2050 },
2051 system_shared_libs: [],
2052 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002053 apex_available: [
2054 "myapex_provider",
2055 "myapex_selfcontained",
2056 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002057 }
2058 `)
2059
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002060 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002061 var provideNativeLibs, requireNativeLibs []string
2062
Sundong Ahnabb64432019-10-22 13:58:29 +09002063 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002064 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2065 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002066 ensureListEmpty(t, provideNativeLibs)
2067 ensureListEmpty(t, requireNativeLibs)
2068
Sundong Ahnabb64432019-10-22 13:58:29 +09002069 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002070 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2071 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002072 ensureListEmpty(t, provideNativeLibs)
2073 ensureListContains(t, requireNativeLibs, "libfoo.so")
2074
Sundong Ahnabb64432019-10-22 13:58:29 +09002075 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002076 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2077 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002078 ensureListContains(t, provideNativeLibs, "libfoo.so")
2079 ensureListEmpty(t, requireNativeLibs)
2080
Sundong Ahnabb64432019-10-22 13:58:29 +09002081 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002082 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2083 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002084 ensureListContains(t, provideNativeLibs, "libfoo.so")
2085 ensureListEmpty(t, requireNativeLibs)
2086}
2087
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002088func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002089 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002090 apex {
2091 name: "myapex",
2092 key: "myapex.key",
2093 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002094 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002095 }
2096
2097 apex_key {
2098 name: "myapex.key",
2099 public_key: "testkey.avbpubkey",
2100 private_key: "testkey.pem",
2101 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002102
2103 cc_library {
2104 name: "mylib",
2105 srcs: ["mylib.cpp"],
2106 system_shared_libs: [],
2107 stl: "none",
2108 apex_available: [
2109 "//apex_available:platform",
2110 "myapex",
2111 ],
2112 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002113 `)
2114
Sundong Ahnabb64432019-10-22 13:58:29 +09002115 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002116 apexManifestRule := module.Rule("apexManifestRule")
2117 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2118 apexRule := module.Rule("apexRule")
2119 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002120
2121 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2122 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2123 name := apexBundle.BaseModuleName()
2124 prefix := "TARGET_"
2125 var builder strings.Builder
2126 data.Custom(&builder, name, prefix, "", data)
2127 androidMk := builder.String()
2128 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2129 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002130}
2131
Alex Light0851b882019-02-07 13:20:53 -08002132func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002133 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002134 apex {
2135 name: "myapex",
2136 key: "myapex.key",
2137 native_shared_libs: ["mylib_common"],
2138 }
2139
2140 apex_key {
2141 name: "myapex.key",
2142 public_key: "testkey.avbpubkey",
2143 private_key: "testkey.pem",
2144 }
2145
2146 cc_library {
2147 name: "mylib_common",
2148 srcs: ["mylib.cpp"],
2149 system_shared_libs: [],
2150 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002151 apex_available: [
2152 "//apex_available:platform",
2153 "myapex",
2154 ],
Alex Light0851b882019-02-07 13:20:53 -08002155 }
2156 `)
2157
Sundong Ahnabb64432019-10-22 13:58:29 +09002158 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002159 apexRule := module.Rule("apexRule")
2160 copyCmds := apexRule.Args["copy_commands"]
2161
2162 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2163 t.Log("Apex was a test apex!")
2164 t.Fail()
2165 }
2166 // Ensure that main rule creates an output
2167 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2168
2169 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002170 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002171
2172 // Ensure that both direct and indirect deps are copied into apex
2173 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2174
Colin Cross7113d202019-11-20 16:39:12 -08002175 // Ensure that the platform variant ends with _shared
2176 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002177
2178 if !android.InAnyApex("mylib_common") {
2179 t.Log("Found mylib_common not in any apex!")
2180 t.Fail()
2181 }
2182}
2183
2184func TestTestApex(t *testing.T) {
2185 if android.InAnyApex("mylib_common_test") {
2186 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!")
2187 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002188 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002189 apex_test {
2190 name: "myapex",
2191 key: "myapex.key",
2192 native_shared_libs: ["mylib_common_test"],
2193 }
2194
2195 apex_key {
2196 name: "myapex.key",
2197 public_key: "testkey.avbpubkey",
2198 private_key: "testkey.pem",
2199 }
2200
2201 cc_library {
2202 name: "mylib_common_test",
2203 srcs: ["mylib.cpp"],
2204 system_shared_libs: [],
2205 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002206 // TODO: remove //apex_available:platform
2207 apex_available: [
2208 "//apex_available:platform",
2209 "myapex",
2210 ],
Alex Light0851b882019-02-07 13:20:53 -08002211 }
2212 `)
2213
Sundong Ahnabb64432019-10-22 13:58:29 +09002214 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002215 apexRule := module.Rule("apexRule")
2216 copyCmds := apexRule.Args["copy_commands"]
2217
2218 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2219 t.Log("Apex was not a test apex!")
2220 t.Fail()
2221 }
2222 // Ensure that main rule creates an output
2223 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2224
2225 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002226 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002227
2228 // Ensure that both direct and indirect deps are copied into apex
2229 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2230
Colin Cross7113d202019-11-20 16:39:12 -08002231 // Ensure that the platform variant ends with _shared
2232 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002233}
2234
Alex Light9670d332019-01-29 18:07:33 -08002235func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002236 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002237 apex {
2238 name: "myapex",
2239 key: "myapex.key",
2240 multilib: {
2241 first: {
2242 native_shared_libs: ["mylib_common"],
2243 }
2244 },
2245 target: {
2246 android: {
2247 multilib: {
2248 first: {
2249 native_shared_libs: ["mylib"],
2250 }
2251 }
2252 },
2253 host: {
2254 multilib: {
2255 first: {
2256 native_shared_libs: ["mylib2"],
2257 }
2258 }
2259 }
2260 }
2261 }
2262
2263 apex_key {
2264 name: "myapex.key",
2265 public_key: "testkey.avbpubkey",
2266 private_key: "testkey.pem",
2267 }
2268
2269 cc_library {
2270 name: "mylib",
2271 srcs: ["mylib.cpp"],
2272 system_shared_libs: [],
2273 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002274 // TODO: remove //apex_available:platform
2275 apex_available: [
2276 "//apex_available:platform",
2277 "myapex",
2278 ],
Alex Light9670d332019-01-29 18:07:33 -08002279 }
2280
2281 cc_library {
2282 name: "mylib_common",
2283 srcs: ["mylib.cpp"],
2284 system_shared_libs: [],
2285 stl: "none",
2286 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002287 // TODO: remove //apex_available:platform
2288 apex_available: [
2289 "//apex_available:platform",
2290 "myapex",
2291 ],
Alex Light9670d332019-01-29 18:07:33 -08002292 }
2293
2294 cc_library {
2295 name: "mylib2",
2296 srcs: ["mylib.cpp"],
2297 system_shared_libs: [],
2298 stl: "none",
2299 compile_multilib: "first",
2300 }
2301 `)
2302
Sundong Ahnabb64432019-10-22 13:58:29 +09002303 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002304 copyCmds := apexRule.Args["copy_commands"]
2305
2306 // Ensure that main rule creates an output
2307 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2308
2309 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002310 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2311 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2312 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002313
2314 // Ensure that both direct and indirect deps are copied into apex
2315 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2316 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2317 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2318
Colin Cross7113d202019-11-20 16:39:12 -08002319 // Ensure that the platform variant ends with _shared
2320 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2321 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2322 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002323}
Jiyong Park04480cf2019-02-06 00:16:29 +09002324
2325func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002326 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002327 apex {
2328 name: "myapex",
2329 key: "myapex.key",
2330 binaries: ["myscript"],
2331 }
2332
2333 apex_key {
2334 name: "myapex.key",
2335 public_key: "testkey.avbpubkey",
2336 private_key: "testkey.pem",
2337 }
2338
2339 sh_binary {
2340 name: "myscript",
2341 src: "mylib.cpp",
2342 filename: "myscript.sh",
2343 sub_dir: "script",
2344 }
2345 `)
2346
Sundong Ahnabb64432019-10-22 13:58:29 +09002347 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002348 copyCmds := apexRule.Args["copy_commands"]
2349
2350 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2351}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002352
Jooyung Han91df2082019-11-20 01:49:42 +09002353func TestApexInVariousPartition(t *testing.T) {
2354 testcases := []struct {
2355 propName, parition, flattenedPartition string
2356 }{
2357 {"", "system", "system_ext"},
2358 {"product_specific: true", "product", "product"},
2359 {"soc_specific: true", "vendor", "vendor"},
2360 {"proprietary: true", "vendor", "vendor"},
2361 {"vendor: true", "vendor", "vendor"},
2362 {"system_ext_specific: true", "system_ext", "system_ext"},
2363 }
2364 for _, tc := range testcases {
2365 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2366 ctx, _ := testApex(t, `
2367 apex {
2368 name: "myapex",
2369 key: "myapex.key",
2370 `+tc.propName+`
2371 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002372
Jooyung Han91df2082019-11-20 01:49:42 +09002373 apex_key {
2374 name: "myapex.key",
2375 public_key: "testkey.avbpubkey",
2376 private_key: "testkey.pem",
2377 }
2378 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002379
Jooyung Han91df2082019-11-20 01:49:42 +09002380 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2381 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2382 actual := apex.installDir.String()
2383 if actual != expected {
2384 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2385 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002386
Jooyung Han91df2082019-11-20 01:49:42 +09002387 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2388 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2389 actual = flattened.installDir.String()
2390 if actual != expected {
2391 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2392 }
2393 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002394 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002395}
Jiyong Park67882562019-03-21 01:11:21 +09002396
Jooyung Han54aca7b2019-11-20 02:26:02 +09002397func TestFileContexts(t *testing.T) {
2398 ctx, _ := testApex(t, `
2399 apex {
2400 name: "myapex",
2401 key: "myapex.key",
2402 }
2403
2404 apex_key {
2405 name: "myapex.key",
2406 public_key: "testkey.avbpubkey",
2407 private_key: "testkey.pem",
2408 }
2409 `)
2410 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2411 apexRule := module.Rule("apexRule")
2412 actual := apexRule.Args["file_contexts"]
2413 expected := "system/sepolicy/apex/myapex-file_contexts"
2414 if actual != expected {
2415 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2416 }
2417
2418 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2419 apex {
2420 name: "myapex",
2421 key: "myapex.key",
2422 file_contexts: "my_own_file_contexts",
2423 }
2424
2425 apex_key {
2426 name: "myapex.key",
2427 public_key: "testkey.avbpubkey",
2428 private_key: "testkey.pem",
2429 }
2430 `, withFiles(map[string][]byte{
2431 "my_own_file_contexts": nil,
2432 }))
2433
2434 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2435 apex {
2436 name: "myapex",
2437 key: "myapex.key",
2438 product_specific: true,
2439 file_contexts: "product_specific_file_contexts",
2440 }
2441
2442 apex_key {
2443 name: "myapex.key",
2444 public_key: "testkey.avbpubkey",
2445 private_key: "testkey.pem",
2446 }
2447 `)
2448
2449 ctx, _ = testApex(t, `
2450 apex {
2451 name: "myapex",
2452 key: "myapex.key",
2453 product_specific: true,
2454 file_contexts: "product_specific_file_contexts",
2455 }
2456
2457 apex_key {
2458 name: "myapex.key",
2459 public_key: "testkey.avbpubkey",
2460 private_key: "testkey.pem",
2461 }
2462 `, withFiles(map[string][]byte{
2463 "product_specific_file_contexts": nil,
2464 }))
2465 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2466 apexRule = module.Rule("apexRule")
2467 actual = apexRule.Args["file_contexts"]
2468 expected = "product_specific_file_contexts"
2469 if actual != expected {
2470 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2471 }
2472
2473 ctx, _ = testApex(t, `
2474 apex {
2475 name: "myapex",
2476 key: "myapex.key",
2477 product_specific: true,
2478 file_contexts: ":my-file-contexts",
2479 }
2480
2481 apex_key {
2482 name: "myapex.key",
2483 public_key: "testkey.avbpubkey",
2484 private_key: "testkey.pem",
2485 }
2486
2487 filegroup {
2488 name: "my-file-contexts",
2489 srcs: ["product_specific_file_contexts"],
2490 }
2491 `, withFiles(map[string][]byte{
2492 "product_specific_file_contexts": nil,
2493 }))
2494 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2495 apexRule = module.Rule("apexRule")
2496 actual = apexRule.Args["file_contexts"]
2497 expected = "product_specific_file_contexts"
2498 if actual != expected {
2499 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2500 }
2501}
2502
Jiyong Park67882562019-03-21 01:11:21 +09002503func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002504 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002505 apex_key {
2506 name: "myapex.key",
2507 public_key: ":my.avbpubkey",
2508 private_key: ":my.pem",
2509 product_specific: true,
2510 }
2511
2512 filegroup {
2513 name: "my.avbpubkey",
2514 srcs: ["testkey2.avbpubkey"],
2515 }
2516
2517 filegroup {
2518 name: "my.pem",
2519 srcs: ["testkey2.pem"],
2520 }
2521 `)
2522
2523 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2524 expected_pubkey := "testkey2.avbpubkey"
2525 actual_pubkey := apex_key.public_key_file.String()
2526 if actual_pubkey != expected_pubkey {
2527 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2528 }
2529 expected_privkey := "testkey2.pem"
2530 actual_privkey := apex_key.private_key_file.String()
2531 if actual_privkey != expected_privkey {
2532 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2533 }
2534}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002535
2536func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002537 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002538 prebuilt_apex {
2539 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002540 arch: {
2541 arm64: {
2542 src: "myapex-arm64.apex",
2543 },
2544 arm: {
2545 src: "myapex-arm.apex",
2546 },
2547 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002548 }
2549 `)
2550
2551 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2552
Jiyong Parkc95714e2019-03-29 14:23:10 +09002553 expectedInput := "myapex-arm64.apex"
2554 if prebuilt.inputApex.String() != expectedInput {
2555 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2556 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002557}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002558
2559func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002560 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002561 prebuilt_apex {
2562 name: "myapex",
2563 src: "myapex-arm.apex",
2564 filename: "notmyapex.apex",
2565 }
2566 `)
2567
2568 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2569
2570 expected := "notmyapex.apex"
2571 if p.installFilename != expected {
2572 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2573 }
2574}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002575
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002576func TestPrebuiltOverrides(t *testing.T) {
2577 ctx, config := testApex(t, `
2578 prebuilt_apex {
2579 name: "myapex.prebuilt",
2580 src: "myapex-arm.apex",
2581 overrides: [
2582 "myapex",
2583 ],
2584 }
2585 `)
2586
2587 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2588
2589 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002590 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002591 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002592 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002593 }
2594}
2595
Roland Levillain630846d2019-06-26 12:48:34 +01002596func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002597 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002598 apex_test {
2599 name: "myapex",
2600 key: "myapex.key",
2601 tests: [
2602 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002603 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002604 ],
2605 }
2606
2607 apex_key {
2608 name: "myapex.key",
2609 public_key: "testkey.avbpubkey",
2610 private_key: "testkey.pem",
2611 }
2612
2613 cc_test {
2614 name: "mytest",
2615 gtest: false,
2616 srcs: ["mytest.cpp"],
2617 relative_install_path: "test",
2618 system_shared_libs: [],
2619 static_executable: true,
2620 stl: "none",
2621 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002622
2623 cc_test {
2624 name: "mytests",
2625 gtest: false,
2626 srcs: [
2627 "mytest1.cpp",
2628 "mytest2.cpp",
2629 "mytest3.cpp",
2630 ],
2631 test_per_src: true,
2632 relative_install_path: "test",
2633 system_shared_libs: [],
2634 static_executable: true,
2635 stl: "none",
2636 }
Roland Levillain630846d2019-06-26 12:48:34 +01002637 `)
2638
Sundong Ahnabb64432019-10-22 13:58:29 +09002639 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002640 copyCmds := apexRule.Args["copy_commands"]
2641
2642 // Ensure that test dep is copied into apex.
2643 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002644
2645 // Ensure that test deps built with `test_per_src` are copied into apex.
2646 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2647 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2648 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002649
2650 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002651 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002652 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2653 name := apexBundle.BaseModuleName()
2654 prefix := "TARGET_"
2655 var builder strings.Builder
2656 data.Custom(&builder, name, prefix, "", data)
2657 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002658 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2659 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2660 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2661 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002662 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002663 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002664 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002665}
2666
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002667func TestInstallExtraFlattenedApexes(t *testing.T) {
2668 ctx, config := testApex(t, `
2669 apex {
2670 name: "myapex",
2671 key: "myapex.key",
2672 }
2673 apex_key {
2674 name: "myapex.key",
2675 public_key: "testkey.avbpubkey",
2676 private_key: "testkey.pem",
2677 }
2678 `, func(fs map[string][]byte, config android.Config) {
2679 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2680 })
2681 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002682 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002683 mk := android.AndroidMkDataForTest(t, config, "", ab)
2684 var builder strings.Builder
2685 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2686 androidMk := builder.String()
2687 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2688}
2689
Jooyung Han5c998b92019-06-27 11:30:33 +09002690func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002691 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002692 apex {
2693 name: "myapex",
2694 key: "myapex.key",
2695 native_shared_libs: ["mylib"],
2696 uses: ["commonapex"],
2697 }
2698
2699 apex {
2700 name: "commonapex",
2701 key: "myapex.key",
2702 native_shared_libs: ["libcommon"],
2703 provide_cpp_shared_libs: true,
2704 }
2705
2706 apex_key {
2707 name: "myapex.key",
2708 public_key: "testkey.avbpubkey",
2709 private_key: "testkey.pem",
2710 }
2711
2712 cc_library {
2713 name: "mylib",
2714 srcs: ["mylib.cpp"],
2715 shared_libs: ["libcommon"],
2716 system_shared_libs: [],
2717 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002718 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002719 }
2720
2721 cc_library {
2722 name: "libcommon",
2723 srcs: ["mylib_common.cpp"],
2724 system_shared_libs: [],
2725 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002726 // TODO: remove //apex_available:platform
2727 apex_available: [
2728 "//apex_available:platform",
2729 "commonapex",
2730 "myapex",
2731 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002732 }
2733 `)
2734
Sundong Ahnabb64432019-10-22 13:58:29 +09002735 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002736 apexRule1 := module1.Rule("apexRule")
2737 copyCmds1 := apexRule1.Args["copy_commands"]
2738
Sundong Ahnabb64432019-10-22 13:58:29 +09002739 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002740 apexRule2 := module2.Rule("apexRule")
2741 copyCmds2 := apexRule2.Args["copy_commands"]
2742
Colin Cross7113d202019-11-20 16:39:12 -08002743 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2744 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002745 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2746 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2747 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2748}
2749
2750func TestApexUsesFailsIfNotProvided(t *testing.T) {
2751 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2752 apex {
2753 name: "myapex",
2754 key: "myapex.key",
2755 uses: ["commonapex"],
2756 }
2757
2758 apex {
2759 name: "commonapex",
2760 key: "myapex.key",
2761 }
2762
2763 apex_key {
2764 name: "myapex.key",
2765 public_key: "testkey.avbpubkey",
2766 private_key: "testkey.pem",
2767 }
2768 `)
2769 testApexError(t, `uses: "commonapex" is not a provider`, `
2770 apex {
2771 name: "myapex",
2772 key: "myapex.key",
2773 uses: ["commonapex"],
2774 }
2775
2776 cc_library {
2777 name: "commonapex",
2778 system_shared_libs: [],
2779 stl: "none",
2780 }
2781
2782 apex_key {
2783 name: "myapex.key",
2784 public_key: "testkey.avbpubkey",
2785 private_key: "testkey.pem",
2786 }
2787 `)
2788}
2789
2790func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2791 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2792 apex {
2793 name: "myapex",
2794 key: "myapex.key",
2795 use_vendor: true,
2796 uses: ["commonapex"],
2797 }
2798
2799 apex {
2800 name: "commonapex",
2801 key: "myapex.key",
2802 provide_cpp_shared_libs: true,
2803 }
2804
2805 apex_key {
2806 name: "myapex.key",
2807 public_key: "testkey.avbpubkey",
2808 private_key: "testkey.pem",
2809 }
Jooyung Handc782442019-11-01 03:14:38 +09002810 `, func(fs map[string][]byte, config android.Config) {
2811 setUseVendorWhitelistForTest(config, []string{"myapex"})
2812 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002813}
2814
Jooyung Hand48f3c32019-08-23 11:18:57 +09002815func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2816 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2817 apex {
2818 name: "myapex",
2819 key: "myapex.key",
2820 native_shared_libs: ["libfoo"],
2821 }
2822
2823 apex_key {
2824 name: "myapex.key",
2825 public_key: "testkey.avbpubkey",
2826 private_key: "testkey.pem",
2827 }
2828
2829 cc_library {
2830 name: "libfoo",
2831 stl: "none",
2832 system_shared_libs: [],
2833 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09002834 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09002835 }
2836 `)
2837 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2838 apex {
2839 name: "myapex",
2840 key: "myapex.key",
2841 java_libs: ["myjar"],
2842 }
2843
2844 apex_key {
2845 name: "myapex.key",
2846 public_key: "testkey.avbpubkey",
2847 private_key: "testkey.pem",
2848 }
2849
2850 java_library {
2851 name: "myjar",
2852 srcs: ["foo/bar/MyClass.java"],
2853 sdk_version: "none",
2854 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09002855 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09002856 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09002857 }
2858 `)
2859}
2860
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002861func TestApexWithApps(t *testing.T) {
2862 ctx, _ := testApex(t, `
2863 apex {
2864 name: "myapex",
2865 key: "myapex.key",
2866 apps: [
2867 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002868 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002869 ],
2870 }
2871
2872 apex_key {
2873 name: "myapex.key",
2874 public_key: "testkey.avbpubkey",
2875 private_key: "testkey.pem",
2876 }
2877
2878 android_app {
2879 name: "AppFoo",
2880 srcs: ["foo/bar/MyClass.java"],
2881 sdk_version: "none",
2882 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002883 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002884 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002885 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002886
2887 android_app {
2888 name: "AppFooPriv",
2889 srcs: ["foo/bar/MyClass.java"],
2890 sdk_version: "none",
2891 system_modules: "none",
2892 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002893 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09002894 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002895
2896 cc_library_shared {
2897 name: "libjni",
2898 srcs: ["mylib.cpp"],
Jooyung Han65041792020-02-25 16:59:29 +09002899 shared_libs: ["libfoo"],
Jiyong Park8be103b2019-11-08 15:53:48 +09002900 stl: "none",
2901 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09002902 apex_available: [ "myapex" ],
Jooyung Han65041792020-02-25 16:59:29 +09002903 sdk_version: "current",
2904 }
2905
2906 cc_library_shared {
2907 name: "libfoo",
2908 stl: "none",
2909 system_shared_libs: [],
2910 apex_available: [ "myapex" ],
2911 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09002912 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002913 `)
2914
Sundong Ahnabb64432019-10-22 13:58:29 +09002915 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002916 apexRule := module.Rule("apexRule")
2917 copyCmds := apexRule.Args["copy_commands"]
2918
2919 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002920 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002921
Jooyung Han65041792020-02-25 16:59:29 +09002922 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
2923 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09002924 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Han65041792020-02-25 16:59:29 +09002925 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002926 }
Jooyung Han65041792020-02-25 16:59:29 +09002927 // JNI libraries including transitive deps are
2928 for _, jni := range []string{"libjni", "libfoo"} {
2929 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
2930 // ... embedded inside APK (jnilibs.zip)
2931 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
2932 // ... and not directly inside the APEX
2933 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
2934 }
Dario Frenicde2a032019-10-27 00:29:22 +01002935}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002936
Dario Frenicde2a032019-10-27 00:29:22 +01002937func TestApexWithAppImports(t *testing.T) {
2938 ctx, _ := testApex(t, `
2939 apex {
2940 name: "myapex",
2941 key: "myapex.key",
2942 apps: [
2943 "AppFooPrebuilt",
2944 "AppFooPrivPrebuilt",
2945 ],
2946 }
2947
2948 apex_key {
2949 name: "myapex.key",
2950 public_key: "testkey.avbpubkey",
2951 private_key: "testkey.pem",
2952 }
2953
2954 android_app_import {
2955 name: "AppFooPrebuilt",
2956 apk: "PrebuiltAppFoo.apk",
2957 presigned: true,
2958 dex_preopt: {
2959 enabled: false,
2960 },
2961 }
2962
2963 android_app_import {
2964 name: "AppFooPrivPrebuilt",
2965 apk: "PrebuiltAppFooPriv.apk",
2966 privileged: true,
2967 presigned: true,
2968 dex_preopt: {
2969 enabled: false,
2970 },
2971 }
2972 `)
2973
Sundong Ahnabb64432019-10-22 13:58:29 +09002974 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002975 apexRule := module.Rule("apexRule")
2976 copyCmds := apexRule.Args["copy_commands"]
2977
2978 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2979 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002980}
2981
Dario Freni6f3937c2019-12-20 22:58:03 +00002982func TestApexWithTestHelperApp(t *testing.T) {
2983 ctx, _ := testApex(t, `
2984 apex {
2985 name: "myapex",
2986 key: "myapex.key",
2987 apps: [
2988 "TesterHelpAppFoo",
2989 ],
2990 }
2991
2992 apex_key {
2993 name: "myapex.key",
2994 public_key: "testkey.avbpubkey",
2995 private_key: "testkey.pem",
2996 }
2997
2998 android_test_helper_app {
2999 name: "TesterHelpAppFoo",
3000 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003001 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003002 }
3003
3004 `)
3005
3006 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3007 apexRule := module.Rule("apexRule")
3008 copyCmds := apexRule.Args["copy_commands"]
3009
3010 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3011}
3012
Jooyung Han18020ea2019-11-13 10:50:48 +09003013func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3014 // libfoo's apex_available comes from cc_defaults
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003015 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003016 apex {
3017 name: "myapex",
3018 key: "myapex.key",
3019 native_shared_libs: ["libfoo"],
3020 }
3021
3022 apex_key {
3023 name: "myapex.key",
3024 public_key: "testkey.avbpubkey",
3025 private_key: "testkey.pem",
3026 }
3027
3028 apex {
3029 name: "otherapex",
3030 key: "myapex.key",
3031 native_shared_libs: ["libfoo"],
3032 }
3033
3034 cc_defaults {
3035 name: "libfoo-defaults",
3036 apex_available: ["otherapex"],
3037 }
3038
3039 cc_library {
3040 name: "libfoo",
3041 defaults: ["libfoo-defaults"],
3042 stl: "none",
3043 system_shared_libs: [],
3044 }`)
3045}
3046
Jiyong Park127b40b2019-09-30 16:04:35 +09003047func TestApexAvailable(t *testing.T) {
3048 // libfoo is not available to myapex, but only to otherapex
3049 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3050 apex {
3051 name: "myapex",
3052 key: "myapex.key",
3053 native_shared_libs: ["libfoo"],
3054 }
3055
3056 apex_key {
3057 name: "myapex.key",
3058 public_key: "testkey.avbpubkey",
3059 private_key: "testkey.pem",
3060 }
3061
3062 apex {
3063 name: "otherapex",
3064 key: "otherapex.key",
3065 native_shared_libs: ["libfoo"],
3066 }
3067
3068 apex_key {
3069 name: "otherapex.key",
3070 public_key: "testkey.avbpubkey",
3071 private_key: "testkey.pem",
3072 }
3073
3074 cc_library {
3075 name: "libfoo",
3076 stl: "none",
3077 system_shared_libs: [],
3078 apex_available: ["otherapex"],
3079 }`)
3080
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003081 // libbbaz is an indirect dep
3082 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003083 apex {
3084 name: "myapex",
3085 key: "myapex.key",
3086 native_shared_libs: ["libfoo"],
3087 }
3088
3089 apex_key {
3090 name: "myapex.key",
3091 public_key: "testkey.avbpubkey",
3092 private_key: "testkey.pem",
3093 }
3094
Jiyong Park127b40b2019-09-30 16:04:35 +09003095 cc_library {
3096 name: "libfoo",
3097 stl: "none",
3098 shared_libs: ["libbar"],
3099 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003100 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003101 }
3102
3103 cc_library {
3104 name: "libbar",
3105 stl: "none",
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003106 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003107 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003108 apex_available: ["myapex"],
3109 }
3110
3111 cc_library {
3112 name: "libbaz",
3113 stl: "none",
3114 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003115 }`)
3116
3117 testApexError(t, "\"otherapex\" is not a valid module name", `
3118 apex {
3119 name: "myapex",
3120 key: "myapex.key",
3121 native_shared_libs: ["libfoo"],
3122 }
3123
3124 apex_key {
3125 name: "myapex.key",
3126 public_key: "testkey.avbpubkey",
3127 private_key: "testkey.pem",
3128 }
3129
3130 cc_library {
3131 name: "libfoo",
3132 stl: "none",
3133 system_shared_libs: [],
3134 apex_available: ["otherapex"],
3135 }`)
3136
3137 ctx, _ := testApex(t, `
3138 apex {
3139 name: "myapex",
3140 key: "myapex.key",
3141 native_shared_libs: ["libfoo", "libbar"],
3142 }
3143
3144 apex_key {
3145 name: "myapex.key",
3146 public_key: "testkey.avbpubkey",
3147 private_key: "testkey.pem",
3148 }
3149
3150 cc_library {
3151 name: "libfoo",
3152 stl: "none",
3153 system_shared_libs: [],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003154 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003155 apex_available: ["myapex"],
3156 }
3157
3158 cc_library {
3159 name: "libbar",
3160 stl: "none",
3161 system_shared_libs: [],
3162 apex_available: ["//apex_available:anyapex"],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003163 }
3164
3165 cc_library {
3166 name: "libbaz",
3167 stl: "none",
3168 system_shared_libs: [],
3169 stubs: {
3170 versions: ["10", "20", "30"],
3171 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003172 }`)
3173
3174 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003175 // TODO(jiyong) the checks for the platform variant are removed because we now create
3176 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3177 // the platform variants are not used from other platform modules. When that is done,
3178 // these checks will be replaced by expecting a specific error message that will be
3179 // emitted when the platform variant is used.
3180 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3181 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3182 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3183 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003184
3185 ctx, _ = testApex(t, `
3186 apex {
3187 name: "myapex",
3188 key: "myapex.key",
3189 }
3190
3191 apex_key {
3192 name: "myapex.key",
3193 public_key: "testkey.avbpubkey",
3194 private_key: "testkey.pem",
3195 }
3196
3197 cc_library {
3198 name: "libfoo",
3199 stl: "none",
3200 system_shared_libs: [],
3201 apex_available: ["//apex_available:platform"],
3202 }`)
3203
3204 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003205 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3206 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003207
3208 ctx, _ = testApex(t, `
3209 apex {
3210 name: "myapex",
3211 key: "myapex.key",
3212 native_shared_libs: ["libfoo"],
3213 }
3214
3215 apex_key {
3216 name: "myapex.key",
3217 public_key: "testkey.avbpubkey",
3218 private_key: "testkey.pem",
3219 }
3220
3221 cc_library {
3222 name: "libfoo",
3223 stl: "none",
3224 system_shared_libs: [],
3225 apex_available: ["myapex"],
3226 static: {
3227 apex_available: ["//apex_available:platform"],
3228 },
3229 }`)
3230
3231 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003232 // TODO(jiyong) the checks for the platform variant are removed because we now create
3233 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3234 // the platform variants are not used from other platform modules. When that is done,
3235 // these checks will be replaced by expecting a specific error message that will be
3236 // emitted when the platform variant is used.
3237 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3238 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3239 // // but the static variant is available to both myapex and the platform
3240 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3241 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003242}
3243
Jiyong Park5d790c32019-11-15 18:40:32 +09003244func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003245 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003246 apex {
3247 name: "myapex",
3248 key: "myapex.key",
3249 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003250 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003251 }
3252
3253 override_apex {
3254 name: "override_myapex",
3255 base: "myapex",
3256 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003257 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003258 logging_parent: "com.foo.bar",
Jiyong Park5d790c32019-11-15 18:40:32 +09003259 }
3260
3261 apex_key {
3262 name: "myapex.key",
3263 public_key: "testkey.avbpubkey",
3264 private_key: "testkey.pem",
3265 }
3266
3267 android_app {
3268 name: "app",
3269 srcs: ["foo/bar/MyClass.java"],
3270 package_name: "foo",
3271 sdk_version: "none",
3272 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003273 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003274 }
3275
3276 override_android_app {
3277 name: "override_app",
3278 base: "app",
3279 package_name: "bar",
3280 }
Jiyong Parka519c542020-03-03 11:45:41 +09003281 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003282
Jiyong Park317645e2019-12-05 13:20:58 +09003283 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3284 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3285 if originalVariant.GetOverriddenBy() != "" {
3286 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3287 }
3288 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3289 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3290 }
3291
Jiyong Park5d790c32019-11-15 18:40:32 +09003292 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3293 apexRule := module.Rule("apexRule")
3294 copyCmds := apexRule.Args["copy_commands"]
3295
3296 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3297 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003298
3299 apexBundle := module.Module().(*apexBundle)
3300 name := apexBundle.Name()
3301 if name != "override_myapex" {
3302 t.Errorf("name should be \"override_myapex\", but was %q", name)
3303 }
3304
Baligh Uddin004d7172020-02-19 21:29:28 -08003305 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3306 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3307 }
3308
Jiyong Parka519c542020-03-03 11:45:41 +09003309 optFlags := apexRule.Args["opt_flags"]
3310 ensureContains(t, optFlags, "--override_apk_package_name com.android.myapex")
3311
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003312 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3313 var builder strings.Builder
3314 data.Custom(&builder, name, "TARGET_", "", data)
3315 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003316 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003317 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3318 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003319 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003320 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003321 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003322 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3323 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003324}
3325
Jooyung Han214bf372019-11-12 13:03:50 +09003326func TestLegacyAndroid10Support(t *testing.T) {
3327 ctx, _ := testApex(t, `
3328 apex {
3329 name: "myapex",
3330 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003331 native_shared_libs: ["mylib"],
Jooyung Han214bf372019-11-12 13:03:50 +09003332 legacy_android10_support: true,
3333 }
3334
3335 apex_key {
3336 name: "myapex.key",
3337 public_key: "testkey.avbpubkey",
3338 private_key: "testkey.pem",
3339 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003340
3341 cc_library {
3342 name: "mylib",
3343 srcs: ["mylib.cpp"],
3344 stl: "libc++",
3345 system_shared_libs: [],
3346 apex_available: [ "myapex" ],
3347 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003348 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003349
3350 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3351 args := module.Rule("apexRule").Args
3352 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003353 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003354
3355 // The copies of the libraries in the apex should have one more dependency than
3356 // the ones outside the apex, namely the unwinder. Ideally we should check
3357 // the dependency names directly here but for some reason the names are blank in
3358 // this test.
3359 for _, lib := range []string{"libc++", "mylib"} {
3360 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3361 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3362 if len(apexImplicits) != len(nonApexImplicits)+1 {
3363 t.Errorf("%q missing unwinder dep", lib)
3364 }
3365 }
Jooyung Han214bf372019-11-12 13:03:50 +09003366}
3367
Jooyung Han58f26ab2019-12-18 15:34:32 +09003368func TestJavaSDKLibrary(t *testing.T) {
3369 ctx, _ := testApex(t, `
3370 apex {
3371 name: "myapex",
3372 key: "myapex.key",
3373 java_libs: ["foo"],
3374 }
3375
3376 apex_key {
3377 name: "myapex.key",
3378 public_key: "testkey.avbpubkey",
3379 private_key: "testkey.pem",
3380 }
3381
3382 java_sdk_library {
3383 name: "foo",
3384 srcs: ["a.java"],
3385 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003386 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003387 }
3388 `, withFiles(map[string][]byte{
3389 "api/current.txt": nil,
3390 "api/removed.txt": nil,
3391 "api/system-current.txt": nil,
3392 "api/system-removed.txt": nil,
3393 "api/test-current.txt": nil,
3394 "api/test-removed.txt": nil,
3395 }))
3396
3397 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003398 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003399 "javalib/foo.jar",
3400 "etc/permissions/foo.xml",
3401 })
3402 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003403 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3404 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003405}
3406
atrost6e126252020-01-27 17:01:16 +00003407func TestCompatConfig(t *testing.T) {
3408 ctx, _ := testApex(t, `
3409 apex {
3410 name: "myapex",
3411 key: "myapex.key",
3412 prebuilts: ["myjar-platform-compat-config"],
3413 java_libs: ["myjar"],
3414 }
3415
3416 apex_key {
3417 name: "myapex.key",
3418 public_key: "testkey.avbpubkey",
3419 private_key: "testkey.pem",
3420 }
3421
3422 platform_compat_config {
3423 name: "myjar-platform-compat-config",
3424 src: ":myjar",
3425 }
3426
3427 java_library {
3428 name: "myjar",
3429 srcs: ["foo/bar/MyClass.java"],
3430 sdk_version: "none",
3431 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003432 apex_available: [ "myapex" ],
3433 }
3434 `)
3435 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3436 "etc/compatconfig/myjar-platform-compat-config.xml",
3437 "javalib/myjar.jar",
3438 })
3439}
3440
Jiyong Park479321d2019-12-16 11:47:12 +09003441func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3442 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3443 apex {
3444 name: "myapex",
3445 key: "myapex.key",
3446 java_libs: ["myjar"],
3447 }
3448
3449 apex_key {
3450 name: "myapex.key",
3451 public_key: "testkey.avbpubkey",
3452 private_key: "testkey.pem",
3453 }
3454
3455 java_library {
3456 name: "myjar",
3457 srcs: ["foo/bar/MyClass.java"],
3458 sdk_version: "none",
3459 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003460 compile_dex: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003461 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003462 }
3463 `)
3464}
3465
Jiyong Park7afd1072019-12-30 16:56:33 +09003466func TestCarryRequiredModuleNames(t *testing.T) {
3467 ctx, config := testApex(t, `
3468 apex {
3469 name: "myapex",
3470 key: "myapex.key",
3471 native_shared_libs: ["mylib"],
3472 }
3473
3474 apex_key {
3475 name: "myapex.key",
3476 public_key: "testkey.avbpubkey",
3477 private_key: "testkey.pem",
3478 }
3479
3480 cc_library {
3481 name: "mylib",
3482 srcs: ["mylib.cpp"],
3483 system_shared_libs: [],
3484 stl: "none",
3485 required: ["a", "b"],
3486 host_required: ["c", "d"],
3487 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003488 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003489 }
3490 `)
3491
3492 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3493 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3494 name := apexBundle.BaseModuleName()
3495 prefix := "TARGET_"
3496 var builder strings.Builder
3497 data.Custom(&builder, name, prefix, "", data)
3498 androidMk := builder.String()
3499 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3500 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3501 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3502}
3503
Jiyong Park7cd10e32020-01-14 09:22:18 +09003504func TestSymlinksFromApexToSystem(t *testing.T) {
3505 bp := `
3506 apex {
3507 name: "myapex",
3508 key: "myapex.key",
3509 native_shared_libs: ["mylib"],
3510 java_libs: ["myjar"],
3511 }
3512
Jiyong Park9d677202020-02-19 16:29:35 +09003513 apex {
3514 name: "myapex.updatable",
3515 key: "myapex.key",
3516 native_shared_libs: ["mylib"],
3517 java_libs: ["myjar"],
3518 updatable: true,
3519 }
3520
Jiyong Park7cd10e32020-01-14 09:22:18 +09003521 apex_key {
3522 name: "myapex.key",
3523 public_key: "testkey.avbpubkey",
3524 private_key: "testkey.pem",
3525 }
3526
3527 cc_library {
3528 name: "mylib",
3529 srcs: ["mylib.cpp"],
3530 shared_libs: ["myotherlib"],
3531 system_shared_libs: [],
3532 stl: "none",
3533 apex_available: [
3534 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003535 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003536 "//apex_available:platform",
3537 ],
3538 }
3539
3540 cc_library {
3541 name: "myotherlib",
3542 srcs: ["mylib.cpp"],
3543 system_shared_libs: [],
3544 stl: "none",
3545 apex_available: [
3546 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003547 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003548 "//apex_available:platform",
3549 ],
3550 }
3551
3552 java_library {
3553 name: "myjar",
3554 srcs: ["foo/bar/MyClass.java"],
3555 sdk_version: "none",
3556 system_modules: "none",
3557 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003558 apex_available: [
3559 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003560 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003561 "//apex_available:platform",
3562 ],
3563 }
3564
3565 java_library {
3566 name: "myotherjar",
3567 srcs: ["foo/bar/MyClass.java"],
3568 sdk_version: "none",
3569 system_modules: "none",
3570 apex_available: [
3571 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003572 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003573 "//apex_available:platform",
3574 ],
3575 }
3576 `
3577
3578 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3579 for _, f := range files {
3580 if f.path == file {
3581 if f.isLink {
3582 t.Errorf("%q is not a real file", file)
3583 }
3584 return
3585 }
3586 }
3587 t.Errorf("%q is not found", file)
3588 }
3589
3590 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3591 for _, f := range files {
3592 if f.path == file {
3593 if !f.isLink {
3594 t.Errorf("%q is not a symlink", file)
3595 }
3596 return
3597 }
3598 }
3599 t.Errorf("%q is not found", file)
3600 }
3601
Jiyong Park9d677202020-02-19 16:29:35 +09003602 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3603 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003604 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003605 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003606 ensureRealfileExists(t, files, "javalib/myjar.jar")
3607 ensureRealfileExists(t, files, "lib64/mylib.so")
3608 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3609
Jiyong Park9d677202020-02-19 16:29:35 +09003610 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3611 ensureRealfileExists(t, files, "javalib/myjar.jar")
3612 ensureRealfileExists(t, files, "lib64/mylib.so")
3613 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3614
3615 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003616 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003617 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003618 ensureRealfileExists(t, files, "javalib/myjar.jar")
3619 ensureRealfileExists(t, files, "lib64/mylib.so")
3620 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09003621
3622 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3623 ensureRealfileExists(t, files, "javalib/myjar.jar")
3624 ensureRealfileExists(t, files, "lib64/mylib.so")
3625 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09003626}
3627
Jiyong Parkd93e1b12020-02-28 15:22:21 +09003628func TestAppBundle(t *testing.T) {
3629 ctx, _ := testApex(t, `
3630 apex {
3631 name: "myapex",
3632 key: "myapex.key",
3633 apps: ["AppFoo"],
3634 }
3635
3636 apex_key {
3637 name: "myapex.key",
3638 public_key: "testkey.avbpubkey",
3639 private_key: "testkey.pem",
3640 }
3641
3642 android_app {
3643 name: "AppFoo",
3644 srcs: ["foo/bar/MyClass.java"],
3645 sdk_version: "none",
3646 system_modules: "none",
3647 apex_available: [ "myapex" ],
3648 }
Jiyong Parkaf8998c2020-02-28 16:51:07 +09003649 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkd93e1b12020-02-28 15:22:21 +09003650
3651 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
3652 content := bundleConfigRule.Args["content"]
3653
3654 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkaf8998c2020-02-28 16:51:07 +09003655 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo/AppFoo.apk"}]}`)
Jiyong Parkd93e1b12020-02-28 15:22:21 +09003656}
3657
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003658func TestMain(m *testing.M) {
3659 run := func() int {
3660 setUp()
3661 defer tearDown()
3662
3663 return m.Run()
3664 }
3665
3666 os.Exit(run())
3667}