blob: 1f395b6c1055d9449b12409b94b23d5c168344c8 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
95func withNativeBridgeEnabled(fs map[string][]byte, config android.Config) {
96 config.Targets[android.Android] = []android.Target{
97 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
98 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
99 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
100 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
101 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
102 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
103 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
104 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
105 }
106}
107
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900108func withManifestPackageNameOverrides(specs []string) testCustomizer {
109 return func(fs map[string][]byte, config android.Config) {
110 config.TestProductVariables.ManifestPackageNameOverrides = specs
111 }
112}
113
Jooyung Han31c470b2019-10-18 16:26:59 +0900114func withBinder32bit(fs map[string][]byte, config android.Config) {
115 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
116}
117
Jiyong Park7cd10e32020-01-14 09:22:18 +0900118func withUnbundledBuild(fs map[string][]byte, config android.Config) {
119 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
120}
121
Jooyung Han344d5432019-08-23 11:17:39 +0900122func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +0900123 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900124
125 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900126 filegroup {
127 name: "myapex-file_contexts",
128 srcs: [
129 "system/sepolicy/apex/myapex-file_contexts",
130 ],
131 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900132 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800133
Colin Crossf9aabd72020-02-15 11:29:50 -0800134 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
135
Dario Frenicde2a032019-10-27 00:29:22 +0100136 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900137
Jooyung Han344d5432019-08-23 11:17:39 +0900138 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900139 "a.java": nil,
140 "PrebuiltAppFoo.apk": nil,
141 "PrebuiltAppFooPriv.apk": nil,
142 "build/make/target/product/security": nil,
143 "apex_manifest.json": nil,
144 "AndroidManifest.xml": nil,
145 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900146 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900147 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900148 "system/sepolicy/apex/otherapex-file_contexts": nil,
149 "system/sepolicy/apex/commonapex-file_contexts": nil,
150 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800151 "mylib.cpp": nil,
152 "mylib_common.cpp": nil,
153 "mytest.cpp": nil,
154 "mytest1.cpp": nil,
155 "mytest2.cpp": nil,
156 "mytest3.cpp": nil,
157 "myprebuilt": nil,
158 "my_include": nil,
159 "foo/bar/MyClass.java": nil,
160 "prebuilt.jar": nil,
161 "vendor/foo/devkeys/test.x509.pem": nil,
162 "vendor/foo/devkeys/test.pk8": nil,
163 "testkey.x509.pem": nil,
164 "testkey.pk8": nil,
165 "testkey.override.x509.pem": nil,
166 "testkey.override.pk8": nil,
167 "vendor/foo/devkeys/testkey.avbpubkey": nil,
168 "vendor/foo/devkeys/testkey.pem": nil,
169 "NOTICE": nil,
170 "custom_notice": nil,
171 "testkey2.avbpubkey": nil,
172 "testkey2.pem": nil,
173 "myapex-arm64.apex": nil,
174 "myapex-arm.apex": nil,
175 "frameworks/base/api/current.txt": nil,
176 "framework/aidl/a.aidl": nil,
177 "build/make/core/proguard.flags": nil,
178 "build/make/core/proguard_basic_keeps.flags": nil,
179 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900180 }
181
Colin Crossf9aabd72020-02-15 11:29:50 -0800182 cc.GatherRequiredFilesForTest(fs)
183
Jooyung Han344d5432019-08-23 11:17:39 +0900184 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800185 // The fs now needs to be populated before creating the config, call handlers twice
186 // for now, once to get any fs changes, and later after the config was created to
187 // set product variables or targets.
188 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
189 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900190 }
191
Colin Cross98be1bb2019-12-13 20:41:13 -0800192 config := android.TestArchConfig(buildDir, nil, bp, fs)
193 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
194 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
195 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
196 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
197 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
198 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
199
200 for _, handler := range handlers {
201 // The fs now needs to be populated before creating the config, call handlers twice
202 // for now, earlier to get any fs changes, and now after the config was created to
203 // set product variables or targets.
204 tempFS := map[string][]byte{}
205 handler(tempFS, config)
206 }
207
208 ctx := android.NewTestArchContext()
209 ctx.RegisterModuleType("apex", BundleFactory)
210 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
211 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
212 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
213 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
214 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
215 ctx.RegisterModuleType("override_apex", overrideApexFactory)
216
Jooyung Hana57af4a2020-01-23 05:36:59 +0000217 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
218 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
219
Paul Duffin77980a82019-12-19 16:01:36 +0000220 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800221 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800222 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
223 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800224 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000225 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800226 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800227 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000228 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000229 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000230 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900231 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800232
Colin Cross98be1bb2019-12-13 20:41:13 -0800233 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800234 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800235
236 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900237
Jooyung Han5c998b92019-06-27 11:30:33 +0900238 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900239}
240
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700241func setUp() {
242 var err error
243 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700245 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900246 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247}
248
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700249func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900250 os.RemoveAll(buildDir)
251}
252
Jooyung Han643adc42020-02-27 13:50:06 +0900253// ensure that 'result' equals 'expected'
254func ensureEquals(t *testing.T, result string, expected string) {
255 t.Helper()
256 if result != expected {
257 t.Errorf("%q != %q", expected, result)
258 }
259}
260
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261// ensure that 'result' contains 'expected'
262func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900263 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900264 if !strings.Contains(result, expected) {
265 t.Errorf("%q is not found in %q", expected, result)
266 }
267}
268
269// ensures that 'result' does not contain 'notExpected'
270func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900271 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900272 if strings.Contains(result, notExpected) {
273 t.Errorf("%q is found in %q", notExpected, result)
274 }
275}
276
277func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900278 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900279 if !android.InList(expected, result) {
280 t.Errorf("%q is not found in %v", expected, result)
281 }
282}
283
284func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900285 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900286 if android.InList(notExpected, result) {
287 t.Errorf("%q is found in %v", notExpected, result)
288 }
289}
290
Jooyung Hane1633032019-08-01 17:41:43 +0900291func ensureListEmpty(t *testing.T, result []string) {
292 t.Helper()
293 if len(result) > 0 {
294 t.Errorf("%q is expected to be empty", result)
295 }
296}
297
Jiyong Park25fc6a92018-11-18 18:02:45 +0900298// Minimal test
299func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700300 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900301 apex_defaults {
302 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900303 manifest: ":myapex.manifest",
304 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900305 key: "myapex.key",
306 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800307 multilib: {
308 both: {
309 binaries: ["foo",],
310 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900311 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900312 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313 }
314
Jiyong Park30ca9372019-02-07 16:27:23 +0900315 apex {
316 name: "myapex",
317 defaults: ["myapex-defaults"],
318 }
319
Jiyong Park25fc6a92018-11-18 18:02:45 +0900320 apex_key {
321 name: "myapex.key",
322 public_key: "testkey.avbpubkey",
323 private_key: "testkey.pem",
324 }
325
Jiyong Park809bb722019-02-13 21:33:49 +0900326 filegroup {
327 name: "myapex.manifest",
328 srcs: ["apex_manifest.json"],
329 }
330
331 filegroup {
332 name: "myapex.androidmanifest",
333 srcs: ["AndroidManifest.xml"],
334 }
335
Jiyong Park25fc6a92018-11-18 18:02:45 +0900336 cc_library {
337 name: "mylib",
338 srcs: ["mylib.cpp"],
339 shared_libs: ["mylib2"],
340 system_shared_libs: [],
341 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000342 // TODO: remove //apex_available:platform
343 apex_available: [
344 "//apex_available:platform",
345 "myapex",
346 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900347 }
348
Alex Light3d673592019-01-18 14:37:31 -0800349 cc_binary {
350 name: "foo",
351 srcs: ["mylib.cpp"],
352 compile_multilib: "both",
353 multilib: {
354 lib32: {
355 suffix: "32",
356 },
357 lib64: {
358 suffix: "64",
359 },
360 },
361 symlinks: ["foo_link_"],
362 symlink_preferred_arch: true,
363 system_shared_libs: [],
364 static_executable: true,
365 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000366 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800367 }
368
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 cc_library {
370 name: "mylib2",
371 srcs: ["mylib.cpp"],
372 system_shared_libs: [],
373 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900374 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000375 // TODO: remove //apex_available:platform
376 apex_available: [
377 "//apex_available:platform",
378 "myapex",
379 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900380 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900381
382 java_library {
383 name: "myjar",
384 srcs: ["foo/bar/MyClass.java"],
385 sdk_version: "none",
386 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900387 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900388 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000389 // TODO: remove //apex_available:platform
390 apex_available: [
391 "//apex_available:platform",
392 "myapex",
393 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900394 }
395
396 java_library {
397 name: "myotherjar",
398 srcs: ["foo/bar/MyClass.java"],
399 sdk_version: "none",
400 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900401 // TODO: remove //apex_available:platform
402 apex_available: [
403 "//apex_available:platform",
404 "myapex",
405 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900406 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900407
408 java_library {
409 name: "mysharedjar",
410 srcs: ["foo/bar/MyClass.java"],
411 sdk_version: "none",
412 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900413 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900414 `)
415
Sundong Ahnabb64432019-10-22 13:58:29 +0900416 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900417
418 optFlags := apexRule.Args["opt_flags"]
419 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700420 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900421 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900422
Jiyong Park25fc6a92018-11-18 18:02:45 +0900423 copyCmds := apexRule.Args["copy_commands"]
424
425 // Ensure that main rule creates an output
426 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
427
428 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800429 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900430 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900431
432 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800433 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900434 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900435
436 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800437 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
438 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900439 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
440 // .. but not for java libs
441 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900442 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800443
Colin Cross7113d202019-11-20 16:39:12 -0800444 // Ensure that the platform variant ends with _shared or _common
445 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
446 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900447 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
448 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900449 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
450
451 // Ensure that dynamic dependency to java libs are not included
452 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800453
454 // Ensure that all symlinks are present.
455 found_foo_link_64 := false
456 found_foo := false
457 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900458 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800459 if strings.HasSuffix(cmd, "bin/foo") {
460 found_foo = true
461 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
462 found_foo_link_64 = true
463 }
464 }
465 }
466 good := found_foo && found_foo_link_64
467 if !good {
468 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
469 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900470
Sundong Ahnabb64432019-10-22 13:58:29 +0900471 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700472 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700473 if len(noticeInputs) != 2 {
474 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900475 }
476 ensureListContains(t, noticeInputs, "NOTICE")
477 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900478
479 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 +0900480 ensureListContains(t, depsInfo, "myjar <- myapex")
481 ensureListContains(t, depsInfo, "mylib <- myapex")
482 ensureListContains(t, depsInfo, "mylib2 <- mylib")
483 ensureListContains(t, depsInfo, "myotherjar <- myjar")
484 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800485}
486
Jooyung Hanf21c7972019-12-16 22:32:06 +0900487func TestDefaults(t *testing.T) {
488 ctx, _ := testApex(t, `
489 apex_defaults {
490 name: "myapex-defaults",
491 key: "myapex.key",
492 prebuilts: ["myetc"],
493 native_shared_libs: ["mylib"],
494 java_libs: ["myjar"],
495 apps: ["AppFoo"],
496 }
497
498 prebuilt_etc {
499 name: "myetc",
500 src: "myprebuilt",
501 }
502
503 apex {
504 name: "myapex",
505 defaults: ["myapex-defaults"],
506 }
507
508 apex_key {
509 name: "myapex.key",
510 public_key: "testkey.avbpubkey",
511 private_key: "testkey.pem",
512 }
513
514 cc_library {
515 name: "mylib",
516 system_shared_libs: [],
517 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000518 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900519 }
520
521 java_library {
522 name: "myjar",
523 srcs: ["foo/bar/MyClass.java"],
524 sdk_version: "none",
525 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000526 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900527 }
528
529 android_app {
530 name: "AppFoo",
531 srcs: ["foo/bar/MyClass.java"],
532 sdk_version: "none",
533 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000534 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900535 }
536 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000537 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900538 "etc/myetc",
539 "javalib/myjar.jar",
540 "lib64/mylib.so",
541 "app/AppFoo/AppFoo.apk",
542 })
543}
544
Jooyung Han01a3ee22019-11-02 02:52:25 +0900545func TestApexManifest(t *testing.T) {
546 ctx, _ := testApex(t, `
547 apex {
548 name: "myapex",
549 key: "myapex.key",
550 }
551
552 apex_key {
553 name: "myapex.key",
554 public_key: "testkey.avbpubkey",
555 private_key: "testkey.pem",
556 }
557 `)
558
559 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900560 args := module.Rule("apexRule").Args
561 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
562 t.Error("manifest should be apex_manifest.pb, but " + manifest)
563 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900564}
565
Alex Light5098a612018-11-29 17:12:15 -0800566func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700567 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800568 apex {
569 name: "myapex",
570 key: "myapex.key",
571 payload_type: "zip",
572 native_shared_libs: ["mylib"],
573 }
574
575 apex_key {
576 name: "myapex.key",
577 public_key: "testkey.avbpubkey",
578 private_key: "testkey.pem",
579 }
580
581 cc_library {
582 name: "mylib",
583 srcs: ["mylib.cpp"],
584 shared_libs: ["mylib2"],
585 system_shared_libs: [],
586 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000587 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800588 }
589
590 cc_library {
591 name: "mylib2",
592 srcs: ["mylib.cpp"],
593 system_shared_libs: [],
594 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000595 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800596 }
597 `)
598
Sundong Ahnabb64432019-10-22 13:58:29 +0900599 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800600 copyCmds := zipApexRule.Args["copy_commands"]
601
602 // Ensure that main rule creates an output
603 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
604
605 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800606 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800607
608 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800609 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800610
611 // Ensure that both direct and indirect deps are copied into apex
612 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
613 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900614}
615
616func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700617 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900618 apex {
619 name: "myapex",
620 key: "myapex.key",
621 native_shared_libs: ["mylib", "mylib3"],
622 }
623
624 apex_key {
625 name: "myapex.key",
626 public_key: "testkey.avbpubkey",
627 private_key: "testkey.pem",
628 }
629
630 cc_library {
631 name: "mylib",
632 srcs: ["mylib.cpp"],
633 shared_libs: ["mylib2", "mylib3"],
634 system_shared_libs: [],
635 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000636 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900637 }
638
639 cc_library {
640 name: "mylib2",
641 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900642 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900643 system_shared_libs: [],
644 stl: "none",
645 stubs: {
646 versions: ["1", "2", "3"],
647 },
648 }
649
650 cc_library {
651 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900652 srcs: ["mylib.cpp"],
653 shared_libs: ["mylib4"],
654 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900655 stl: "none",
656 stubs: {
657 versions: ["10", "11", "12"],
658 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000659 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900660 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900661
662 cc_library {
663 name: "mylib4",
664 srcs: ["mylib.cpp"],
665 system_shared_libs: [],
666 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000667 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900668 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669 `)
670
Sundong Ahnabb64432019-10-22 13:58:29 +0900671 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900672 copyCmds := apexRule.Args["copy_commands"]
673
674 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800675 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900676
677 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800678 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900679
680 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800681 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900682
Colin Cross7113d202019-11-20 16:39:12 -0800683 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900684
685 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900686 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900687 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900688 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900689
690 // 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 -0800691 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900692 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800693 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900694
695 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900696 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900697 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900698
699 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900700 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900701
Jooyung Hana57af4a2020-01-23 05:36:59 +0000702 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900703 "lib64/mylib.so",
704 "lib64/mylib3.so",
705 "lib64/mylib4.so",
706 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900707}
708
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900709func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700710 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900711 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900712 name: "myapex2",
713 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900714 native_shared_libs: ["mylib"],
715 }
716
717 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900718 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900719 public_key: "testkey.avbpubkey",
720 private_key: "testkey.pem",
721 }
722
723 cc_library {
724 name: "mylib",
725 srcs: ["mylib.cpp"],
726 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900727 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900728 system_shared_libs: [],
729 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000730 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900731 }
732
733 cc_library {
734 name: "libfoo",
735 srcs: ["mylib.cpp"],
736 shared_libs: ["libbar"],
737 system_shared_libs: [],
738 stl: "none",
739 stubs: {
740 versions: ["10", "20", "30"],
741 },
742 }
743
744 cc_library {
745 name: "libbar",
746 srcs: ["mylib.cpp"],
747 system_shared_libs: [],
748 stl: "none",
749 }
750
Jiyong Park678c8812020-02-07 17:25:49 +0900751 cc_library_static {
752 name: "libbaz",
753 srcs: ["mylib.cpp"],
754 system_shared_libs: [],
755 stl: "none",
756 apex_available: [ "myapex2" ],
757 }
758
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900759 `)
760
Jiyong Park83dc74b2020-01-14 18:38:44 +0900761 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900762 copyCmds := apexRule.Args["copy_commands"]
763
764 // Ensure that direct non-stubs dep is always included
765 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
766
767 // Ensure that indirect stubs dep is not included
768 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
769
770 // Ensure that dependency of stubs is not included
771 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
772
Jiyong Park83dc74b2020-01-14 18:38:44 +0900773 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900774
775 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900776 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900777 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900778 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900779
Jiyong Park3ff16992019-12-27 14:11:47 +0900780 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900781
782 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
783 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900784
785 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 +0900786
787 ensureListContains(t, depsInfo, "mylib <- myapex2")
788 ensureListContains(t, depsInfo, "libbaz <- mylib")
789 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900790}
791
Jooyung Hand3639552019-08-09 12:57:43 +0900792func TestApexWithRuntimeLibsDependency(t *testing.T) {
793 /*
794 myapex
795 |
796 v (runtime_libs)
797 mylib ------+------> libfoo [provides stub]
798 |
799 `------> libbar
800 */
801 ctx, _ := testApex(t, `
802 apex {
803 name: "myapex",
804 key: "myapex.key",
805 native_shared_libs: ["mylib"],
806 }
807
808 apex_key {
809 name: "myapex.key",
810 public_key: "testkey.avbpubkey",
811 private_key: "testkey.pem",
812 }
813
814 cc_library {
815 name: "mylib",
816 srcs: ["mylib.cpp"],
817 runtime_libs: ["libfoo", "libbar"],
818 system_shared_libs: [],
819 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000820 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900821 }
822
823 cc_library {
824 name: "libfoo",
825 srcs: ["mylib.cpp"],
826 system_shared_libs: [],
827 stl: "none",
828 stubs: {
829 versions: ["10", "20", "30"],
830 },
831 }
832
833 cc_library {
834 name: "libbar",
835 srcs: ["mylib.cpp"],
836 system_shared_libs: [],
837 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000838 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900839 }
840
841 `)
842
Sundong Ahnabb64432019-10-22 13:58:29 +0900843 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900844 copyCmds := apexRule.Args["copy_commands"]
845
846 // Ensure that direct non-stubs dep is always included
847 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
848
849 // Ensure that indirect stubs dep is not included
850 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
851
852 // Ensure that runtime_libs dep in included
853 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
854
Sundong Ahnabb64432019-10-22 13:58:29 +0900855 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900856 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
857 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900858
859}
860
Jooyung Han9c80bae2019-08-20 17:30:57 +0900861func TestApexDependencyToLLNDK(t *testing.T) {
862 ctx, _ := testApex(t, `
863 apex {
864 name: "myapex",
865 key: "myapex.key",
866 use_vendor: true,
867 native_shared_libs: ["mylib"],
868 }
869
870 apex_key {
871 name: "myapex.key",
872 public_key: "testkey.avbpubkey",
873 private_key: "testkey.pem",
874 }
875
876 cc_library {
877 name: "mylib",
878 srcs: ["mylib.cpp"],
879 vendor_available: true,
880 shared_libs: ["libbar"],
881 system_shared_libs: [],
882 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000883 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900884 }
885
886 cc_library {
887 name: "libbar",
888 srcs: ["mylib.cpp"],
889 system_shared_libs: [],
890 stl: "none",
891 }
892
893 llndk_library {
894 name: "libbar",
895 symbol_file: "",
896 }
Jooyung Handc782442019-11-01 03:14:38 +0900897 `, func(fs map[string][]byte, config android.Config) {
898 setUseVendorWhitelistForTest(config, []string{"myapex"})
899 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900900
Sundong Ahnabb64432019-10-22 13:58:29 +0900901 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900902 copyCmds := apexRule.Args["copy_commands"]
903
904 // Ensure that LLNDK dep is not included
905 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
906
Sundong Ahnabb64432019-10-22 13:58:29 +0900907 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900908 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900909
910 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900911 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900912
913}
914
Jiyong Park25fc6a92018-11-18 18:02:45 +0900915func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700916 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900917 apex {
918 name: "myapex",
919 key: "myapex.key",
920 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
921 }
922
923 apex_key {
924 name: "myapex.key",
925 public_key: "testkey.avbpubkey",
926 private_key: "testkey.pem",
927 }
928
929 cc_library {
930 name: "mylib",
931 srcs: ["mylib.cpp"],
932 shared_libs: ["libdl#27"],
933 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000934 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900935 }
936
937 cc_library_shared {
938 name: "mylib_shared",
939 srcs: ["mylib.cpp"],
940 shared_libs: ["libdl#27"],
941 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000942 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900943 }
944
945 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900946 name: "libBootstrap",
947 srcs: ["mylib.cpp"],
948 stl: "none",
949 bootstrap: true,
950 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900951 `)
952
Sundong Ahnabb64432019-10-22 13:58:29 +0900953 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900954 copyCmds := apexRule.Args["copy_commands"]
955
956 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800957 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900958 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
959 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900960
961 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900962 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900963
Colin Cross7113d202019-11-20 16:39:12 -0800964 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
965 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
966 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967
968 // For dependency to libc
969 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900970 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900971 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900972 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900973 // ... Cflags from stub is correctly exported to mylib
974 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
975 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
976
977 // For dependency to libm
978 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800979 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900981 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900982 // ... and is not compiling with the stub
983 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
984 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
985
986 // For dependency to libdl
987 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900988 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900989 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900990 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
991 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800993 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900994 // ... Cflags from stub is correctly exported to mylib
995 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
996 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900997
998 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800999 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1000 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1001 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1002 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001003}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001004
Jooyung Han03b51852020-02-26 22:45:42 +09001005func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
1006 // there are three links between liba --> libz
1007 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
1008 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
1009 // 3) (platform) -> liba -> libz : this should be non-stub link
1010 ctx, _ := testApex(t, `
1011 apex {
1012 name: "myapex",
1013 key: "myapex.key",
1014 native_shared_libs: ["libx"],
1015 min_sdk_version: "2",
1016 }
1017
1018 apex {
1019 name: "otherapex",
1020 key: "myapex.key",
1021 native_shared_libs: ["liby"],
1022 min_sdk_version: "3",
1023 }
1024
1025 apex_key {
1026 name: "myapex.key",
1027 public_key: "testkey.avbpubkey",
1028 private_key: "testkey.pem",
1029 }
1030
1031 cc_library {
1032 name: "libx",
1033 shared_libs: ["liba"],
1034 system_shared_libs: [],
1035 stl: "none",
1036 apex_available: [ "myapex" ],
1037 }
1038
1039 cc_library {
1040 name: "liby",
1041 shared_libs: ["liba"],
1042 system_shared_libs: [],
1043 stl: "none",
1044 apex_available: [ "otherapex" ],
1045 }
1046
1047 cc_library {
1048 name: "liba",
1049 shared_libs: ["libz"],
1050 system_shared_libs: [],
1051 stl: "none",
1052 apex_available: [
1053 "//apex_available:anyapex",
1054 "//apex_available:platform",
1055 ],
1056 }
1057
1058 cc_library {
1059 name: "libz",
1060 system_shared_libs: [],
1061 stl: "none",
1062 stubs: {
1063 versions: ["1", "3"],
1064 },
1065 }
1066 `, withUnbundledBuild)
1067
1068 expectLink := func(from, from_variant, to, to_variant string) {
1069 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1070 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1071 }
1072 expectNoLink := func(from, from_variant, to, to_variant string) {
1073 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1074 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1075 }
1076 // platform liba is linked to non-stub version
1077 expectLink("liba", "shared", "libz", "shared")
1078 // liba in myapex is linked to #1
1079 expectLink("liba", "shared_myapex", "libz", "shared_1")
1080 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1081 expectNoLink("liba", "shared_myapex", "libz", "shared")
1082 // liba in otherapex is linked to #3
1083 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1084 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1085 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1086}
1087
1088func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1089 ctx, _ := testApex(t, `
1090 apex {
1091 name: "myapex",
1092 key: "myapex.key",
1093 native_shared_libs: ["libx"],
1094 }
1095
1096 apex_key {
1097 name: "myapex.key",
1098 public_key: "testkey.avbpubkey",
1099 private_key: "testkey.pem",
1100 }
1101
1102 cc_library {
1103 name: "libx",
1104 shared_libs: ["libz"],
1105 system_shared_libs: [],
1106 stl: "none",
1107 apex_available: [ "myapex" ],
1108 }
1109
1110 cc_library {
1111 name: "libz",
1112 system_shared_libs: [],
1113 stl: "none",
1114 stubs: {
1115 versions: ["1", "2"],
1116 },
1117 }
1118 `)
1119
1120 expectLink := func(from, from_variant, to, to_variant string) {
1121 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1122 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1123 }
1124 expectNoLink := func(from, from_variant, to, to_variant string) {
1125 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1126 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1127 }
1128 expectLink("libx", "shared_myapex", "libz", "shared_2")
1129 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1130 expectNoLink("libx", "shared_myapex", "libz", "shared")
1131}
1132
1133func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1134 ctx, _ := testApex(t, `
1135 apex {
1136 name: "myapex",
1137 key: "myapex.key",
1138 native_shared_libs: ["libx"],
1139 }
1140
1141 apex_key {
1142 name: "myapex.key",
1143 public_key: "testkey.avbpubkey",
1144 private_key: "testkey.pem",
1145 }
1146
1147 cc_library {
1148 name: "libx",
1149 system_shared_libs: [],
1150 stl: "none",
1151 apex_available: [ "myapex" ],
1152 stubs: {
1153 versions: ["1", "2"],
1154 },
1155 }
1156
1157 cc_library {
1158 name: "libz",
1159 shared_libs: ["libx"],
1160 system_shared_libs: [],
1161 stl: "none",
1162 }
1163 `)
1164
1165 expectLink := func(from, from_variant, to, to_variant string) {
1166 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1167 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1168 }
1169 expectNoLink := func(from, from_variant, to, to_variant string) {
1170 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1171 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1172 }
1173 expectLink("libz", "shared", "libx", "shared_2")
1174 expectNoLink("libz", "shared", "libz", "shared_1")
1175 expectNoLink("libz", "shared", "libz", "shared")
1176}
1177
1178func TestQApexesUseLatestStubsInBundledBuilds(t *testing.T) {
1179 ctx, _ := testApex(t, `
1180 apex {
1181 name: "myapex",
1182 key: "myapex.key",
1183 native_shared_libs: ["libx"],
1184 min_sdk_version: "29",
1185 }
1186
1187 apex_key {
1188 name: "myapex.key",
1189 public_key: "testkey.avbpubkey",
1190 private_key: "testkey.pem",
1191 }
1192
1193 cc_library {
1194 name: "libx",
1195 shared_libs: ["libbar"],
1196 apex_available: [ "myapex" ],
1197 }
1198
1199 cc_library {
1200 name: "libbar",
1201 stubs: {
1202 versions: ["29", "30"],
1203 },
1204 }
1205 `)
1206 expectLink := func(from, from_variant, to, to_variant string) {
1207 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1208 libFlags := ld.Args["libFlags"]
1209 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1210 }
1211 expectLink("libx", "shared_myapex", "libbar", "shared_30")
1212}
1213
1214func TestQTargetApexUseStaticUnwinder(t *testing.T) {
1215 ctx, _ := testApex(t, `
1216 apex {
1217 name: "myapex",
1218 key: "myapex.key",
1219 native_shared_libs: ["libx"],
1220 min_sdk_version: "29",
1221 }
1222
1223 apex_key {
1224 name: "myapex.key",
1225 public_key: "testkey.avbpubkey",
1226 private_key: "testkey.pem",
1227 }
1228
1229 cc_library {
1230 name: "libx",
1231 apex_available: [ "myapex" ],
1232 }
1233
1234 `, withUnbundledBuild)
1235
1236 // ensure apex variant of c++ is linked with static unwinder
1237 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1238 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1239 // note that platform variant is not.
1240 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1241 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1242
1243 libFlags := ctx.ModuleForTests("libx", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1244 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_myapex/libc++.so")
1245 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_29/libc.so") // min_sdk_version applied
1246}
1247
1248func TestInvalidMinSdkVersion(t *testing.T) {
1249 testApexError(t, `"libz" .*: min_sdk_version is set 29.*`, `
1250 apex {
1251 name: "myapex",
1252 key: "myapex.key",
1253 native_shared_libs: ["libx"],
1254 min_sdk_version: "29",
1255 }
1256
1257 apex_key {
1258 name: "myapex.key",
1259 public_key: "testkey.avbpubkey",
1260 private_key: "testkey.pem",
1261 }
1262
1263 cc_library {
1264 name: "libx",
1265 shared_libs: ["libz"],
1266 system_shared_libs: [],
1267 stl: "none",
1268 apex_available: [ "myapex" ],
1269 }
1270
1271 cc_library {
1272 name: "libz",
1273 system_shared_libs: [],
1274 stl: "none",
1275 stubs: {
1276 versions: ["30"],
1277 },
1278 }
1279 `, withUnbundledBuild)
1280
1281 testApexError(t, `"myapex" .*: min_sdk_version: should be .*`, `
1282 apex {
1283 name: "myapex",
1284 key: "myapex.key",
1285 min_sdk_version: "R",
1286 }
1287
1288 apex_key {
1289 name: "myapex.key",
1290 public_key: "testkey.avbpubkey",
1291 private_key: "testkey.pem",
1292 }
1293 `)
1294}
1295
Jiyong Park7c2ee712018-12-07 00:42:25 +09001296func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001297 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001298 apex {
1299 name: "myapex",
1300 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001301 native_shared_libs: ["mylib"],
1302 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001303 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001304 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001305 }
1306
1307 apex_key {
1308 name: "myapex.key",
1309 public_key: "testkey.avbpubkey",
1310 private_key: "testkey.pem",
1311 }
1312
1313 prebuilt_etc {
1314 name: "myetc",
1315 src: "myprebuilt",
1316 sub_dir: "foo/bar",
1317 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001318
1319 cc_library {
1320 name: "mylib",
1321 srcs: ["mylib.cpp"],
1322 relative_install_path: "foo/bar",
1323 system_shared_libs: [],
1324 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001325 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001326 }
1327
1328 cc_binary {
1329 name: "mybin",
1330 srcs: ["mylib.cpp"],
1331 relative_install_path: "foo/bar",
1332 system_shared_libs: [],
1333 static_executable: true,
1334 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001335 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001336 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001337 `)
1338
Sundong Ahnabb64432019-10-22 13:58:29 +09001339 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001340 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1341
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001342 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001343 ensureListContains(t, dirs, "etc")
1344 ensureListContains(t, dirs, "etc/foo")
1345 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001346 ensureListContains(t, dirs, "lib64")
1347 ensureListContains(t, dirs, "lib64/foo")
1348 ensureListContains(t, dirs, "lib64/foo/bar")
1349 ensureListContains(t, dirs, "lib")
1350 ensureListContains(t, dirs, "lib/foo")
1351 ensureListContains(t, dirs, "lib/foo/bar")
1352
Jiyong Parkbd13e442019-03-15 18:10:35 +09001353 ensureListContains(t, dirs, "bin")
1354 ensureListContains(t, dirs, "bin/foo")
1355 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001356}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001357
Jooyung Han35155c42020-02-06 17:33:20 +09001358func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
1359 ctx, _ := testApex(t, `
1360 apex {
1361 name: "myapex",
1362 key: "myapex.key",
1363 multilib: {
1364 both: {
1365 native_shared_libs: ["mylib"],
1366 binaries: ["mybin"],
1367 },
1368 },
1369 compile_multilib: "both",
1370 native_bridge_supported: true,
1371 }
1372
1373 apex_key {
1374 name: "myapex.key",
1375 public_key: "testkey.avbpubkey",
1376 private_key: "testkey.pem",
1377 }
1378
1379 cc_library {
1380 name: "mylib",
1381 relative_install_path: "foo/bar",
1382 system_shared_libs: [],
1383 stl: "none",
1384 apex_available: [ "myapex" ],
1385 native_bridge_supported: true,
1386 }
1387
1388 cc_binary {
1389 name: "mybin",
1390 relative_install_path: "foo/bar",
1391 system_shared_libs: [],
1392 static_executable: true,
1393 stl: "none",
1394 apex_available: [ "myapex" ],
1395 native_bridge_supported: true,
1396 compile_multilib: "both", // default is "first" for binary
1397 multilib: {
1398 lib64: {
1399 suffix: "64",
1400 },
1401 },
1402 }
1403 `, withNativeBridgeEnabled)
1404 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1405 "bin/foo/bar/mybin",
1406 "bin/foo/bar/mybin64",
1407 "bin/arm/foo/bar/mybin",
1408 "bin/arm64/foo/bar/mybin64",
1409 "lib/foo/bar/mylib.so",
1410 "lib/arm/foo/bar/mylib.so",
1411 "lib64/foo/bar/mylib.so",
1412 "lib64/arm64/foo/bar/mylib.so",
1413 })
1414}
1415
Jiyong Parkda6eb592018-12-19 17:12:36 +09001416func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001417 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001418 apex {
1419 name: "myapex",
1420 key: "myapex.key",
1421 native_shared_libs: ["mylib"],
1422 use_vendor: true,
1423 }
1424
1425 apex_key {
1426 name: "myapex.key",
1427 public_key: "testkey.avbpubkey",
1428 private_key: "testkey.pem",
1429 }
1430
1431 cc_library {
1432 name: "mylib",
1433 srcs: ["mylib.cpp"],
1434 shared_libs: ["mylib2"],
1435 system_shared_libs: [],
1436 vendor_available: true,
1437 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001438 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001439 }
1440
1441 cc_library {
1442 name: "mylib2",
1443 srcs: ["mylib.cpp"],
1444 system_shared_libs: [],
1445 vendor_available: true,
1446 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001447 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001448 }
Jooyung Handc782442019-11-01 03:14:38 +09001449 `, func(fs map[string][]byte, config android.Config) {
1450 setUseVendorWhitelistForTest(config, []string{"myapex"})
1451 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001452
1453 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001454 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001455 for _, implicit := range i.Implicits {
1456 inputsList = append(inputsList, implicit.String())
1457 }
1458 }
1459 inputsString := strings.Join(inputsList, " ")
1460
1461 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001462 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1463 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001464
1465 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001466 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1467 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001468}
Jiyong Park16e91a02018-12-20 18:18:08 +09001469
Jooyung Handc782442019-11-01 03:14:38 +09001470func TestUseVendorRestriction(t *testing.T) {
1471 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1472 apex {
1473 name: "myapex",
1474 key: "myapex.key",
1475 use_vendor: true,
1476 }
1477 apex_key {
1478 name: "myapex.key",
1479 public_key: "testkey.avbpubkey",
1480 private_key: "testkey.pem",
1481 }
1482 `, func(fs map[string][]byte, config android.Config) {
1483 setUseVendorWhitelistForTest(config, []string{""})
1484 })
1485 // no error with whitelist
1486 testApex(t, `
1487 apex {
1488 name: "myapex",
1489 key: "myapex.key",
1490 use_vendor: true,
1491 }
1492 apex_key {
1493 name: "myapex.key",
1494 public_key: "testkey.avbpubkey",
1495 private_key: "testkey.pem",
1496 }
1497 `, func(fs map[string][]byte, config android.Config) {
1498 setUseVendorWhitelistForTest(config, []string{"myapex"})
1499 })
1500}
1501
Jooyung Han5c998b92019-06-27 11:30:33 +09001502func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1503 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1504 apex {
1505 name: "myapex",
1506 key: "myapex.key",
1507 native_shared_libs: ["mylib"],
1508 use_vendor: true,
1509 }
1510
1511 apex_key {
1512 name: "myapex.key",
1513 public_key: "testkey.avbpubkey",
1514 private_key: "testkey.pem",
1515 }
1516
1517 cc_library {
1518 name: "mylib",
1519 srcs: ["mylib.cpp"],
1520 system_shared_libs: [],
1521 stl: "none",
1522 }
1523 `)
1524}
1525
Jiyong Park16e91a02018-12-20 18:18:08 +09001526func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001527 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001528 apex {
1529 name: "myapex",
1530 key: "myapex.key",
1531 native_shared_libs: ["mylib"],
1532 }
1533
1534 apex_key {
1535 name: "myapex.key",
1536 public_key: "testkey.avbpubkey",
1537 private_key: "testkey.pem",
1538 }
1539
1540 cc_library {
1541 name: "mylib",
1542 srcs: ["mylib.cpp"],
1543 system_shared_libs: [],
1544 stl: "none",
1545 stubs: {
1546 versions: ["1", "2", "3"],
1547 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001548 apex_available: [
1549 "//apex_available:platform",
1550 "myapex",
1551 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001552 }
1553
1554 cc_binary {
1555 name: "not_in_apex",
1556 srcs: ["mylib.cpp"],
1557 static_libs: ["mylib"],
1558 static_executable: true,
1559 system_shared_libs: [],
1560 stl: "none",
1561 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001562 `)
1563
Colin Cross7113d202019-11-20 16:39:12 -08001564 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001565
1566 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001567 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001568}
Jiyong Park9335a262018-12-24 11:31:58 +09001569
1570func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001571 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001572 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001573 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001574 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001575 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001576 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001577 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001578 }
1579
1580 cc_library {
1581 name: "mylib",
1582 srcs: ["mylib.cpp"],
1583 system_shared_libs: [],
1584 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001585 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001586 }
1587
1588 apex_key {
1589 name: "myapex.key",
1590 public_key: "testkey.avbpubkey",
1591 private_key: "testkey.pem",
1592 }
1593
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001594 android_app_certificate {
1595 name: "myapex.certificate",
1596 certificate: "testkey",
1597 }
1598
1599 android_app_certificate {
1600 name: "myapex.certificate.override",
1601 certificate: "testkey.override",
1602 }
1603
Jiyong Park9335a262018-12-24 11:31:58 +09001604 `)
1605
1606 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001607 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001608
1609 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1610 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1611 "vendor/foo/devkeys/testkey.avbpubkey")
1612 }
1613 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1614 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1615 "vendor/foo/devkeys/testkey.pem")
1616 }
1617
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001618 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001619 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001620 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001621 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001622 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001623 }
1624}
Jiyong Park58e364a2019-01-19 19:24:06 +09001625
Jooyung Hanf121a652019-12-17 14:30:11 +09001626func TestCertificate(t *testing.T) {
1627 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1628 ctx, _ := testApex(t, `
1629 apex {
1630 name: "myapex",
1631 key: "myapex.key",
1632 }
1633 apex_key {
1634 name: "myapex.key",
1635 public_key: "testkey.avbpubkey",
1636 private_key: "testkey.pem",
1637 }`)
1638 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1639 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1640 if actual := rule.Args["certificates"]; actual != expected {
1641 t.Errorf("certificates should be %q, not %q", expected, actual)
1642 }
1643 })
1644 t.Run("override when unspecified", func(t *testing.T) {
1645 ctx, _ := testApex(t, `
1646 apex {
1647 name: "myapex_keytest",
1648 key: "myapex.key",
1649 file_contexts: ":myapex-file_contexts",
1650 }
1651 apex_key {
1652 name: "myapex.key",
1653 public_key: "testkey.avbpubkey",
1654 private_key: "testkey.pem",
1655 }
1656 android_app_certificate {
1657 name: "myapex.certificate.override",
1658 certificate: "testkey.override",
1659 }`)
1660 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1661 expected := "testkey.override.x509.pem testkey.override.pk8"
1662 if actual := rule.Args["certificates"]; actual != expected {
1663 t.Errorf("certificates should be %q, not %q", expected, actual)
1664 }
1665 })
1666 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1667 ctx, _ := testApex(t, `
1668 apex {
1669 name: "myapex",
1670 key: "myapex.key",
1671 certificate: ":myapex.certificate",
1672 }
1673 apex_key {
1674 name: "myapex.key",
1675 public_key: "testkey.avbpubkey",
1676 private_key: "testkey.pem",
1677 }
1678 android_app_certificate {
1679 name: "myapex.certificate",
1680 certificate: "testkey",
1681 }`)
1682 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1683 expected := "testkey.x509.pem testkey.pk8"
1684 if actual := rule.Args["certificates"]; actual != expected {
1685 t.Errorf("certificates should be %q, not %q", expected, actual)
1686 }
1687 })
1688 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1689 ctx, _ := testApex(t, `
1690 apex {
1691 name: "myapex_keytest",
1692 key: "myapex.key",
1693 file_contexts: ":myapex-file_contexts",
1694 certificate: ":myapex.certificate",
1695 }
1696 apex_key {
1697 name: "myapex.key",
1698 public_key: "testkey.avbpubkey",
1699 private_key: "testkey.pem",
1700 }
1701 android_app_certificate {
1702 name: "myapex.certificate.override",
1703 certificate: "testkey.override",
1704 }`)
1705 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1706 expected := "testkey.override.x509.pem testkey.override.pk8"
1707 if actual := rule.Args["certificates"]; actual != expected {
1708 t.Errorf("certificates should be %q, not %q", expected, actual)
1709 }
1710 })
1711 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1712 ctx, _ := testApex(t, `
1713 apex {
1714 name: "myapex",
1715 key: "myapex.key",
1716 certificate: "testkey",
1717 }
1718 apex_key {
1719 name: "myapex.key",
1720 public_key: "testkey.avbpubkey",
1721 private_key: "testkey.pem",
1722 }`)
1723 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1724 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1725 if actual := rule.Args["certificates"]; actual != expected {
1726 t.Errorf("certificates should be %q, not %q", expected, actual)
1727 }
1728 })
1729 t.Run("override when specified as <name>", func(t *testing.T) {
1730 ctx, _ := testApex(t, `
1731 apex {
1732 name: "myapex_keytest",
1733 key: "myapex.key",
1734 file_contexts: ":myapex-file_contexts",
1735 certificate: "testkey",
1736 }
1737 apex_key {
1738 name: "myapex.key",
1739 public_key: "testkey.avbpubkey",
1740 private_key: "testkey.pem",
1741 }
1742 android_app_certificate {
1743 name: "myapex.certificate.override",
1744 certificate: "testkey.override",
1745 }`)
1746 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1747 expected := "testkey.override.x509.pem testkey.override.pk8"
1748 if actual := rule.Args["certificates"]; actual != expected {
1749 t.Errorf("certificates should be %q, not %q", expected, actual)
1750 }
1751 })
1752}
1753
Jiyong Park58e364a2019-01-19 19:24:06 +09001754func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001755 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001756 apex {
1757 name: "myapex",
1758 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001759 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001760 }
1761
1762 apex {
1763 name: "otherapex",
1764 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001765 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001766 }
1767
1768 apex_key {
1769 name: "myapex.key",
1770 public_key: "testkey.avbpubkey",
1771 private_key: "testkey.pem",
1772 }
1773
1774 cc_library {
1775 name: "mylib",
1776 srcs: ["mylib.cpp"],
1777 system_shared_libs: [],
1778 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001779 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001780 "myapex",
1781 "otherapex",
1782 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001783 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09001784 cc_library {
1785 name: "mylib2",
1786 srcs: ["mylib.cpp"],
1787 system_shared_libs: [],
1788 stl: "none",
1789 apex_available: [
1790 "myapex",
1791 "otherapex",
1792 ],
1793 use_apex_name_macro: true,
1794 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001795 `)
1796
Jooyung Hanc87a0592020-03-02 17:44:33 +09001797 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001798 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001799 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001800
1801 // APEX variant has __ANDROID_APEX__ defined
1802 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1803 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001804 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001805
1806 // APEX variant has __ANDROID_APEX__ defined
1807 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1808 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001809 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001810
Jooyung Hanc87a0592020-03-02 17:44:33 +09001811 // When cc_library sets use_apex_name_macro: true
1812 // apex variants define additional macro to distinguish which apex variant it is built for
1813
1814 // non-APEX variant does not have __ANDROID_APEX__ defined
1815 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1816 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1817
1818 // APEX variant has __ANDROID_APEX__ defined
1819 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001820 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001821 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1822 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001823
Jooyung Hanc87a0592020-03-02 17:44:33 +09001824 // APEX variant has __ANDROID_APEX__ defined
1825 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001826 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001827 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1828 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001829}
Jiyong Park7e636d02019-01-28 16:16:54 +09001830
1831func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001832 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001833 apex {
1834 name: "myapex",
1835 key: "myapex.key",
1836 native_shared_libs: ["mylib"],
1837 }
1838
1839 apex_key {
1840 name: "myapex.key",
1841 public_key: "testkey.avbpubkey",
1842 private_key: "testkey.pem",
1843 }
1844
1845 cc_library_headers {
1846 name: "mylib_headers",
1847 export_include_dirs: ["my_include"],
1848 system_shared_libs: [],
1849 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001850 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001851 }
1852
1853 cc_library {
1854 name: "mylib",
1855 srcs: ["mylib.cpp"],
1856 system_shared_libs: [],
1857 stl: "none",
1858 header_libs: ["mylib_headers"],
1859 export_header_lib_headers: ["mylib_headers"],
1860 stubs: {
1861 versions: ["1", "2", "3"],
1862 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001863 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001864 }
1865
1866 cc_library {
1867 name: "otherlib",
1868 srcs: ["mylib.cpp"],
1869 system_shared_libs: [],
1870 stl: "none",
1871 shared_libs: ["mylib"],
1872 }
1873 `)
1874
Colin Cross7113d202019-11-20 16:39:12 -08001875 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001876
1877 // Ensure that the include path of the header lib is exported to 'otherlib'
1878 ensureContains(t, cFlags, "-Imy_include")
1879}
Alex Light9670d332019-01-29 18:07:33 -08001880
Jiyong Park7cd10e32020-01-14 09:22:18 +09001881type fileInApex struct {
1882 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001883 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001884 isLink bool
1885}
1886
Jooyung Hana57af4a2020-01-23 05:36:59 +00001887func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001888 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001889 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001890 copyCmds := apexRule.Args["copy_commands"]
1891 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001892 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001893 for _, cmd := range strings.Split(copyCmds, "&&") {
1894 cmd = strings.TrimSpace(cmd)
1895 if cmd == "" {
1896 continue
1897 }
1898 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001899 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001900 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001901 switch terms[0] {
1902 case "mkdir":
1903 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001904 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001905 t.Fatal("copyCmds contains invalid cp command", cmd)
1906 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001907 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001908 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001909 isLink = false
1910 case "ln":
1911 if len(terms) != 3 && len(terms) != 4 {
1912 // ln LINK TARGET or ln -s LINK TARGET
1913 t.Fatal("copyCmds contains invalid ln command", cmd)
1914 }
1915 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001916 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001917 isLink = true
1918 default:
1919 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1920 }
1921 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001922 index := strings.Index(dst, imageApexDir)
1923 if index == -1 {
1924 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1925 }
1926 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001927 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001928 }
1929 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001930 return ret
1931}
1932
Jooyung Hana57af4a2020-01-23 05:36:59 +00001933func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1934 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001935 var failed bool
1936 var surplus []string
1937 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001938 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001939 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001940 for _, expected := range files {
1941 if matched, _ := path.Match(expected, file.path); matched {
1942 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001943 mactchFound = true
1944 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001945 }
1946 }
Jooyung Hane6436d72020-02-27 13:31:56 +09001947 if !mactchFound {
1948 surplus = append(surplus, file.path)
1949 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001950 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001951
Jooyung Han31c470b2019-10-18 16:26:59 +09001952 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001953 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001954 t.Log("surplus files", surplus)
1955 failed = true
1956 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001957
1958 if len(files) > len(filesMatched) {
1959 var missing []string
1960 for _, expected := range files {
1961 if !filesMatched[expected] {
1962 missing = append(missing, expected)
1963 }
1964 }
1965 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001966 t.Log("missing files", missing)
1967 failed = true
1968 }
1969 if failed {
1970 t.Fail()
1971 }
1972}
1973
Jooyung Han344d5432019-08-23 11:17:39 +09001974func TestVndkApexCurrent(t *testing.T) {
1975 ctx, _ := testApex(t, `
1976 apex_vndk {
1977 name: "myapex",
1978 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001979 }
1980
1981 apex_key {
1982 name: "myapex.key",
1983 public_key: "testkey.avbpubkey",
1984 private_key: "testkey.pem",
1985 }
1986
1987 cc_library {
1988 name: "libvndk",
1989 srcs: ["mylib.cpp"],
1990 vendor_available: true,
1991 vndk: {
1992 enabled: true,
1993 },
1994 system_shared_libs: [],
1995 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001996 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001997 }
1998
1999 cc_library {
2000 name: "libvndksp",
2001 srcs: ["mylib.cpp"],
2002 vendor_available: true,
2003 vndk: {
2004 enabled: true,
2005 support_system_process: true,
2006 },
2007 system_shared_libs: [],
2008 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002009 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002010 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002011 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002012
Jooyung Hana57af4a2020-01-23 05:36:59 +00002013 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002014 "lib/libvndk.so",
2015 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002016 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002017 "lib64/libvndk.so",
2018 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002019 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002020 "etc/llndk.libraries.VER.txt",
2021 "etc/vndkcore.libraries.VER.txt",
2022 "etc/vndksp.libraries.VER.txt",
2023 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002024 })
Jooyung Han344d5432019-08-23 11:17:39 +09002025}
2026
2027func TestVndkApexWithPrebuilt(t *testing.T) {
2028 ctx, _ := testApex(t, `
2029 apex_vndk {
2030 name: "myapex",
2031 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002032 }
2033
2034 apex_key {
2035 name: "myapex.key",
2036 public_key: "testkey.avbpubkey",
2037 private_key: "testkey.pem",
2038 }
2039
2040 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002041 name: "libvndk",
2042 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002043 vendor_available: true,
2044 vndk: {
2045 enabled: true,
2046 },
2047 system_shared_libs: [],
2048 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002049 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002050 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002051
2052 cc_prebuilt_library_shared {
2053 name: "libvndk.arm",
2054 srcs: ["libvndk.arm.so"],
2055 vendor_available: true,
2056 vndk: {
2057 enabled: true,
2058 },
2059 enabled: false,
2060 arch: {
2061 arm: {
2062 enabled: true,
2063 },
2064 },
2065 system_shared_libs: [],
2066 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002067 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002068 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002069 `+vndkLibrariesTxtFiles("current"),
2070 withFiles(map[string][]byte{
2071 "libvndk.so": nil,
2072 "libvndk.arm.so": nil,
2073 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002074
Jooyung Hana57af4a2020-01-23 05:36:59 +00002075 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002076 "lib/libvndk.so",
2077 "lib/libvndk.arm.so",
2078 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002079 "lib/libc++.so",
2080 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002081 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002082 })
Jooyung Han344d5432019-08-23 11:17:39 +09002083}
2084
Jooyung Han39edb6c2019-11-06 16:53:07 +09002085func vndkLibrariesTxtFiles(vers ...string) (result string) {
2086 for _, v := range vers {
2087 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002088 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002089 result += `
2090 vndk_libraries_txt {
2091 name: "` + txt + `.libraries.txt",
2092 }
2093 `
2094 }
2095 } else {
2096 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2097 result += `
2098 prebuilt_etc {
2099 name: "` + txt + `.libraries.` + v + `.txt",
2100 src: "dummy.txt",
2101 }
2102 `
2103 }
2104 }
2105 }
2106 return
2107}
2108
Jooyung Han344d5432019-08-23 11:17:39 +09002109func TestVndkApexVersion(t *testing.T) {
2110 ctx, _ := testApex(t, `
2111 apex_vndk {
2112 name: "myapex_v27",
2113 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002114 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002115 vndk_version: "27",
2116 }
2117
2118 apex_key {
2119 name: "myapex.key",
2120 public_key: "testkey.avbpubkey",
2121 private_key: "testkey.pem",
2122 }
2123
Jooyung Han31c470b2019-10-18 16:26:59 +09002124 vndk_prebuilt_shared {
2125 name: "libvndk27",
2126 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002127 vendor_available: true,
2128 vndk: {
2129 enabled: true,
2130 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002131 target_arch: "arm64",
2132 arch: {
2133 arm: {
2134 srcs: ["libvndk27_arm.so"],
2135 },
2136 arm64: {
2137 srcs: ["libvndk27_arm64.so"],
2138 },
2139 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002140 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002141 }
2142
2143 vndk_prebuilt_shared {
2144 name: "libvndk27",
2145 version: "27",
2146 vendor_available: true,
2147 vndk: {
2148 enabled: true,
2149 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002150 target_arch: "x86_64",
2151 arch: {
2152 x86: {
2153 srcs: ["libvndk27_x86.so"],
2154 },
2155 x86_64: {
2156 srcs: ["libvndk27_x86_64.so"],
2157 },
2158 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002159 }
2160 `+vndkLibrariesTxtFiles("27"),
2161 withFiles(map[string][]byte{
2162 "libvndk27_arm.so": nil,
2163 "libvndk27_arm64.so": nil,
2164 "libvndk27_x86.so": nil,
2165 "libvndk27_x86_64.so": nil,
2166 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002167
Jooyung Hana57af4a2020-01-23 05:36:59 +00002168 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002169 "lib/libvndk27_arm.so",
2170 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002171 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002172 })
Jooyung Han344d5432019-08-23 11:17:39 +09002173}
2174
2175func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2176 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2177 apex_vndk {
2178 name: "myapex_v27",
2179 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002180 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002181 vndk_version: "27",
2182 }
2183 apex_vndk {
2184 name: "myapex_v27_other",
2185 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002186 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002187 vndk_version: "27",
2188 }
2189
2190 apex_key {
2191 name: "myapex.key",
2192 public_key: "testkey.avbpubkey",
2193 private_key: "testkey.pem",
2194 }
2195
2196 cc_library {
2197 name: "libvndk",
2198 srcs: ["mylib.cpp"],
2199 vendor_available: true,
2200 vndk: {
2201 enabled: true,
2202 },
2203 system_shared_libs: [],
2204 stl: "none",
2205 }
2206
2207 vndk_prebuilt_shared {
2208 name: "libvndk",
2209 version: "27",
2210 vendor_available: true,
2211 vndk: {
2212 enabled: true,
2213 },
2214 srcs: ["libvndk.so"],
2215 }
2216 `, withFiles(map[string][]byte{
2217 "libvndk.so": nil,
2218 }))
2219}
2220
Jooyung Han90eee022019-10-01 20:02:42 +09002221func TestVndkApexNameRule(t *testing.T) {
2222 ctx, _ := testApex(t, `
2223 apex_vndk {
2224 name: "myapex",
2225 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002226 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002227 }
2228 apex_vndk {
2229 name: "myapex_v28",
2230 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002231 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002232 vndk_version: "28",
2233 }
2234 apex_key {
2235 name: "myapex.key",
2236 public_key: "testkey.avbpubkey",
2237 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002238 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002239
2240 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002241 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002242 actual := proptools.String(bundle.properties.Apex_name)
2243 if !reflect.DeepEqual(actual, expected) {
2244 t.Errorf("Got '%v', expected '%v'", actual, expected)
2245 }
2246 }
2247
2248 assertApexName("com.android.vndk.vVER", "myapex")
2249 assertApexName("com.android.vndk.v28", "myapex_v28")
2250}
2251
Jooyung Han344d5432019-08-23 11:17:39 +09002252func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2253 ctx, _ := testApex(t, `
2254 apex_vndk {
2255 name: "myapex",
2256 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002257 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002258 }
2259
2260 apex_key {
2261 name: "myapex.key",
2262 public_key: "testkey.avbpubkey",
2263 private_key: "testkey.pem",
2264 }
2265
2266 cc_library {
2267 name: "libvndk",
2268 srcs: ["mylib.cpp"],
2269 vendor_available: true,
2270 native_bridge_supported: true,
2271 host_supported: true,
2272 vndk: {
2273 enabled: true,
2274 },
2275 system_shared_libs: [],
2276 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002277 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002278 }
Jooyung Han35155c42020-02-06 17:33:20 +09002279 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09002280
Jooyung Hana57af4a2020-01-23 05:36:59 +00002281 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002282 "lib/libvndk.so",
2283 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002284 "lib/libc++.so",
2285 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002286 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002287 })
Jooyung Han344d5432019-08-23 11:17:39 +09002288}
2289
2290func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2291 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2292 apex_vndk {
2293 name: "myapex",
2294 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002295 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002296 native_bridge_supported: true,
2297 }
2298
2299 apex_key {
2300 name: "myapex.key",
2301 public_key: "testkey.avbpubkey",
2302 private_key: "testkey.pem",
2303 }
2304
2305 cc_library {
2306 name: "libvndk",
2307 srcs: ["mylib.cpp"],
2308 vendor_available: true,
2309 native_bridge_supported: true,
2310 host_supported: true,
2311 vndk: {
2312 enabled: true,
2313 },
2314 system_shared_libs: [],
2315 stl: "none",
2316 }
2317 `)
2318}
2319
Jooyung Han31c470b2019-10-18 16:26:59 +09002320func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002321 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002322 apex_vndk {
2323 name: "myapex_v27",
2324 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002325 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002326 vndk_version: "27",
2327 }
2328
2329 apex_key {
2330 name: "myapex.key",
2331 public_key: "testkey.avbpubkey",
2332 private_key: "testkey.pem",
2333 }
2334
2335 vndk_prebuilt_shared {
2336 name: "libvndk27",
2337 version: "27",
2338 target_arch: "arm",
2339 vendor_available: true,
2340 vndk: {
2341 enabled: true,
2342 },
2343 arch: {
2344 arm: {
2345 srcs: ["libvndk27.so"],
2346 }
2347 },
2348 }
2349
2350 vndk_prebuilt_shared {
2351 name: "libvndk27",
2352 version: "27",
2353 target_arch: "arm",
2354 binder32bit: true,
2355 vendor_available: true,
2356 vndk: {
2357 enabled: true,
2358 },
2359 arch: {
2360 arm: {
2361 srcs: ["libvndk27binder32.so"],
2362 }
2363 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002364 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002365 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002366 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002367 withFiles(map[string][]byte{
2368 "libvndk27.so": nil,
2369 "libvndk27binder32.so": nil,
2370 }),
2371 withBinder32bit,
2372 withTargets(map[android.OsType][]android.Target{
2373 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09002374 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
2375 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09002376 },
2377 }),
2378 )
2379
Jooyung Hana57af4a2020-01-23 05:36:59 +00002380 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002381 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002382 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002383 })
2384}
2385
Jooyung Hane1633032019-08-01 17:41:43 +09002386func TestDependenciesInApexManifest(t *testing.T) {
2387 ctx, _ := testApex(t, `
2388 apex {
2389 name: "myapex_nodep",
2390 key: "myapex.key",
2391 native_shared_libs: ["lib_nodep"],
2392 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002393 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002394 }
2395
2396 apex {
2397 name: "myapex_dep",
2398 key: "myapex.key",
2399 native_shared_libs: ["lib_dep"],
2400 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002401 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002402 }
2403
2404 apex {
2405 name: "myapex_provider",
2406 key: "myapex.key",
2407 native_shared_libs: ["libfoo"],
2408 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002409 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002410 }
2411
2412 apex {
2413 name: "myapex_selfcontained",
2414 key: "myapex.key",
2415 native_shared_libs: ["lib_dep", "libfoo"],
2416 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002417 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002418 }
2419
2420 apex_key {
2421 name: "myapex.key",
2422 public_key: "testkey.avbpubkey",
2423 private_key: "testkey.pem",
2424 }
2425
2426 cc_library {
2427 name: "lib_nodep",
2428 srcs: ["mylib.cpp"],
2429 system_shared_libs: [],
2430 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002431 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002432 }
2433
2434 cc_library {
2435 name: "lib_dep",
2436 srcs: ["mylib.cpp"],
2437 shared_libs: ["libfoo"],
2438 system_shared_libs: [],
2439 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002440 apex_available: [
2441 "myapex_dep",
2442 "myapex_provider",
2443 "myapex_selfcontained",
2444 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002445 }
2446
2447 cc_library {
2448 name: "libfoo",
2449 srcs: ["mytest.cpp"],
2450 stubs: {
2451 versions: ["1"],
2452 },
2453 system_shared_libs: [],
2454 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002455 apex_available: [
2456 "myapex_provider",
2457 "myapex_selfcontained",
2458 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002459 }
2460 `)
2461
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002462 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002463 var provideNativeLibs, requireNativeLibs []string
2464
Sundong Ahnabb64432019-10-22 13:58:29 +09002465 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002466 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2467 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002468 ensureListEmpty(t, provideNativeLibs)
2469 ensureListEmpty(t, requireNativeLibs)
2470
Sundong Ahnabb64432019-10-22 13:58:29 +09002471 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002472 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2473 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002474 ensureListEmpty(t, provideNativeLibs)
2475 ensureListContains(t, requireNativeLibs, "libfoo.so")
2476
Sundong Ahnabb64432019-10-22 13:58:29 +09002477 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002478 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2479 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002480 ensureListContains(t, provideNativeLibs, "libfoo.so")
2481 ensureListEmpty(t, requireNativeLibs)
2482
Sundong Ahnabb64432019-10-22 13:58:29 +09002483 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002484 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2485 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002486 ensureListContains(t, provideNativeLibs, "libfoo.so")
2487 ensureListEmpty(t, requireNativeLibs)
2488}
2489
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002490func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002491 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002492 apex {
2493 name: "myapex",
2494 key: "myapex.key",
2495 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002496 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002497 }
2498
2499 apex_key {
2500 name: "myapex.key",
2501 public_key: "testkey.avbpubkey",
2502 private_key: "testkey.pem",
2503 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002504
2505 cc_library {
2506 name: "mylib",
2507 srcs: ["mylib.cpp"],
2508 system_shared_libs: [],
2509 stl: "none",
2510 apex_available: [
2511 "//apex_available:platform",
2512 "myapex",
2513 ],
2514 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002515 `)
2516
Sundong Ahnabb64432019-10-22 13:58:29 +09002517 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002518 apexManifestRule := module.Rule("apexManifestRule")
2519 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2520 apexRule := module.Rule("apexRule")
2521 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002522
2523 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2524 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2525 name := apexBundle.BaseModuleName()
2526 prefix := "TARGET_"
2527 var builder strings.Builder
2528 data.Custom(&builder, name, prefix, "", data)
2529 androidMk := builder.String()
2530 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2531 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002532}
2533
Alex Light0851b882019-02-07 13:20:53 -08002534func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002535 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002536 apex {
2537 name: "myapex",
2538 key: "myapex.key",
2539 native_shared_libs: ["mylib_common"],
2540 }
2541
2542 apex_key {
2543 name: "myapex.key",
2544 public_key: "testkey.avbpubkey",
2545 private_key: "testkey.pem",
2546 }
2547
2548 cc_library {
2549 name: "mylib_common",
2550 srcs: ["mylib.cpp"],
2551 system_shared_libs: [],
2552 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002553 apex_available: [
2554 "//apex_available:platform",
2555 "myapex",
2556 ],
Alex Light0851b882019-02-07 13:20:53 -08002557 }
2558 `)
2559
Sundong Ahnabb64432019-10-22 13:58:29 +09002560 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002561 apexRule := module.Rule("apexRule")
2562 copyCmds := apexRule.Args["copy_commands"]
2563
2564 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2565 t.Log("Apex was a test apex!")
2566 t.Fail()
2567 }
2568 // Ensure that main rule creates an output
2569 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2570
2571 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002572 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002573
2574 // Ensure that both direct and indirect deps are copied into apex
2575 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2576
Colin Cross7113d202019-11-20 16:39:12 -08002577 // Ensure that the platform variant ends with _shared
2578 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002579
2580 if !android.InAnyApex("mylib_common") {
2581 t.Log("Found mylib_common not in any apex!")
2582 t.Fail()
2583 }
2584}
2585
2586func TestTestApex(t *testing.T) {
2587 if android.InAnyApex("mylib_common_test") {
2588 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!")
2589 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002590 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002591 apex_test {
2592 name: "myapex",
2593 key: "myapex.key",
2594 native_shared_libs: ["mylib_common_test"],
2595 }
2596
2597 apex_key {
2598 name: "myapex.key",
2599 public_key: "testkey.avbpubkey",
2600 private_key: "testkey.pem",
2601 }
2602
2603 cc_library {
2604 name: "mylib_common_test",
2605 srcs: ["mylib.cpp"],
2606 system_shared_libs: [],
2607 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002608 // TODO: remove //apex_available:platform
2609 apex_available: [
2610 "//apex_available:platform",
2611 "myapex",
2612 ],
Alex Light0851b882019-02-07 13:20:53 -08002613 }
2614 `)
2615
Sundong Ahnabb64432019-10-22 13:58:29 +09002616 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002617 apexRule := module.Rule("apexRule")
2618 copyCmds := apexRule.Args["copy_commands"]
2619
2620 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2621 t.Log("Apex was not a test apex!")
2622 t.Fail()
2623 }
2624 // Ensure that main rule creates an output
2625 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2626
2627 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002628 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002629
2630 // Ensure that both direct and indirect deps are copied into apex
2631 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2632
Colin Cross7113d202019-11-20 16:39:12 -08002633 // Ensure that the platform variant ends with _shared
2634 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002635}
2636
Alex Light9670d332019-01-29 18:07:33 -08002637func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002638 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002639 apex {
2640 name: "myapex",
2641 key: "myapex.key",
2642 multilib: {
2643 first: {
2644 native_shared_libs: ["mylib_common"],
2645 }
2646 },
2647 target: {
2648 android: {
2649 multilib: {
2650 first: {
2651 native_shared_libs: ["mylib"],
2652 }
2653 }
2654 },
2655 host: {
2656 multilib: {
2657 first: {
2658 native_shared_libs: ["mylib2"],
2659 }
2660 }
2661 }
2662 }
2663 }
2664
2665 apex_key {
2666 name: "myapex.key",
2667 public_key: "testkey.avbpubkey",
2668 private_key: "testkey.pem",
2669 }
2670
2671 cc_library {
2672 name: "mylib",
2673 srcs: ["mylib.cpp"],
2674 system_shared_libs: [],
2675 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002676 // TODO: remove //apex_available:platform
2677 apex_available: [
2678 "//apex_available:platform",
2679 "myapex",
2680 ],
Alex Light9670d332019-01-29 18:07:33 -08002681 }
2682
2683 cc_library {
2684 name: "mylib_common",
2685 srcs: ["mylib.cpp"],
2686 system_shared_libs: [],
2687 stl: "none",
2688 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002689 // TODO: remove //apex_available:platform
2690 apex_available: [
2691 "//apex_available:platform",
2692 "myapex",
2693 ],
Alex Light9670d332019-01-29 18:07:33 -08002694 }
2695
2696 cc_library {
2697 name: "mylib2",
2698 srcs: ["mylib.cpp"],
2699 system_shared_libs: [],
2700 stl: "none",
2701 compile_multilib: "first",
2702 }
2703 `)
2704
Sundong Ahnabb64432019-10-22 13:58:29 +09002705 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002706 copyCmds := apexRule.Args["copy_commands"]
2707
2708 // Ensure that main rule creates an output
2709 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2710
2711 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002712 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2713 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2714 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002715
2716 // Ensure that both direct and indirect deps are copied into apex
2717 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2718 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2719 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2720
Colin Cross7113d202019-11-20 16:39:12 -08002721 // Ensure that the platform variant ends with _shared
2722 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2723 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2724 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002725}
Jiyong Park04480cf2019-02-06 00:16:29 +09002726
2727func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002728 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002729 apex {
2730 name: "myapex",
2731 key: "myapex.key",
2732 binaries: ["myscript"],
2733 }
2734
2735 apex_key {
2736 name: "myapex.key",
2737 public_key: "testkey.avbpubkey",
2738 private_key: "testkey.pem",
2739 }
2740
2741 sh_binary {
2742 name: "myscript",
2743 src: "mylib.cpp",
2744 filename: "myscript.sh",
2745 sub_dir: "script",
2746 }
2747 `)
2748
Sundong Ahnabb64432019-10-22 13:58:29 +09002749 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002750 copyCmds := apexRule.Args["copy_commands"]
2751
2752 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2753}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002754
Jooyung Han91df2082019-11-20 01:49:42 +09002755func TestApexInVariousPartition(t *testing.T) {
2756 testcases := []struct {
2757 propName, parition, flattenedPartition string
2758 }{
2759 {"", "system", "system_ext"},
2760 {"product_specific: true", "product", "product"},
2761 {"soc_specific: true", "vendor", "vendor"},
2762 {"proprietary: true", "vendor", "vendor"},
2763 {"vendor: true", "vendor", "vendor"},
2764 {"system_ext_specific: true", "system_ext", "system_ext"},
2765 }
2766 for _, tc := range testcases {
2767 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2768 ctx, _ := testApex(t, `
2769 apex {
2770 name: "myapex",
2771 key: "myapex.key",
2772 `+tc.propName+`
2773 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002774
Jooyung Han91df2082019-11-20 01:49:42 +09002775 apex_key {
2776 name: "myapex.key",
2777 public_key: "testkey.avbpubkey",
2778 private_key: "testkey.pem",
2779 }
2780 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002781
Jooyung Han91df2082019-11-20 01:49:42 +09002782 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2783 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2784 actual := apex.installDir.String()
2785 if actual != expected {
2786 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2787 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002788
Jooyung Han91df2082019-11-20 01:49:42 +09002789 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2790 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2791 actual = flattened.installDir.String()
2792 if actual != expected {
2793 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2794 }
2795 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002796 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002797}
Jiyong Park67882562019-03-21 01:11:21 +09002798
Jooyung Han54aca7b2019-11-20 02:26:02 +09002799func TestFileContexts(t *testing.T) {
2800 ctx, _ := testApex(t, `
2801 apex {
2802 name: "myapex",
2803 key: "myapex.key",
2804 }
2805
2806 apex_key {
2807 name: "myapex.key",
2808 public_key: "testkey.avbpubkey",
2809 private_key: "testkey.pem",
2810 }
2811 `)
2812 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2813 apexRule := module.Rule("apexRule")
2814 actual := apexRule.Args["file_contexts"]
2815 expected := "system/sepolicy/apex/myapex-file_contexts"
2816 if actual != expected {
2817 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2818 }
2819
2820 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2821 apex {
2822 name: "myapex",
2823 key: "myapex.key",
2824 file_contexts: "my_own_file_contexts",
2825 }
2826
2827 apex_key {
2828 name: "myapex.key",
2829 public_key: "testkey.avbpubkey",
2830 private_key: "testkey.pem",
2831 }
2832 `, withFiles(map[string][]byte{
2833 "my_own_file_contexts": nil,
2834 }))
2835
2836 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2837 apex {
2838 name: "myapex",
2839 key: "myapex.key",
2840 product_specific: true,
2841 file_contexts: "product_specific_file_contexts",
2842 }
2843
2844 apex_key {
2845 name: "myapex.key",
2846 public_key: "testkey.avbpubkey",
2847 private_key: "testkey.pem",
2848 }
2849 `)
2850
2851 ctx, _ = testApex(t, `
2852 apex {
2853 name: "myapex",
2854 key: "myapex.key",
2855 product_specific: true,
2856 file_contexts: "product_specific_file_contexts",
2857 }
2858
2859 apex_key {
2860 name: "myapex.key",
2861 public_key: "testkey.avbpubkey",
2862 private_key: "testkey.pem",
2863 }
2864 `, withFiles(map[string][]byte{
2865 "product_specific_file_contexts": nil,
2866 }))
2867 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2868 apexRule = module.Rule("apexRule")
2869 actual = apexRule.Args["file_contexts"]
2870 expected = "product_specific_file_contexts"
2871 if actual != expected {
2872 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2873 }
2874
2875 ctx, _ = testApex(t, `
2876 apex {
2877 name: "myapex",
2878 key: "myapex.key",
2879 product_specific: true,
2880 file_contexts: ":my-file-contexts",
2881 }
2882
2883 apex_key {
2884 name: "myapex.key",
2885 public_key: "testkey.avbpubkey",
2886 private_key: "testkey.pem",
2887 }
2888
2889 filegroup {
2890 name: "my-file-contexts",
2891 srcs: ["product_specific_file_contexts"],
2892 }
2893 `, withFiles(map[string][]byte{
2894 "product_specific_file_contexts": nil,
2895 }))
2896 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2897 apexRule = module.Rule("apexRule")
2898 actual = apexRule.Args["file_contexts"]
2899 expected = "product_specific_file_contexts"
2900 if actual != expected {
2901 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2902 }
2903}
2904
Jiyong Park67882562019-03-21 01:11:21 +09002905func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002906 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002907 apex_key {
2908 name: "myapex.key",
2909 public_key: ":my.avbpubkey",
2910 private_key: ":my.pem",
2911 product_specific: true,
2912 }
2913
2914 filegroup {
2915 name: "my.avbpubkey",
2916 srcs: ["testkey2.avbpubkey"],
2917 }
2918
2919 filegroup {
2920 name: "my.pem",
2921 srcs: ["testkey2.pem"],
2922 }
2923 `)
2924
2925 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2926 expected_pubkey := "testkey2.avbpubkey"
2927 actual_pubkey := apex_key.public_key_file.String()
2928 if actual_pubkey != expected_pubkey {
2929 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2930 }
2931 expected_privkey := "testkey2.pem"
2932 actual_privkey := apex_key.private_key_file.String()
2933 if actual_privkey != expected_privkey {
2934 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2935 }
2936}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002937
2938func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002939 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002940 prebuilt_apex {
2941 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002942 arch: {
2943 arm64: {
2944 src: "myapex-arm64.apex",
2945 },
2946 arm: {
2947 src: "myapex-arm.apex",
2948 },
2949 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002950 }
2951 `)
2952
2953 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2954
Jiyong Parkc95714e2019-03-29 14:23:10 +09002955 expectedInput := "myapex-arm64.apex"
2956 if prebuilt.inputApex.String() != expectedInput {
2957 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2958 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002959}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002960
2961func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002962 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002963 prebuilt_apex {
2964 name: "myapex",
2965 src: "myapex-arm.apex",
2966 filename: "notmyapex.apex",
2967 }
2968 `)
2969
2970 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2971
2972 expected := "notmyapex.apex"
2973 if p.installFilename != expected {
2974 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2975 }
2976}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002977
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002978func TestPrebuiltOverrides(t *testing.T) {
2979 ctx, config := testApex(t, `
2980 prebuilt_apex {
2981 name: "myapex.prebuilt",
2982 src: "myapex-arm.apex",
2983 overrides: [
2984 "myapex",
2985 ],
2986 }
2987 `)
2988
2989 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2990
2991 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002992 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002993 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002994 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002995 }
2996}
2997
Roland Levillain630846d2019-06-26 12:48:34 +01002998func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002999 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003000 apex_test {
3001 name: "myapex",
3002 key: "myapex.key",
3003 tests: [
3004 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003005 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003006 ],
3007 }
3008
3009 apex_key {
3010 name: "myapex.key",
3011 public_key: "testkey.avbpubkey",
3012 private_key: "testkey.pem",
3013 }
3014
3015 cc_test {
3016 name: "mytest",
3017 gtest: false,
3018 srcs: ["mytest.cpp"],
3019 relative_install_path: "test",
3020 system_shared_libs: [],
3021 static_executable: true,
3022 stl: "none",
3023 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003024
3025 cc_test {
3026 name: "mytests",
3027 gtest: false,
3028 srcs: [
3029 "mytest1.cpp",
3030 "mytest2.cpp",
3031 "mytest3.cpp",
3032 ],
3033 test_per_src: true,
3034 relative_install_path: "test",
3035 system_shared_libs: [],
3036 static_executable: true,
3037 stl: "none",
3038 }
Roland Levillain630846d2019-06-26 12:48:34 +01003039 `)
3040
Sundong Ahnabb64432019-10-22 13:58:29 +09003041 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003042 copyCmds := apexRule.Args["copy_commands"]
3043
3044 // Ensure that test dep is copied into apex.
3045 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003046
3047 // Ensure that test deps built with `test_per_src` are copied into apex.
3048 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3049 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3050 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003051
3052 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003053 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003054 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3055 name := apexBundle.BaseModuleName()
3056 prefix := "TARGET_"
3057 var builder strings.Builder
3058 data.Custom(&builder, name, prefix, "", data)
3059 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003060 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3061 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3062 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3063 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003064 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003065 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003066 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003067}
3068
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003069func TestInstallExtraFlattenedApexes(t *testing.T) {
3070 ctx, config := testApex(t, `
3071 apex {
3072 name: "myapex",
3073 key: "myapex.key",
3074 }
3075 apex_key {
3076 name: "myapex.key",
3077 public_key: "testkey.avbpubkey",
3078 private_key: "testkey.pem",
3079 }
3080 `, func(fs map[string][]byte, config android.Config) {
3081 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3082 })
3083 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003084 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003085 mk := android.AndroidMkDataForTest(t, config, "", ab)
3086 var builder strings.Builder
3087 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3088 androidMk := builder.String()
3089 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3090}
3091
Jooyung Han5c998b92019-06-27 11:30:33 +09003092func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003093 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003094 apex {
3095 name: "myapex",
3096 key: "myapex.key",
3097 native_shared_libs: ["mylib"],
3098 uses: ["commonapex"],
3099 }
3100
3101 apex {
3102 name: "commonapex",
3103 key: "myapex.key",
3104 native_shared_libs: ["libcommon"],
3105 provide_cpp_shared_libs: true,
3106 }
3107
3108 apex_key {
3109 name: "myapex.key",
3110 public_key: "testkey.avbpubkey",
3111 private_key: "testkey.pem",
3112 }
3113
3114 cc_library {
3115 name: "mylib",
3116 srcs: ["mylib.cpp"],
3117 shared_libs: ["libcommon"],
3118 system_shared_libs: [],
3119 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003120 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003121 }
3122
3123 cc_library {
3124 name: "libcommon",
3125 srcs: ["mylib_common.cpp"],
3126 system_shared_libs: [],
3127 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003128 // TODO: remove //apex_available:platform
3129 apex_available: [
3130 "//apex_available:platform",
3131 "commonapex",
3132 "myapex",
3133 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003134 }
3135 `)
3136
Sundong Ahnabb64432019-10-22 13:58:29 +09003137 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003138 apexRule1 := module1.Rule("apexRule")
3139 copyCmds1 := apexRule1.Args["copy_commands"]
3140
Sundong Ahnabb64432019-10-22 13:58:29 +09003141 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003142 apexRule2 := module2.Rule("apexRule")
3143 copyCmds2 := apexRule2.Args["copy_commands"]
3144
Colin Cross7113d202019-11-20 16:39:12 -08003145 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3146 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003147 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3148 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3149 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3150}
3151
3152func TestApexUsesFailsIfNotProvided(t *testing.T) {
3153 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3154 apex {
3155 name: "myapex",
3156 key: "myapex.key",
3157 uses: ["commonapex"],
3158 }
3159
3160 apex {
3161 name: "commonapex",
3162 key: "myapex.key",
3163 }
3164
3165 apex_key {
3166 name: "myapex.key",
3167 public_key: "testkey.avbpubkey",
3168 private_key: "testkey.pem",
3169 }
3170 `)
3171 testApexError(t, `uses: "commonapex" is not a provider`, `
3172 apex {
3173 name: "myapex",
3174 key: "myapex.key",
3175 uses: ["commonapex"],
3176 }
3177
3178 cc_library {
3179 name: "commonapex",
3180 system_shared_libs: [],
3181 stl: "none",
3182 }
3183
3184 apex_key {
3185 name: "myapex.key",
3186 public_key: "testkey.avbpubkey",
3187 private_key: "testkey.pem",
3188 }
3189 `)
3190}
3191
3192func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3193 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3194 apex {
3195 name: "myapex",
3196 key: "myapex.key",
3197 use_vendor: true,
3198 uses: ["commonapex"],
3199 }
3200
3201 apex {
3202 name: "commonapex",
3203 key: "myapex.key",
3204 provide_cpp_shared_libs: true,
3205 }
3206
3207 apex_key {
3208 name: "myapex.key",
3209 public_key: "testkey.avbpubkey",
3210 private_key: "testkey.pem",
3211 }
Jooyung Handc782442019-11-01 03:14:38 +09003212 `, func(fs map[string][]byte, config android.Config) {
3213 setUseVendorWhitelistForTest(config, []string{"myapex"})
3214 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003215}
3216
Jooyung Hand48f3c32019-08-23 11:18:57 +09003217func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3218 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3219 apex {
3220 name: "myapex",
3221 key: "myapex.key",
3222 native_shared_libs: ["libfoo"],
3223 }
3224
3225 apex_key {
3226 name: "myapex.key",
3227 public_key: "testkey.avbpubkey",
3228 private_key: "testkey.pem",
3229 }
3230
3231 cc_library {
3232 name: "libfoo",
3233 stl: "none",
3234 system_shared_libs: [],
3235 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003236 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003237 }
3238 `)
3239 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3240 apex {
3241 name: "myapex",
3242 key: "myapex.key",
3243 java_libs: ["myjar"],
3244 }
3245
3246 apex_key {
3247 name: "myapex.key",
3248 public_key: "testkey.avbpubkey",
3249 private_key: "testkey.pem",
3250 }
3251
3252 java_library {
3253 name: "myjar",
3254 srcs: ["foo/bar/MyClass.java"],
3255 sdk_version: "none",
3256 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003257 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003258 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003259 }
3260 `)
3261}
3262
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003263func TestApexWithApps(t *testing.T) {
3264 ctx, _ := testApex(t, `
3265 apex {
3266 name: "myapex",
3267 key: "myapex.key",
3268 apps: [
3269 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003270 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003271 ],
3272 }
3273
3274 apex_key {
3275 name: "myapex.key",
3276 public_key: "testkey.avbpubkey",
3277 private_key: "testkey.pem",
3278 }
3279
3280 android_app {
3281 name: "AppFoo",
3282 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003283 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003284 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003285 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08003286 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003287 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003288 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003289
3290 android_app {
3291 name: "AppFooPriv",
3292 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003293 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09003294 system_modules: "none",
3295 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08003296 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003297 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003298 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003299
3300 cc_library_shared {
3301 name: "libjni",
3302 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003303 shared_libs: ["libfoo"],
3304 stl: "none",
3305 system_shared_libs: [],
3306 apex_available: [ "myapex" ],
3307 sdk_version: "current",
3308 }
3309
3310 cc_library_shared {
3311 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09003312 stl: "none",
3313 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003314 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08003315 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003316 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003317 `)
3318
Sundong Ahnabb64432019-10-22 13:58:29 +09003319 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003320 apexRule := module.Rule("apexRule")
3321 copyCmds := apexRule.Args["copy_commands"]
3322
3323 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003324 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003325
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003326 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3327 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003328 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003329 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003330 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003331 // JNI libraries including transitive deps are
3332 for _, jni := range []string{"libjni", "libfoo"} {
3333 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
3334 // ... embedded inside APK (jnilibs.zip)
3335 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3336 // ... and not directly inside the APEX
3337 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3338 }
Dario Frenicde2a032019-10-27 00:29:22 +01003339}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003340
Dario Frenicde2a032019-10-27 00:29:22 +01003341func TestApexWithAppImports(t *testing.T) {
3342 ctx, _ := testApex(t, `
3343 apex {
3344 name: "myapex",
3345 key: "myapex.key",
3346 apps: [
3347 "AppFooPrebuilt",
3348 "AppFooPrivPrebuilt",
3349 ],
3350 }
3351
3352 apex_key {
3353 name: "myapex.key",
3354 public_key: "testkey.avbpubkey",
3355 private_key: "testkey.pem",
3356 }
3357
3358 android_app_import {
3359 name: "AppFooPrebuilt",
3360 apk: "PrebuiltAppFoo.apk",
3361 presigned: true,
3362 dex_preopt: {
3363 enabled: false,
3364 },
3365 }
3366
3367 android_app_import {
3368 name: "AppFooPrivPrebuilt",
3369 apk: "PrebuiltAppFooPriv.apk",
3370 privileged: true,
3371 presigned: true,
3372 dex_preopt: {
3373 enabled: false,
3374 },
3375 }
3376 `)
3377
Sundong Ahnabb64432019-10-22 13:58:29 +09003378 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003379 apexRule := module.Rule("apexRule")
3380 copyCmds := apexRule.Args["copy_commands"]
3381
3382 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3383 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003384}
3385
Dario Freni6f3937c2019-12-20 22:58:03 +00003386func TestApexWithTestHelperApp(t *testing.T) {
3387 ctx, _ := testApex(t, `
3388 apex {
3389 name: "myapex",
3390 key: "myapex.key",
3391 apps: [
3392 "TesterHelpAppFoo",
3393 ],
3394 }
3395
3396 apex_key {
3397 name: "myapex.key",
3398 public_key: "testkey.avbpubkey",
3399 private_key: "testkey.pem",
3400 }
3401
3402 android_test_helper_app {
3403 name: "TesterHelpAppFoo",
3404 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003405 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003406 }
3407
3408 `)
3409
3410 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3411 apexRule := module.Rule("apexRule")
3412 copyCmds := apexRule.Args["copy_commands"]
3413
3414 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3415}
3416
Jooyung Han18020ea2019-11-13 10:50:48 +09003417func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3418 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09003419 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003420 apex {
3421 name: "myapex",
3422 key: "myapex.key",
3423 native_shared_libs: ["libfoo"],
3424 }
3425
3426 apex_key {
3427 name: "myapex.key",
3428 public_key: "testkey.avbpubkey",
3429 private_key: "testkey.pem",
3430 }
3431
3432 apex {
3433 name: "otherapex",
3434 key: "myapex.key",
3435 native_shared_libs: ["libfoo"],
3436 }
3437
3438 cc_defaults {
3439 name: "libfoo-defaults",
3440 apex_available: ["otherapex"],
3441 }
3442
3443 cc_library {
3444 name: "libfoo",
3445 defaults: ["libfoo-defaults"],
3446 stl: "none",
3447 system_shared_libs: [],
3448 }`)
3449}
3450
Jiyong Park127b40b2019-09-30 16:04:35 +09003451func TestApexAvailable(t *testing.T) {
3452 // libfoo is not available to myapex, but only to otherapex
3453 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3454 apex {
3455 name: "myapex",
3456 key: "myapex.key",
3457 native_shared_libs: ["libfoo"],
3458 }
3459
3460 apex_key {
3461 name: "myapex.key",
3462 public_key: "testkey.avbpubkey",
3463 private_key: "testkey.pem",
3464 }
3465
3466 apex {
3467 name: "otherapex",
3468 key: "otherapex.key",
3469 native_shared_libs: ["libfoo"],
3470 }
3471
3472 apex_key {
3473 name: "otherapex.key",
3474 public_key: "testkey.avbpubkey",
3475 private_key: "testkey.pem",
3476 }
3477
3478 cc_library {
3479 name: "libfoo",
3480 stl: "none",
3481 system_shared_libs: [],
3482 apex_available: ["otherapex"],
3483 }`)
3484
Jooyung Han5e9013b2020-03-10 06:23:13 +09003485 // libbbaz is an indirect dep
3486 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003487 apex {
3488 name: "myapex",
3489 key: "myapex.key",
3490 native_shared_libs: ["libfoo"],
3491 }
3492
3493 apex_key {
3494 name: "myapex.key",
3495 public_key: "testkey.avbpubkey",
3496 private_key: "testkey.pem",
3497 }
3498
Jiyong Park127b40b2019-09-30 16:04:35 +09003499 cc_library {
3500 name: "libfoo",
3501 stl: "none",
3502 shared_libs: ["libbar"],
3503 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003504 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003505 }
3506
3507 cc_library {
3508 name: "libbar",
3509 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09003510 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003511 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003512 apex_available: ["myapex"],
3513 }
3514
3515 cc_library {
3516 name: "libbaz",
3517 stl: "none",
3518 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003519 }`)
3520
3521 testApexError(t, "\"otherapex\" is not a valid module name", `
3522 apex {
3523 name: "myapex",
3524 key: "myapex.key",
3525 native_shared_libs: ["libfoo"],
3526 }
3527
3528 apex_key {
3529 name: "myapex.key",
3530 public_key: "testkey.avbpubkey",
3531 private_key: "testkey.pem",
3532 }
3533
3534 cc_library {
3535 name: "libfoo",
3536 stl: "none",
3537 system_shared_libs: [],
3538 apex_available: ["otherapex"],
3539 }`)
3540
3541 ctx, _ := testApex(t, `
3542 apex {
3543 name: "myapex",
3544 key: "myapex.key",
3545 native_shared_libs: ["libfoo", "libbar"],
3546 }
3547
3548 apex_key {
3549 name: "myapex.key",
3550 public_key: "testkey.avbpubkey",
3551 private_key: "testkey.pem",
3552 }
3553
3554 cc_library {
3555 name: "libfoo",
3556 stl: "none",
3557 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09003558 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003559 apex_available: ["myapex"],
3560 }
3561
3562 cc_library {
3563 name: "libbar",
3564 stl: "none",
3565 system_shared_libs: [],
3566 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09003567 }
3568
3569 cc_library {
3570 name: "libbaz",
3571 stl: "none",
3572 system_shared_libs: [],
3573 stubs: {
3574 versions: ["10", "20", "30"],
3575 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003576 }`)
3577
3578 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003579 // TODO(jiyong) the checks for the platform variant are removed because we now create
3580 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3581 // the platform variants are not used from other platform modules. When that is done,
3582 // these checks will be replaced by expecting a specific error message that will be
3583 // emitted when the platform variant is used.
3584 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3585 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3586 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3587 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003588
3589 ctx, _ = testApex(t, `
3590 apex {
3591 name: "myapex",
3592 key: "myapex.key",
3593 }
3594
3595 apex_key {
3596 name: "myapex.key",
3597 public_key: "testkey.avbpubkey",
3598 private_key: "testkey.pem",
3599 }
3600
3601 cc_library {
3602 name: "libfoo",
3603 stl: "none",
3604 system_shared_libs: [],
3605 apex_available: ["//apex_available:platform"],
3606 }`)
3607
3608 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003609 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3610 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003611
3612 ctx, _ = testApex(t, `
3613 apex {
3614 name: "myapex",
3615 key: "myapex.key",
3616 native_shared_libs: ["libfoo"],
3617 }
3618
3619 apex_key {
3620 name: "myapex.key",
3621 public_key: "testkey.avbpubkey",
3622 private_key: "testkey.pem",
3623 }
3624
3625 cc_library {
3626 name: "libfoo",
3627 stl: "none",
3628 system_shared_libs: [],
3629 apex_available: ["myapex"],
3630 static: {
3631 apex_available: ["//apex_available:platform"],
3632 },
3633 }`)
3634
3635 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003636 // TODO(jiyong) the checks for the platform variant are removed because we now create
3637 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3638 // the platform variants are not used from other platform modules. When that is done,
3639 // these checks will be replaced by expecting a specific error message that will be
3640 // emitted when the platform variant is used.
3641 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3642 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3643 // // but the static variant is available to both myapex and the platform
3644 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3645 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003646}
3647
Jiyong Park5d790c32019-11-15 18:40:32 +09003648func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003649 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003650 apex {
3651 name: "myapex",
3652 key: "myapex.key",
3653 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003654 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003655 }
3656
3657 override_apex {
3658 name: "override_myapex",
3659 base: "myapex",
3660 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003661 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003662 logging_parent: "com.foo.bar",
Jiyong Park5d790c32019-11-15 18:40:32 +09003663 }
3664
3665 apex_key {
3666 name: "myapex.key",
3667 public_key: "testkey.avbpubkey",
3668 private_key: "testkey.pem",
3669 }
3670
3671 android_app {
3672 name: "app",
3673 srcs: ["foo/bar/MyClass.java"],
3674 package_name: "foo",
3675 sdk_version: "none",
3676 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003677 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003678 }
3679
3680 override_android_app {
3681 name: "override_app",
3682 base: "app",
3683 package_name: "bar",
3684 }
Jiyong Park20bacab2020-03-03 11:45:41 +09003685 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003686
Jiyong Park317645e2019-12-05 13:20:58 +09003687 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3688 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3689 if originalVariant.GetOverriddenBy() != "" {
3690 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3691 }
3692 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3693 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3694 }
3695
Jiyong Park5d790c32019-11-15 18:40:32 +09003696 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3697 apexRule := module.Rule("apexRule")
3698 copyCmds := apexRule.Args["copy_commands"]
3699
3700 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3701 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003702
3703 apexBundle := module.Module().(*apexBundle)
3704 name := apexBundle.Name()
3705 if name != "override_myapex" {
3706 t.Errorf("name should be \"override_myapex\", but was %q", name)
3707 }
3708
Baligh Uddin004d7172020-02-19 21:29:28 -08003709 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3710 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3711 }
3712
Jiyong Park20bacab2020-03-03 11:45:41 +09003713 optFlags := apexRule.Args["opt_flags"]
3714 ensureContains(t, optFlags, "--override_apk_package_name com.android.myapex")
3715
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003716 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3717 var builder strings.Builder
3718 data.Custom(&builder, name, "TARGET_", "", data)
3719 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003720 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003721 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3722 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003723 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003724 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003725 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003726 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3727 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003728}
3729
Jooyung Han214bf372019-11-12 13:03:50 +09003730func TestLegacyAndroid10Support(t *testing.T) {
3731 ctx, _ := testApex(t, `
3732 apex {
3733 name: "myapex",
3734 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003735 native_shared_libs: ["mylib"],
Jooyung Han214bf372019-11-12 13:03:50 +09003736 legacy_android10_support: true,
3737 }
3738
3739 apex_key {
3740 name: "myapex.key",
3741 public_key: "testkey.avbpubkey",
3742 private_key: "testkey.pem",
3743 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003744
3745 cc_library {
3746 name: "mylib",
3747 srcs: ["mylib.cpp"],
3748 stl: "libc++",
3749 system_shared_libs: [],
3750 apex_available: [ "myapex" ],
3751 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003752 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003753
3754 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3755 args := module.Rule("apexRule").Args
3756 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003757 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003758
3759 // The copies of the libraries in the apex should have one more dependency than
3760 // the ones outside the apex, namely the unwinder. Ideally we should check
3761 // the dependency names directly here but for some reason the names are blank in
3762 // this test.
3763 for _, lib := range []string{"libc++", "mylib"} {
3764 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3765 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3766 if len(apexImplicits) != len(nonApexImplicits)+1 {
3767 t.Errorf("%q missing unwinder dep", lib)
3768 }
3769 }
Jooyung Han214bf372019-11-12 13:03:50 +09003770}
3771
Jooyung Han58f26ab2019-12-18 15:34:32 +09003772func TestJavaSDKLibrary(t *testing.T) {
3773 ctx, _ := testApex(t, `
3774 apex {
3775 name: "myapex",
3776 key: "myapex.key",
3777 java_libs: ["foo"],
3778 }
3779
3780 apex_key {
3781 name: "myapex.key",
3782 public_key: "testkey.avbpubkey",
3783 private_key: "testkey.pem",
3784 }
3785
3786 java_sdk_library {
3787 name: "foo",
3788 srcs: ["a.java"],
3789 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003790 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003791 }
3792 `, withFiles(map[string][]byte{
3793 "api/current.txt": nil,
3794 "api/removed.txt": nil,
3795 "api/system-current.txt": nil,
3796 "api/system-removed.txt": nil,
3797 "api/test-current.txt": nil,
3798 "api/test-removed.txt": nil,
3799 }))
3800
3801 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003802 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003803 "javalib/foo.jar",
3804 "etc/permissions/foo.xml",
3805 })
3806 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003807 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3808 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003809}
3810
atrost6e126252020-01-27 17:01:16 +00003811func TestCompatConfig(t *testing.T) {
3812 ctx, _ := testApex(t, `
3813 apex {
3814 name: "myapex",
3815 key: "myapex.key",
3816 prebuilts: ["myjar-platform-compat-config"],
3817 java_libs: ["myjar"],
3818 }
3819
3820 apex_key {
3821 name: "myapex.key",
3822 public_key: "testkey.avbpubkey",
3823 private_key: "testkey.pem",
3824 }
3825
3826 platform_compat_config {
3827 name: "myjar-platform-compat-config",
3828 src: ":myjar",
3829 }
3830
3831 java_library {
3832 name: "myjar",
3833 srcs: ["foo/bar/MyClass.java"],
3834 sdk_version: "none",
3835 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003836 apex_available: [ "myapex" ],
3837 }
3838 `)
3839 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3840 "etc/compatconfig/myjar-platform-compat-config.xml",
3841 "javalib/myjar.jar",
3842 })
3843}
3844
Jiyong Park479321d2019-12-16 11:47:12 +09003845func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3846 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3847 apex {
3848 name: "myapex",
3849 key: "myapex.key",
3850 java_libs: ["myjar"],
3851 }
3852
3853 apex_key {
3854 name: "myapex.key",
3855 public_key: "testkey.avbpubkey",
3856 private_key: "testkey.pem",
3857 }
3858
3859 java_library {
3860 name: "myjar",
3861 srcs: ["foo/bar/MyClass.java"],
3862 sdk_version: "none",
3863 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003864 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003865 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003866 }
3867 `)
3868}
3869
Jiyong Park7afd1072019-12-30 16:56:33 +09003870func TestCarryRequiredModuleNames(t *testing.T) {
3871 ctx, config := testApex(t, `
3872 apex {
3873 name: "myapex",
3874 key: "myapex.key",
3875 native_shared_libs: ["mylib"],
3876 }
3877
3878 apex_key {
3879 name: "myapex.key",
3880 public_key: "testkey.avbpubkey",
3881 private_key: "testkey.pem",
3882 }
3883
3884 cc_library {
3885 name: "mylib",
3886 srcs: ["mylib.cpp"],
3887 system_shared_libs: [],
3888 stl: "none",
3889 required: ["a", "b"],
3890 host_required: ["c", "d"],
3891 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003892 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003893 }
3894 `)
3895
3896 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3897 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3898 name := apexBundle.BaseModuleName()
3899 prefix := "TARGET_"
3900 var builder strings.Builder
3901 data.Custom(&builder, name, prefix, "", data)
3902 androidMk := builder.String()
3903 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3904 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3905 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3906}
3907
Jiyong Park7cd10e32020-01-14 09:22:18 +09003908func TestSymlinksFromApexToSystem(t *testing.T) {
3909 bp := `
3910 apex {
3911 name: "myapex",
3912 key: "myapex.key",
3913 native_shared_libs: ["mylib"],
3914 java_libs: ["myjar"],
3915 }
3916
Jiyong Park9d677202020-02-19 16:29:35 +09003917 apex {
3918 name: "myapex.updatable",
3919 key: "myapex.key",
3920 native_shared_libs: ["mylib"],
3921 java_libs: ["myjar"],
3922 updatable: true,
3923 }
3924
Jiyong Park7cd10e32020-01-14 09:22:18 +09003925 apex_key {
3926 name: "myapex.key",
3927 public_key: "testkey.avbpubkey",
3928 private_key: "testkey.pem",
3929 }
3930
3931 cc_library {
3932 name: "mylib",
3933 srcs: ["mylib.cpp"],
3934 shared_libs: ["myotherlib"],
3935 system_shared_libs: [],
3936 stl: "none",
3937 apex_available: [
3938 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003939 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003940 "//apex_available:platform",
3941 ],
3942 }
3943
3944 cc_library {
3945 name: "myotherlib",
3946 srcs: ["mylib.cpp"],
3947 system_shared_libs: [],
3948 stl: "none",
3949 apex_available: [
3950 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003951 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003952 "//apex_available:platform",
3953 ],
3954 }
3955
3956 java_library {
3957 name: "myjar",
3958 srcs: ["foo/bar/MyClass.java"],
3959 sdk_version: "none",
3960 system_modules: "none",
3961 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003962 apex_available: [
3963 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003964 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003965 "//apex_available:platform",
3966 ],
3967 }
3968
3969 java_library {
3970 name: "myotherjar",
3971 srcs: ["foo/bar/MyClass.java"],
3972 sdk_version: "none",
3973 system_modules: "none",
3974 apex_available: [
3975 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003976 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003977 "//apex_available:platform",
3978 ],
3979 }
3980 `
3981
3982 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3983 for _, f := range files {
3984 if f.path == file {
3985 if f.isLink {
3986 t.Errorf("%q is not a real file", file)
3987 }
3988 return
3989 }
3990 }
3991 t.Errorf("%q is not found", file)
3992 }
3993
3994 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3995 for _, f := range files {
3996 if f.path == file {
3997 if !f.isLink {
3998 t.Errorf("%q is not a symlink", file)
3999 }
4000 return
4001 }
4002 }
4003 t.Errorf("%q is not found", file)
4004 }
4005
Jiyong Park9d677202020-02-19 16:29:35 +09004006 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4007 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004008 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004009 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004010 ensureRealfileExists(t, files, "javalib/myjar.jar")
4011 ensureRealfileExists(t, files, "lib64/mylib.so")
4012 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4013
Jiyong Park9d677202020-02-19 16:29:35 +09004014 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4015 ensureRealfileExists(t, files, "javalib/myjar.jar")
4016 ensureRealfileExists(t, files, "lib64/mylib.so")
4017 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4018
4019 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004020 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004021 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004022 ensureRealfileExists(t, files, "javalib/myjar.jar")
4023 ensureRealfileExists(t, files, "lib64/mylib.so")
4024 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004025
4026 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4027 ensureRealfileExists(t, files, "javalib/myjar.jar")
4028 ensureRealfileExists(t, files, "lib64/mylib.so")
4029 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004030}
4031
Jooyung Han643adc42020-02-27 13:50:06 +09004032func TestApexWithJniLibs(t *testing.T) {
4033 ctx, _ := testApex(t, `
4034 apex {
4035 name: "myapex",
4036 key: "myapex.key",
4037 jni_libs: ["mylib"],
4038 }
4039
4040 apex_key {
4041 name: "myapex.key",
4042 public_key: "testkey.avbpubkey",
4043 private_key: "testkey.pem",
4044 }
4045
4046 cc_library {
4047 name: "mylib",
4048 srcs: ["mylib.cpp"],
4049 shared_libs: ["mylib2"],
4050 system_shared_libs: [],
4051 stl: "none",
4052 apex_available: [ "myapex" ],
4053 }
4054
4055 cc_library {
4056 name: "mylib2",
4057 srcs: ["mylib.cpp"],
4058 system_shared_libs: [],
4059 stl: "none",
4060 apex_available: [ "myapex" ],
4061 }
4062 `)
4063
4064 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
4065 // Notice mylib2.so (transitive dep) is not added as a jni_lib
4066 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
4067 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4068 "lib64/mylib.so",
4069 "lib64/mylib2.so",
4070 })
4071}
4072
4073func TestApexWithJniLibs_Errors(t *testing.T) {
4074 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
4075 apex {
4076 name: "myapex",
4077 key: "myapex.key",
4078 jni_libs: ["xxx"],
4079 }
4080
4081 apex_key {
4082 name: "myapex.key",
4083 public_key: "testkey.avbpubkey",
4084 private_key: "testkey.pem",
4085 }
4086
4087 prebuilt_etc {
4088 name: "xxx",
4089 src: "xxx",
4090 }
4091 `, withFiles(map[string][]byte{
4092 "xxx": nil,
4093 }))
4094}
4095
Jiyong Parkbd159612020-02-28 15:22:21 +09004096func TestAppBundle(t *testing.T) {
4097 ctx, _ := testApex(t, `
4098 apex {
4099 name: "myapex",
4100 key: "myapex.key",
4101 apps: ["AppFoo"],
4102 }
4103
4104 apex_key {
4105 name: "myapex.key",
4106 public_key: "testkey.avbpubkey",
4107 private_key: "testkey.pem",
4108 }
4109
4110 android_app {
4111 name: "AppFoo",
4112 srcs: ["foo/bar/MyClass.java"],
4113 sdk_version: "none",
4114 system_modules: "none",
4115 apex_available: [ "myapex" ],
4116 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004117 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09004118
4119 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4120 content := bundleConfigRule.Args["content"]
4121
4122 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004123 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo/AppFoo.apk"}]}`)
Jiyong Parkbd159612020-02-28 15:22:21 +09004124}
4125
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004126func TestMain(m *testing.M) {
4127 run := func() int {
4128 setUp()
4129 defer tearDown()
4130
4131 return m.Run()
4132 }
4133
4134 os.Exit(run())
4135}