blob: 21d517fc63cf3d33bec860e92e648676c663febb [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 Han380fc362020-03-13 15:23:36 +0900861func TestApexDependsOnLLNDKTransitively(t *testing.T) {
862 testcases := []struct {
863 name string
864 minSdkVersion string
865 shouldLink string
866 shouldNotLink []string
867 }{
868 {
869 name: "should link to test latest",
870 minSdkVersion: "current",
871 shouldLink: "30",
872 shouldNotLink: []string{"29"},
873 },
874 {
875 name: "should link to llndk#29",
876 minSdkVersion: "29",
877 shouldLink: "29",
878 shouldNotLink: []string{"30"},
879 },
880 }
881 for _, tc := range testcases {
882 t.Run(tc.name, func(t *testing.T) {
883 ctx, _ := testApex(t, `
884 apex {
885 name: "myapex",
886 key: "myapex.key",
887 use_vendor: true,
888 native_shared_libs: ["mylib"],
889 min_sdk_version: "`+tc.minSdkVersion+`",
890 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900891
Jooyung Han380fc362020-03-13 15:23:36 +0900892 apex_key {
893 name: "myapex.key",
894 public_key: "testkey.avbpubkey",
895 private_key: "testkey.pem",
896 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900897
Jooyung Han380fc362020-03-13 15:23:36 +0900898 cc_library {
899 name: "mylib",
900 srcs: ["mylib.cpp"],
901 vendor_available: true,
902 shared_libs: ["libbar"],
903 system_shared_libs: [],
904 stl: "none",
905 apex_available: [ "myapex" ],
906 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900907
Jooyung Han380fc362020-03-13 15:23:36 +0900908 cc_library {
909 name: "libbar",
910 srcs: ["mylib.cpp"],
911 system_shared_libs: [],
912 stl: "none",
913 stubs: { versions: ["29","30"] },
914 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900915
Jooyung Han380fc362020-03-13 15:23:36 +0900916 llndk_library {
917 name: "libbar",
918 symbol_file: "",
919 }
920 `, func(fs map[string][]byte, config android.Config) {
921 setUseVendorWhitelistForTest(config, []string{"myapex"})
922 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +0900923
Jooyung Han380fc362020-03-13 15:23:36 +0900924 // Ensure that LLNDK dep is not included
925 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
926 "lib64/mylib.so",
927 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900928
Jooyung Han380fc362020-03-13 15:23:36 +0900929 // Ensure that LLNDK dep is required
930 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
931 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
932 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900933
Jooyung Han380fc362020-03-13 15:23:36 +0900934 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
935 ensureContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
936 for _, ver := range tc.shouldNotLink {
937 ensureNotContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
938 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900939
Jooyung Han380fc362020-03-13 15:23:36 +0900940 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
941 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
942 })
943 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900944}
945
Jiyong Park25fc6a92018-11-18 18:02:45 +0900946func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700947 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900948 apex {
949 name: "myapex",
950 key: "myapex.key",
951 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
952 }
953
954 apex_key {
955 name: "myapex.key",
956 public_key: "testkey.avbpubkey",
957 private_key: "testkey.pem",
958 }
959
960 cc_library {
961 name: "mylib",
962 srcs: ["mylib.cpp"],
963 shared_libs: ["libdl#27"],
964 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000965 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900966 }
967
968 cc_library_shared {
969 name: "mylib_shared",
970 srcs: ["mylib.cpp"],
971 shared_libs: ["libdl#27"],
972 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000973 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900974 }
975
976 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900977 name: "libBootstrap",
978 srcs: ["mylib.cpp"],
979 stl: "none",
980 bootstrap: true,
981 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900982 `)
983
Sundong Ahnabb64432019-10-22 13:58:29 +0900984 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900985 copyCmds := apexRule.Args["copy_commands"]
986
987 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800988 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900989 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
990 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900991
992 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900993 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900994
Colin Cross7113d202019-11-20 16:39:12 -0800995 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
996 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
997 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900998
999 // For dependency to libc
1000 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001001 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001002 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001003 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001004 // ... Cflags from stub is correctly exported to mylib
1005 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1006 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1007
1008 // For dependency to libm
1009 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001010 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001012 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001013 // ... and is not compiling with the stub
1014 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1015 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1016
1017 // For dependency to libdl
1018 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001019 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001020 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001021 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1022 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001023 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001024 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001025 // ... Cflags from stub is correctly exported to mylib
1026 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1027 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001028
1029 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001030 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1031 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1032 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1033 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001034}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001035
Jooyung Han03b51852020-02-26 22:45:42 +09001036func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
1037 // there are three links between liba --> libz
1038 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
1039 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
1040 // 3) (platform) -> liba -> libz : this should be non-stub link
1041 ctx, _ := testApex(t, `
1042 apex {
1043 name: "myapex",
1044 key: "myapex.key",
1045 native_shared_libs: ["libx"],
1046 min_sdk_version: "2",
1047 }
1048
1049 apex {
1050 name: "otherapex",
1051 key: "myapex.key",
1052 native_shared_libs: ["liby"],
1053 min_sdk_version: "3",
1054 }
1055
1056 apex_key {
1057 name: "myapex.key",
1058 public_key: "testkey.avbpubkey",
1059 private_key: "testkey.pem",
1060 }
1061
1062 cc_library {
1063 name: "libx",
1064 shared_libs: ["liba"],
1065 system_shared_libs: [],
1066 stl: "none",
1067 apex_available: [ "myapex" ],
1068 }
1069
1070 cc_library {
1071 name: "liby",
1072 shared_libs: ["liba"],
1073 system_shared_libs: [],
1074 stl: "none",
1075 apex_available: [ "otherapex" ],
1076 }
1077
1078 cc_library {
1079 name: "liba",
1080 shared_libs: ["libz"],
1081 system_shared_libs: [],
1082 stl: "none",
1083 apex_available: [
1084 "//apex_available:anyapex",
1085 "//apex_available:platform",
1086 ],
1087 }
1088
1089 cc_library {
1090 name: "libz",
1091 system_shared_libs: [],
1092 stl: "none",
1093 stubs: {
1094 versions: ["1", "3"],
1095 },
1096 }
1097 `, withUnbundledBuild)
1098
1099 expectLink := func(from, from_variant, to, to_variant string) {
1100 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1101 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1102 }
1103 expectNoLink := func(from, from_variant, to, to_variant string) {
1104 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1105 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1106 }
1107 // platform liba is linked to non-stub version
1108 expectLink("liba", "shared", "libz", "shared")
1109 // liba in myapex is linked to #1
1110 expectLink("liba", "shared_myapex", "libz", "shared_1")
1111 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1112 expectNoLink("liba", "shared_myapex", "libz", "shared")
1113 // liba in otherapex is linked to #3
1114 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1115 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1116 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1117}
1118
1119func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1120 ctx, _ := testApex(t, `
1121 apex {
1122 name: "myapex",
1123 key: "myapex.key",
1124 native_shared_libs: ["libx"],
1125 }
1126
1127 apex_key {
1128 name: "myapex.key",
1129 public_key: "testkey.avbpubkey",
1130 private_key: "testkey.pem",
1131 }
1132
1133 cc_library {
1134 name: "libx",
1135 shared_libs: ["libz"],
1136 system_shared_libs: [],
1137 stl: "none",
1138 apex_available: [ "myapex" ],
1139 }
1140
1141 cc_library {
1142 name: "libz",
1143 system_shared_libs: [],
1144 stl: "none",
1145 stubs: {
1146 versions: ["1", "2"],
1147 },
1148 }
1149 `)
1150
1151 expectLink := func(from, from_variant, to, to_variant string) {
1152 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1153 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1154 }
1155 expectNoLink := func(from, from_variant, to, to_variant string) {
1156 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1157 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1158 }
1159 expectLink("libx", "shared_myapex", "libz", "shared_2")
1160 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1161 expectNoLink("libx", "shared_myapex", "libz", "shared")
1162}
1163
1164func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1165 ctx, _ := testApex(t, `
1166 apex {
1167 name: "myapex",
1168 key: "myapex.key",
1169 native_shared_libs: ["libx"],
1170 }
1171
1172 apex_key {
1173 name: "myapex.key",
1174 public_key: "testkey.avbpubkey",
1175 private_key: "testkey.pem",
1176 }
1177
1178 cc_library {
1179 name: "libx",
1180 system_shared_libs: [],
1181 stl: "none",
1182 apex_available: [ "myapex" ],
1183 stubs: {
1184 versions: ["1", "2"],
1185 },
1186 }
1187
1188 cc_library {
1189 name: "libz",
1190 shared_libs: ["libx"],
1191 system_shared_libs: [],
1192 stl: "none",
1193 }
1194 `)
1195
1196 expectLink := func(from, from_variant, to, to_variant string) {
1197 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1198 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1199 }
1200 expectNoLink := func(from, from_variant, to, to_variant string) {
1201 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1202 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1203 }
1204 expectLink("libz", "shared", "libx", "shared_2")
1205 expectNoLink("libz", "shared", "libz", "shared_1")
1206 expectNoLink("libz", "shared", "libz", "shared")
1207}
1208
1209func TestQApexesUseLatestStubsInBundledBuilds(t *testing.T) {
1210 ctx, _ := testApex(t, `
1211 apex {
1212 name: "myapex",
1213 key: "myapex.key",
1214 native_shared_libs: ["libx"],
1215 min_sdk_version: "29",
1216 }
1217
1218 apex_key {
1219 name: "myapex.key",
1220 public_key: "testkey.avbpubkey",
1221 private_key: "testkey.pem",
1222 }
1223
1224 cc_library {
1225 name: "libx",
1226 shared_libs: ["libbar"],
1227 apex_available: [ "myapex" ],
1228 }
1229
1230 cc_library {
1231 name: "libbar",
1232 stubs: {
1233 versions: ["29", "30"],
1234 },
1235 }
1236 `)
1237 expectLink := func(from, from_variant, to, to_variant string) {
1238 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1239 libFlags := ld.Args["libFlags"]
1240 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1241 }
1242 expectLink("libx", "shared_myapex", "libbar", "shared_30")
1243}
1244
1245func TestQTargetApexUseStaticUnwinder(t *testing.T) {
1246 ctx, _ := testApex(t, `
1247 apex {
1248 name: "myapex",
1249 key: "myapex.key",
1250 native_shared_libs: ["libx"],
1251 min_sdk_version: "29",
1252 }
1253
1254 apex_key {
1255 name: "myapex.key",
1256 public_key: "testkey.avbpubkey",
1257 private_key: "testkey.pem",
1258 }
1259
1260 cc_library {
1261 name: "libx",
1262 apex_available: [ "myapex" ],
1263 }
1264
1265 `, withUnbundledBuild)
1266
1267 // ensure apex variant of c++ is linked with static unwinder
1268 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1269 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1270 // note that platform variant is not.
1271 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1272 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Han03b51852020-02-26 22:45:42 +09001273}
1274
1275func TestInvalidMinSdkVersion(t *testing.T) {
1276 testApexError(t, `"libz" .*: min_sdk_version is set 29.*`, `
1277 apex {
1278 name: "myapex",
1279 key: "myapex.key",
1280 native_shared_libs: ["libx"],
1281 min_sdk_version: "29",
1282 }
1283
1284 apex_key {
1285 name: "myapex.key",
1286 public_key: "testkey.avbpubkey",
1287 private_key: "testkey.pem",
1288 }
1289
1290 cc_library {
1291 name: "libx",
1292 shared_libs: ["libz"],
1293 system_shared_libs: [],
1294 stl: "none",
1295 apex_available: [ "myapex" ],
1296 }
1297
1298 cc_library {
1299 name: "libz",
1300 system_shared_libs: [],
1301 stl: "none",
1302 stubs: {
1303 versions: ["30"],
1304 },
1305 }
1306 `, withUnbundledBuild)
1307
1308 testApexError(t, `"myapex" .*: min_sdk_version: should be .*`, `
1309 apex {
1310 name: "myapex",
1311 key: "myapex.key",
1312 min_sdk_version: "R",
1313 }
1314
1315 apex_key {
1316 name: "myapex.key",
1317 public_key: "testkey.avbpubkey",
1318 private_key: "testkey.pem",
1319 }
1320 `)
1321}
1322
Jiyong Park7c2ee712018-12-07 00:42:25 +09001323func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001324 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001325 apex {
1326 name: "myapex",
1327 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001328 native_shared_libs: ["mylib"],
1329 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001330 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001331 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001332 }
1333
1334 apex_key {
1335 name: "myapex.key",
1336 public_key: "testkey.avbpubkey",
1337 private_key: "testkey.pem",
1338 }
1339
1340 prebuilt_etc {
1341 name: "myetc",
1342 src: "myprebuilt",
1343 sub_dir: "foo/bar",
1344 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001345
1346 cc_library {
1347 name: "mylib",
1348 srcs: ["mylib.cpp"],
1349 relative_install_path: "foo/bar",
1350 system_shared_libs: [],
1351 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001352 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001353 }
1354
1355 cc_binary {
1356 name: "mybin",
1357 srcs: ["mylib.cpp"],
1358 relative_install_path: "foo/bar",
1359 system_shared_libs: [],
1360 static_executable: true,
1361 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001362 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001363 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001364 `)
1365
Sundong Ahnabb64432019-10-22 13:58:29 +09001366 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001367 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1368
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001369 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001370 ensureListContains(t, dirs, "etc")
1371 ensureListContains(t, dirs, "etc/foo")
1372 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001373 ensureListContains(t, dirs, "lib64")
1374 ensureListContains(t, dirs, "lib64/foo")
1375 ensureListContains(t, dirs, "lib64/foo/bar")
1376 ensureListContains(t, dirs, "lib")
1377 ensureListContains(t, dirs, "lib/foo")
1378 ensureListContains(t, dirs, "lib/foo/bar")
1379
Jiyong Parkbd13e442019-03-15 18:10:35 +09001380 ensureListContains(t, dirs, "bin")
1381 ensureListContains(t, dirs, "bin/foo")
1382 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001383}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001384
Jooyung Han35155c42020-02-06 17:33:20 +09001385func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
1386 ctx, _ := testApex(t, `
1387 apex {
1388 name: "myapex",
1389 key: "myapex.key",
1390 multilib: {
1391 both: {
1392 native_shared_libs: ["mylib"],
1393 binaries: ["mybin"],
1394 },
1395 },
1396 compile_multilib: "both",
1397 native_bridge_supported: true,
1398 }
1399
1400 apex_key {
1401 name: "myapex.key",
1402 public_key: "testkey.avbpubkey",
1403 private_key: "testkey.pem",
1404 }
1405
1406 cc_library {
1407 name: "mylib",
1408 relative_install_path: "foo/bar",
1409 system_shared_libs: [],
1410 stl: "none",
1411 apex_available: [ "myapex" ],
1412 native_bridge_supported: true,
1413 }
1414
1415 cc_binary {
1416 name: "mybin",
1417 relative_install_path: "foo/bar",
1418 system_shared_libs: [],
1419 static_executable: true,
1420 stl: "none",
1421 apex_available: [ "myapex" ],
1422 native_bridge_supported: true,
1423 compile_multilib: "both", // default is "first" for binary
1424 multilib: {
1425 lib64: {
1426 suffix: "64",
1427 },
1428 },
1429 }
1430 `, withNativeBridgeEnabled)
1431 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1432 "bin/foo/bar/mybin",
1433 "bin/foo/bar/mybin64",
1434 "bin/arm/foo/bar/mybin",
1435 "bin/arm64/foo/bar/mybin64",
1436 "lib/foo/bar/mylib.so",
1437 "lib/arm/foo/bar/mylib.so",
1438 "lib64/foo/bar/mylib.so",
1439 "lib64/arm64/foo/bar/mylib.so",
1440 })
1441}
1442
Jiyong Parkda6eb592018-12-19 17:12:36 +09001443func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001444 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001445 apex {
1446 name: "myapex",
1447 key: "myapex.key",
1448 native_shared_libs: ["mylib"],
1449 use_vendor: true,
1450 }
1451
1452 apex_key {
1453 name: "myapex.key",
1454 public_key: "testkey.avbpubkey",
1455 private_key: "testkey.pem",
1456 }
1457
1458 cc_library {
1459 name: "mylib",
1460 srcs: ["mylib.cpp"],
1461 shared_libs: ["mylib2"],
1462 system_shared_libs: [],
1463 vendor_available: true,
1464 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001465 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001466 }
1467
1468 cc_library {
1469 name: "mylib2",
1470 srcs: ["mylib.cpp"],
1471 system_shared_libs: [],
1472 vendor_available: true,
1473 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001474 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001475 }
Jooyung Handc782442019-11-01 03:14:38 +09001476 `, func(fs map[string][]byte, config android.Config) {
1477 setUseVendorWhitelistForTest(config, []string{"myapex"})
1478 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001479
1480 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001481 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001482 for _, implicit := range i.Implicits {
1483 inputsList = append(inputsList, implicit.String())
1484 }
1485 }
1486 inputsString := strings.Join(inputsList, " ")
1487
1488 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001489 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1490 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001491
1492 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001493 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1494 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001495}
Jiyong Park16e91a02018-12-20 18:18:08 +09001496
Jooyung Handc782442019-11-01 03:14:38 +09001497func TestUseVendorRestriction(t *testing.T) {
1498 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1499 apex {
1500 name: "myapex",
1501 key: "myapex.key",
1502 use_vendor: true,
1503 }
1504 apex_key {
1505 name: "myapex.key",
1506 public_key: "testkey.avbpubkey",
1507 private_key: "testkey.pem",
1508 }
1509 `, func(fs map[string][]byte, config android.Config) {
1510 setUseVendorWhitelistForTest(config, []string{""})
1511 })
1512 // no error with whitelist
1513 testApex(t, `
1514 apex {
1515 name: "myapex",
1516 key: "myapex.key",
1517 use_vendor: true,
1518 }
1519 apex_key {
1520 name: "myapex.key",
1521 public_key: "testkey.avbpubkey",
1522 private_key: "testkey.pem",
1523 }
1524 `, func(fs map[string][]byte, config android.Config) {
1525 setUseVendorWhitelistForTest(config, []string{"myapex"})
1526 })
1527}
1528
Jooyung Han5c998b92019-06-27 11:30:33 +09001529func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1530 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1531 apex {
1532 name: "myapex",
1533 key: "myapex.key",
1534 native_shared_libs: ["mylib"],
1535 use_vendor: true,
1536 }
1537
1538 apex_key {
1539 name: "myapex.key",
1540 public_key: "testkey.avbpubkey",
1541 private_key: "testkey.pem",
1542 }
1543
1544 cc_library {
1545 name: "mylib",
1546 srcs: ["mylib.cpp"],
1547 system_shared_libs: [],
1548 stl: "none",
1549 }
1550 `)
1551}
1552
Jiyong Park16e91a02018-12-20 18:18:08 +09001553func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001554 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001555 apex {
1556 name: "myapex",
1557 key: "myapex.key",
1558 native_shared_libs: ["mylib"],
1559 }
1560
1561 apex_key {
1562 name: "myapex.key",
1563 public_key: "testkey.avbpubkey",
1564 private_key: "testkey.pem",
1565 }
1566
1567 cc_library {
1568 name: "mylib",
1569 srcs: ["mylib.cpp"],
1570 system_shared_libs: [],
1571 stl: "none",
1572 stubs: {
1573 versions: ["1", "2", "3"],
1574 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001575 apex_available: [
1576 "//apex_available:platform",
1577 "myapex",
1578 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001579 }
1580
1581 cc_binary {
1582 name: "not_in_apex",
1583 srcs: ["mylib.cpp"],
1584 static_libs: ["mylib"],
1585 static_executable: true,
1586 system_shared_libs: [],
1587 stl: "none",
1588 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001589 `)
1590
Colin Cross7113d202019-11-20 16:39:12 -08001591 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001592
1593 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001594 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001595}
Jiyong Park9335a262018-12-24 11:31:58 +09001596
1597func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001598 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001599 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001600 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001601 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001602 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001603 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001604 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001605 }
1606
1607 cc_library {
1608 name: "mylib",
1609 srcs: ["mylib.cpp"],
1610 system_shared_libs: [],
1611 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001612 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001613 }
1614
1615 apex_key {
1616 name: "myapex.key",
1617 public_key: "testkey.avbpubkey",
1618 private_key: "testkey.pem",
1619 }
1620
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001621 android_app_certificate {
1622 name: "myapex.certificate",
1623 certificate: "testkey",
1624 }
1625
1626 android_app_certificate {
1627 name: "myapex.certificate.override",
1628 certificate: "testkey.override",
1629 }
1630
Jiyong Park9335a262018-12-24 11:31:58 +09001631 `)
1632
1633 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001634 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001635
1636 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1637 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1638 "vendor/foo/devkeys/testkey.avbpubkey")
1639 }
1640 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1641 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1642 "vendor/foo/devkeys/testkey.pem")
1643 }
1644
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001645 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001646 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001647 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001648 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001649 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001650 }
1651}
Jiyong Park58e364a2019-01-19 19:24:06 +09001652
Jooyung Hanf121a652019-12-17 14:30:11 +09001653func TestCertificate(t *testing.T) {
1654 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1655 ctx, _ := testApex(t, `
1656 apex {
1657 name: "myapex",
1658 key: "myapex.key",
1659 }
1660 apex_key {
1661 name: "myapex.key",
1662 public_key: "testkey.avbpubkey",
1663 private_key: "testkey.pem",
1664 }`)
1665 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1666 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1667 if actual := rule.Args["certificates"]; actual != expected {
1668 t.Errorf("certificates should be %q, not %q", expected, actual)
1669 }
1670 })
1671 t.Run("override when unspecified", func(t *testing.T) {
1672 ctx, _ := testApex(t, `
1673 apex {
1674 name: "myapex_keytest",
1675 key: "myapex.key",
1676 file_contexts: ":myapex-file_contexts",
1677 }
1678 apex_key {
1679 name: "myapex.key",
1680 public_key: "testkey.avbpubkey",
1681 private_key: "testkey.pem",
1682 }
1683 android_app_certificate {
1684 name: "myapex.certificate.override",
1685 certificate: "testkey.override",
1686 }`)
1687 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1688 expected := "testkey.override.x509.pem testkey.override.pk8"
1689 if actual := rule.Args["certificates"]; actual != expected {
1690 t.Errorf("certificates should be %q, not %q", expected, actual)
1691 }
1692 })
1693 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1694 ctx, _ := testApex(t, `
1695 apex {
1696 name: "myapex",
1697 key: "myapex.key",
1698 certificate: ":myapex.certificate",
1699 }
1700 apex_key {
1701 name: "myapex.key",
1702 public_key: "testkey.avbpubkey",
1703 private_key: "testkey.pem",
1704 }
1705 android_app_certificate {
1706 name: "myapex.certificate",
1707 certificate: "testkey",
1708 }`)
1709 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1710 expected := "testkey.x509.pem testkey.pk8"
1711 if actual := rule.Args["certificates"]; actual != expected {
1712 t.Errorf("certificates should be %q, not %q", expected, actual)
1713 }
1714 })
1715 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1716 ctx, _ := testApex(t, `
1717 apex {
1718 name: "myapex_keytest",
1719 key: "myapex.key",
1720 file_contexts: ":myapex-file_contexts",
1721 certificate: ":myapex.certificate",
1722 }
1723 apex_key {
1724 name: "myapex.key",
1725 public_key: "testkey.avbpubkey",
1726 private_key: "testkey.pem",
1727 }
1728 android_app_certificate {
1729 name: "myapex.certificate.override",
1730 certificate: "testkey.override",
1731 }`)
1732 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1733 expected := "testkey.override.x509.pem testkey.override.pk8"
1734 if actual := rule.Args["certificates"]; actual != expected {
1735 t.Errorf("certificates should be %q, not %q", expected, actual)
1736 }
1737 })
1738 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1739 ctx, _ := testApex(t, `
1740 apex {
1741 name: "myapex",
1742 key: "myapex.key",
1743 certificate: "testkey",
1744 }
1745 apex_key {
1746 name: "myapex.key",
1747 public_key: "testkey.avbpubkey",
1748 private_key: "testkey.pem",
1749 }`)
1750 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1751 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1752 if actual := rule.Args["certificates"]; actual != expected {
1753 t.Errorf("certificates should be %q, not %q", expected, actual)
1754 }
1755 })
1756 t.Run("override when specified as <name>", func(t *testing.T) {
1757 ctx, _ := testApex(t, `
1758 apex {
1759 name: "myapex_keytest",
1760 key: "myapex.key",
1761 file_contexts: ":myapex-file_contexts",
1762 certificate: "testkey",
1763 }
1764 apex_key {
1765 name: "myapex.key",
1766 public_key: "testkey.avbpubkey",
1767 private_key: "testkey.pem",
1768 }
1769 android_app_certificate {
1770 name: "myapex.certificate.override",
1771 certificate: "testkey.override",
1772 }`)
1773 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1774 expected := "testkey.override.x509.pem testkey.override.pk8"
1775 if actual := rule.Args["certificates"]; actual != expected {
1776 t.Errorf("certificates should be %q, not %q", expected, actual)
1777 }
1778 })
1779}
1780
Jiyong Park58e364a2019-01-19 19:24:06 +09001781func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001782 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001783 apex {
1784 name: "myapex",
1785 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001786 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001787 }
1788
1789 apex {
1790 name: "otherapex",
1791 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001792 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09001793 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09001794 }
1795
1796 apex_key {
1797 name: "myapex.key",
1798 public_key: "testkey.avbpubkey",
1799 private_key: "testkey.pem",
1800 }
1801
1802 cc_library {
1803 name: "mylib",
1804 srcs: ["mylib.cpp"],
1805 system_shared_libs: [],
1806 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001807 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001808 "myapex",
1809 "otherapex",
1810 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001811 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09001812 cc_library {
1813 name: "mylib2",
1814 srcs: ["mylib.cpp"],
1815 system_shared_libs: [],
1816 stl: "none",
1817 apex_available: [
1818 "myapex",
1819 "otherapex",
1820 ],
1821 use_apex_name_macro: true,
1822 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001823 `)
1824
Jooyung Hanc87a0592020-03-02 17:44:33 +09001825 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001826 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001827 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001828 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001829
Jooyung Hanccce2f22020-03-07 03:45:53 +09001830 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001831 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1832 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001833 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09001834 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001835
Jooyung Hanccce2f22020-03-07 03:45:53 +09001836 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001837 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1838 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001839 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09001840 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001841
Jooyung Hanc87a0592020-03-02 17:44:33 +09001842 // When cc_library sets use_apex_name_macro: true
1843 // apex variants define additional macro to distinguish which apex variant it is built for
1844
1845 // non-APEX variant does not have __ANDROID_APEX__ defined
1846 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1847 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1848
1849 // APEX variant has __ANDROID_APEX__ defined
1850 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001851 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001852 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1853 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001854
Jooyung Hanc87a0592020-03-02 17:44:33 +09001855 // APEX variant has __ANDROID_APEX__ defined
1856 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001857 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001858 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1859 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001860}
Jiyong Park7e636d02019-01-28 16:16:54 +09001861
1862func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001863 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001864 apex {
1865 name: "myapex",
1866 key: "myapex.key",
1867 native_shared_libs: ["mylib"],
1868 }
1869
1870 apex_key {
1871 name: "myapex.key",
1872 public_key: "testkey.avbpubkey",
1873 private_key: "testkey.pem",
1874 }
1875
1876 cc_library_headers {
1877 name: "mylib_headers",
1878 export_include_dirs: ["my_include"],
1879 system_shared_libs: [],
1880 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001881 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001882 }
1883
1884 cc_library {
1885 name: "mylib",
1886 srcs: ["mylib.cpp"],
1887 system_shared_libs: [],
1888 stl: "none",
1889 header_libs: ["mylib_headers"],
1890 export_header_lib_headers: ["mylib_headers"],
1891 stubs: {
1892 versions: ["1", "2", "3"],
1893 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001894 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001895 }
1896
1897 cc_library {
1898 name: "otherlib",
1899 srcs: ["mylib.cpp"],
1900 system_shared_libs: [],
1901 stl: "none",
1902 shared_libs: ["mylib"],
1903 }
1904 `)
1905
Colin Cross7113d202019-11-20 16:39:12 -08001906 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001907
1908 // Ensure that the include path of the header lib is exported to 'otherlib'
1909 ensureContains(t, cFlags, "-Imy_include")
1910}
Alex Light9670d332019-01-29 18:07:33 -08001911
Jiyong Park7cd10e32020-01-14 09:22:18 +09001912type fileInApex struct {
1913 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001914 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001915 isLink bool
1916}
1917
Jooyung Hana57af4a2020-01-23 05:36:59 +00001918func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001919 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001920 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001921 copyCmds := apexRule.Args["copy_commands"]
1922 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001923 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001924 for _, cmd := range strings.Split(copyCmds, "&&") {
1925 cmd = strings.TrimSpace(cmd)
1926 if cmd == "" {
1927 continue
1928 }
1929 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001930 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001931 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001932 switch terms[0] {
1933 case "mkdir":
1934 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001935 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001936 t.Fatal("copyCmds contains invalid cp command", cmd)
1937 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001938 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001939 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001940 isLink = false
1941 case "ln":
1942 if len(terms) != 3 && len(terms) != 4 {
1943 // ln LINK TARGET or ln -s LINK TARGET
1944 t.Fatal("copyCmds contains invalid ln command", cmd)
1945 }
1946 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001947 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001948 isLink = true
1949 default:
1950 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1951 }
1952 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001953 index := strings.Index(dst, imageApexDir)
1954 if index == -1 {
1955 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1956 }
1957 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001958 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001959 }
1960 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001961 return ret
1962}
1963
Jooyung Hana57af4a2020-01-23 05:36:59 +00001964func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1965 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001966 var failed bool
1967 var surplus []string
1968 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001969 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001970 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001971 for _, expected := range files {
1972 if matched, _ := path.Match(expected, file.path); matched {
1973 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001974 mactchFound = true
1975 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001976 }
1977 }
Jooyung Hane6436d72020-02-27 13:31:56 +09001978 if !mactchFound {
1979 surplus = append(surplus, file.path)
1980 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001981 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001982
Jooyung Han31c470b2019-10-18 16:26:59 +09001983 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001984 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001985 t.Log("surplus files", surplus)
1986 failed = true
1987 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001988
1989 if len(files) > len(filesMatched) {
1990 var missing []string
1991 for _, expected := range files {
1992 if !filesMatched[expected] {
1993 missing = append(missing, expected)
1994 }
1995 }
1996 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001997 t.Log("missing files", missing)
1998 failed = true
1999 }
2000 if failed {
2001 t.Fail()
2002 }
2003}
2004
Jooyung Han344d5432019-08-23 11:17:39 +09002005func TestVndkApexCurrent(t *testing.T) {
2006 ctx, _ := testApex(t, `
2007 apex_vndk {
2008 name: "myapex",
2009 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002010 }
2011
2012 apex_key {
2013 name: "myapex.key",
2014 public_key: "testkey.avbpubkey",
2015 private_key: "testkey.pem",
2016 }
2017
2018 cc_library {
2019 name: "libvndk",
2020 srcs: ["mylib.cpp"],
2021 vendor_available: true,
2022 vndk: {
2023 enabled: true,
2024 },
2025 system_shared_libs: [],
2026 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002027 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002028 }
2029
2030 cc_library {
2031 name: "libvndksp",
2032 srcs: ["mylib.cpp"],
2033 vendor_available: true,
2034 vndk: {
2035 enabled: true,
2036 support_system_process: true,
2037 },
2038 system_shared_libs: [],
2039 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002040 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002041 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002042 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002043
Jooyung Hana57af4a2020-01-23 05:36:59 +00002044 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002045 "lib/libvndk.so",
2046 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002047 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002048 "lib64/libvndk.so",
2049 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002050 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002051 "etc/llndk.libraries.VER.txt",
2052 "etc/vndkcore.libraries.VER.txt",
2053 "etc/vndksp.libraries.VER.txt",
2054 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002055 })
Jooyung Han344d5432019-08-23 11:17:39 +09002056}
2057
2058func TestVndkApexWithPrebuilt(t *testing.T) {
2059 ctx, _ := testApex(t, `
2060 apex_vndk {
2061 name: "myapex",
2062 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002063 }
2064
2065 apex_key {
2066 name: "myapex.key",
2067 public_key: "testkey.avbpubkey",
2068 private_key: "testkey.pem",
2069 }
2070
2071 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002072 name: "libvndk",
2073 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002074 vendor_available: true,
2075 vndk: {
2076 enabled: true,
2077 },
2078 system_shared_libs: [],
2079 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002080 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002081 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002082
2083 cc_prebuilt_library_shared {
2084 name: "libvndk.arm",
2085 srcs: ["libvndk.arm.so"],
2086 vendor_available: true,
2087 vndk: {
2088 enabled: true,
2089 },
2090 enabled: false,
2091 arch: {
2092 arm: {
2093 enabled: true,
2094 },
2095 },
2096 system_shared_libs: [],
2097 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002098 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002099 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002100 `+vndkLibrariesTxtFiles("current"),
2101 withFiles(map[string][]byte{
2102 "libvndk.so": nil,
2103 "libvndk.arm.so": nil,
2104 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002105
Jooyung Hana57af4a2020-01-23 05:36:59 +00002106 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002107 "lib/libvndk.so",
2108 "lib/libvndk.arm.so",
2109 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002110 "lib/libc++.so",
2111 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002112 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002113 })
Jooyung Han344d5432019-08-23 11:17:39 +09002114}
2115
Jooyung Han39edb6c2019-11-06 16:53:07 +09002116func vndkLibrariesTxtFiles(vers ...string) (result string) {
2117 for _, v := range vers {
2118 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002119 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002120 result += `
2121 vndk_libraries_txt {
2122 name: "` + txt + `.libraries.txt",
2123 }
2124 `
2125 }
2126 } else {
2127 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2128 result += `
2129 prebuilt_etc {
2130 name: "` + txt + `.libraries.` + v + `.txt",
2131 src: "dummy.txt",
2132 }
2133 `
2134 }
2135 }
2136 }
2137 return
2138}
2139
Jooyung Han344d5432019-08-23 11:17:39 +09002140func TestVndkApexVersion(t *testing.T) {
2141 ctx, _ := testApex(t, `
2142 apex_vndk {
2143 name: "myapex_v27",
2144 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002145 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002146 vndk_version: "27",
2147 }
2148
2149 apex_key {
2150 name: "myapex.key",
2151 public_key: "testkey.avbpubkey",
2152 private_key: "testkey.pem",
2153 }
2154
Jooyung Han31c470b2019-10-18 16:26:59 +09002155 vndk_prebuilt_shared {
2156 name: "libvndk27",
2157 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002158 vendor_available: true,
2159 vndk: {
2160 enabled: true,
2161 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002162 target_arch: "arm64",
2163 arch: {
2164 arm: {
2165 srcs: ["libvndk27_arm.so"],
2166 },
2167 arm64: {
2168 srcs: ["libvndk27_arm64.so"],
2169 },
2170 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002171 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002172 }
2173
2174 vndk_prebuilt_shared {
2175 name: "libvndk27",
2176 version: "27",
2177 vendor_available: true,
2178 vndk: {
2179 enabled: true,
2180 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002181 target_arch: "x86_64",
2182 arch: {
2183 x86: {
2184 srcs: ["libvndk27_x86.so"],
2185 },
2186 x86_64: {
2187 srcs: ["libvndk27_x86_64.so"],
2188 },
2189 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002190 }
2191 `+vndkLibrariesTxtFiles("27"),
2192 withFiles(map[string][]byte{
2193 "libvndk27_arm.so": nil,
2194 "libvndk27_arm64.so": nil,
2195 "libvndk27_x86.so": nil,
2196 "libvndk27_x86_64.so": nil,
2197 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002198
Jooyung Hana57af4a2020-01-23 05:36:59 +00002199 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002200 "lib/libvndk27_arm.so",
2201 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002202 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002203 })
Jooyung Han344d5432019-08-23 11:17:39 +09002204}
2205
2206func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2207 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2208 apex_vndk {
2209 name: "myapex_v27",
2210 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002211 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002212 vndk_version: "27",
2213 }
2214 apex_vndk {
2215 name: "myapex_v27_other",
2216 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002217 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002218 vndk_version: "27",
2219 }
2220
2221 apex_key {
2222 name: "myapex.key",
2223 public_key: "testkey.avbpubkey",
2224 private_key: "testkey.pem",
2225 }
2226
2227 cc_library {
2228 name: "libvndk",
2229 srcs: ["mylib.cpp"],
2230 vendor_available: true,
2231 vndk: {
2232 enabled: true,
2233 },
2234 system_shared_libs: [],
2235 stl: "none",
2236 }
2237
2238 vndk_prebuilt_shared {
2239 name: "libvndk",
2240 version: "27",
2241 vendor_available: true,
2242 vndk: {
2243 enabled: true,
2244 },
2245 srcs: ["libvndk.so"],
2246 }
2247 `, withFiles(map[string][]byte{
2248 "libvndk.so": nil,
2249 }))
2250}
2251
Jooyung Han90eee022019-10-01 20:02:42 +09002252func TestVndkApexNameRule(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 Han90eee022019-10-01 20:02:42 +09002258 }
2259 apex_vndk {
2260 name: "myapex_v28",
2261 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002262 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002263 vndk_version: "28",
2264 }
2265 apex_key {
2266 name: "myapex.key",
2267 public_key: "testkey.avbpubkey",
2268 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002269 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002270
2271 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002272 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002273 actual := proptools.String(bundle.properties.Apex_name)
2274 if !reflect.DeepEqual(actual, expected) {
2275 t.Errorf("Got '%v', expected '%v'", actual, expected)
2276 }
2277 }
2278
2279 assertApexName("com.android.vndk.vVER", "myapex")
2280 assertApexName("com.android.vndk.v28", "myapex_v28")
2281}
2282
Jooyung Han344d5432019-08-23 11:17:39 +09002283func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2284 ctx, _ := testApex(t, `
2285 apex_vndk {
2286 name: "myapex",
2287 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002288 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002289 }
2290
2291 apex_key {
2292 name: "myapex.key",
2293 public_key: "testkey.avbpubkey",
2294 private_key: "testkey.pem",
2295 }
2296
2297 cc_library {
2298 name: "libvndk",
2299 srcs: ["mylib.cpp"],
2300 vendor_available: true,
2301 native_bridge_supported: true,
2302 host_supported: true,
2303 vndk: {
2304 enabled: true,
2305 },
2306 system_shared_libs: [],
2307 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002308 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002309 }
Jooyung Han35155c42020-02-06 17:33:20 +09002310 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09002311
Jooyung Hana57af4a2020-01-23 05:36:59 +00002312 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002313 "lib/libvndk.so",
2314 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002315 "lib/libc++.so",
2316 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002317 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002318 })
Jooyung Han344d5432019-08-23 11:17:39 +09002319}
2320
2321func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2322 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2323 apex_vndk {
2324 name: "myapex",
2325 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002326 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002327 native_bridge_supported: true,
2328 }
2329
2330 apex_key {
2331 name: "myapex.key",
2332 public_key: "testkey.avbpubkey",
2333 private_key: "testkey.pem",
2334 }
2335
2336 cc_library {
2337 name: "libvndk",
2338 srcs: ["mylib.cpp"],
2339 vendor_available: true,
2340 native_bridge_supported: true,
2341 host_supported: true,
2342 vndk: {
2343 enabled: true,
2344 },
2345 system_shared_libs: [],
2346 stl: "none",
2347 }
2348 `)
2349}
2350
Jooyung Han31c470b2019-10-18 16:26:59 +09002351func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002352 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002353 apex_vndk {
2354 name: "myapex_v27",
2355 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002356 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002357 vndk_version: "27",
2358 }
2359
2360 apex_key {
2361 name: "myapex.key",
2362 public_key: "testkey.avbpubkey",
2363 private_key: "testkey.pem",
2364 }
2365
2366 vndk_prebuilt_shared {
2367 name: "libvndk27",
2368 version: "27",
2369 target_arch: "arm",
2370 vendor_available: true,
2371 vndk: {
2372 enabled: true,
2373 },
2374 arch: {
2375 arm: {
2376 srcs: ["libvndk27.so"],
2377 }
2378 },
2379 }
2380
2381 vndk_prebuilt_shared {
2382 name: "libvndk27",
2383 version: "27",
2384 target_arch: "arm",
2385 binder32bit: true,
2386 vendor_available: true,
2387 vndk: {
2388 enabled: true,
2389 },
2390 arch: {
2391 arm: {
2392 srcs: ["libvndk27binder32.so"],
2393 }
2394 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002395 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002396 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002397 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002398 withFiles(map[string][]byte{
2399 "libvndk27.so": nil,
2400 "libvndk27binder32.so": nil,
2401 }),
2402 withBinder32bit,
2403 withTargets(map[android.OsType][]android.Target{
2404 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09002405 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
2406 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09002407 },
2408 }),
2409 )
2410
Jooyung Hana57af4a2020-01-23 05:36:59 +00002411 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002412 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002413 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002414 })
2415}
2416
Jooyung Hane1633032019-08-01 17:41:43 +09002417func TestDependenciesInApexManifest(t *testing.T) {
2418 ctx, _ := testApex(t, `
2419 apex {
2420 name: "myapex_nodep",
2421 key: "myapex.key",
2422 native_shared_libs: ["lib_nodep"],
2423 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002424 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002425 }
2426
2427 apex {
2428 name: "myapex_dep",
2429 key: "myapex.key",
2430 native_shared_libs: ["lib_dep"],
2431 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002432 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002433 }
2434
2435 apex {
2436 name: "myapex_provider",
2437 key: "myapex.key",
2438 native_shared_libs: ["libfoo"],
2439 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002440 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002441 }
2442
2443 apex {
2444 name: "myapex_selfcontained",
2445 key: "myapex.key",
2446 native_shared_libs: ["lib_dep", "libfoo"],
2447 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002448 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002449 }
2450
2451 apex_key {
2452 name: "myapex.key",
2453 public_key: "testkey.avbpubkey",
2454 private_key: "testkey.pem",
2455 }
2456
2457 cc_library {
2458 name: "lib_nodep",
2459 srcs: ["mylib.cpp"],
2460 system_shared_libs: [],
2461 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002462 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002463 }
2464
2465 cc_library {
2466 name: "lib_dep",
2467 srcs: ["mylib.cpp"],
2468 shared_libs: ["libfoo"],
2469 system_shared_libs: [],
2470 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002471 apex_available: [
2472 "myapex_dep",
2473 "myapex_provider",
2474 "myapex_selfcontained",
2475 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002476 }
2477
2478 cc_library {
2479 name: "libfoo",
2480 srcs: ["mytest.cpp"],
2481 stubs: {
2482 versions: ["1"],
2483 },
2484 system_shared_libs: [],
2485 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002486 apex_available: [
2487 "myapex_provider",
2488 "myapex_selfcontained",
2489 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002490 }
2491 `)
2492
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002493 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002494 var provideNativeLibs, requireNativeLibs []string
2495
Sundong Ahnabb64432019-10-22 13:58:29 +09002496 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002497 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2498 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002499 ensureListEmpty(t, provideNativeLibs)
2500 ensureListEmpty(t, requireNativeLibs)
2501
Sundong Ahnabb64432019-10-22 13:58:29 +09002502 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002503 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2504 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002505 ensureListEmpty(t, provideNativeLibs)
2506 ensureListContains(t, requireNativeLibs, "libfoo.so")
2507
Sundong Ahnabb64432019-10-22 13:58:29 +09002508 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002509 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2510 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002511 ensureListContains(t, provideNativeLibs, "libfoo.so")
2512 ensureListEmpty(t, requireNativeLibs)
2513
Sundong Ahnabb64432019-10-22 13:58:29 +09002514 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002515 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2516 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002517 ensureListContains(t, provideNativeLibs, "libfoo.so")
2518 ensureListEmpty(t, requireNativeLibs)
2519}
2520
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002521func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002522 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002523 apex {
2524 name: "myapex",
2525 key: "myapex.key",
2526 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002527 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002528 }
2529
2530 apex_key {
2531 name: "myapex.key",
2532 public_key: "testkey.avbpubkey",
2533 private_key: "testkey.pem",
2534 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002535
2536 cc_library {
2537 name: "mylib",
2538 srcs: ["mylib.cpp"],
2539 system_shared_libs: [],
2540 stl: "none",
2541 apex_available: [
2542 "//apex_available:platform",
2543 "myapex",
2544 ],
2545 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002546 `)
2547
Sundong Ahnabb64432019-10-22 13:58:29 +09002548 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002549 apexManifestRule := module.Rule("apexManifestRule")
2550 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2551 apexRule := module.Rule("apexRule")
2552 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002553
2554 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2555 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2556 name := apexBundle.BaseModuleName()
2557 prefix := "TARGET_"
2558 var builder strings.Builder
2559 data.Custom(&builder, name, prefix, "", data)
2560 androidMk := builder.String()
2561 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2562 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002563}
2564
Alex Light0851b882019-02-07 13:20:53 -08002565func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002566 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002567 apex {
2568 name: "myapex",
2569 key: "myapex.key",
2570 native_shared_libs: ["mylib_common"],
2571 }
2572
2573 apex_key {
2574 name: "myapex.key",
2575 public_key: "testkey.avbpubkey",
2576 private_key: "testkey.pem",
2577 }
2578
2579 cc_library {
2580 name: "mylib_common",
2581 srcs: ["mylib.cpp"],
2582 system_shared_libs: [],
2583 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002584 apex_available: [
2585 "//apex_available:platform",
2586 "myapex",
2587 ],
Alex Light0851b882019-02-07 13:20:53 -08002588 }
2589 `)
2590
Sundong Ahnabb64432019-10-22 13:58:29 +09002591 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002592 apexRule := module.Rule("apexRule")
2593 copyCmds := apexRule.Args["copy_commands"]
2594
2595 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2596 t.Log("Apex was a test apex!")
2597 t.Fail()
2598 }
2599 // Ensure that main rule creates an output
2600 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2601
2602 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002603 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002604
2605 // Ensure that both direct and indirect deps are copied into apex
2606 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2607
Colin Cross7113d202019-11-20 16:39:12 -08002608 // Ensure that the platform variant ends with _shared
2609 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002610
2611 if !android.InAnyApex("mylib_common") {
2612 t.Log("Found mylib_common not in any apex!")
2613 t.Fail()
2614 }
2615}
2616
2617func TestTestApex(t *testing.T) {
2618 if android.InAnyApex("mylib_common_test") {
2619 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!")
2620 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002621 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002622 apex_test {
2623 name: "myapex",
2624 key: "myapex.key",
2625 native_shared_libs: ["mylib_common_test"],
2626 }
2627
2628 apex_key {
2629 name: "myapex.key",
2630 public_key: "testkey.avbpubkey",
2631 private_key: "testkey.pem",
2632 }
2633
2634 cc_library {
2635 name: "mylib_common_test",
2636 srcs: ["mylib.cpp"],
2637 system_shared_libs: [],
2638 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002639 // TODO: remove //apex_available:platform
2640 apex_available: [
2641 "//apex_available:platform",
2642 "myapex",
2643 ],
Alex Light0851b882019-02-07 13:20:53 -08002644 }
2645 `)
2646
Sundong Ahnabb64432019-10-22 13:58:29 +09002647 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002648 apexRule := module.Rule("apexRule")
2649 copyCmds := apexRule.Args["copy_commands"]
2650
2651 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2652 t.Log("Apex was not a test apex!")
2653 t.Fail()
2654 }
2655 // Ensure that main rule creates an output
2656 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2657
2658 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002659 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002660
2661 // Ensure that both direct and indirect deps are copied into apex
2662 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2663
Colin Cross7113d202019-11-20 16:39:12 -08002664 // Ensure that the platform variant ends with _shared
2665 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002666}
2667
Alex Light9670d332019-01-29 18:07:33 -08002668func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002669 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002670 apex {
2671 name: "myapex",
2672 key: "myapex.key",
2673 multilib: {
2674 first: {
2675 native_shared_libs: ["mylib_common"],
2676 }
2677 },
2678 target: {
2679 android: {
2680 multilib: {
2681 first: {
2682 native_shared_libs: ["mylib"],
2683 }
2684 }
2685 },
2686 host: {
2687 multilib: {
2688 first: {
2689 native_shared_libs: ["mylib2"],
2690 }
2691 }
2692 }
2693 }
2694 }
2695
2696 apex_key {
2697 name: "myapex.key",
2698 public_key: "testkey.avbpubkey",
2699 private_key: "testkey.pem",
2700 }
2701
2702 cc_library {
2703 name: "mylib",
2704 srcs: ["mylib.cpp"],
2705 system_shared_libs: [],
2706 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002707 // TODO: remove //apex_available:platform
2708 apex_available: [
2709 "//apex_available:platform",
2710 "myapex",
2711 ],
Alex Light9670d332019-01-29 18:07:33 -08002712 }
2713
2714 cc_library {
2715 name: "mylib_common",
2716 srcs: ["mylib.cpp"],
2717 system_shared_libs: [],
2718 stl: "none",
2719 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002720 // TODO: remove //apex_available:platform
2721 apex_available: [
2722 "//apex_available:platform",
2723 "myapex",
2724 ],
Alex Light9670d332019-01-29 18:07:33 -08002725 }
2726
2727 cc_library {
2728 name: "mylib2",
2729 srcs: ["mylib.cpp"],
2730 system_shared_libs: [],
2731 stl: "none",
2732 compile_multilib: "first",
2733 }
2734 `)
2735
Sundong Ahnabb64432019-10-22 13:58:29 +09002736 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002737 copyCmds := apexRule.Args["copy_commands"]
2738
2739 // Ensure that main rule creates an output
2740 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2741
2742 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002743 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2744 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2745 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002746
2747 // Ensure that both direct and indirect deps are copied into apex
2748 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2749 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2750 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2751
Colin Cross7113d202019-11-20 16:39:12 -08002752 // Ensure that the platform variant ends with _shared
2753 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2754 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2755 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002756}
Jiyong Park04480cf2019-02-06 00:16:29 +09002757
2758func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002759 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002760 apex {
2761 name: "myapex",
2762 key: "myapex.key",
2763 binaries: ["myscript"],
2764 }
2765
2766 apex_key {
2767 name: "myapex.key",
2768 public_key: "testkey.avbpubkey",
2769 private_key: "testkey.pem",
2770 }
2771
2772 sh_binary {
2773 name: "myscript",
2774 src: "mylib.cpp",
2775 filename: "myscript.sh",
2776 sub_dir: "script",
2777 }
2778 `)
2779
Sundong Ahnabb64432019-10-22 13:58:29 +09002780 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002781 copyCmds := apexRule.Args["copy_commands"]
2782
2783 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2784}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002785
Jooyung Han91df2082019-11-20 01:49:42 +09002786func TestApexInVariousPartition(t *testing.T) {
2787 testcases := []struct {
2788 propName, parition, flattenedPartition string
2789 }{
2790 {"", "system", "system_ext"},
2791 {"product_specific: true", "product", "product"},
2792 {"soc_specific: true", "vendor", "vendor"},
2793 {"proprietary: true", "vendor", "vendor"},
2794 {"vendor: true", "vendor", "vendor"},
2795 {"system_ext_specific: true", "system_ext", "system_ext"},
2796 }
2797 for _, tc := range testcases {
2798 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2799 ctx, _ := testApex(t, `
2800 apex {
2801 name: "myapex",
2802 key: "myapex.key",
2803 `+tc.propName+`
2804 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002805
Jooyung Han91df2082019-11-20 01:49:42 +09002806 apex_key {
2807 name: "myapex.key",
2808 public_key: "testkey.avbpubkey",
2809 private_key: "testkey.pem",
2810 }
2811 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002812
Jooyung Han91df2082019-11-20 01:49:42 +09002813 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2814 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2815 actual := apex.installDir.String()
2816 if actual != expected {
2817 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2818 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002819
Jooyung Han91df2082019-11-20 01:49:42 +09002820 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2821 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2822 actual = flattened.installDir.String()
2823 if actual != expected {
2824 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2825 }
2826 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002827 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002828}
Jiyong Park67882562019-03-21 01:11:21 +09002829
Jooyung Han54aca7b2019-11-20 02:26:02 +09002830func TestFileContexts(t *testing.T) {
2831 ctx, _ := testApex(t, `
2832 apex {
2833 name: "myapex",
2834 key: "myapex.key",
2835 }
2836
2837 apex_key {
2838 name: "myapex.key",
2839 public_key: "testkey.avbpubkey",
2840 private_key: "testkey.pem",
2841 }
2842 `)
2843 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2844 apexRule := module.Rule("apexRule")
2845 actual := apexRule.Args["file_contexts"]
2846 expected := "system/sepolicy/apex/myapex-file_contexts"
2847 if actual != expected {
2848 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2849 }
2850
2851 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2852 apex {
2853 name: "myapex",
2854 key: "myapex.key",
2855 file_contexts: "my_own_file_contexts",
2856 }
2857
2858 apex_key {
2859 name: "myapex.key",
2860 public_key: "testkey.avbpubkey",
2861 private_key: "testkey.pem",
2862 }
2863 `, withFiles(map[string][]byte{
2864 "my_own_file_contexts": nil,
2865 }))
2866
2867 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2868 apex {
2869 name: "myapex",
2870 key: "myapex.key",
2871 product_specific: true,
2872 file_contexts: "product_specific_file_contexts",
2873 }
2874
2875 apex_key {
2876 name: "myapex.key",
2877 public_key: "testkey.avbpubkey",
2878 private_key: "testkey.pem",
2879 }
2880 `)
2881
2882 ctx, _ = testApex(t, `
2883 apex {
2884 name: "myapex",
2885 key: "myapex.key",
2886 product_specific: true,
2887 file_contexts: "product_specific_file_contexts",
2888 }
2889
2890 apex_key {
2891 name: "myapex.key",
2892 public_key: "testkey.avbpubkey",
2893 private_key: "testkey.pem",
2894 }
2895 `, withFiles(map[string][]byte{
2896 "product_specific_file_contexts": nil,
2897 }))
2898 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2899 apexRule = module.Rule("apexRule")
2900 actual = apexRule.Args["file_contexts"]
2901 expected = "product_specific_file_contexts"
2902 if actual != expected {
2903 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2904 }
2905
2906 ctx, _ = testApex(t, `
2907 apex {
2908 name: "myapex",
2909 key: "myapex.key",
2910 product_specific: true,
2911 file_contexts: ":my-file-contexts",
2912 }
2913
2914 apex_key {
2915 name: "myapex.key",
2916 public_key: "testkey.avbpubkey",
2917 private_key: "testkey.pem",
2918 }
2919
2920 filegroup {
2921 name: "my-file-contexts",
2922 srcs: ["product_specific_file_contexts"],
2923 }
2924 `, withFiles(map[string][]byte{
2925 "product_specific_file_contexts": nil,
2926 }))
2927 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2928 apexRule = module.Rule("apexRule")
2929 actual = apexRule.Args["file_contexts"]
2930 expected = "product_specific_file_contexts"
2931 if actual != expected {
2932 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2933 }
2934}
2935
Jiyong Park67882562019-03-21 01:11:21 +09002936func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002937 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002938 apex_key {
2939 name: "myapex.key",
2940 public_key: ":my.avbpubkey",
2941 private_key: ":my.pem",
2942 product_specific: true,
2943 }
2944
2945 filegroup {
2946 name: "my.avbpubkey",
2947 srcs: ["testkey2.avbpubkey"],
2948 }
2949
2950 filegroup {
2951 name: "my.pem",
2952 srcs: ["testkey2.pem"],
2953 }
2954 `)
2955
2956 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2957 expected_pubkey := "testkey2.avbpubkey"
2958 actual_pubkey := apex_key.public_key_file.String()
2959 if actual_pubkey != expected_pubkey {
2960 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2961 }
2962 expected_privkey := "testkey2.pem"
2963 actual_privkey := apex_key.private_key_file.String()
2964 if actual_privkey != expected_privkey {
2965 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2966 }
2967}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002968
2969func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002970 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002971 prebuilt_apex {
2972 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002973 arch: {
2974 arm64: {
2975 src: "myapex-arm64.apex",
2976 },
2977 arm: {
2978 src: "myapex-arm.apex",
2979 },
2980 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002981 }
2982 `)
2983
2984 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2985
Jiyong Parkc95714e2019-03-29 14:23:10 +09002986 expectedInput := "myapex-arm64.apex"
2987 if prebuilt.inputApex.String() != expectedInput {
2988 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2989 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002990}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002991
2992func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002993 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002994 prebuilt_apex {
2995 name: "myapex",
2996 src: "myapex-arm.apex",
2997 filename: "notmyapex.apex",
2998 }
2999 `)
3000
3001 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3002
3003 expected := "notmyapex.apex"
3004 if p.installFilename != expected {
3005 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
3006 }
3007}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003008
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003009func TestPrebuiltOverrides(t *testing.T) {
3010 ctx, config := testApex(t, `
3011 prebuilt_apex {
3012 name: "myapex.prebuilt",
3013 src: "myapex-arm.apex",
3014 overrides: [
3015 "myapex",
3016 ],
3017 }
3018 `)
3019
3020 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3021
3022 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003023 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003024 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003025 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003026 }
3027}
3028
Roland Levillain630846d2019-06-26 12:48:34 +01003029func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003030 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003031 apex_test {
3032 name: "myapex",
3033 key: "myapex.key",
3034 tests: [
3035 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003036 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003037 ],
3038 }
3039
3040 apex_key {
3041 name: "myapex.key",
3042 public_key: "testkey.avbpubkey",
3043 private_key: "testkey.pem",
3044 }
3045
3046 cc_test {
3047 name: "mytest",
3048 gtest: false,
3049 srcs: ["mytest.cpp"],
3050 relative_install_path: "test",
3051 system_shared_libs: [],
3052 static_executable: true,
3053 stl: "none",
3054 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003055
3056 cc_test {
3057 name: "mytests",
3058 gtest: false,
3059 srcs: [
3060 "mytest1.cpp",
3061 "mytest2.cpp",
3062 "mytest3.cpp",
3063 ],
3064 test_per_src: true,
3065 relative_install_path: "test",
3066 system_shared_libs: [],
3067 static_executable: true,
3068 stl: "none",
3069 }
Roland Levillain630846d2019-06-26 12:48:34 +01003070 `)
3071
Sundong Ahnabb64432019-10-22 13:58:29 +09003072 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003073 copyCmds := apexRule.Args["copy_commands"]
3074
3075 // Ensure that test dep is copied into apex.
3076 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003077
3078 // Ensure that test deps built with `test_per_src` are copied into apex.
3079 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3080 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3081 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003082
3083 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003084 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003085 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3086 name := apexBundle.BaseModuleName()
3087 prefix := "TARGET_"
3088 var builder strings.Builder
3089 data.Custom(&builder, name, prefix, "", data)
3090 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003091 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3092 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3093 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3094 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003095 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003096 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003097 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003098}
3099
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003100func TestInstallExtraFlattenedApexes(t *testing.T) {
3101 ctx, config := testApex(t, `
3102 apex {
3103 name: "myapex",
3104 key: "myapex.key",
3105 }
3106 apex_key {
3107 name: "myapex.key",
3108 public_key: "testkey.avbpubkey",
3109 private_key: "testkey.pem",
3110 }
3111 `, func(fs map[string][]byte, config android.Config) {
3112 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3113 })
3114 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003115 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003116 mk := android.AndroidMkDataForTest(t, config, "", ab)
3117 var builder strings.Builder
3118 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3119 androidMk := builder.String()
3120 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3121}
3122
Jooyung Han5c998b92019-06-27 11:30:33 +09003123func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003124 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003125 apex {
3126 name: "myapex",
3127 key: "myapex.key",
3128 native_shared_libs: ["mylib"],
3129 uses: ["commonapex"],
3130 }
3131
3132 apex {
3133 name: "commonapex",
3134 key: "myapex.key",
3135 native_shared_libs: ["libcommon"],
3136 provide_cpp_shared_libs: true,
3137 }
3138
3139 apex_key {
3140 name: "myapex.key",
3141 public_key: "testkey.avbpubkey",
3142 private_key: "testkey.pem",
3143 }
3144
3145 cc_library {
3146 name: "mylib",
3147 srcs: ["mylib.cpp"],
3148 shared_libs: ["libcommon"],
3149 system_shared_libs: [],
3150 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003151 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003152 }
3153
3154 cc_library {
3155 name: "libcommon",
3156 srcs: ["mylib_common.cpp"],
3157 system_shared_libs: [],
3158 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003159 // TODO: remove //apex_available:platform
3160 apex_available: [
3161 "//apex_available:platform",
3162 "commonapex",
3163 "myapex",
3164 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003165 }
3166 `)
3167
Sundong Ahnabb64432019-10-22 13:58:29 +09003168 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003169 apexRule1 := module1.Rule("apexRule")
3170 copyCmds1 := apexRule1.Args["copy_commands"]
3171
Sundong Ahnabb64432019-10-22 13:58:29 +09003172 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003173 apexRule2 := module2.Rule("apexRule")
3174 copyCmds2 := apexRule2.Args["copy_commands"]
3175
Colin Cross7113d202019-11-20 16:39:12 -08003176 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3177 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003178 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3179 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3180 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3181}
3182
3183func TestApexUsesFailsIfNotProvided(t *testing.T) {
3184 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3185 apex {
3186 name: "myapex",
3187 key: "myapex.key",
3188 uses: ["commonapex"],
3189 }
3190
3191 apex {
3192 name: "commonapex",
3193 key: "myapex.key",
3194 }
3195
3196 apex_key {
3197 name: "myapex.key",
3198 public_key: "testkey.avbpubkey",
3199 private_key: "testkey.pem",
3200 }
3201 `)
3202 testApexError(t, `uses: "commonapex" is not a provider`, `
3203 apex {
3204 name: "myapex",
3205 key: "myapex.key",
3206 uses: ["commonapex"],
3207 }
3208
3209 cc_library {
3210 name: "commonapex",
3211 system_shared_libs: [],
3212 stl: "none",
3213 }
3214
3215 apex_key {
3216 name: "myapex.key",
3217 public_key: "testkey.avbpubkey",
3218 private_key: "testkey.pem",
3219 }
3220 `)
3221}
3222
3223func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3224 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3225 apex {
3226 name: "myapex",
3227 key: "myapex.key",
3228 use_vendor: true,
3229 uses: ["commonapex"],
3230 }
3231
3232 apex {
3233 name: "commonapex",
3234 key: "myapex.key",
3235 provide_cpp_shared_libs: true,
3236 }
3237
3238 apex_key {
3239 name: "myapex.key",
3240 public_key: "testkey.avbpubkey",
3241 private_key: "testkey.pem",
3242 }
Jooyung Handc782442019-11-01 03:14:38 +09003243 `, func(fs map[string][]byte, config android.Config) {
3244 setUseVendorWhitelistForTest(config, []string{"myapex"})
3245 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003246}
3247
Jooyung Hand48f3c32019-08-23 11:18:57 +09003248func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3249 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3250 apex {
3251 name: "myapex",
3252 key: "myapex.key",
3253 native_shared_libs: ["libfoo"],
3254 }
3255
3256 apex_key {
3257 name: "myapex.key",
3258 public_key: "testkey.avbpubkey",
3259 private_key: "testkey.pem",
3260 }
3261
3262 cc_library {
3263 name: "libfoo",
3264 stl: "none",
3265 system_shared_libs: [],
3266 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003267 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003268 }
3269 `)
3270 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3271 apex {
3272 name: "myapex",
3273 key: "myapex.key",
3274 java_libs: ["myjar"],
3275 }
3276
3277 apex_key {
3278 name: "myapex.key",
3279 public_key: "testkey.avbpubkey",
3280 private_key: "testkey.pem",
3281 }
3282
3283 java_library {
3284 name: "myjar",
3285 srcs: ["foo/bar/MyClass.java"],
3286 sdk_version: "none",
3287 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003288 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003289 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003290 }
3291 `)
3292}
3293
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003294func TestApexWithApps(t *testing.T) {
3295 ctx, _ := testApex(t, `
3296 apex {
3297 name: "myapex",
3298 key: "myapex.key",
3299 apps: [
3300 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003301 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003302 ],
3303 }
3304
3305 apex_key {
3306 name: "myapex.key",
3307 public_key: "testkey.avbpubkey",
3308 private_key: "testkey.pem",
3309 }
3310
3311 android_app {
3312 name: "AppFoo",
3313 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003314 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003315 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003316 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08003317 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003318 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003319 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003320
3321 android_app {
3322 name: "AppFooPriv",
3323 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003324 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09003325 system_modules: "none",
3326 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08003327 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003328 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003329 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003330
3331 cc_library_shared {
3332 name: "libjni",
3333 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003334 shared_libs: ["libfoo"],
3335 stl: "none",
3336 system_shared_libs: [],
3337 apex_available: [ "myapex" ],
3338 sdk_version: "current",
3339 }
3340
3341 cc_library_shared {
3342 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09003343 stl: "none",
3344 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003345 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08003346 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003347 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003348 `)
3349
Sundong Ahnabb64432019-10-22 13:58:29 +09003350 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003351 apexRule := module.Rule("apexRule")
3352 copyCmds := apexRule.Args["copy_commands"]
3353
3354 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003355 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003356
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003357 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3358 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003359 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003360 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003361 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003362 // JNI libraries including transitive deps are
3363 for _, jni := range []string{"libjni", "libfoo"} {
3364 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
3365 // ... embedded inside APK (jnilibs.zip)
3366 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3367 // ... and not directly inside the APEX
3368 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3369 }
Dario Frenicde2a032019-10-27 00:29:22 +01003370}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003371
Dario Frenicde2a032019-10-27 00:29:22 +01003372func TestApexWithAppImports(t *testing.T) {
3373 ctx, _ := testApex(t, `
3374 apex {
3375 name: "myapex",
3376 key: "myapex.key",
3377 apps: [
3378 "AppFooPrebuilt",
3379 "AppFooPrivPrebuilt",
3380 ],
3381 }
3382
3383 apex_key {
3384 name: "myapex.key",
3385 public_key: "testkey.avbpubkey",
3386 private_key: "testkey.pem",
3387 }
3388
3389 android_app_import {
3390 name: "AppFooPrebuilt",
3391 apk: "PrebuiltAppFoo.apk",
3392 presigned: true,
3393 dex_preopt: {
3394 enabled: false,
3395 },
3396 }
3397
3398 android_app_import {
3399 name: "AppFooPrivPrebuilt",
3400 apk: "PrebuiltAppFooPriv.apk",
3401 privileged: true,
3402 presigned: true,
3403 dex_preopt: {
3404 enabled: false,
3405 },
3406 }
3407 `)
3408
Sundong Ahnabb64432019-10-22 13:58:29 +09003409 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003410 apexRule := module.Rule("apexRule")
3411 copyCmds := apexRule.Args["copy_commands"]
3412
3413 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3414 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003415}
3416
Dario Freni6f3937c2019-12-20 22:58:03 +00003417func TestApexWithTestHelperApp(t *testing.T) {
3418 ctx, _ := testApex(t, `
3419 apex {
3420 name: "myapex",
3421 key: "myapex.key",
3422 apps: [
3423 "TesterHelpAppFoo",
3424 ],
3425 }
3426
3427 apex_key {
3428 name: "myapex.key",
3429 public_key: "testkey.avbpubkey",
3430 private_key: "testkey.pem",
3431 }
3432
3433 android_test_helper_app {
3434 name: "TesterHelpAppFoo",
3435 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003436 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003437 }
3438
3439 `)
3440
3441 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3442 apexRule := module.Rule("apexRule")
3443 copyCmds := apexRule.Args["copy_commands"]
3444
3445 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3446}
3447
Jooyung Han18020ea2019-11-13 10:50:48 +09003448func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3449 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09003450 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003451 apex {
3452 name: "myapex",
3453 key: "myapex.key",
3454 native_shared_libs: ["libfoo"],
3455 }
3456
3457 apex_key {
3458 name: "myapex.key",
3459 public_key: "testkey.avbpubkey",
3460 private_key: "testkey.pem",
3461 }
3462
3463 apex {
3464 name: "otherapex",
3465 key: "myapex.key",
3466 native_shared_libs: ["libfoo"],
3467 }
3468
3469 cc_defaults {
3470 name: "libfoo-defaults",
3471 apex_available: ["otherapex"],
3472 }
3473
3474 cc_library {
3475 name: "libfoo",
3476 defaults: ["libfoo-defaults"],
3477 stl: "none",
3478 system_shared_libs: [],
3479 }`)
3480}
3481
Jiyong Park127b40b2019-09-30 16:04:35 +09003482func TestApexAvailable(t *testing.T) {
3483 // libfoo is not available to myapex, but only to otherapex
3484 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3485 apex {
3486 name: "myapex",
3487 key: "myapex.key",
3488 native_shared_libs: ["libfoo"],
3489 }
3490
3491 apex_key {
3492 name: "myapex.key",
3493 public_key: "testkey.avbpubkey",
3494 private_key: "testkey.pem",
3495 }
3496
3497 apex {
3498 name: "otherapex",
3499 key: "otherapex.key",
3500 native_shared_libs: ["libfoo"],
3501 }
3502
3503 apex_key {
3504 name: "otherapex.key",
3505 public_key: "testkey.avbpubkey",
3506 private_key: "testkey.pem",
3507 }
3508
3509 cc_library {
3510 name: "libfoo",
3511 stl: "none",
3512 system_shared_libs: [],
3513 apex_available: ["otherapex"],
3514 }`)
3515
Jooyung Han5e9013b2020-03-10 06:23:13 +09003516 // libbbaz is an indirect dep
3517 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003518 apex {
3519 name: "myapex",
3520 key: "myapex.key",
3521 native_shared_libs: ["libfoo"],
3522 }
3523
3524 apex_key {
3525 name: "myapex.key",
3526 public_key: "testkey.avbpubkey",
3527 private_key: "testkey.pem",
3528 }
3529
Jiyong Park127b40b2019-09-30 16:04:35 +09003530 cc_library {
3531 name: "libfoo",
3532 stl: "none",
3533 shared_libs: ["libbar"],
3534 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003535 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003536 }
3537
3538 cc_library {
3539 name: "libbar",
3540 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09003541 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003542 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003543 apex_available: ["myapex"],
3544 }
3545
3546 cc_library {
3547 name: "libbaz",
3548 stl: "none",
3549 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003550 }`)
3551
3552 testApexError(t, "\"otherapex\" is not a valid module name", `
3553 apex {
3554 name: "myapex",
3555 key: "myapex.key",
3556 native_shared_libs: ["libfoo"],
3557 }
3558
3559 apex_key {
3560 name: "myapex.key",
3561 public_key: "testkey.avbpubkey",
3562 private_key: "testkey.pem",
3563 }
3564
3565 cc_library {
3566 name: "libfoo",
3567 stl: "none",
3568 system_shared_libs: [],
3569 apex_available: ["otherapex"],
3570 }`)
3571
3572 ctx, _ := testApex(t, `
3573 apex {
3574 name: "myapex",
3575 key: "myapex.key",
3576 native_shared_libs: ["libfoo", "libbar"],
3577 }
3578
3579 apex_key {
3580 name: "myapex.key",
3581 public_key: "testkey.avbpubkey",
3582 private_key: "testkey.pem",
3583 }
3584
3585 cc_library {
3586 name: "libfoo",
3587 stl: "none",
3588 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09003589 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003590 apex_available: ["myapex"],
3591 }
3592
3593 cc_library {
3594 name: "libbar",
3595 stl: "none",
3596 system_shared_libs: [],
3597 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09003598 }
3599
3600 cc_library {
3601 name: "libbaz",
3602 stl: "none",
3603 system_shared_libs: [],
3604 stubs: {
3605 versions: ["10", "20", "30"],
3606 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003607 }`)
3608
3609 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003610 // TODO(jiyong) the checks for the platform variant are removed because we now create
3611 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3612 // the platform variants are not used from other platform modules. When that is done,
3613 // these checks will be replaced by expecting a specific error message that will be
3614 // emitted when the platform variant is used.
3615 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3616 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3617 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3618 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003619
3620 ctx, _ = testApex(t, `
3621 apex {
3622 name: "myapex",
3623 key: "myapex.key",
3624 }
3625
3626 apex_key {
3627 name: "myapex.key",
3628 public_key: "testkey.avbpubkey",
3629 private_key: "testkey.pem",
3630 }
3631
3632 cc_library {
3633 name: "libfoo",
3634 stl: "none",
3635 system_shared_libs: [],
3636 apex_available: ["//apex_available:platform"],
3637 }`)
3638
3639 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003640 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3641 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003642
3643 ctx, _ = testApex(t, `
3644 apex {
3645 name: "myapex",
3646 key: "myapex.key",
3647 native_shared_libs: ["libfoo"],
3648 }
3649
3650 apex_key {
3651 name: "myapex.key",
3652 public_key: "testkey.avbpubkey",
3653 private_key: "testkey.pem",
3654 }
3655
3656 cc_library {
3657 name: "libfoo",
3658 stl: "none",
3659 system_shared_libs: [],
3660 apex_available: ["myapex"],
3661 static: {
3662 apex_available: ["//apex_available:platform"],
3663 },
3664 }`)
3665
3666 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003667 // TODO(jiyong) the checks for the platform variant are removed because we now create
3668 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3669 // the platform variants are not used from other platform modules. When that is done,
3670 // these checks will be replaced by expecting a specific error message that will be
3671 // emitted when the platform variant is used.
3672 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3673 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3674 // // but the static variant is available to both myapex and the platform
3675 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3676 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003677}
3678
Jiyong Park5d790c32019-11-15 18:40:32 +09003679func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003680 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003681 apex {
3682 name: "myapex",
3683 key: "myapex.key",
3684 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003685 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003686 }
3687
3688 override_apex {
3689 name: "override_myapex",
3690 base: "myapex",
3691 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003692 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003693 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003694 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09003695 }
3696
3697 apex_key {
3698 name: "myapex.key",
3699 public_key: "testkey.avbpubkey",
3700 private_key: "testkey.pem",
3701 }
3702
3703 android_app {
3704 name: "app",
3705 srcs: ["foo/bar/MyClass.java"],
3706 package_name: "foo",
3707 sdk_version: "none",
3708 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003709 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003710 }
3711
3712 override_android_app {
3713 name: "override_app",
3714 base: "app",
3715 package_name: "bar",
3716 }
Jiyong Park20bacab2020-03-03 11:45:41 +09003717 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003718
Jiyong Park317645e2019-12-05 13:20:58 +09003719 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3720 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3721 if originalVariant.GetOverriddenBy() != "" {
3722 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3723 }
3724 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3725 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3726 }
3727
Jiyong Park5d790c32019-11-15 18:40:32 +09003728 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3729 apexRule := module.Rule("apexRule")
3730 copyCmds := apexRule.Args["copy_commands"]
3731
3732 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3733 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003734
3735 apexBundle := module.Module().(*apexBundle)
3736 name := apexBundle.Name()
3737 if name != "override_myapex" {
3738 t.Errorf("name should be \"override_myapex\", but was %q", name)
3739 }
3740
Baligh Uddin004d7172020-02-19 21:29:28 -08003741 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3742 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3743 }
3744
Jiyong Park20bacab2020-03-03 11:45:41 +09003745 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003746 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09003747
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003748 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3749 var builder strings.Builder
3750 data.Custom(&builder, name, "TARGET_", "", data)
3751 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003752 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003753 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3754 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003755 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003756 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003757 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003758 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3759 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003760}
3761
Jooyung Han214bf372019-11-12 13:03:50 +09003762func TestLegacyAndroid10Support(t *testing.T) {
3763 ctx, _ := testApex(t, `
3764 apex {
3765 name: "myapex",
3766 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003767 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09003768 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09003769 }
3770
3771 apex_key {
3772 name: "myapex.key",
3773 public_key: "testkey.avbpubkey",
3774 private_key: "testkey.pem",
3775 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003776
3777 cc_library {
3778 name: "mylib",
3779 srcs: ["mylib.cpp"],
3780 stl: "libc++",
3781 system_shared_libs: [],
3782 apex_available: [ "myapex" ],
3783 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003784 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003785
3786 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3787 args := module.Rule("apexRule").Args
3788 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003789 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003790
3791 // The copies of the libraries in the apex should have one more dependency than
3792 // the ones outside the apex, namely the unwinder. Ideally we should check
3793 // the dependency names directly here but for some reason the names are blank in
3794 // this test.
3795 for _, lib := range []string{"libc++", "mylib"} {
3796 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3797 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3798 if len(apexImplicits) != len(nonApexImplicits)+1 {
3799 t.Errorf("%q missing unwinder dep", lib)
3800 }
3801 }
Jooyung Han214bf372019-11-12 13:03:50 +09003802}
3803
Jooyung Han58f26ab2019-12-18 15:34:32 +09003804func TestJavaSDKLibrary(t *testing.T) {
3805 ctx, _ := testApex(t, `
3806 apex {
3807 name: "myapex",
3808 key: "myapex.key",
3809 java_libs: ["foo"],
3810 }
3811
3812 apex_key {
3813 name: "myapex.key",
3814 public_key: "testkey.avbpubkey",
3815 private_key: "testkey.pem",
3816 }
3817
3818 java_sdk_library {
3819 name: "foo",
3820 srcs: ["a.java"],
3821 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003822 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003823 }
3824 `, withFiles(map[string][]byte{
3825 "api/current.txt": nil,
3826 "api/removed.txt": nil,
3827 "api/system-current.txt": nil,
3828 "api/system-removed.txt": nil,
3829 "api/test-current.txt": nil,
3830 "api/test-removed.txt": nil,
3831 }))
3832
3833 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003834 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003835 "javalib/foo.jar",
3836 "etc/permissions/foo.xml",
3837 })
3838 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003839 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3840 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003841}
3842
atrost6e126252020-01-27 17:01:16 +00003843func TestCompatConfig(t *testing.T) {
3844 ctx, _ := testApex(t, `
3845 apex {
3846 name: "myapex",
3847 key: "myapex.key",
3848 prebuilts: ["myjar-platform-compat-config"],
3849 java_libs: ["myjar"],
3850 }
3851
3852 apex_key {
3853 name: "myapex.key",
3854 public_key: "testkey.avbpubkey",
3855 private_key: "testkey.pem",
3856 }
3857
3858 platform_compat_config {
3859 name: "myjar-platform-compat-config",
3860 src: ":myjar",
3861 }
3862
3863 java_library {
3864 name: "myjar",
3865 srcs: ["foo/bar/MyClass.java"],
3866 sdk_version: "none",
3867 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003868 apex_available: [ "myapex" ],
3869 }
3870 `)
3871 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3872 "etc/compatconfig/myjar-platform-compat-config.xml",
3873 "javalib/myjar.jar",
3874 })
3875}
3876
Jiyong Park479321d2019-12-16 11:47:12 +09003877func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3878 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3879 apex {
3880 name: "myapex",
3881 key: "myapex.key",
3882 java_libs: ["myjar"],
3883 }
3884
3885 apex_key {
3886 name: "myapex.key",
3887 public_key: "testkey.avbpubkey",
3888 private_key: "testkey.pem",
3889 }
3890
3891 java_library {
3892 name: "myjar",
3893 srcs: ["foo/bar/MyClass.java"],
3894 sdk_version: "none",
3895 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003896 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003897 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003898 }
3899 `)
3900}
3901
Jiyong Park7afd1072019-12-30 16:56:33 +09003902func TestCarryRequiredModuleNames(t *testing.T) {
3903 ctx, config := testApex(t, `
3904 apex {
3905 name: "myapex",
3906 key: "myapex.key",
3907 native_shared_libs: ["mylib"],
3908 }
3909
3910 apex_key {
3911 name: "myapex.key",
3912 public_key: "testkey.avbpubkey",
3913 private_key: "testkey.pem",
3914 }
3915
3916 cc_library {
3917 name: "mylib",
3918 srcs: ["mylib.cpp"],
3919 system_shared_libs: [],
3920 stl: "none",
3921 required: ["a", "b"],
3922 host_required: ["c", "d"],
3923 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003924 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003925 }
3926 `)
3927
3928 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3929 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3930 name := apexBundle.BaseModuleName()
3931 prefix := "TARGET_"
3932 var builder strings.Builder
3933 data.Custom(&builder, name, prefix, "", data)
3934 androidMk := builder.String()
3935 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3936 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3937 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3938}
3939
Jiyong Park7cd10e32020-01-14 09:22:18 +09003940func TestSymlinksFromApexToSystem(t *testing.T) {
3941 bp := `
3942 apex {
3943 name: "myapex",
3944 key: "myapex.key",
3945 native_shared_libs: ["mylib"],
3946 java_libs: ["myjar"],
3947 }
3948
Jiyong Park9d677202020-02-19 16:29:35 +09003949 apex {
3950 name: "myapex.updatable",
3951 key: "myapex.key",
3952 native_shared_libs: ["mylib"],
3953 java_libs: ["myjar"],
3954 updatable: true,
3955 }
3956
Jiyong Park7cd10e32020-01-14 09:22:18 +09003957 apex_key {
3958 name: "myapex.key",
3959 public_key: "testkey.avbpubkey",
3960 private_key: "testkey.pem",
3961 }
3962
3963 cc_library {
3964 name: "mylib",
3965 srcs: ["mylib.cpp"],
3966 shared_libs: ["myotherlib"],
3967 system_shared_libs: [],
3968 stl: "none",
3969 apex_available: [
3970 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003971 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003972 "//apex_available:platform",
3973 ],
3974 }
3975
3976 cc_library {
3977 name: "myotherlib",
3978 srcs: ["mylib.cpp"],
3979 system_shared_libs: [],
3980 stl: "none",
3981 apex_available: [
3982 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003983 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003984 "//apex_available:platform",
3985 ],
3986 }
3987
3988 java_library {
3989 name: "myjar",
3990 srcs: ["foo/bar/MyClass.java"],
3991 sdk_version: "none",
3992 system_modules: "none",
3993 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003994 apex_available: [
3995 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003996 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003997 "//apex_available:platform",
3998 ],
3999 }
4000
4001 java_library {
4002 name: "myotherjar",
4003 srcs: ["foo/bar/MyClass.java"],
4004 sdk_version: "none",
4005 system_modules: "none",
4006 apex_available: [
4007 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004008 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004009 "//apex_available:platform",
4010 ],
4011 }
4012 `
4013
4014 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
4015 for _, f := range files {
4016 if f.path == file {
4017 if f.isLink {
4018 t.Errorf("%q is not a real file", file)
4019 }
4020 return
4021 }
4022 }
4023 t.Errorf("%q is not found", file)
4024 }
4025
4026 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
4027 for _, f := range files {
4028 if f.path == file {
4029 if !f.isLink {
4030 t.Errorf("%q is not a symlink", file)
4031 }
4032 return
4033 }
4034 }
4035 t.Errorf("%q is not found", file)
4036 }
4037
Jiyong Park9d677202020-02-19 16:29:35 +09004038 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4039 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004040 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004041 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004042 ensureRealfileExists(t, files, "javalib/myjar.jar")
4043 ensureRealfileExists(t, files, "lib64/mylib.so")
4044 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4045
Jiyong Park9d677202020-02-19 16:29:35 +09004046 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4047 ensureRealfileExists(t, files, "javalib/myjar.jar")
4048 ensureRealfileExists(t, files, "lib64/mylib.so")
4049 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4050
4051 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004052 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004053 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004054 ensureRealfileExists(t, files, "javalib/myjar.jar")
4055 ensureRealfileExists(t, files, "lib64/mylib.so")
4056 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004057
4058 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4059 ensureRealfileExists(t, files, "javalib/myjar.jar")
4060 ensureRealfileExists(t, files, "lib64/mylib.so")
4061 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004062}
4063
Jooyung Han643adc42020-02-27 13:50:06 +09004064func TestApexWithJniLibs(t *testing.T) {
4065 ctx, _ := testApex(t, `
4066 apex {
4067 name: "myapex",
4068 key: "myapex.key",
4069 jni_libs: ["mylib"],
4070 }
4071
4072 apex_key {
4073 name: "myapex.key",
4074 public_key: "testkey.avbpubkey",
4075 private_key: "testkey.pem",
4076 }
4077
4078 cc_library {
4079 name: "mylib",
4080 srcs: ["mylib.cpp"],
4081 shared_libs: ["mylib2"],
4082 system_shared_libs: [],
4083 stl: "none",
4084 apex_available: [ "myapex" ],
4085 }
4086
4087 cc_library {
4088 name: "mylib2",
4089 srcs: ["mylib.cpp"],
4090 system_shared_libs: [],
4091 stl: "none",
4092 apex_available: [ "myapex" ],
4093 }
4094 `)
4095
4096 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
4097 // Notice mylib2.so (transitive dep) is not added as a jni_lib
4098 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
4099 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4100 "lib64/mylib.so",
4101 "lib64/mylib2.so",
4102 })
4103}
4104
4105func TestApexWithJniLibs_Errors(t *testing.T) {
4106 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
4107 apex {
4108 name: "myapex",
4109 key: "myapex.key",
4110 jni_libs: ["xxx"],
4111 }
4112
4113 apex_key {
4114 name: "myapex.key",
4115 public_key: "testkey.avbpubkey",
4116 private_key: "testkey.pem",
4117 }
4118
4119 prebuilt_etc {
4120 name: "xxx",
4121 src: "xxx",
4122 }
4123 `, withFiles(map[string][]byte{
4124 "xxx": nil,
4125 }))
4126}
4127
Jiyong Parkbd159612020-02-28 15:22:21 +09004128func TestAppBundle(t *testing.T) {
4129 ctx, _ := testApex(t, `
4130 apex {
4131 name: "myapex",
4132 key: "myapex.key",
4133 apps: ["AppFoo"],
4134 }
4135
4136 apex_key {
4137 name: "myapex.key",
4138 public_key: "testkey.avbpubkey",
4139 private_key: "testkey.pem",
4140 }
4141
4142 android_app {
4143 name: "AppFoo",
4144 srcs: ["foo/bar/MyClass.java"],
4145 sdk_version: "none",
4146 system_modules: "none",
4147 apex_available: [ "myapex" ],
4148 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004149 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09004150
4151 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4152 content := bundleConfigRule.Args["content"]
4153
4154 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004155 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 +09004156}
4157
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004158func TestMain(m *testing.M) {
4159 run := func() int {
4160 setUp()
4161 defer tearDown()
4162
4163 return m.Run()
4164 }
4165
4166 os.Exit(run())
4167}