blob: a7b6dcf5dfbbd0032c5fa88677f8ad024b648795 [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 },
Jiyong Park0f80c182020-01-31 02:49:53 +0900805 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900806 }
807
808 cc_library {
809 name: "libbar",
810 srcs: ["mylib.cpp"],
811 system_shared_libs: [],
812 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000813 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900814 }
815
816 `)
817
Sundong Ahnabb64432019-10-22 13:58:29 +0900818 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900819 copyCmds := apexRule.Args["copy_commands"]
820
821 // Ensure that direct non-stubs dep is always included
822 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
823
824 // Ensure that indirect stubs dep is not included
825 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
826
827 // Ensure that runtime_libs dep in included
828 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
829
Sundong Ahnabb64432019-10-22 13:58:29 +0900830 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900831 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
832 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900833
834}
835
Jooyung Han9c80bae2019-08-20 17:30:57 +0900836func TestApexDependencyToLLNDK(t *testing.T) {
837 ctx, _ := testApex(t, `
838 apex {
839 name: "myapex",
840 key: "myapex.key",
841 use_vendor: true,
842 native_shared_libs: ["mylib"],
843 }
844
845 apex_key {
846 name: "myapex.key",
847 public_key: "testkey.avbpubkey",
848 private_key: "testkey.pem",
849 }
850
851 cc_library {
852 name: "mylib",
853 srcs: ["mylib.cpp"],
854 vendor_available: true,
855 shared_libs: ["libbar"],
856 system_shared_libs: [],
857 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000858 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900859 }
860
861 cc_library {
862 name: "libbar",
863 srcs: ["mylib.cpp"],
864 system_shared_libs: [],
865 stl: "none",
866 }
867
868 llndk_library {
869 name: "libbar",
870 symbol_file: "",
871 }
Jooyung Handc782442019-11-01 03:14:38 +0900872 `, func(fs map[string][]byte, config android.Config) {
873 setUseVendorWhitelistForTest(config, []string{"myapex"})
874 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900875
Sundong Ahnabb64432019-10-22 13:58:29 +0900876 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900877 copyCmds := apexRule.Args["copy_commands"]
878
879 // Ensure that LLNDK dep is not included
880 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
881
Sundong Ahnabb64432019-10-22 13:58:29 +0900882 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900883 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900884
885 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900886 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900887
888}
889
Jiyong Park25fc6a92018-11-18 18:02:45 +0900890func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700891 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900892 apex {
893 name: "myapex",
894 key: "myapex.key",
895 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
896 }
897
898 apex_key {
899 name: "myapex.key",
900 public_key: "testkey.avbpubkey",
901 private_key: "testkey.pem",
902 }
903
904 cc_library {
905 name: "mylib",
906 srcs: ["mylib.cpp"],
907 shared_libs: ["libdl#27"],
908 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000909 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900910 }
911
912 cc_library_shared {
913 name: "mylib_shared",
914 srcs: ["mylib.cpp"],
915 shared_libs: ["libdl#27"],
916 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000917 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900918 }
919
920 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900921 name: "libBootstrap",
922 srcs: ["mylib.cpp"],
923 stl: "none",
924 bootstrap: true,
925 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900926 `)
927
Sundong Ahnabb64432019-10-22 13:58:29 +0900928 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900929 copyCmds := apexRule.Args["copy_commands"]
930
931 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800932 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900933 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
934 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900935
936 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900937 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900938
Colin Cross7113d202019-11-20 16:39:12 -0800939 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
940 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
941 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900942
943 // For dependency to libc
944 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900945 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900946 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900947 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900948 // ... Cflags from stub is correctly exported to mylib
949 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
950 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
951
952 // For dependency to libm
953 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800954 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900955 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900956 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900957 // ... and is not compiling with the stub
958 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
959 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
960
961 // For dependency to libdl
962 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900963 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900965 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
966 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800968 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900969 // ... Cflags from stub is correctly exported to mylib
970 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
971 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900972
973 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800974 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
975 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
976 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
977 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900978}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900979
980func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700981 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900982 apex {
983 name: "myapex",
984 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900985 native_shared_libs: ["mylib"],
986 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900987 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900988 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900989 }
990
991 apex_key {
992 name: "myapex.key",
993 public_key: "testkey.avbpubkey",
994 private_key: "testkey.pem",
995 }
996
997 prebuilt_etc {
998 name: "myetc",
999 src: "myprebuilt",
1000 sub_dir: "foo/bar",
1001 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001002
1003 cc_library {
1004 name: "mylib",
1005 srcs: ["mylib.cpp"],
1006 relative_install_path: "foo/bar",
1007 system_shared_libs: [],
1008 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001009 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001010 }
1011
1012 cc_binary {
1013 name: "mybin",
1014 srcs: ["mylib.cpp"],
1015 relative_install_path: "foo/bar",
1016 system_shared_libs: [],
1017 static_executable: true,
1018 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001019 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001020 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001021 `)
1022
Sundong Ahnabb64432019-10-22 13:58:29 +09001023 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001024 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1025
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001026 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001027 ensureListContains(t, dirs, "etc")
1028 ensureListContains(t, dirs, "etc/foo")
1029 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001030 ensureListContains(t, dirs, "lib64")
1031 ensureListContains(t, dirs, "lib64/foo")
1032 ensureListContains(t, dirs, "lib64/foo/bar")
1033 ensureListContains(t, dirs, "lib")
1034 ensureListContains(t, dirs, "lib/foo")
1035 ensureListContains(t, dirs, "lib/foo/bar")
1036
Jiyong Parkbd13e442019-03-15 18:10:35 +09001037 ensureListContains(t, dirs, "bin")
1038 ensureListContains(t, dirs, "bin/foo")
1039 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001040}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001041
1042func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001043 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001044 apex {
1045 name: "myapex",
1046 key: "myapex.key",
1047 native_shared_libs: ["mylib"],
1048 use_vendor: true,
1049 }
1050
1051 apex_key {
1052 name: "myapex.key",
1053 public_key: "testkey.avbpubkey",
1054 private_key: "testkey.pem",
1055 }
1056
1057 cc_library {
1058 name: "mylib",
1059 srcs: ["mylib.cpp"],
1060 shared_libs: ["mylib2"],
1061 system_shared_libs: [],
1062 vendor_available: true,
1063 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001064 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001065 }
1066
1067 cc_library {
1068 name: "mylib2",
1069 srcs: ["mylib.cpp"],
1070 system_shared_libs: [],
1071 vendor_available: true,
1072 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001073 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001074 }
Jooyung Handc782442019-11-01 03:14:38 +09001075 `, func(fs map[string][]byte, config android.Config) {
1076 setUseVendorWhitelistForTest(config, []string{"myapex"})
1077 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001078
1079 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001080 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001081 for _, implicit := range i.Implicits {
1082 inputsList = append(inputsList, implicit.String())
1083 }
1084 }
1085 inputsString := strings.Join(inputsList, " ")
1086
1087 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001088 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1089 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001090
1091 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001092 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1093 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001094}
Jiyong Park16e91a02018-12-20 18:18:08 +09001095
Jooyung Handc782442019-11-01 03:14:38 +09001096func TestUseVendorRestriction(t *testing.T) {
1097 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1098 apex {
1099 name: "myapex",
1100 key: "myapex.key",
1101 use_vendor: true,
1102 }
1103 apex_key {
1104 name: "myapex.key",
1105 public_key: "testkey.avbpubkey",
1106 private_key: "testkey.pem",
1107 }
1108 `, func(fs map[string][]byte, config android.Config) {
1109 setUseVendorWhitelistForTest(config, []string{""})
1110 })
1111 // no error with whitelist
1112 testApex(t, `
1113 apex {
1114 name: "myapex",
1115 key: "myapex.key",
1116 use_vendor: true,
1117 }
1118 apex_key {
1119 name: "myapex.key",
1120 public_key: "testkey.avbpubkey",
1121 private_key: "testkey.pem",
1122 }
1123 `, func(fs map[string][]byte, config android.Config) {
1124 setUseVendorWhitelistForTest(config, []string{"myapex"})
1125 })
1126}
1127
Jooyung Han5c998b92019-06-27 11:30:33 +09001128func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1129 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1130 apex {
1131 name: "myapex",
1132 key: "myapex.key",
1133 native_shared_libs: ["mylib"],
1134 use_vendor: true,
1135 }
1136
1137 apex_key {
1138 name: "myapex.key",
1139 public_key: "testkey.avbpubkey",
1140 private_key: "testkey.pem",
1141 }
1142
1143 cc_library {
1144 name: "mylib",
1145 srcs: ["mylib.cpp"],
1146 system_shared_libs: [],
1147 stl: "none",
1148 }
1149 `)
1150}
1151
Jiyong Park16e91a02018-12-20 18:18:08 +09001152func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001153 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001154 apex {
1155 name: "myapex",
1156 key: "myapex.key",
1157 native_shared_libs: ["mylib"],
1158 }
1159
1160 apex_key {
1161 name: "myapex.key",
1162 public_key: "testkey.avbpubkey",
1163 private_key: "testkey.pem",
1164 }
1165
1166 cc_library {
1167 name: "mylib",
1168 srcs: ["mylib.cpp"],
1169 system_shared_libs: [],
1170 stl: "none",
1171 stubs: {
1172 versions: ["1", "2", "3"],
1173 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001174 apex_available: [
1175 "//apex_available:platform",
1176 "myapex",
1177 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001178 }
1179
1180 cc_binary {
1181 name: "not_in_apex",
1182 srcs: ["mylib.cpp"],
1183 static_libs: ["mylib"],
1184 static_executable: true,
1185 system_shared_libs: [],
1186 stl: "none",
1187 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001188 `)
1189
Colin Cross7113d202019-11-20 16:39:12 -08001190 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001191
1192 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001193 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001194}
Jiyong Park9335a262018-12-24 11:31:58 +09001195
1196func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001197 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001198 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001199 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001200 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001201 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001202 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001203 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001204 }
1205
1206 cc_library {
1207 name: "mylib",
1208 srcs: ["mylib.cpp"],
1209 system_shared_libs: [],
1210 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001211 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001212 }
1213
1214 apex_key {
1215 name: "myapex.key",
1216 public_key: "testkey.avbpubkey",
1217 private_key: "testkey.pem",
1218 }
1219
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001220 android_app_certificate {
1221 name: "myapex.certificate",
1222 certificate: "testkey",
1223 }
1224
1225 android_app_certificate {
1226 name: "myapex.certificate.override",
1227 certificate: "testkey.override",
1228 }
1229
Jiyong Park9335a262018-12-24 11:31:58 +09001230 `)
1231
1232 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001233 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001234
1235 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1236 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1237 "vendor/foo/devkeys/testkey.avbpubkey")
1238 }
1239 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1240 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1241 "vendor/foo/devkeys/testkey.pem")
1242 }
1243
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001244 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001245 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001246 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001247 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001248 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001249 }
1250}
Jiyong Park58e364a2019-01-19 19:24:06 +09001251
Jooyung Hanf121a652019-12-17 14:30:11 +09001252func TestCertificate(t *testing.T) {
1253 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1254 ctx, _ := testApex(t, `
1255 apex {
1256 name: "myapex",
1257 key: "myapex.key",
1258 }
1259 apex_key {
1260 name: "myapex.key",
1261 public_key: "testkey.avbpubkey",
1262 private_key: "testkey.pem",
1263 }`)
1264 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1265 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1266 if actual := rule.Args["certificates"]; actual != expected {
1267 t.Errorf("certificates should be %q, not %q", expected, actual)
1268 }
1269 })
1270 t.Run("override when unspecified", func(t *testing.T) {
1271 ctx, _ := testApex(t, `
1272 apex {
1273 name: "myapex_keytest",
1274 key: "myapex.key",
1275 file_contexts: ":myapex-file_contexts",
1276 }
1277 apex_key {
1278 name: "myapex.key",
1279 public_key: "testkey.avbpubkey",
1280 private_key: "testkey.pem",
1281 }
1282 android_app_certificate {
1283 name: "myapex.certificate.override",
1284 certificate: "testkey.override",
1285 }`)
1286 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1287 expected := "testkey.override.x509.pem testkey.override.pk8"
1288 if actual := rule.Args["certificates"]; actual != expected {
1289 t.Errorf("certificates should be %q, not %q", expected, actual)
1290 }
1291 })
1292 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1293 ctx, _ := testApex(t, `
1294 apex {
1295 name: "myapex",
1296 key: "myapex.key",
1297 certificate: ":myapex.certificate",
1298 }
1299 apex_key {
1300 name: "myapex.key",
1301 public_key: "testkey.avbpubkey",
1302 private_key: "testkey.pem",
1303 }
1304 android_app_certificate {
1305 name: "myapex.certificate",
1306 certificate: "testkey",
1307 }`)
1308 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1309 expected := "testkey.x509.pem testkey.pk8"
1310 if actual := rule.Args["certificates"]; actual != expected {
1311 t.Errorf("certificates should be %q, not %q", expected, actual)
1312 }
1313 })
1314 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1315 ctx, _ := testApex(t, `
1316 apex {
1317 name: "myapex_keytest",
1318 key: "myapex.key",
1319 file_contexts: ":myapex-file_contexts",
1320 certificate: ":myapex.certificate",
1321 }
1322 apex_key {
1323 name: "myapex.key",
1324 public_key: "testkey.avbpubkey",
1325 private_key: "testkey.pem",
1326 }
1327 android_app_certificate {
1328 name: "myapex.certificate.override",
1329 certificate: "testkey.override",
1330 }`)
1331 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1332 expected := "testkey.override.x509.pem testkey.override.pk8"
1333 if actual := rule.Args["certificates"]; actual != expected {
1334 t.Errorf("certificates should be %q, not %q", expected, actual)
1335 }
1336 })
1337 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1338 ctx, _ := testApex(t, `
1339 apex {
1340 name: "myapex",
1341 key: "myapex.key",
1342 certificate: "testkey",
1343 }
1344 apex_key {
1345 name: "myapex.key",
1346 public_key: "testkey.avbpubkey",
1347 private_key: "testkey.pem",
1348 }`)
1349 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1350 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1351 if actual := rule.Args["certificates"]; actual != expected {
1352 t.Errorf("certificates should be %q, not %q", expected, actual)
1353 }
1354 })
1355 t.Run("override when specified as <name>", func(t *testing.T) {
1356 ctx, _ := testApex(t, `
1357 apex {
1358 name: "myapex_keytest",
1359 key: "myapex.key",
1360 file_contexts: ":myapex-file_contexts",
1361 certificate: "testkey",
1362 }
1363 apex_key {
1364 name: "myapex.key",
1365 public_key: "testkey.avbpubkey",
1366 private_key: "testkey.pem",
1367 }
1368 android_app_certificate {
1369 name: "myapex.certificate.override",
1370 certificate: "testkey.override",
1371 }`)
1372 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1373 expected := "testkey.override.x509.pem testkey.override.pk8"
1374 if actual := rule.Args["certificates"]; actual != expected {
1375 t.Errorf("certificates should be %q, not %q", expected, actual)
1376 }
1377 })
1378}
1379
Jiyong Park58e364a2019-01-19 19:24:06 +09001380func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001381 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001382 apex {
1383 name: "myapex",
1384 key: "myapex.key",
1385 native_shared_libs: ["mylib"],
1386 }
1387
1388 apex {
1389 name: "otherapex",
1390 key: "myapex.key",
1391 native_shared_libs: ["mylib"],
1392 }
1393
1394 apex_key {
1395 name: "myapex.key",
1396 public_key: "testkey.avbpubkey",
1397 private_key: "testkey.pem",
1398 }
1399
1400 cc_library {
1401 name: "mylib",
1402 srcs: ["mylib.cpp"],
1403 system_shared_libs: [],
1404 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001405 // TODO: remove //apex_available:platform
1406 apex_available: [
1407 "//apex_available:platform",
1408 "myapex",
1409 "otherapex",
1410 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001411 }
1412 `)
1413
Jooyung Han6b8459b2019-10-30 08:29:25 +09001414 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001415 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001416 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001417 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1418 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001419
Jooyung Han6b8459b2019-10-30 08:29:25 +09001420 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001421 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001422 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001423 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1424 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001425
Jooyung Han6b8459b2019-10-30 08:29:25 +09001426 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001427 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001428 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001429 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1430 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001431}
Jiyong Park7e636d02019-01-28 16:16:54 +09001432
1433func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001434 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001435 apex {
1436 name: "myapex",
1437 key: "myapex.key",
1438 native_shared_libs: ["mylib"],
1439 }
1440
1441 apex_key {
1442 name: "myapex.key",
1443 public_key: "testkey.avbpubkey",
1444 private_key: "testkey.pem",
1445 }
1446
1447 cc_library_headers {
1448 name: "mylib_headers",
1449 export_include_dirs: ["my_include"],
1450 system_shared_libs: [],
1451 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001452 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001453 }
1454
1455 cc_library {
1456 name: "mylib",
1457 srcs: ["mylib.cpp"],
1458 system_shared_libs: [],
1459 stl: "none",
1460 header_libs: ["mylib_headers"],
1461 export_header_lib_headers: ["mylib_headers"],
1462 stubs: {
1463 versions: ["1", "2", "3"],
1464 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001465 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001466 }
1467
1468 cc_library {
1469 name: "otherlib",
1470 srcs: ["mylib.cpp"],
1471 system_shared_libs: [],
1472 stl: "none",
1473 shared_libs: ["mylib"],
1474 }
1475 `)
1476
Colin Cross7113d202019-11-20 16:39:12 -08001477 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001478
1479 // Ensure that the include path of the header lib is exported to 'otherlib'
1480 ensureContains(t, cFlags, "-Imy_include")
1481}
Alex Light9670d332019-01-29 18:07:33 -08001482
Jiyong Park7cd10e32020-01-14 09:22:18 +09001483type fileInApex struct {
1484 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001485 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001486 isLink bool
1487}
1488
Jooyung Hana57af4a2020-01-23 05:36:59 +00001489func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001490 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001491 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001492 copyCmds := apexRule.Args["copy_commands"]
1493 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001494 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001495 for _, cmd := range strings.Split(copyCmds, "&&") {
1496 cmd = strings.TrimSpace(cmd)
1497 if cmd == "" {
1498 continue
1499 }
1500 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001501 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001502 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001503 switch terms[0] {
1504 case "mkdir":
1505 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001506 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001507 t.Fatal("copyCmds contains invalid cp command", cmd)
1508 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001509 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001510 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001511 isLink = false
1512 case "ln":
1513 if len(terms) != 3 && len(terms) != 4 {
1514 // ln LINK TARGET or ln -s LINK TARGET
1515 t.Fatal("copyCmds contains invalid ln command", cmd)
1516 }
1517 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001518 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001519 isLink = true
1520 default:
1521 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1522 }
1523 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001524 index := strings.Index(dst, imageApexDir)
1525 if index == -1 {
1526 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1527 }
1528 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001529 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001530 }
1531 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001532 return ret
1533}
1534
Jooyung Hana57af4a2020-01-23 05:36:59 +00001535func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1536 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001537 var failed bool
1538 var surplus []string
1539 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001540 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09001541 for _, expected := range files {
1542 if matched, _ := path.Match(expected, file.path); matched {
1543 filesMatched[expected] = true
1544 return
1545 }
1546 }
1547 surplus = append(surplus, file.path)
1548 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001549
Jooyung Han31c470b2019-10-18 16:26:59 +09001550 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001551 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001552 t.Log("surplus files", surplus)
1553 failed = true
1554 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001555
1556 if len(files) > len(filesMatched) {
1557 var missing []string
1558 for _, expected := range files {
1559 if !filesMatched[expected] {
1560 missing = append(missing, expected)
1561 }
1562 }
1563 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001564 t.Log("missing files", missing)
1565 failed = true
1566 }
1567 if failed {
1568 t.Fail()
1569 }
1570}
1571
Jooyung Han344d5432019-08-23 11:17:39 +09001572func TestVndkApexCurrent(t *testing.T) {
1573 ctx, _ := testApex(t, `
1574 apex_vndk {
1575 name: "myapex",
1576 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001577 }
1578
1579 apex_key {
1580 name: "myapex.key",
1581 public_key: "testkey.avbpubkey",
1582 private_key: "testkey.pem",
1583 }
1584
1585 cc_library {
1586 name: "libvndk",
1587 srcs: ["mylib.cpp"],
1588 vendor_available: true,
1589 vndk: {
1590 enabled: true,
1591 },
1592 system_shared_libs: [],
1593 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001594 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001595 }
1596
1597 cc_library {
1598 name: "libvndksp",
1599 srcs: ["mylib.cpp"],
1600 vendor_available: true,
1601 vndk: {
1602 enabled: true,
1603 support_system_process: true,
1604 },
1605 system_shared_libs: [],
1606 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001607 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001608 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001609 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001610
Jooyung Hana57af4a2020-01-23 05:36:59 +00001611 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001612 "lib/libvndk.so",
1613 "lib/libvndksp.so",
1614 "lib64/libvndk.so",
1615 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001616 "etc/llndk.libraries.VER.txt",
1617 "etc/vndkcore.libraries.VER.txt",
1618 "etc/vndksp.libraries.VER.txt",
1619 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001620 })
Jooyung Han344d5432019-08-23 11:17:39 +09001621}
1622
1623func TestVndkApexWithPrebuilt(t *testing.T) {
1624 ctx, _ := testApex(t, `
1625 apex_vndk {
1626 name: "myapex",
1627 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001628 }
1629
1630 apex_key {
1631 name: "myapex.key",
1632 public_key: "testkey.avbpubkey",
1633 private_key: "testkey.pem",
1634 }
1635
1636 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001637 name: "libvndk",
1638 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001639 vendor_available: true,
1640 vndk: {
1641 enabled: true,
1642 },
1643 system_shared_libs: [],
1644 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001645 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001646 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001647
1648 cc_prebuilt_library_shared {
1649 name: "libvndk.arm",
1650 srcs: ["libvndk.arm.so"],
1651 vendor_available: true,
1652 vndk: {
1653 enabled: true,
1654 },
1655 enabled: false,
1656 arch: {
1657 arm: {
1658 enabled: true,
1659 },
1660 },
1661 system_shared_libs: [],
1662 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001663 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001664 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001665 `+vndkLibrariesTxtFiles("current"),
1666 withFiles(map[string][]byte{
1667 "libvndk.so": nil,
1668 "libvndk.arm.so": nil,
1669 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001670
Jooyung Hana57af4a2020-01-23 05:36:59 +00001671 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001672 "lib/libvndk.so",
1673 "lib/libvndk.arm.so",
1674 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001675 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001676 })
Jooyung Han344d5432019-08-23 11:17:39 +09001677}
1678
Jooyung Han39edb6c2019-11-06 16:53:07 +09001679func vndkLibrariesTxtFiles(vers ...string) (result string) {
1680 for _, v := range vers {
1681 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001682 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001683 result += `
1684 vndk_libraries_txt {
1685 name: "` + txt + `.libraries.txt",
1686 }
1687 `
1688 }
1689 } else {
1690 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1691 result += `
1692 prebuilt_etc {
1693 name: "` + txt + `.libraries.` + v + `.txt",
1694 src: "dummy.txt",
1695 }
1696 `
1697 }
1698 }
1699 }
1700 return
1701}
1702
Jooyung Han344d5432019-08-23 11:17:39 +09001703func TestVndkApexVersion(t *testing.T) {
1704 ctx, _ := testApex(t, `
1705 apex_vndk {
1706 name: "myapex_v27",
1707 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001708 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001709 vndk_version: "27",
1710 }
1711
1712 apex_key {
1713 name: "myapex.key",
1714 public_key: "testkey.avbpubkey",
1715 private_key: "testkey.pem",
1716 }
1717
Jooyung Han31c470b2019-10-18 16:26:59 +09001718 vndk_prebuilt_shared {
1719 name: "libvndk27",
1720 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001721 vendor_available: true,
1722 vndk: {
1723 enabled: true,
1724 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001725 target_arch: "arm64",
1726 arch: {
1727 arm: {
1728 srcs: ["libvndk27_arm.so"],
1729 },
1730 arm64: {
1731 srcs: ["libvndk27_arm64.so"],
1732 },
1733 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001734 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001735 }
1736
1737 vndk_prebuilt_shared {
1738 name: "libvndk27",
1739 version: "27",
1740 vendor_available: true,
1741 vndk: {
1742 enabled: true,
1743 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001744 target_arch: "x86_64",
1745 arch: {
1746 x86: {
1747 srcs: ["libvndk27_x86.so"],
1748 },
1749 x86_64: {
1750 srcs: ["libvndk27_x86_64.so"],
1751 },
1752 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001753 }
1754 `+vndkLibrariesTxtFiles("27"),
1755 withFiles(map[string][]byte{
1756 "libvndk27_arm.so": nil,
1757 "libvndk27_arm64.so": nil,
1758 "libvndk27_x86.so": nil,
1759 "libvndk27_x86_64.so": nil,
1760 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001761
Jooyung Hana57af4a2020-01-23 05:36:59 +00001762 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001763 "lib/libvndk27_arm.so",
1764 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001765 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001766 })
Jooyung Han344d5432019-08-23 11:17:39 +09001767}
1768
1769func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1770 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1771 apex_vndk {
1772 name: "myapex_v27",
1773 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001774 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001775 vndk_version: "27",
1776 }
1777 apex_vndk {
1778 name: "myapex_v27_other",
1779 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001780 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001781 vndk_version: "27",
1782 }
1783
1784 apex_key {
1785 name: "myapex.key",
1786 public_key: "testkey.avbpubkey",
1787 private_key: "testkey.pem",
1788 }
1789
1790 cc_library {
1791 name: "libvndk",
1792 srcs: ["mylib.cpp"],
1793 vendor_available: true,
1794 vndk: {
1795 enabled: true,
1796 },
1797 system_shared_libs: [],
1798 stl: "none",
1799 }
1800
1801 vndk_prebuilt_shared {
1802 name: "libvndk",
1803 version: "27",
1804 vendor_available: true,
1805 vndk: {
1806 enabled: true,
1807 },
1808 srcs: ["libvndk.so"],
1809 }
1810 `, withFiles(map[string][]byte{
1811 "libvndk.so": nil,
1812 }))
1813}
1814
Jooyung Han90eee022019-10-01 20:02:42 +09001815func TestVndkApexNameRule(t *testing.T) {
1816 ctx, _ := testApex(t, `
1817 apex_vndk {
1818 name: "myapex",
1819 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001820 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001821 }
1822 apex_vndk {
1823 name: "myapex_v28",
1824 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001825 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001826 vndk_version: "28",
1827 }
1828 apex_key {
1829 name: "myapex.key",
1830 public_key: "testkey.avbpubkey",
1831 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001832 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001833
1834 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001835 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001836 actual := proptools.String(bundle.properties.Apex_name)
1837 if !reflect.DeepEqual(actual, expected) {
1838 t.Errorf("Got '%v', expected '%v'", actual, expected)
1839 }
1840 }
1841
1842 assertApexName("com.android.vndk.vVER", "myapex")
1843 assertApexName("com.android.vndk.v28", "myapex_v28")
1844}
1845
Jooyung Han344d5432019-08-23 11:17:39 +09001846func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1847 ctx, _ := testApex(t, `
1848 apex_vndk {
1849 name: "myapex",
1850 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001851 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001852 }
1853
1854 apex_key {
1855 name: "myapex.key",
1856 public_key: "testkey.avbpubkey",
1857 private_key: "testkey.pem",
1858 }
1859
1860 cc_library {
1861 name: "libvndk",
1862 srcs: ["mylib.cpp"],
1863 vendor_available: true,
1864 native_bridge_supported: true,
1865 host_supported: true,
1866 vndk: {
1867 enabled: true,
1868 },
1869 system_shared_libs: [],
1870 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001871 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001872 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001873 `+vndkLibrariesTxtFiles("current"),
1874 withTargets(map[android.OsType][]android.Target{
1875 android.Android: []android.Target{
1876 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1877 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1878 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1879 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1880 },
1881 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001882
Jooyung Hana57af4a2020-01-23 05:36:59 +00001883 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001884 "lib/libvndk.so",
1885 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001886 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001887 })
Jooyung Han344d5432019-08-23 11:17:39 +09001888}
1889
1890func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1891 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1892 apex_vndk {
1893 name: "myapex",
1894 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001895 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001896 native_bridge_supported: true,
1897 }
1898
1899 apex_key {
1900 name: "myapex.key",
1901 public_key: "testkey.avbpubkey",
1902 private_key: "testkey.pem",
1903 }
1904
1905 cc_library {
1906 name: "libvndk",
1907 srcs: ["mylib.cpp"],
1908 vendor_available: true,
1909 native_bridge_supported: true,
1910 host_supported: true,
1911 vndk: {
1912 enabled: true,
1913 },
1914 system_shared_libs: [],
1915 stl: "none",
1916 }
1917 `)
1918}
1919
Jooyung Han31c470b2019-10-18 16:26:59 +09001920func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001921 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001922 apex_vndk {
1923 name: "myapex_v27",
1924 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001925 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001926 vndk_version: "27",
1927 }
1928
1929 apex_key {
1930 name: "myapex.key",
1931 public_key: "testkey.avbpubkey",
1932 private_key: "testkey.pem",
1933 }
1934
1935 vndk_prebuilt_shared {
1936 name: "libvndk27",
1937 version: "27",
1938 target_arch: "arm",
1939 vendor_available: true,
1940 vndk: {
1941 enabled: true,
1942 },
1943 arch: {
1944 arm: {
1945 srcs: ["libvndk27.so"],
1946 }
1947 },
1948 }
1949
1950 vndk_prebuilt_shared {
1951 name: "libvndk27",
1952 version: "27",
1953 target_arch: "arm",
1954 binder32bit: true,
1955 vendor_available: true,
1956 vndk: {
1957 enabled: true,
1958 },
1959 arch: {
1960 arm: {
1961 srcs: ["libvndk27binder32.so"],
1962 }
1963 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001964 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001965 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001966 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001967 withFiles(map[string][]byte{
1968 "libvndk27.so": nil,
1969 "libvndk27binder32.so": nil,
1970 }),
1971 withBinder32bit,
1972 withTargets(map[android.OsType][]android.Target{
1973 android.Android: []android.Target{
1974 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1975 },
1976 }),
1977 )
1978
Jooyung Hana57af4a2020-01-23 05:36:59 +00001979 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001980 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001981 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001982 })
1983}
1984
Jooyung Hane1633032019-08-01 17:41:43 +09001985func TestDependenciesInApexManifest(t *testing.T) {
1986 ctx, _ := testApex(t, `
1987 apex {
1988 name: "myapex_nodep",
1989 key: "myapex.key",
1990 native_shared_libs: ["lib_nodep"],
1991 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001992 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001993 }
1994
1995 apex {
1996 name: "myapex_dep",
1997 key: "myapex.key",
1998 native_shared_libs: ["lib_dep"],
1999 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002000 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002001 }
2002
2003 apex {
2004 name: "myapex_provider",
2005 key: "myapex.key",
2006 native_shared_libs: ["libfoo"],
2007 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002008 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002009 }
2010
2011 apex {
2012 name: "myapex_selfcontained",
2013 key: "myapex.key",
2014 native_shared_libs: ["lib_dep", "libfoo"],
2015 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002016 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002017 }
2018
2019 apex_key {
2020 name: "myapex.key",
2021 public_key: "testkey.avbpubkey",
2022 private_key: "testkey.pem",
2023 }
2024
2025 cc_library {
2026 name: "lib_nodep",
2027 srcs: ["mylib.cpp"],
2028 system_shared_libs: [],
2029 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002030 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002031 }
2032
2033 cc_library {
2034 name: "lib_dep",
2035 srcs: ["mylib.cpp"],
2036 shared_libs: ["libfoo"],
2037 system_shared_libs: [],
2038 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002039 apex_available: [
2040 "myapex_dep",
2041 "myapex_provider",
2042 "myapex_selfcontained",
2043 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002044 }
2045
2046 cc_library {
2047 name: "libfoo",
2048 srcs: ["mytest.cpp"],
2049 stubs: {
2050 versions: ["1"],
2051 },
2052 system_shared_libs: [],
2053 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002054 apex_available: [
2055 "myapex_provider",
2056 "myapex_selfcontained",
2057 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002058 }
2059 `)
2060
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002061 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002062 var provideNativeLibs, requireNativeLibs []string
2063
Sundong Ahnabb64432019-10-22 13:58:29 +09002064 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002065 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2066 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002067 ensureListEmpty(t, provideNativeLibs)
2068 ensureListEmpty(t, requireNativeLibs)
2069
Sundong Ahnabb64432019-10-22 13:58:29 +09002070 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002071 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2072 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002073 ensureListEmpty(t, provideNativeLibs)
2074 ensureListContains(t, requireNativeLibs, "libfoo.so")
2075
Sundong Ahnabb64432019-10-22 13:58:29 +09002076 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002077 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2078 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002079 ensureListContains(t, provideNativeLibs, "libfoo.so")
2080 ensureListEmpty(t, requireNativeLibs)
2081
Sundong Ahnabb64432019-10-22 13:58:29 +09002082 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002083 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2084 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002085 ensureListContains(t, provideNativeLibs, "libfoo.so")
2086 ensureListEmpty(t, requireNativeLibs)
2087}
2088
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002089func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002090 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002091 apex {
2092 name: "myapex",
2093 key: "myapex.key",
2094 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002095 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002096 }
2097
2098 apex_key {
2099 name: "myapex.key",
2100 public_key: "testkey.avbpubkey",
2101 private_key: "testkey.pem",
2102 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002103
2104 cc_library {
2105 name: "mylib",
2106 srcs: ["mylib.cpp"],
2107 system_shared_libs: [],
2108 stl: "none",
2109 apex_available: [
2110 "//apex_available:platform",
2111 "myapex",
2112 ],
2113 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002114 `)
2115
Sundong Ahnabb64432019-10-22 13:58:29 +09002116 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002117 apexManifestRule := module.Rule("apexManifestRule")
2118 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2119 apexRule := module.Rule("apexRule")
2120 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002121
2122 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2123 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2124 name := apexBundle.BaseModuleName()
2125 prefix := "TARGET_"
2126 var builder strings.Builder
2127 data.Custom(&builder, name, prefix, "", data)
2128 androidMk := builder.String()
2129 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2130 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002131}
2132
Alex Light0851b882019-02-07 13:20:53 -08002133func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002134 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002135 apex {
2136 name: "myapex",
2137 key: "myapex.key",
2138 native_shared_libs: ["mylib_common"],
2139 }
2140
2141 apex_key {
2142 name: "myapex.key",
2143 public_key: "testkey.avbpubkey",
2144 private_key: "testkey.pem",
2145 }
2146
2147 cc_library {
2148 name: "mylib_common",
2149 srcs: ["mylib.cpp"],
2150 system_shared_libs: [],
2151 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002152 apex_available: [
2153 "//apex_available:platform",
2154 "myapex",
2155 ],
Alex Light0851b882019-02-07 13:20:53 -08002156 }
2157 `)
2158
Sundong Ahnabb64432019-10-22 13:58:29 +09002159 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002160 apexRule := module.Rule("apexRule")
2161 copyCmds := apexRule.Args["copy_commands"]
2162
2163 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2164 t.Log("Apex was a test apex!")
2165 t.Fail()
2166 }
2167 // Ensure that main rule creates an output
2168 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2169
2170 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002171 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002172
2173 // Ensure that both direct and indirect deps are copied into apex
2174 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2175
Colin Cross7113d202019-11-20 16:39:12 -08002176 // Ensure that the platform variant ends with _shared
2177 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002178
2179 if !android.InAnyApex("mylib_common") {
2180 t.Log("Found mylib_common not in any apex!")
2181 t.Fail()
2182 }
2183}
2184
2185func TestTestApex(t *testing.T) {
2186 if android.InAnyApex("mylib_common_test") {
2187 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!")
2188 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002189 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002190 apex_test {
2191 name: "myapex",
2192 key: "myapex.key",
2193 native_shared_libs: ["mylib_common_test"],
2194 }
2195
2196 apex_key {
2197 name: "myapex.key",
2198 public_key: "testkey.avbpubkey",
2199 private_key: "testkey.pem",
2200 }
2201
2202 cc_library {
2203 name: "mylib_common_test",
2204 srcs: ["mylib.cpp"],
2205 system_shared_libs: [],
2206 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002207 // TODO: remove //apex_available:platform
2208 apex_available: [
2209 "//apex_available:platform",
2210 "myapex",
2211 ],
Alex Light0851b882019-02-07 13:20:53 -08002212 }
2213 `)
2214
Sundong Ahnabb64432019-10-22 13:58:29 +09002215 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002216 apexRule := module.Rule("apexRule")
2217 copyCmds := apexRule.Args["copy_commands"]
2218
2219 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2220 t.Log("Apex was not a test apex!")
2221 t.Fail()
2222 }
2223 // Ensure that main rule creates an output
2224 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2225
2226 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002227 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002228
2229 // Ensure that both direct and indirect deps are copied into apex
2230 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2231
Colin Cross7113d202019-11-20 16:39:12 -08002232 // Ensure that the platform variant ends with _shared
2233 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002234}
2235
Alex Light9670d332019-01-29 18:07:33 -08002236func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002237 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002238 apex {
2239 name: "myapex",
2240 key: "myapex.key",
2241 multilib: {
2242 first: {
2243 native_shared_libs: ["mylib_common"],
2244 }
2245 },
2246 target: {
2247 android: {
2248 multilib: {
2249 first: {
2250 native_shared_libs: ["mylib"],
2251 }
2252 }
2253 },
2254 host: {
2255 multilib: {
2256 first: {
2257 native_shared_libs: ["mylib2"],
2258 }
2259 }
2260 }
2261 }
2262 }
2263
2264 apex_key {
2265 name: "myapex.key",
2266 public_key: "testkey.avbpubkey",
2267 private_key: "testkey.pem",
2268 }
2269
2270 cc_library {
2271 name: "mylib",
2272 srcs: ["mylib.cpp"],
2273 system_shared_libs: [],
2274 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002275 // TODO: remove //apex_available:platform
2276 apex_available: [
2277 "//apex_available:platform",
2278 "myapex",
2279 ],
Alex Light9670d332019-01-29 18:07:33 -08002280 }
2281
2282 cc_library {
2283 name: "mylib_common",
2284 srcs: ["mylib.cpp"],
2285 system_shared_libs: [],
2286 stl: "none",
2287 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002288 // TODO: remove //apex_available:platform
2289 apex_available: [
2290 "//apex_available:platform",
2291 "myapex",
2292 ],
Alex Light9670d332019-01-29 18:07:33 -08002293 }
2294
2295 cc_library {
2296 name: "mylib2",
2297 srcs: ["mylib.cpp"],
2298 system_shared_libs: [],
2299 stl: "none",
2300 compile_multilib: "first",
2301 }
2302 `)
2303
Sundong Ahnabb64432019-10-22 13:58:29 +09002304 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002305 copyCmds := apexRule.Args["copy_commands"]
2306
2307 // Ensure that main rule creates an output
2308 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2309
2310 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002311 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2312 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2313 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002314
2315 // Ensure that both direct and indirect deps are copied into apex
2316 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2317 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2318 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2319
Colin Cross7113d202019-11-20 16:39:12 -08002320 // Ensure that the platform variant ends with _shared
2321 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2322 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2323 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002324}
Jiyong Park04480cf2019-02-06 00:16:29 +09002325
2326func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002327 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002328 apex {
2329 name: "myapex",
2330 key: "myapex.key",
2331 binaries: ["myscript"],
2332 }
2333
2334 apex_key {
2335 name: "myapex.key",
2336 public_key: "testkey.avbpubkey",
2337 private_key: "testkey.pem",
2338 }
2339
2340 sh_binary {
2341 name: "myscript",
2342 src: "mylib.cpp",
2343 filename: "myscript.sh",
2344 sub_dir: "script",
2345 }
2346 `)
2347
Sundong Ahnabb64432019-10-22 13:58:29 +09002348 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002349 copyCmds := apexRule.Args["copy_commands"]
2350
2351 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2352}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002353
Jooyung Han91df2082019-11-20 01:49:42 +09002354func TestApexInVariousPartition(t *testing.T) {
2355 testcases := []struct {
2356 propName, parition, flattenedPartition string
2357 }{
2358 {"", "system", "system_ext"},
2359 {"product_specific: true", "product", "product"},
2360 {"soc_specific: true", "vendor", "vendor"},
2361 {"proprietary: true", "vendor", "vendor"},
2362 {"vendor: true", "vendor", "vendor"},
2363 {"system_ext_specific: true", "system_ext", "system_ext"},
2364 }
2365 for _, tc := range testcases {
2366 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2367 ctx, _ := testApex(t, `
2368 apex {
2369 name: "myapex",
2370 key: "myapex.key",
2371 `+tc.propName+`
2372 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002373
Jooyung Han91df2082019-11-20 01:49:42 +09002374 apex_key {
2375 name: "myapex.key",
2376 public_key: "testkey.avbpubkey",
2377 private_key: "testkey.pem",
2378 }
2379 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002380
Jooyung Han91df2082019-11-20 01:49:42 +09002381 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2382 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2383 actual := apex.installDir.String()
2384 if actual != expected {
2385 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2386 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002387
Jooyung Han91df2082019-11-20 01:49:42 +09002388 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2389 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2390 actual = flattened.installDir.String()
2391 if actual != expected {
2392 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2393 }
2394 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002395 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002396}
Jiyong Park67882562019-03-21 01:11:21 +09002397
Jooyung Han54aca7b2019-11-20 02:26:02 +09002398func TestFileContexts(t *testing.T) {
2399 ctx, _ := testApex(t, `
2400 apex {
2401 name: "myapex",
2402 key: "myapex.key",
2403 }
2404
2405 apex_key {
2406 name: "myapex.key",
2407 public_key: "testkey.avbpubkey",
2408 private_key: "testkey.pem",
2409 }
2410 `)
2411 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2412 apexRule := module.Rule("apexRule")
2413 actual := apexRule.Args["file_contexts"]
2414 expected := "system/sepolicy/apex/myapex-file_contexts"
2415 if actual != expected {
2416 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2417 }
2418
2419 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2420 apex {
2421 name: "myapex",
2422 key: "myapex.key",
2423 file_contexts: "my_own_file_contexts",
2424 }
2425
2426 apex_key {
2427 name: "myapex.key",
2428 public_key: "testkey.avbpubkey",
2429 private_key: "testkey.pem",
2430 }
2431 `, withFiles(map[string][]byte{
2432 "my_own_file_contexts": nil,
2433 }))
2434
2435 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2436 apex {
2437 name: "myapex",
2438 key: "myapex.key",
2439 product_specific: true,
2440 file_contexts: "product_specific_file_contexts",
2441 }
2442
2443 apex_key {
2444 name: "myapex.key",
2445 public_key: "testkey.avbpubkey",
2446 private_key: "testkey.pem",
2447 }
2448 `)
2449
2450 ctx, _ = testApex(t, `
2451 apex {
2452 name: "myapex",
2453 key: "myapex.key",
2454 product_specific: true,
2455 file_contexts: "product_specific_file_contexts",
2456 }
2457
2458 apex_key {
2459 name: "myapex.key",
2460 public_key: "testkey.avbpubkey",
2461 private_key: "testkey.pem",
2462 }
2463 `, withFiles(map[string][]byte{
2464 "product_specific_file_contexts": nil,
2465 }))
2466 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2467 apexRule = module.Rule("apexRule")
2468 actual = apexRule.Args["file_contexts"]
2469 expected = "product_specific_file_contexts"
2470 if actual != expected {
2471 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2472 }
2473
2474 ctx, _ = testApex(t, `
2475 apex {
2476 name: "myapex",
2477 key: "myapex.key",
2478 product_specific: true,
2479 file_contexts: ":my-file-contexts",
2480 }
2481
2482 apex_key {
2483 name: "myapex.key",
2484 public_key: "testkey.avbpubkey",
2485 private_key: "testkey.pem",
2486 }
2487
2488 filegroup {
2489 name: "my-file-contexts",
2490 srcs: ["product_specific_file_contexts"],
2491 }
2492 `, withFiles(map[string][]byte{
2493 "product_specific_file_contexts": nil,
2494 }))
2495 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2496 apexRule = module.Rule("apexRule")
2497 actual = apexRule.Args["file_contexts"]
2498 expected = "product_specific_file_contexts"
2499 if actual != expected {
2500 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2501 }
2502}
2503
Jiyong Park67882562019-03-21 01:11:21 +09002504func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002505 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002506 apex_key {
2507 name: "myapex.key",
2508 public_key: ":my.avbpubkey",
2509 private_key: ":my.pem",
2510 product_specific: true,
2511 }
2512
2513 filegroup {
2514 name: "my.avbpubkey",
2515 srcs: ["testkey2.avbpubkey"],
2516 }
2517
2518 filegroup {
2519 name: "my.pem",
2520 srcs: ["testkey2.pem"],
2521 }
2522 `)
2523
2524 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2525 expected_pubkey := "testkey2.avbpubkey"
2526 actual_pubkey := apex_key.public_key_file.String()
2527 if actual_pubkey != expected_pubkey {
2528 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2529 }
2530 expected_privkey := "testkey2.pem"
2531 actual_privkey := apex_key.private_key_file.String()
2532 if actual_privkey != expected_privkey {
2533 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2534 }
2535}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002536
2537func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002538 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002539 prebuilt_apex {
2540 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002541 arch: {
2542 arm64: {
2543 src: "myapex-arm64.apex",
2544 },
2545 arm: {
2546 src: "myapex-arm.apex",
2547 },
2548 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002549 }
2550 `)
2551
2552 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2553
Jiyong Parkc95714e2019-03-29 14:23:10 +09002554 expectedInput := "myapex-arm64.apex"
2555 if prebuilt.inputApex.String() != expectedInput {
2556 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2557 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002558}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002559
2560func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002561 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002562 prebuilt_apex {
2563 name: "myapex",
2564 src: "myapex-arm.apex",
2565 filename: "notmyapex.apex",
2566 }
2567 `)
2568
2569 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2570
2571 expected := "notmyapex.apex"
2572 if p.installFilename != expected {
2573 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2574 }
2575}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002576
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002577func TestPrebuiltOverrides(t *testing.T) {
2578 ctx, config := testApex(t, `
2579 prebuilt_apex {
2580 name: "myapex.prebuilt",
2581 src: "myapex-arm.apex",
2582 overrides: [
2583 "myapex",
2584 ],
2585 }
2586 `)
2587
2588 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2589
2590 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002591 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002592 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002593 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002594 }
2595}
2596
Roland Levillain630846d2019-06-26 12:48:34 +01002597func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002598 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002599 apex_test {
2600 name: "myapex",
2601 key: "myapex.key",
2602 tests: [
2603 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002604 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002605 ],
2606 }
2607
2608 apex_key {
2609 name: "myapex.key",
2610 public_key: "testkey.avbpubkey",
2611 private_key: "testkey.pem",
2612 }
2613
2614 cc_test {
2615 name: "mytest",
2616 gtest: false,
2617 srcs: ["mytest.cpp"],
2618 relative_install_path: "test",
2619 system_shared_libs: [],
2620 static_executable: true,
2621 stl: "none",
2622 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002623
2624 cc_test {
2625 name: "mytests",
2626 gtest: false,
2627 srcs: [
2628 "mytest1.cpp",
2629 "mytest2.cpp",
2630 "mytest3.cpp",
2631 ],
2632 test_per_src: true,
2633 relative_install_path: "test",
2634 system_shared_libs: [],
2635 static_executable: true,
2636 stl: "none",
2637 }
Roland Levillain630846d2019-06-26 12:48:34 +01002638 `)
2639
Sundong Ahnabb64432019-10-22 13:58:29 +09002640 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002641 copyCmds := apexRule.Args["copy_commands"]
2642
2643 // Ensure that test dep is copied into apex.
2644 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002645
2646 // Ensure that test deps built with `test_per_src` are copied into apex.
2647 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2648 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2649 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002650
2651 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002652 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002653 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2654 name := apexBundle.BaseModuleName()
2655 prefix := "TARGET_"
2656 var builder strings.Builder
2657 data.Custom(&builder, name, prefix, "", data)
2658 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002659 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2660 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2661 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2662 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002663 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002664 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002665 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002666}
2667
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002668func TestInstallExtraFlattenedApexes(t *testing.T) {
2669 ctx, config := testApex(t, `
2670 apex {
2671 name: "myapex",
2672 key: "myapex.key",
2673 }
2674 apex_key {
2675 name: "myapex.key",
2676 public_key: "testkey.avbpubkey",
2677 private_key: "testkey.pem",
2678 }
2679 `, func(fs map[string][]byte, config android.Config) {
2680 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2681 })
2682 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002683 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002684 mk := android.AndroidMkDataForTest(t, config, "", ab)
2685 var builder strings.Builder
2686 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2687 androidMk := builder.String()
2688 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2689}
2690
Jooyung Han5c998b92019-06-27 11:30:33 +09002691func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002692 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002693 apex {
2694 name: "myapex",
2695 key: "myapex.key",
2696 native_shared_libs: ["mylib"],
2697 uses: ["commonapex"],
2698 }
2699
2700 apex {
2701 name: "commonapex",
2702 key: "myapex.key",
2703 native_shared_libs: ["libcommon"],
2704 provide_cpp_shared_libs: true,
2705 }
2706
2707 apex_key {
2708 name: "myapex.key",
2709 public_key: "testkey.avbpubkey",
2710 private_key: "testkey.pem",
2711 }
2712
2713 cc_library {
2714 name: "mylib",
2715 srcs: ["mylib.cpp"],
2716 shared_libs: ["libcommon"],
2717 system_shared_libs: [],
2718 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002719 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002720 }
2721
2722 cc_library {
2723 name: "libcommon",
2724 srcs: ["mylib_common.cpp"],
2725 system_shared_libs: [],
2726 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002727 // TODO: remove //apex_available:platform
2728 apex_available: [
2729 "//apex_available:platform",
2730 "commonapex",
2731 "myapex",
2732 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002733 }
2734 `)
2735
Sundong Ahnabb64432019-10-22 13:58:29 +09002736 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002737 apexRule1 := module1.Rule("apexRule")
2738 copyCmds1 := apexRule1.Args["copy_commands"]
2739
Sundong Ahnabb64432019-10-22 13:58:29 +09002740 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002741 apexRule2 := module2.Rule("apexRule")
2742 copyCmds2 := apexRule2.Args["copy_commands"]
2743
Colin Cross7113d202019-11-20 16:39:12 -08002744 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2745 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002746 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2747 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2748 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2749}
2750
2751func TestApexUsesFailsIfNotProvided(t *testing.T) {
2752 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2753 apex {
2754 name: "myapex",
2755 key: "myapex.key",
2756 uses: ["commonapex"],
2757 }
2758
2759 apex {
2760 name: "commonapex",
2761 key: "myapex.key",
2762 }
2763
2764 apex_key {
2765 name: "myapex.key",
2766 public_key: "testkey.avbpubkey",
2767 private_key: "testkey.pem",
2768 }
2769 `)
2770 testApexError(t, `uses: "commonapex" is not a provider`, `
2771 apex {
2772 name: "myapex",
2773 key: "myapex.key",
2774 uses: ["commonapex"],
2775 }
2776
2777 cc_library {
2778 name: "commonapex",
2779 system_shared_libs: [],
2780 stl: "none",
2781 }
2782
2783 apex_key {
2784 name: "myapex.key",
2785 public_key: "testkey.avbpubkey",
2786 private_key: "testkey.pem",
2787 }
2788 `)
2789}
2790
2791func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2792 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2793 apex {
2794 name: "myapex",
2795 key: "myapex.key",
2796 use_vendor: true,
2797 uses: ["commonapex"],
2798 }
2799
2800 apex {
2801 name: "commonapex",
2802 key: "myapex.key",
2803 provide_cpp_shared_libs: true,
2804 }
2805
2806 apex_key {
2807 name: "myapex.key",
2808 public_key: "testkey.avbpubkey",
2809 private_key: "testkey.pem",
2810 }
Jooyung Handc782442019-11-01 03:14:38 +09002811 `, func(fs map[string][]byte, config android.Config) {
2812 setUseVendorWhitelistForTest(config, []string{"myapex"})
2813 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002814}
2815
Jooyung Hand48f3c32019-08-23 11:18:57 +09002816func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2817 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2818 apex {
2819 name: "myapex",
2820 key: "myapex.key",
2821 native_shared_libs: ["libfoo"],
2822 }
2823
2824 apex_key {
2825 name: "myapex.key",
2826 public_key: "testkey.avbpubkey",
2827 private_key: "testkey.pem",
2828 }
2829
2830 cc_library {
2831 name: "libfoo",
2832 stl: "none",
2833 system_shared_libs: [],
2834 enabled: false,
2835 }
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,
2856 }
2857 `)
2858}
2859
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002860func TestApexWithApps(t *testing.T) {
2861 ctx, _ := testApex(t, `
2862 apex {
2863 name: "myapex",
2864 key: "myapex.key",
2865 apps: [
2866 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002867 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002868 ],
2869 }
2870
2871 apex_key {
2872 name: "myapex.key",
2873 public_key: "testkey.avbpubkey",
2874 private_key: "testkey.pem",
2875 }
2876
2877 android_app {
2878 name: "AppFoo",
2879 srcs: ["foo/bar/MyClass.java"],
2880 sdk_version: "none",
2881 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002882 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002883 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002884 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002885
2886 android_app {
2887 name: "AppFooPriv",
2888 srcs: ["foo/bar/MyClass.java"],
2889 sdk_version: "none",
2890 system_modules: "none",
2891 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002892 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09002893 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002894
2895 cc_library_shared {
2896 name: "libjni",
2897 srcs: ["mylib.cpp"],
Jooyung Han65041792020-02-25 16:59:29 +09002898 shared_libs: ["libfoo"],
Jiyong Park8be103b2019-11-08 15:53:48 +09002899 stl: "none",
2900 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09002901 apex_available: [ "myapex" ],
Jooyung Han65041792020-02-25 16:59:29 +09002902 sdk_version: "current",
2903 }
2904
2905 cc_library_shared {
2906 name: "libfoo",
2907 stl: "none",
2908 system_shared_libs: [],
2909 apex_available: [ "myapex" ],
2910 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09002911 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002912 `)
2913
Sundong Ahnabb64432019-10-22 13:58:29 +09002914 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002915 apexRule := module.Rule("apexRule")
2916 copyCmds := apexRule.Args["copy_commands"]
2917
2918 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002919 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002920
Jooyung Han65041792020-02-25 16:59:29 +09002921 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
2922 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09002923 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Han65041792020-02-25 16:59:29 +09002924 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002925 }
Jooyung Han65041792020-02-25 16:59:29 +09002926 // JNI libraries including transitive deps are
2927 for _, jni := range []string{"libjni", "libfoo"} {
2928 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
2929 // ... embedded inside APK (jnilibs.zip)
2930 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
2931 // ... and not directly inside the APEX
2932 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
2933 }
Dario Frenicde2a032019-10-27 00:29:22 +01002934}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002935
Dario Frenicde2a032019-10-27 00:29:22 +01002936func TestApexWithAppImports(t *testing.T) {
2937 ctx, _ := testApex(t, `
2938 apex {
2939 name: "myapex",
2940 key: "myapex.key",
2941 apps: [
2942 "AppFooPrebuilt",
2943 "AppFooPrivPrebuilt",
2944 ],
2945 }
2946
2947 apex_key {
2948 name: "myapex.key",
2949 public_key: "testkey.avbpubkey",
2950 private_key: "testkey.pem",
2951 }
2952
2953 android_app_import {
2954 name: "AppFooPrebuilt",
2955 apk: "PrebuiltAppFoo.apk",
2956 presigned: true,
2957 dex_preopt: {
2958 enabled: false,
2959 },
2960 }
2961
2962 android_app_import {
2963 name: "AppFooPrivPrebuilt",
2964 apk: "PrebuiltAppFooPriv.apk",
2965 privileged: true,
2966 presigned: true,
2967 dex_preopt: {
2968 enabled: false,
2969 },
2970 }
2971 `)
2972
Sundong Ahnabb64432019-10-22 13:58:29 +09002973 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002974 apexRule := module.Rule("apexRule")
2975 copyCmds := apexRule.Args["copy_commands"]
2976
2977 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2978 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002979}
2980
Dario Freni6f3937c2019-12-20 22:58:03 +00002981func TestApexWithTestHelperApp(t *testing.T) {
2982 ctx, _ := testApex(t, `
2983 apex {
2984 name: "myapex",
2985 key: "myapex.key",
2986 apps: [
2987 "TesterHelpAppFoo",
2988 ],
2989 }
2990
2991 apex_key {
2992 name: "myapex.key",
2993 public_key: "testkey.avbpubkey",
2994 private_key: "testkey.pem",
2995 }
2996
2997 android_test_helper_app {
2998 name: "TesterHelpAppFoo",
2999 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003000 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003001 }
3002
3003 `)
3004
3005 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3006 apexRule := module.Rule("apexRule")
3007 copyCmds := apexRule.Args["copy_commands"]
3008
3009 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3010}
3011
Jooyung Han18020ea2019-11-13 10:50:48 +09003012func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3013 // libfoo's apex_available comes from cc_defaults
3014 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3015 apex {
3016 name: "myapex",
3017 key: "myapex.key",
3018 native_shared_libs: ["libfoo"],
3019 }
3020
3021 apex_key {
3022 name: "myapex.key",
3023 public_key: "testkey.avbpubkey",
3024 private_key: "testkey.pem",
3025 }
3026
3027 apex {
3028 name: "otherapex",
3029 key: "myapex.key",
3030 native_shared_libs: ["libfoo"],
3031 }
3032
3033 cc_defaults {
3034 name: "libfoo-defaults",
3035 apex_available: ["otherapex"],
3036 }
3037
3038 cc_library {
3039 name: "libfoo",
3040 defaults: ["libfoo-defaults"],
3041 stl: "none",
3042 system_shared_libs: [],
3043 }`)
3044}
3045
Jiyong Park127b40b2019-09-30 16:04:35 +09003046func TestApexAvailable(t *testing.T) {
3047 // libfoo is not available to myapex, but only to otherapex
3048 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3049 apex {
3050 name: "myapex",
3051 key: "myapex.key",
3052 native_shared_libs: ["libfoo"],
3053 }
3054
3055 apex_key {
3056 name: "myapex.key",
3057 public_key: "testkey.avbpubkey",
3058 private_key: "testkey.pem",
3059 }
3060
3061 apex {
3062 name: "otherapex",
3063 key: "otherapex.key",
3064 native_shared_libs: ["libfoo"],
3065 }
3066
3067 apex_key {
3068 name: "otherapex.key",
3069 public_key: "testkey.avbpubkey",
3070 private_key: "testkey.pem",
3071 }
3072
3073 cc_library {
3074 name: "libfoo",
3075 stl: "none",
3076 system_shared_libs: [],
3077 apex_available: ["otherapex"],
3078 }`)
3079
3080 // libbar is an indirect dep
3081 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3082 apex {
3083 name: "myapex",
3084 key: "myapex.key",
3085 native_shared_libs: ["libfoo"],
3086 }
3087
3088 apex_key {
3089 name: "myapex.key",
3090 public_key: "testkey.avbpubkey",
3091 private_key: "testkey.pem",
3092 }
3093
3094 apex {
3095 name: "otherapex",
3096 key: "otherapex.key",
3097 native_shared_libs: ["libfoo"],
3098 }
3099
3100 apex_key {
3101 name: "otherapex.key",
3102 public_key: "testkey.avbpubkey",
3103 private_key: "testkey.pem",
3104 }
3105
3106 cc_library {
3107 name: "libfoo",
3108 stl: "none",
3109 shared_libs: ["libbar"],
3110 system_shared_libs: [],
3111 apex_available: ["myapex", "otherapex"],
3112 }
3113
3114 cc_library {
3115 name: "libbar",
3116 stl: "none",
3117 system_shared_libs: [],
3118 apex_available: ["otherapex"],
3119 }`)
3120
3121 testApexError(t, "\"otherapex\" is not a valid module name", `
3122 apex {
3123 name: "myapex",
3124 key: "myapex.key",
3125 native_shared_libs: ["libfoo"],
3126 }
3127
3128 apex_key {
3129 name: "myapex.key",
3130 public_key: "testkey.avbpubkey",
3131 private_key: "testkey.pem",
3132 }
3133
3134 cc_library {
3135 name: "libfoo",
3136 stl: "none",
3137 system_shared_libs: [],
3138 apex_available: ["otherapex"],
3139 }`)
3140
3141 ctx, _ := testApex(t, `
3142 apex {
3143 name: "myapex",
3144 key: "myapex.key",
3145 native_shared_libs: ["libfoo", "libbar"],
3146 }
3147
3148 apex_key {
3149 name: "myapex.key",
3150 public_key: "testkey.avbpubkey",
3151 private_key: "testkey.pem",
3152 }
3153
3154 cc_library {
3155 name: "libfoo",
3156 stl: "none",
3157 system_shared_libs: [],
3158 apex_available: ["myapex"],
3159 }
3160
3161 cc_library {
3162 name: "libbar",
3163 stl: "none",
3164 system_shared_libs: [],
3165 apex_available: ["//apex_available:anyapex"],
3166 }`)
3167
3168 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003169 // TODO(jiyong) the checks for the platform variant are removed because we now create
3170 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3171 // the platform variants are not used from other platform modules. When that is done,
3172 // these checks will be replaced by expecting a specific error message that will be
3173 // emitted when the platform variant is used.
3174 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3175 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3176 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3177 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003178
3179 ctx, _ = testApex(t, `
3180 apex {
3181 name: "myapex",
3182 key: "myapex.key",
3183 }
3184
3185 apex_key {
3186 name: "myapex.key",
3187 public_key: "testkey.avbpubkey",
3188 private_key: "testkey.pem",
3189 }
3190
3191 cc_library {
3192 name: "libfoo",
3193 stl: "none",
3194 system_shared_libs: [],
3195 apex_available: ["//apex_available:platform"],
3196 }`)
3197
3198 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003199 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3200 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003201
3202 ctx, _ = testApex(t, `
3203 apex {
3204 name: "myapex",
3205 key: "myapex.key",
3206 native_shared_libs: ["libfoo"],
3207 }
3208
3209 apex_key {
3210 name: "myapex.key",
3211 public_key: "testkey.avbpubkey",
3212 private_key: "testkey.pem",
3213 }
3214
3215 cc_library {
3216 name: "libfoo",
3217 stl: "none",
3218 system_shared_libs: [],
3219 apex_available: ["myapex"],
3220 static: {
3221 apex_available: ["//apex_available:platform"],
3222 },
3223 }`)
3224
3225 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003226 // TODO(jiyong) the checks for the platform variant are removed because we now create
3227 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3228 // the platform variants are not used from other platform modules. When that is done,
3229 // these checks will be replaced by expecting a specific error message that will be
3230 // emitted when the platform variant is used.
3231 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3232 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3233 // // but the static variant is available to both myapex and the platform
3234 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3235 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003236}
3237
Jiyong Park5d790c32019-11-15 18:40:32 +09003238func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003239 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003240 apex {
3241 name: "myapex",
3242 key: "myapex.key",
3243 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003244 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003245 }
3246
3247 override_apex {
3248 name: "override_myapex",
3249 base: "myapex",
3250 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003251 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003252 logging_parent: "com.foo.bar",
Jiyong Park5d790c32019-11-15 18:40:32 +09003253 }
3254
3255 apex_key {
3256 name: "myapex.key",
3257 public_key: "testkey.avbpubkey",
3258 private_key: "testkey.pem",
3259 }
3260
3261 android_app {
3262 name: "app",
3263 srcs: ["foo/bar/MyClass.java"],
3264 package_name: "foo",
3265 sdk_version: "none",
3266 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003267 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003268 }
3269
3270 override_android_app {
3271 name: "override_app",
3272 base: "app",
3273 package_name: "bar",
3274 }
3275 `)
3276
Jiyong Park317645e2019-12-05 13:20:58 +09003277 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3278 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3279 if originalVariant.GetOverriddenBy() != "" {
3280 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3281 }
3282 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3283 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3284 }
3285
Jiyong Park5d790c32019-11-15 18:40:32 +09003286 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3287 apexRule := module.Rule("apexRule")
3288 copyCmds := apexRule.Args["copy_commands"]
3289
3290 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3291 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003292
3293 apexBundle := module.Module().(*apexBundle)
3294 name := apexBundle.Name()
3295 if name != "override_myapex" {
3296 t.Errorf("name should be \"override_myapex\", but was %q", name)
3297 }
3298
Baligh Uddin004d7172020-02-19 21:29:28 -08003299 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3300 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3301 }
3302
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003303 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3304 var builder strings.Builder
3305 data.Custom(&builder, name, "TARGET_", "", data)
3306 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003307 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003308 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3309 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003310 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003311 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003312 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003313 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3314 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003315}
3316
Jooyung Han214bf372019-11-12 13:03:50 +09003317func TestLegacyAndroid10Support(t *testing.T) {
3318 ctx, _ := testApex(t, `
3319 apex {
3320 name: "myapex",
3321 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003322 native_shared_libs: ["mylib"],
Jooyung Han214bf372019-11-12 13:03:50 +09003323 legacy_android10_support: true,
3324 }
3325
3326 apex_key {
3327 name: "myapex.key",
3328 public_key: "testkey.avbpubkey",
3329 private_key: "testkey.pem",
3330 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003331
3332 cc_library {
3333 name: "mylib",
3334 srcs: ["mylib.cpp"],
3335 stl: "libc++",
3336 system_shared_libs: [],
3337 apex_available: [ "myapex" ],
3338 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003339 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003340
3341 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3342 args := module.Rule("apexRule").Args
3343 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003344 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003345
3346 // The copies of the libraries in the apex should have one more dependency than
3347 // the ones outside the apex, namely the unwinder. Ideally we should check
3348 // the dependency names directly here but for some reason the names are blank in
3349 // this test.
3350 for _, lib := range []string{"libc++", "mylib"} {
3351 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3352 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3353 if len(apexImplicits) != len(nonApexImplicits)+1 {
3354 t.Errorf("%q missing unwinder dep", lib)
3355 }
3356 }
Jooyung Han214bf372019-11-12 13:03:50 +09003357}
3358
Jooyung Han58f26ab2019-12-18 15:34:32 +09003359func TestJavaSDKLibrary(t *testing.T) {
3360 ctx, _ := testApex(t, `
3361 apex {
3362 name: "myapex",
3363 key: "myapex.key",
3364 java_libs: ["foo"],
3365 }
3366
3367 apex_key {
3368 name: "myapex.key",
3369 public_key: "testkey.avbpubkey",
3370 private_key: "testkey.pem",
3371 }
3372
3373 java_sdk_library {
3374 name: "foo",
3375 srcs: ["a.java"],
3376 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003377 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003378 }
3379 `, withFiles(map[string][]byte{
3380 "api/current.txt": nil,
3381 "api/removed.txt": nil,
3382 "api/system-current.txt": nil,
3383 "api/system-removed.txt": nil,
3384 "api/test-current.txt": nil,
3385 "api/test-removed.txt": nil,
3386 }))
3387
3388 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003389 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003390 "javalib/foo.jar",
3391 "etc/permissions/foo.xml",
3392 })
3393 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003394 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3395 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003396}
3397
atrost6e126252020-01-27 17:01:16 +00003398func TestCompatConfig(t *testing.T) {
3399 ctx, _ := testApex(t, `
3400 apex {
3401 name: "myapex",
3402 key: "myapex.key",
3403 prebuilts: ["myjar-platform-compat-config"],
3404 java_libs: ["myjar"],
3405 }
3406
3407 apex_key {
3408 name: "myapex.key",
3409 public_key: "testkey.avbpubkey",
3410 private_key: "testkey.pem",
3411 }
3412
3413 platform_compat_config {
3414 name: "myjar-platform-compat-config",
3415 src: ":myjar",
3416 }
3417
3418 java_library {
3419 name: "myjar",
3420 srcs: ["foo/bar/MyClass.java"],
3421 sdk_version: "none",
3422 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003423 apex_available: [ "myapex" ],
3424 }
3425 `)
3426 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3427 "etc/compatconfig/myjar-platform-compat-config.xml",
3428 "javalib/myjar.jar",
3429 })
3430}
3431
Jiyong Park479321d2019-12-16 11:47:12 +09003432func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3433 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3434 apex {
3435 name: "myapex",
3436 key: "myapex.key",
3437 java_libs: ["myjar"],
3438 }
3439
3440 apex_key {
3441 name: "myapex.key",
3442 public_key: "testkey.avbpubkey",
3443 private_key: "testkey.pem",
3444 }
3445
3446 java_library {
3447 name: "myjar",
3448 srcs: ["foo/bar/MyClass.java"],
3449 sdk_version: "none",
3450 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003451 compile_dex: false,
Jiyong Park479321d2019-12-16 11:47:12 +09003452 }
3453 `)
3454}
3455
Jiyong Park7afd1072019-12-30 16:56:33 +09003456func TestCarryRequiredModuleNames(t *testing.T) {
3457 ctx, config := testApex(t, `
3458 apex {
3459 name: "myapex",
3460 key: "myapex.key",
3461 native_shared_libs: ["mylib"],
3462 }
3463
3464 apex_key {
3465 name: "myapex.key",
3466 public_key: "testkey.avbpubkey",
3467 private_key: "testkey.pem",
3468 }
3469
3470 cc_library {
3471 name: "mylib",
3472 srcs: ["mylib.cpp"],
3473 system_shared_libs: [],
3474 stl: "none",
3475 required: ["a", "b"],
3476 host_required: ["c", "d"],
3477 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003478 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003479 }
3480 `)
3481
3482 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3483 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3484 name := apexBundle.BaseModuleName()
3485 prefix := "TARGET_"
3486 var builder strings.Builder
3487 data.Custom(&builder, name, prefix, "", data)
3488 androidMk := builder.String()
3489 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3490 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3491 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3492}
3493
Jiyong Park7cd10e32020-01-14 09:22:18 +09003494func TestSymlinksFromApexToSystem(t *testing.T) {
3495 bp := `
3496 apex {
3497 name: "myapex",
3498 key: "myapex.key",
3499 native_shared_libs: ["mylib"],
3500 java_libs: ["myjar"],
3501 }
3502
Jiyong Park9d677202020-02-19 16:29:35 +09003503 apex {
3504 name: "myapex.updatable",
3505 key: "myapex.key",
3506 native_shared_libs: ["mylib"],
3507 java_libs: ["myjar"],
3508 updatable: true,
3509 }
3510
Jiyong Park7cd10e32020-01-14 09:22:18 +09003511 apex_key {
3512 name: "myapex.key",
3513 public_key: "testkey.avbpubkey",
3514 private_key: "testkey.pem",
3515 }
3516
3517 cc_library {
3518 name: "mylib",
3519 srcs: ["mylib.cpp"],
3520 shared_libs: ["myotherlib"],
3521 system_shared_libs: [],
3522 stl: "none",
3523 apex_available: [
3524 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003525 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003526 "//apex_available:platform",
3527 ],
3528 }
3529
3530 cc_library {
3531 name: "myotherlib",
3532 srcs: ["mylib.cpp"],
3533 system_shared_libs: [],
3534 stl: "none",
3535 apex_available: [
3536 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003537 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003538 "//apex_available:platform",
3539 ],
3540 }
3541
3542 java_library {
3543 name: "myjar",
3544 srcs: ["foo/bar/MyClass.java"],
3545 sdk_version: "none",
3546 system_modules: "none",
3547 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003548 apex_available: [
3549 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003550 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003551 "//apex_available:platform",
3552 ],
3553 }
3554
3555 java_library {
3556 name: "myotherjar",
3557 srcs: ["foo/bar/MyClass.java"],
3558 sdk_version: "none",
3559 system_modules: "none",
3560 apex_available: [
3561 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003562 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003563 "//apex_available:platform",
3564 ],
3565 }
3566 `
3567
3568 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3569 for _, f := range files {
3570 if f.path == file {
3571 if f.isLink {
3572 t.Errorf("%q is not a real file", file)
3573 }
3574 return
3575 }
3576 }
3577 t.Errorf("%q is not found", file)
3578 }
3579
3580 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3581 for _, f := range files {
3582 if f.path == file {
3583 if !f.isLink {
3584 t.Errorf("%q is not a symlink", file)
3585 }
3586 return
3587 }
3588 }
3589 t.Errorf("%q is not found", file)
3590 }
3591
Jiyong Park9d677202020-02-19 16:29:35 +09003592 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3593 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003594 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003595 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003596 ensureRealfileExists(t, files, "javalib/myjar.jar")
3597 ensureRealfileExists(t, files, "lib64/mylib.so")
3598 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3599
Jiyong Park9d677202020-02-19 16:29:35 +09003600 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3601 ensureRealfileExists(t, files, "javalib/myjar.jar")
3602 ensureRealfileExists(t, files, "lib64/mylib.so")
3603 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3604
3605 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003606 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003607 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003608 ensureRealfileExists(t, files, "javalib/myjar.jar")
3609 ensureRealfileExists(t, files, "lib64/mylib.so")
3610 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09003611
3612 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3613 ensureRealfileExists(t, files, "javalib/myjar.jar")
3614 ensureRealfileExists(t, files, "lib64/mylib.so")
3615 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09003616}
3617
Jiyong Parkd93e1b12020-02-28 15:22:21 +09003618func TestAppBundle(t *testing.T) {
3619 ctx, _ := testApex(t, `
3620 apex {
3621 name: "myapex",
3622 key: "myapex.key",
3623 apps: ["AppFoo"],
3624 }
3625
3626 apex_key {
3627 name: "myapex.key",
3628 public_key: "testkey.avbpubkey",
3629 private_key: "testkey.pem",
3630 }
3631
3632 android_app {
3633 name: "AppFoo",
3634 srcs: ["foo/bar/MyClass.java"],
3635 sdk_version: "none",
3636 system_modules: "none",
3637 apex_available: [ "myapex" ],
3638 }
Jiyong Parkaf8998c2020-02-28 16:51:07 +09003639 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkd93e1b12020-02-28 15:22:21 +09003640
3641 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
3642 content := bundleConfigRule.Args["content"]
3643
3644 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkaf8998c2020-02-28 16:51:07 +09003645 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 +09003646}
3647
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003648func TestMain(m *testing.M) {
3649 run := func() int {
3650 setUp()
3651 defer tearDown()
3652
3653 return m.Run()
3654 }
3655
3656 os.Exit(run())
3657}