blob: b8e8878366f80931ae2ca3f27a1c2bd9e8aec604 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
95func withNativeBridgeEnabled(fs map[string][]byte, config android.Config) {
96 config.Targets[android.Android] = []android.Target{
97 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
98 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
99 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
100 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
101 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
102 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
103 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
104 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
105 }
106}
107
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900108func withManifestPackageNameOverrides(specs []string) testCustomizer {
109 return func(fs map[string][]byte, config android.Config) {
110 config.TestProductVariables.ManifestPackageNameOverrides = specs
111 }
112}
113
Jooyung Han31c470b2019-10-18 16:26:59 +0900114func withBinder32bit(fs map[string][]byte, config android.Config) {
115 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
116}
117
Jiyong Park7cd10e32020-01-14 09:22:18 +0900118func withUnbundledBuild(fs map[string][]byte, config android.Config) {
119 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
120}
121
Jooyung Han344d5432019-08-23 11:17:39 +0900122func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +0900123 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900124
125 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900126 filegroup {
127 name: "myapex-file_contexts",
128 srcs: [
129 "system/sepolicy/apex/myapex-file_contexts",
130 ],
131 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900132 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800133
Colin Crossf9aabd72020-02-15 11:29:50 -0800134 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
135
Dario Frenicde2a032019-10-27 00:29:22 +0100136 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900137
Jooyung Han344d5432019-08-23 11:17:39 +0900138 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900139 "a.java": nil,
140 "PrebuiltAppFoo.apk": nil,
141 "PrebuiltAppFooPriv.apk": nil,
142 "build/make/target/product/security": nil,
143 "apex_manifest.json": nil,
144 "AndroidManifest.xml": nil,
145 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900146 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900147 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900148 "system/sepolicy/apex/otherapex-file_contexts": nil,
149 "system/sepolicy/apex/commonapex-file_contexts": nil,
150 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800151 "mylib.cpp": nil,
152 "mylib_common.cpp": nil,
153 "mytest.cpp": nil,
154 "mytest1.cpp": nil,
155 "mytest2.cpp": nil,
156 "mytest3.cpp": nil,
157 "myprebuilt": nil,
158 "my_include": nil,
159 "foo/bar/MyClass.java": nil,
160 "prebuilt.jar": nil,
161 "vendor/foo/devkeys/test.x509.pem": nil,
162 "vendor/foo/devkeys/test.pk8": nil,
163 "testkey.x509.pem": nil,
164 "testkey.pk8": nil,
165 "testkey.override.x509.pem": nil,
166 "testkey.override.pk8": nil,
167 "vendor/foo/devkeys/testkey.avbpubkey": nil,
168 "vendor/foo/devkeys/testkey.pem": nil,
169 "NOTICE": nil,
170 "custom_notice": nil,
171 "testkey2.avbpubkey": nil,
172 "testkey2.pem": nil,
173 "myapex-arm64.apex": nil,
174 "myapex-arm.apex": nil,
175 "frameworks/base/api/current.txt": nil,
176 "framework/aidl/a.aidl": nil,
177 "build/make/core/proguard.flags": nil,
178 "build/make/core/proguard_basic_keeps.flags": nil,
179 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900180 }
181
Colin Crossf9aabd72020-02-15 11:29:50 -0800182 cc.GatherRequiredFilesForTest(fs)
183
Jooyung Han344d5432019-08-23 11:17:39 +0900184 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800185 // The fs now needs to be populated before creating the config, call handlers twice
186 // for now, once to get any fs changes, and later after the config was created to
187 // set product variables or targets.
188 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
189 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900190 }
191
Colin Cross98be1bb2019-12-13 20:41:13 -0800192 config := android.TestArchConfig(buildDir, nil, bp, fs)
193 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
194 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
195 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
196 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
197 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
198 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
199
200 for _, handler := range handlers {
201 // The fs now needs to be populated before creating the config, call handlers twice
202 // for now, earlier to get any fs changes, and now after the config was created to
203 // set product variables or targets.
204 tempFS := map[string][]byte{}
205 handler(tempFS, config)
206 }
207
208 ctx := android.NewTestArchContext()
209 ctx.RegisterModuleType("apex", BundleFactory)
210 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
211 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
212 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
213 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
214 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
215 ctx.RegisterModuleType("override_apex", overrideApexFactory)
216
Jooyung Hana57af4a2020-01-23 05:36:59 +0000217 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
218 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
219
Paul Duffin77980a82019-12-19 16:01:36 +0000220 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800221 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800222 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
223 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800224 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000225 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800226 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800227 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000228 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000229 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000230 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900231 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800232
Colin Cross98be1bb2019-12-13 20:41:13 -0800233 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800234 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800235
236 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900237
Jooyung Han5c998b92019-06-27 11:30:33 +0900238 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900239}
240
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700241func setUp() {
242 var err error
243 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700245 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900246 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247}
248
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700249func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900250 os.RemoveAll(buildDir)
251}
252
Jooyung Han643adc42020-02-27 13:50:06 +0900253// ensure that 'result' equals 'expected'
254func ensureEquals(t *testing.T, result string, expected string) {
255 t.Helper()
256 if result != expected {
257 t.Errorf("%q != %q", expected, result)
258 }
259}
260
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261// ensure that 'result' contains 'expected'
262func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900263 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900264 if !strings.Contains(result, expected) {
265 t.Errorf("%q is not found in %q", expected, result)
266 }
267}
268
269// ensures that 'result' does not contain 'notExpected'
270func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900271 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900272 if strings.Contains(result, notExpected) {
273 t.Errorf("%q is found in %q", notExpected, result)
274 }
275}
276
277func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900278 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900279 if !android.InList(expected, result) {
280 t.Errorf("%q is not found in %v", expected, result)
281 }
282}
283
284func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900285 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900286 if android.InList(notExpected, result) {
287 t.Errorf("%q is found in %v", notExpected, result)
288 }
289}
290
Jooyung Hane1633032019-08-01 17:41:43 +0900291func ensureListEmpty(t *testing.T, result []string) {
292 t.Helper()
293 if len(result) > 0 {
294 t.Errorf("%q is expected to be empty", result)
295 }
296}
297
Jiyong Park25fc6a92018-11-18 18:02:45 +0900298// Minimal test
299func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700300 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900301 apex_defaults {
302 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900303 manifest: ":myapex.manifest",
304 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900305 key: "myapex.key",
306 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800307 multilib: {
308 both: {
309 binaries: ["foo",],
310 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900311 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900312 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313 }
314
Jiyong Park30ca9372019-02-07 16:27:23 +0900315 apex {
316 name: "myapex",
317 defaults: ["myapex-defaults"],
318 }
319
Jiyong Park25fc6a92018-11-18 18:02:45 +0900320 apex_key {
321 name: "myapex.key",
322 public_key: "testkey.avbpubkey",
323 private_key: "testkey.pem",
324 }
325
Jiyong Park809bb722019-02-13 21:33:49 +0900326 filegroup {
327 name: "myapex.manifest",
328 srcs: ["apex_manifest.json"],
329 }
330
331 filegroup {
332 name: "myapex.androidmanifest",
333 srcs: ["AndroidManifest.xml"],
334 }
335
Jiyong Park25fc6a92018-11-18 18:02:45 +0900336 cc_library {
337 name: "mylib",
338 srcs: ["mylib.cpp"],
339 shared_libs: ["mylib2"],
340 system_shared_libs: [],
341 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000342 // TODO: remove //apex_available:platform
343 apex_available: [
344 "//apex_available:platform",
345 "myapex",
346 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900347 }
348
Alex Light3d673592019-01-18 14:37:31 -0800349 cc_binary {
350 name: "foo",
351 srcs: ["mylib.cpp"],
352 compile_multilib: "both",
353 multilib: {
354 lib32: {
355 suffix: "32",
356 },
357 lib64: {
358 suffix: "64",
359 },
360 },
361 symlinks: ["foo_link_"],
362 symlink_preferred_arch: true,
363 system_shared_libs: [],
364 static_executable: true,
365 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000366 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800367 }
368
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 cc_library {
370 name: "mylib2",
371 srcs: ["mylib.cpp"],
372 system_shared_libs: [],
373 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900374 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000375 // TODO: remove //apex_available:platform
376 apex_available: [
377 "//apex_available:platform",
378 "myapex",
379 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900380 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900381
382 java_library {
383 name: "myjar",
384 srcs: ["foo/bar/MyClass.java"],
385 sdk_version: "none",
386 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900387 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900388 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000389 // TODO: remove //apex_available:platform
390 apex_available: [
391 "//apex_available:platform",
392 "myapex",
393 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900394 }
395
396 java_library {
397 name: "myotherjar",
398 srcs: ["foo/bar/MyClass.java"],
399 sdk_version: "none",
400 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900401 // TODO: remove //apex_available:platform
402 apex_available: [
403 "//apex_available:platform",
404 "myapex",
405 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900406 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900407
408 java_library {
409 name: "mysharedjar",
410 srcs: ["foo/bar/MyClass.java"],
411 sdk_version: "none",
412 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900413 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900414 `)
415
Sundong Ahnabb64432019-10-22 13:58:29 +0900416 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900417
418 optFlags := apexRule.Args["opt_flags"]
419 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700420 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900421 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900422
Jiyong Park25fc6a92018-11-18 18:02:45 +0900423 copyCmds := apexRule.Args["copy_commands"]
424
425 // Ensure that main rule creates an output
426 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
427
428 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800429 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900430 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900431
432 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800433 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900434 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900435
436 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800437 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
438 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900439 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
440 // .. but not for java libs
441 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900442 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800443
Colin Cross7113d202019-11-20 16:39:12 -0800444 // Ensure that the platform variant ends with _shared or _common
445 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
446 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900447 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
448 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900449 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
450
451 // Ensure that dynamic dependency to java libs are not included
452 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800453
454 // Ensure that all symlinks are present.
455 found_foo_link_64 := false
456 found_foo := false
457 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900458 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800459 if strings.HasSuffix(cmd, "bin/foo") {
460 found_foo = true
461 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
462 found_foo_link_64 = true
463 }
464 }
465 }
466 good := found_foo && found_foo_link_64
467 if !good {
468 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
469 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900470
Sundong Ahnabb64432019-10-22 13:58:29 +0900471 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700472 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700473 if len(noticeInputs) != 2 {
474 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900475 }
476 ensureListContains(t, noticeInputs, "NOTICE")
477 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900478
479 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900480 ensureListContains(t, depsInfo, "myjar <- myapex")
481 ensureListContains(t, depsInfo, "mylib <- myapex")
482 ensureListContains(t, depsInfo, "mylib2 <- mylib")
483 ensureListContains(t, depsInfo, "myotherjar <- myjar")
484 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800485}
486
Jooyung Hanf21c7972019-12-16 22:32:06 +0900487func TestDefaults(t *testing.T) {
488 ctx, _ := testApex(t, `
489 apex_defaults {
490 name: "myapex-defaults",
491 key: "myapex.key",
492 prebuilts: ["myetc"],
493 native_shared_libs: ["mylib"],
494 java_libs: ["myjar"],
495 apps: ["AppFoo"],
496 }
497
498 prebuilt_etc {
499 name: "myetc",
500 src: "myprebuilt",
501 }
502
503 apex {
504 name: "myapex",
505 defaults: ["myapex-defaults"],
506 }
507
508 apex_key {
509 name: "myapex.key",
510 public_key: "testkey.avbpubkey",
511 private_key: "testkey.pem",
512 }
513
514 cc_library {
515 name: "mylib",
516 system_shared_libs: [],
517 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000518 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900519 }
520
521 java_library {
522 name: "myjar",
523 srcs: ["foo/bar/MyClass.java"],
524 sdk_version: "none",
525 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000526 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900527 }
528
529 android_app {
530 name: "AppFoo",
531 srcs: ["foo/bar/MyClass.java"],
532 sdk_version: "none",
533 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000534 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900535 }
536 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000537 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900538 "etc/myetc",
539 "javalib/myjar.jar",
540 "lib64/mylib.so",
541 "app/AppFoo/AppFoo.apk",
542 })
543}
544
Jooyung Han01a3ee22019-11-02 02:52:25 +0900545func TestApexManifest(t *testing.T) {
546 ctx, _ := testApex(t, `
547 apex {
548 name: "myapex",
549 key: "myapex.key",
550 }
551
552 apex_key {
553 name: "myapex.key",
554 public_key: "testkey.avbpubkey",
555 private_key: "testkey.pem",
556 }
557 `)
558
559 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900560 args := module.Rule("apexRule").Args
561 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
562 t.Error("manifest should be apex_manifest.pb, but " + manifest)
563 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900564}
565
Alex Light5098a612018-11-29 17:12:15 -0800566func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700567 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800568 apex {
569 name: "myapex",
570 key: "myapex.key",
571 payload_type: "zip",
572 native_shared_libs: ["mylib"],
573 }
574
575 apex_key {
576 name: "myapex.key",
577 public_key: "testkey.avbpubkey",
578 private_key: "testkey.pem",
579 }
580
581 cc_library {
582 name: "mylib",
583 srcs: ["mylib.cpp"],
584 shared_libs: ["mylib2"],
585 system_shared_libs: [],
586 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000587 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800588 }
589
590 cc_library {
591 name: "mylib2",
592 srcs: ["mylib.cpp"],
593 system_shared_libs: [],
594 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000595 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800596 }
597 `)
598
Sundong Ahnabb64432019-10-22 13:58:29 +0900599 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800600 copyCmds := zipApexRule.Args["copy_commands"]
601
602 // Ensure that main rule creates an output
603 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
604
605 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800606 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800607
608 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800609 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800610
611 // Ensure that both direct and indirect deps are copied into apex
612 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
613 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900614}
615
616func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700617 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900618 apex {
619 name: "myapex",
620 key: "myapex.key",
621 native_shared_libs: ["mylib", "mylib3"],
622 }
623
624 apex_key {
625 name: "myapex.key",
626 public_key: "testkey.avbpubkey",
627 private_key: "testkey.pem",
628 }
629
630 cc_library {
631 name: "mylib",
632 srcs: ["mylib.cpp"],
633 shared_libs: ["mylib2", "mylib3"],
634 system_shared_libs: [],
635 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000636 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900637 }
638
639 cc_library {
640 name: "mylib2",
641 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900642 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900643 system_shared_libs: [],
644 stl: "none",
645 stubs: {
646 versions: ["1", "2", "3"],
647 },
648 }
649
650 cc_library {
651 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900652 srcs: ["mylib.cpp"],
653 shared_libs: ["mylib4"],
654 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900655 stl: "none",
656 stubs: {
657 versions: ["10", "11", "12"],
658 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000659 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900660 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900661
662 cc_library {
663 name: "mylib4",
664 srcs: ["mylib.cpp"],
665 system_shared_libs: [],
666 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000667 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900668 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669 `)
670
Sundong Ahnabb64432019-10-22 13:58:29 +0900671 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900672 copyCmds := apexRule.Args["copy_commands"]
673
674 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800675 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900676
677 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800678 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900679
680 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800681 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900682
Colin Cross7113d202019-11-20 16:39:12 -0800683 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900684
685 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900686 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900687 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900688 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900689
690 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Cross7113d202019-11-20 16:39:12 -0800691 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900692 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800693 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900694
695 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900696 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900697 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900698
699 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900700 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900701
Jooyung Hana57af4a2020-01-23 05:36:59 +0000702 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900703 "lib64/mylib.so",
704 "lib64/mylib3.so",
705 "lib64/mylib4.so",
706 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900707}
708
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900709func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700710 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900711 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900712 name: "myapex2",
713 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900714 native_shared_libs: ["mylib"],
715 }
716
717 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900718 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900719 public_key: "testkey.avbpubkey",
720 private_key: "testkey.pem",
721 }
722
723 cc_library {
724 name: "mylib",
725 srcs: ["mylib.cpp"],
726 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900727 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900728 system_shared_libs: [],
729 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000730 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900731 }
732
733 cc_library {
734 name: "libfoo",
735 srcs: ["mylib.cpp"],
736 shared_libs: ["libbar"],
737 system_shared_libs: [],
738 stl: "none",
739 stubs: {
740 versions: ["10", "20", "30"],
741 },
742 }
743
744 cc_library {
745 name: "libbar",
746 srcs: ["mylib.cpp"],
747 system_shared_libs: [],
748 stl: "none",
749 }
750
Jiyong Park678c8812020-02-07 17:25:49 +0900751 cc_library_static {
752 name: "libbaz",
753 srcs: ["mylib.cpp"],
754 system_shared_libs: [],
755 stl: "none",
756 apex_available: [ "myapex2" ],
757 }
758
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900759 `)
760
Jiyong Park83dc74b2020-01-14 18:38:44 +0900761 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900762 copyCmds := apexRule.Args["copy_commands"]
763
764 // Ensure that direct non-stubs dep is always included
765 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
766
767 // Ensure that indirect stubs dep is not included
768 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
769
770 // Ensure that dependency of stubs is not included
771 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
772
Jiyong Park83dc74b2020-01-14 18:38:44 +0900773 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900774
775 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900776 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900777 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900778 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900779
Jiyong Park3ff16992019-12-27 14:11:47 +0900780 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900781
782 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
783 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900784
785 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900786
787 ensureListContains(t, depsInfo, "mylib <- myapex2")
788 ensureListContains(t, depsInfo, "libbaz <- mylib")
789 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900790}
791
Jooyung Hand3639552019-08-09 12:57:43 +0900792func TestApexWithRuntimeLibsDependency(t *testing.T) {
793 /*
794 myapex
795 |
796 v (runtime_libs)
797 mylib ------+------> libfoo [provides stub]
798 |
799 `------> libbar
800 */
801 ctx, _ := testApex(t, `
802 apex {
803 name: "myapex",
804 key: "myapex.key",
805 native_shared_libs: ["mylib"],
806 }
807
808 apex_key {
809 name: "myapex.key",
810 public_key: "testkey.avbpubkey",
811 private_key: "testkey.pem",
812 }
813
814 cc_library {
815 name: "mylib",
816 srcs: ["mylib.cpp"],
817 runtime_libs: ["libfoo", "libbar"],
818 system_shared_libs: [],
819 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000820 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900821 }
822
823 cc_library {
824 name: "libfoo",
825 srcs: ["mylib.cpp"],
826 system_shared_libs: [],
827 stl: "none",
828 stubs: {
829 versions: ["10", "20", "30"],
830 },
831 }
832
833 cc_library {
834 name: "libbar",
835 srcs: ["mylib.cpp"],
836 system_shared_libs: [],
837 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000838 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900839 }
840
841 `)
842
Sundong Ahnabb64432019-10-22 13:58:29 +0900843 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900844 copyCmds := apexRule.Args["copy_commands"]
845
846 // Ensure that direct non-stubs dep is always included
847 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
848
849 // Ensure that indirect stubs dep is not included
850 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
851
852 // Ensure that runtime_libs dep in included
853 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
854
Sundong Ahnabb64432019-10-22 13:58:29 +0900855 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900856 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
857 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900858
859}
860
Jooyung Han9c80bae2019-08-20 17:30:57 +0900861func TestApexDependencyToLLNDK(t *testing.T) {
862 ctx, _ := testApex(t, `
863 apex {
864 name: "myapex",
865 key: "myapex.key",
866 use_vendor: true,
867 native_shared_libs: ["mylib"],
868 }
869
870 apex_key {
871 name: "myapex.key",
872 public_key: "testkey.avbpubkey",
873 private_key: "testkey.pem",
874 }
875
876 cc_library {
877 name: "mylib",
878 srcs: ["mylib.cpp"],
879 vendor_available: true,
880 shared_libs: ["libbar"],
881 system_shared_libs: [],
882 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000883 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900884 }
885
886 cc_library {
887 name: "libbar",
888 srcs: ["mylib.cpp"],
889 system_shared_libs: [],
890 stl: "none",
891 }
892
893 llndk_library {
894 name: "libbar",
895 symbol_file: "",
896 }
Jooyung Handc782442019-11-01 03:14:38 +0900897 `, func(fs map[string][]byte, config android.Config) {
898 setUseVendorWhitelistForTest(config, []string{"myapex"})
899 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900900
Sundong Ahnabb64432019-10-22 13:58:29 +0900901 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900902 copyCmds := apexRule.Args["copy_commands"]
903
904 // Ensure that LLNDK dep is not included
905 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
906
Sundong Ahnabb64432019-10-22 13:58:29 +0900907 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900908 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900909
910 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900911 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900912
913}
914
Jiyong Park25fc6a92018-11-18 18:02:45 +0900915func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700916 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900917 apex {
918 name: "myapex",
919 key: "myapex.key",
920 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
921 }
922
923 apex_key {
924 name: "myapex.key",
925 public_key: "testkey.avbpubkey",
926 private_key: "testkey.pem",
927 }
928
929 cc_library {
930 name: "mylib",
931 srcs: ["mylib.cpp"],
932 shared_libs: ["libdl#27"],
933 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000934 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900935 }
936
937 cc_library_shared {
938 name: "mylib_shared",
939 srcs: ["mylib.cpp"],
940 shared_libs: ["libdl#27"],
941 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000942 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900943 }
944
945 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900946 name: "libBootstrap",
947 srcs: ["mylib.cpp"],
948 stl: "none",
949 bootstrap: true,
950 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900951 `)
952
Sundong Ahnabb64432019-10-22 13:58:29 +0900953 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900954 copyCmds := apexRule.Args["copy_commands"]
955
956 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800957 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900958 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
959 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900960
961 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900962 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900963
Colin Cross7113d202019-11-20 16:39:12 -0800964 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
965 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
966 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967
968 // For dependency to libc
969 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900970 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900971 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900972 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900973 // ... Cflags from stub is correctly exported to mylib
974 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
975 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
976
977 // For dependency to libm
978 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800979 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900981 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900982 // ... and is not compiling with the stub
983 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
984 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
985
986 // For dependency to libdl
987 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900988 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900989 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900990 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
991 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800993 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900994 // ... Cflags from stub is correctly exported to mylib
995 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
996 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900997
998 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800999 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1000 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1001 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1002 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001003}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001004
Jooyung Han03b51852020-02-26 22:45:42 +09001005func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
1006 // there are three links between liba --> libz
1007 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
1008 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
1009 // 3) (platform) -> liba -> libz : this should be non-stub link
1010 ctx, _ := testApex(t, `
1011 apex {
1012 name: "myapex",
1013 key: "myapex.key",
1014 native_shared_libs: ["libx"],
1015 min_sdk_version: "2",
1016 }
1017
1018 apex {
1019 name: "otherapex",
1020 key: "myapex.key",
1021 native_shared_libs: ["liby"],
1022 min_sdk_version: "3",
1023 }
1024
1025 apex_key {
1026 name: "myapex.key",
1027 public_key: "testkey.avbpubkey",
1028 private_key: "testkey.pem",
1029 }
1030
1031 cc_library {
1032 name: "libx",
1033 shared_libs: ["liba"],
1034 system_shared_libs: [],
1035 stl: "none",
1036 apex_available: [ "myapex" ],
1037 }
1038
1039 cc_library {
1040 name: "liby",
1041 shared_libs: ["liba"],
1042 system_shared_libs: [],
1043 stl: "none",
1044 apex_available: [ "otherapex" ],
1045 }
1046
1047 cc_library {
1048 name: "liba",
1049 shared_libs: ["libz"],
1050 system_shared_libs: [],
1051 stl: "none",
1052 apex_available: [
1053 "//apex_available:anyapex",
1054 "//apex_available:platform",
1055 ],
1056 }
1057
1058 cc_library {
1059 name: "libz",
1060 system_shared_libs: [],
1061 stl: "none",
1062 stubs: {
1063 versions: ["1", "3"],
1064 },
1065 }
1066 `, withUnbundledBuild)
1067
1068 expectLink := func(from, from_variant, to, to_variant string) {
1069 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1070 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1071 }
1072 expectNoLink := func(from, from_variant, to, to_variant string) {
1073 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1074 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1075 }
1076 // platform liba is linked to non-stub version
1077 expectLink("liba", "shared", "libz", "shared")
1078 // liba in myapex is linked to #1
1079 expectLink("liba", "shared_myapex", "libz", "shared_1")
1080 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1081 expectNoLink("liba", "shared_myapex", "libz", "shared")
1082 // liba in otherapex is linked to #3
1083 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1084 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1085 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1086}
1087
1088func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1089 ctx, _ := testApex(t, `
1090 apex {
1091 name: "myapex",
1092 key: "myapex.key",
1093 native_shared_libs: ["libx"],
1094 }
1095
1096 apex_key {
1097 name: "myapex.key",
1098 public_key: "testkey.avbpubkey",
1099 private_key: "testkey.pem",
1100 }
1101
1102 cc_library {
1103 name: "libx",
1104 shared_libs: ["libz"],
1105 system_shared_libs: [],
1106 stl: "none",
1107 apex_available: [ "myapex" ],
1108 }
1109
1110 cc_library {
1111 name: "libz",
1112 system_shared_libs: [],
1113 stl: "none",
1114 stubs: {
1115 versions: ["1", "2"],
1116 },
1117 }
1118 `)
1119
1120 expectLink := func(from, from_variant, to, to_variant string) {
1121 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1122 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1123 }
1124 expectNoLink := func(from, from_variant, to, to_variant string) {
1125 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1126 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1127 }
1128 expectLink("libx", "shared_myapex", "libz", "shared_2")
1129 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1130 expectNoLink("libx", "shared_myapex", "libz", "shared")
1131}
1132
1133func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1134 ctx, _ := testApex(t, `
1135 apex {
1136 name: "myapex",
1137 key: "myapex.key",
1138 native_shared_libs: ["libx"],
1139 }
1140
1141 apex_key {
1142 name: "myapex.key",
1143 public_key: "testkey.avbpubkey",
1144 private_key: "testkey.pem",
1145 }
1146
1147 cc_library {
1148 name: "libx",
1149 system_shared_libs: [],
1150 stl: "none",
1151 apex_available: [ "myapex" ],
1152 stubs: {
1153 versions: ["1", "2"],
1154 },
1155 }
1156
1157 cc_library {
1158 name: "libz",
1159 shared_libs: ["libx"],
1160 system_shared_libs: [],
1161 stl: "none",
1162 }
1163 `)
1164
1165 expectLink := func(from, from_variant, to, to_variant string) {
1166 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1167 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1168 }
1169 expectNoLink := func(from, from_variant, to, to_variant string) {
1170 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1171 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1172 }
1173 expectLink("libz", "shared", "libx", "shared_2")
1174 expectNoLink("libz", "shared", "libz", "shared_1")
1175 expectNoLink("libz", "shared", "libz", "shared")
1176}
1177
1178func TestQApexesUseLatestStubsInBundledBuilds(t *testing.T) {
1179 ctx, _ := testApex(t, `
1180 apex {
1181 name: "myapex",
1182 key: "myapex.key",
1183 native_shared_libs: ["libx"],
1184 min_sdk_version: "29",
1185 }
1186
1187 apex_key {
1188 name: "myapex.key",
1189 public_key: "testkey.avbpubkey",
1190 private_key: "testkey.pem",
1191 }
1192
1193 cc_library {
1194 name: "libx",
1195 shared_libs: ["libbar"],
1196 apex_available: [ "myapex" ],
1197 }
1198
1199 cc_library {
1200 name: "libbar",
1201 stubs: {
1202 versions: ["29", "30"],
1203 },
1204 }
1205 `)
1206 expectLink := func(from, from_variant, to, to_variant string) {
1207 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1208 libFlags := ld.Args["libFlags"]
1209 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1210 }
1211 expectLink("libx", "shared_myapex", "libbar", "shared_30")
1212}
1213
1214func TestQTargetApexUseStaticUnwinder(t *testing.T) {
1215 ctx, _ := testApex(t, `
1216 apex {
1217 name: "myapex",
1218 key: "myapex.key",
1219 native_shared_libs: ["libx"],
1220 min_sdk_version: "29",
1221 }
1222
1223 apex_key {
1224 name: "myapex.key",
1225 public_key: "testkey.avbpubkey",
1226 private_key: "testkey.pem",
1227 }
1228
1229 cc_library {
1230 name: "libx",
1231 apex_available: [ "myapex" ],
1232 }
1233
1234 `, withUnbundledBuild)
1235
1236 // ensure apex variant of c++ is linked with static unwinder
1237 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1238 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1239 // note that platform variant is not.
1240 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1241 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1242
1243 libFlags := ctx.ModuleForTests("libx", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1244 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_myapex/libc++.so")
1245 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_29/libc.so") // min_sdk_version applied
1246}
1247
1248func TestInvalidMinSdkVersion(t *testing.T) {
1249 testApexError(t, `"libz" .*: min_sdk_version is set 29.*`, `
1250 apex {
1251 name: "myapex",
1252 key: "myapex.key",
1253 native_shared_libs: ["libx"],
1254 min_sdk_version: "29",
1255 }
1256
1257 apex_key {
1258 name: "myapex.key",
1259 public_key: "testkey.avbpubkey",
1260 private_key: "testkey.pem",
1261 }
1262
1263 cc_library {
1264 name: "libx",
1265 shared_libs: ["libz"],
1266 system_shared_libs: [],
1267 stl: "none",
1268 apex_available: [ "myapex" ],
1269 }
1270
1271 cc_library {
1272 name: "libz",
1273 system_shared_libs: [],
1274 stl: "none",
1275 stubs: {
1276 versions: ["30"],
1277 },
1278 }
1279 `, withUnbundledBuild)
1280
1281 testApexError(t, `"myapex" .*: min_sdk_version: should be .*`, `
1282 apex {
1283 name: "myapex",
1284 key: "myapex.key",
1285 min_sdk_version: "R",
1286 }
1287
1288 apex_key {
1289 name: "myapex.key",
1290 public_key: "testkey.avbpubkey",
1291 private_key: "testkey.pem",
1292 }
1293 `)
1294}
1295
Jiyong Park7c2ee712018-12-07 00:42:25 +09001296func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001297 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001298 apex {
1299 name: "myapex",
1300 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001301 native_shared_libs: ["mylib"],
1302 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001303 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001304 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001305 }
1306
1307 apex_key {
1308 name: "myapex.key",
1309 public_key: "testkey.avbpubkey",
1310 private_key: "testkey.pem",
1311 }
1312
1313 prebuilt_etc {
1314 name: "myetc",
1315 src: "myprebuilt",
1316 sub_dir: "foo/bar",
1317 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001318
1319 cc_library {
1320 name: "mylib",
1321 srcs: ["mylib.cpp"],
1322 relative_install_path: "foo/bar",
1323 system_shared_libs: [],
1324 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001325 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001326 }
1327
1328 cc_binary {
1329 name: "mybin",
1330 srcs: ["mylib.cpp"],
1331 relative_install_path: "foo/bar",
1332 system_shared_libs: [],
1333 static_executable: true,
1334 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001335 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001336 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001337 `)
1338
Sundong Ahnabb64432019-10-22 13:58:29 +09001339 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001340 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1341
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001342 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001343 ensureListContains(t, dirs, "etc")
1344 ensureListContains(t, dirs, "etc/foo")
1345 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001346 ensureListContains(t, dirs, "lib64")
1347 ensureListContains(t, dirs, "lib64/foo")
1348 ensureListContains(t, dirs, "lib64/foo/bar")
1349 ensureListContains(t, dirs, "lib")
1350 ensureListContains(t, dirs, "lib/foo")
1351 ensureListContains(t, dirs, "lib/foo/bar")
1352
Jiyong Parkbd13e442019-03-15 18:10:35 +09001353 ensureListContains(t, dirs, "bin")
1354 ensureListContains(t, dirs, "bin/foo")
1355 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001356}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001357
Jooyung Han35155c42020-02-06 17:33:20 +09001358func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
1359 ctx, _ := testApex(t, `
1360 apex {
1361 name: "myapex",
1362 key: "myapex.key",
1363 multilib: {
1364 both: {
1365 native_shared_libs: ["mylib"],
1366 binaries: ["mybin"],
1367 },
1368 },
1369 compile_multilib: "both",
1370 native_bridge_supported: true,
1371 }
1372
1373 apex_key {
1374 name: "myapex.key",
1375 public_key: "testkey.avbpubkey",
1376 private_key: "testkey.pem",
1377 }
1378
1379 cc_library {
1380 name: "mylib",
1381 relative_install_path: "foo/bar",
1382 system_shared_libs: [],
1383 stl: "none",
1384 apex_available: [ "myapex" ],
1385 native_bridge_supported: true,
1386 }
1387
1388 cc_binary {
1389 name: "mybin",
1390 relative_install_path: "foo/bar",
1391 system_shared_libs: [],
1392 static_executable: true,
1393 stl: "none",
1394 apex_available: [ "myapex" ],
1395 native_bridge_supported: true,
1396 compile_multilib: "both", // default is "first" for binary
1397 multilib: {
1398 lib64: {
1399 suffix: "64",
1400 },
1401 },
1402 }
1403 `, withNativeBridgeEnabled)
1404 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1405 "bin/foo/bar/mybin",
1406 "bin/foo/bar/mybin64",
1407 "bin/arm/foo/bar/mybin",
1408 "bin/arm64/foo/bar/mybin64",
1409 "lib/foo/bar/mylib.so",
1410 "lib/arm/foo/bar/mylib.so",
1411 "lib64/foo/bar/mylib.so",
1412 "lib64/arm64/foo/bar/mylib.so",
1413 })
1414}
1415
Jiyong Parkda6eb592018-12-19 17:12:36 +09001416func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001417 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001418 apex {
1419 name: "myapex",
1420 key: "myapex.key",
1421 native_shared_libs: ["mylib"],
1422 use_vendor: true,
1423 }
1424
1425 apex_key {
1426 name: "myapex.key",
1427 public_key: "testkey.avbpubkey",
1428 private_key: "testkey.pem",
1429 }
1430
1431 cc_library {
1432 name: "mylib",
1433 srcs: ["mylib.cpp"],
1434 shared_libs: ["mylib2"],
1435 system_shared_libs: [],
1436 vendor_available: true,
1437 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001438 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001439 }
1440
1441 cc_library {
1442 name: "mylib2",
1443 srcs: ["mylib.cpp"],
1444 system_shared_libs: [],
1445 vendor_available: true,
1446 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001447 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001448 }
Jooyung Handc782442019-11-01 03:14:38 +09001449 `, func(fs map[string][]byte, config android.Config) {
1450 setUseVendorWhitelistForTest(config, []string{"myapex"})
1451 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001452
1453 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001454 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001455 for _, implicit := range i.Implicits {
1456 inputsList = append(inputsList, implicit.String())
1457 }
1458 }
1459 inputsString := strings.Join(inputsList, " ")
1460
1461 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001462 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1463 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001464
1465 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001466 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1467 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001468}
Jiyong Park16e91a02018-12-20 18:18:08 +09001469
Jooyung Handc782442019-11-01 03:14:38 +09001470func TestUseVendorRestriction(t *testing.T) {
1471 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1472 apex {
1473 name: "myapex",
1474 key: "myapex.key",
1475 use_vendor: true,
1476 }
1477 apex_key {
1478 name: "myapex.key",
1479 public_key: "testkey.avbpubkey",
1480 private_key: "testkey.pem",
1481 }
1482 `, func(fs map[string][]byte, config android.Config) {
1483 setUseVendorWhitelistForTest(config, []string{""})
1484 })
1485 // no error with whitelist
1486 testApex(t, `
1487 apex {
1488 name: "myapex",
1489 key: "myapex.key",
1490 use_vendor: true,
1491 }
1492 apex_key {
1493 name: "myapex.key",
1494 public_key: "testkey.avbpubkey",
1495 private_key: "testkey.pem",
1496 }
1497 `, func(fs map[string][]byte, config android.Config) {
1498 setUseVendorWhitelistForTest(config, []string{"myapex"})
1499 })
1500}
1501
Jooyung Han5c998b92019-06-27 11:30:33 +09001502func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1503 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1504 apex {
1505 name: "myapex",
1506 key: "myapex.key",
1507 native_shared_libs: ["mylib"],
1508 use_vendor: true,
1509 }
1510
1511 apex_key {
1512 name: "myapex.key",
1513 public_key: "testkey.avbpubkey",
1514 private_key: "testkey.pem",
1515 }
1516
1517 cc_library {
1518 name: "mylib",
1519 srcs: ["mylib.cpp"],
1520 system_shared_libs: [],
1521 stl: "none",
1522 }
1523 `)
1524}
1525
Jiyong Park16e91a02018-12-20 18:18:08 +09001526func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001527 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001528 apex {
1529 name: "myapex",
1530 key: "myapex.key",
1531 native_shared_libs: ["mylib"],
1532 }
1533
1534 apex_key {
1535 name: "myapex.key",
1536 public_key: "testkey.avbpubkey",
1537 private_key: "testkey.pem",
1538 }
1539
1540 cc_library {
1541 name: "mylib",
1542 srcs: ["mylib.cpp"],
1543 system_shared_libs: [],
1544 stl: "none",
1545 stubs: {
1546 versions: ["1", "2", "3"],
1547 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001548 apex_available: [
1549 "//apex_available:platform",
1550 "myapex",
1551 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001552 }
1553
1554 cc_binary {
1555 name: "not_in_apex",
1556 srcs: ["mylib.cpp"],
1557 static_libs: ["mylib"],
1558 static_executable: true,
1559 system_shared_libs: [],
1560 stl: "none",
1561 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001562 `)
1563
Colin Cross7113d202019-11-20 16:39:12 -08001564 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001565
1566 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001567 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001568}
Jiyong Park9335a262018-12-24 11:31:58 +09001569
1570func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001571 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001572 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001573 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001574 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001575 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001576 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001577 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001578 }
1579
1580 cc_library {
1581 name: "mylib",
1582 srcs: ["mylib.cpp"],
1583 system_shared_libs: [],
1584 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001585 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001586 }
1587
1588 apex_key {
1589 name: "myapex.key",
1590 public_key: "testkey.avbpubkey",
1591 private_key: "testkey.pem",
1592 }
1593
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001594 android_app_certificate {
1595 name: "myapex.certificate",
1596 certificate: "testkey",
1597 }
1598
1599 android_app_certificate {
1600 name: "myapex.certificate.override",
1601 certificate: "testkey.override",
1602 }
1603
Jiyong Park9335a262018-12-24 11:31:58 +09001604 `)
1605
1606 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001607 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001608
1609 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1610 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1611 "vendor/foo/devkeys/testkey.avbpubkey")
1612 }
1613 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1614 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1615 "vendor/foo/devkeys/testkey.pem")
1616 }
1617
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001618 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001619 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001620 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001621 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001622 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001623 }
1624}
Jiyong Park58e364a2019-01-19 19:24:06 +09001625
Jooyung Hanf121a652019-12-17 14:30:11 +09001626func TestCertificate(t *testing.T) {
1627 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1628 ctx, _ := testApex(t, `
1629 apex {
1630 name: "myapex",
1631 key: "myapex.key",
1632 }
1633 apex_key {
1634 name: "myapex.key",
1635 public_key: "testkey.avbpubkey",
1636 private_key: "testkey.pem",
1637 }`)
1638 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1639 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1640 if actual := rule.Args["certificates"]; actual != expected {
1641 t.Errorf("certificates should be %q, not %q", expected, actual)
1642 }
1643 })
1644 t.Run("override when unspecified", func(t *testing.T) {
1645 ctx, _ := testApex(t, `
1646 apex {
1647 name: "myapex_keytest",
1648 key: "myapex.key",
1649 file_contexts: ":myapex-file_contexts",
1650 }
1651 apex_key {
1652 name: "myapex.key",
1653 public_key: "testkey.avbpubkey",
1654 private_key: "testkey.pem",
1655 }
1656 android_app_certificate {
1657 name: "myapex.certificate.override",
1658 certificate: "testkey.override",
1659 }`)
1660 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1661 expected := "testkey.override.x509.pem testkey.override.pk8"
1662 if actual := rule.Args["certificates"]; actual != expected {
1663 t.Errorf("certificates should be %q, not %q", expected, actual)
1664 }
1665 })
1666 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1667 ctx, _ := testApex(t, `
1668 apex {
1669 name: "myapex",
1670 key: "myapex.key",
1671 certificate: ":myapex.certificate",
1672 }
1673 apex_key {
1674 name: "myapex.key",
1675 public_key: "testkey.avbpubkey",
1676 private_key: "testkey.pem",
1677 }
1678 android_app_certificate {
1679 name: "myapex.certificate",
1680 certificate: "testkey",
1681 }`)
1682 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1683 expected := "testkey.x509.pem testkey.pk8"
1684 if actual := rule.Args["certificates"]; actual != expected {
1685 t.Errorf("certificates should be %q, not %q", expected, actual)
1686 }
1687 })
1688 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1689 ctx, _ := testApex(t, `
1690 apex {
1691 name: "myapex_keytest",
1692 key: "myapex.key",
1693 file_contexts: ":myapex-file_contexts",
1694 certificate: ":myapex.certificate",
1695 }
1696 apex_key {
1697 name: "myapex.key",
1698 public_key: "testkey.avbpubkey",
1699 private_key: "testkey.pem",
1700 }
1701 android_app_certificate {
1702 name: "myapex.certificate.override",
1703 certificate: "testkey.override",
1704 }`)
1705 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1706 expected := "testkey.override.x509.pem testkey.override.pk8"
1707 if actual := rule.Args["certificates"]; actual != expected {
1708 t.Errorf("certificates should be %q, not %q", expected, actual)
1709 }
1710 })
1711 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1712 ctx, _ := testApex(t, `
1713 apex {
1714 name: "myapex",
1715 key: "myapex.key",
1716 certificate: "testkey",
1717 }
1718 apex_key {
1719 name: "myapex.key",
1720 public_key: "testkey.avbpubkey",
1721 private_key: "testkey.pem",
1722 }`)
1723 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1724 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1725 if actual := rule.Args["certificates"]; actual != expected {
1726 t.Errorf("certificates should be %q, not %q", expected, actual)
1727 }
1728 })
1729 t.Run("override when specified as <name>", func(t *testing.T) {
1730 ctx, _ := testApex(t, `
1731 apex {
1732 name: "myapex_keytest",
1733 key: "myapex.key",
1734 file_contexts: ":myapex-file_contexts",
1735 certificate: "testkey",
1736 }
1737 apex_key {
1738 name: "myapex.key",
1739 public_key: "testkey.avbpubkey",
1740 private_key: "testkey.pem",
1741 }
1742 android_app_certificate {
1743 name: "myapex.certificate.override",
1744 certificate: "testkey.override",
1745 }`)
1746 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1747 expected := "testkey.override.x509.pem testkey.override.pk8"
1748 if actual := rule.Args["certificates"]; actual != expected {
1749 t.Errorf("certificates should be %q, not %q", expected, actual)
1750 }
1751 })
1752}
1753
Jiyong Park58e364a2019-01-19 19:24:06 +09001754func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001755 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001756 apex {
1757 name: "myapex",
1758 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001759 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001760 }
1761
1762 apex {
1763 name: "otherapex",
1764 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001765 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09001766 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09001767 }
1768
1769 apex_key {
1770 name: "myapex.key",
1771 public_key: "testkey.avbpubkey",
1772 private_key: "testkey.pem",
1773 }
1774
1775 cc_library {
1776 name: "mylib",
1777 srcs: ["mylib.cpp"],
1778 system_shared_libs: [],
1779 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001780 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001781 "myapex",
1782 "otherapex",
1783 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001784 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09001785 cc_library {
1786 name: "mylib2",
1787 srcs: ["mylib.cpp"],
1788 system_shared_libs: [],
1789 stl: "none",
1790 apex_available: [
1791 "myapex",
1792 "otherapex",
1793 ],
1794 use_apex_name_macro: true,
1795 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001796 `)
1797
Jooyung Hanc87a0592020-03-02 17:44:33 +09001798 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001799 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001800 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001801 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001802
Jooyung Hanccce2f22020-03-07 03:45:53 +09001803 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001804 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1805 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001806 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09001807 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001808
Jooyung Hanccce2f22020-03-07 03:45:53 +09001809 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001810 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1811 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001812 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09001813 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001814
Jooyung Hanc87a0592020-03-02 17:44:33 +09001815 // When cc_library sets use_apex_name_macro: true
1816 // apex variants define additional macro to distinguish which apex variant it is built for
1817
1818 // non-APEX variant does not have __ANDROID_APEX__ defined
1819 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1820 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1821
1822 // APEX variant has __ANDROID_APEX__ defined
1823 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001824 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001825 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1826 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001827
Jooyung Hanc87a0592020-03-02 17:44:33 +09001828 // APEX variant has __ANDROID_APEX__ defined
1829 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001830 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001831 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1832 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001833}
Jiyong Park7e636d02019-01-28 16:16:54 +09001834
1835func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001836 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001837 apex {
1838 name: "myapex",
1839 key: "myapex.key",
1840 native_shared_libs: ["mylib"],
1841 }
1842
1843 apex_key {
1844 name: "myapex.key",
1845 public_key: "testkey.avbpubkey",
1846 private_key: "testkey.pem",
1847 }
1848
1849 cc_library_headers {
1850 name: "mylib_headers",
1851 export_include_dirs: ["my_include"],
1852 system_shared_libs: [],
1853 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001854 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001855 }
1856
1857 cc_library {
1858 name: "mylib",
1859 srcs: ["mylib.cpp"],
1860 system_shared_libs: [],
1861 stl: "none",
1862 header_libs: ["mylib_headers"],
1863 export_header_lib_headers: ["mylib_headers"],
1864 stubs: {
1865 versions: ["1", "2", "3"],
1866 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001867 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001868 }
1869
1870 cc_library {
1871 name: "otherlib",
1872 srcs: ["mylib.cpp"],
1873 system_shared_libs: [],
1874 stl: "none",
1875 shared_libs: ["mylib"],
1876 }
1877 `)
1878
Colin Cross7113d202019-11-20 16:39:12 -08001879 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001880
1881 // Ensure that the include path of the header lib is exported to 'otherlib'
1882 ensureContains(t, cFlags, "-Imy_include")
1883}
Alex Light9670d332019-01-29 18:07:33 -08001884
Jiyong Park7cd10e32020-01-14 09:22:18 +09001885type fileInApex struct {
1886 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001887 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001888 isLink bool
1889}
1890
Jooyung Hana57af4a2020-01-23 05:36:59 +00001891func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001892 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001893 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001894 copyCmds := apexRule.Args["copy_commands"]
1895 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001896 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001897 for _, cmd := range strings.Split(copyCmds, "&&") {
1898 cmd = strings.TrimSpace(cmd)
1899 if cmd == "" {
1900 continue
1901 }
1902 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001903 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001904 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001905 switch terms[0] {
1906 case "mkdir":
1907 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001908 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001909 t.Fatal("copyCmds contains invalid cp command", cmd)
1910 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001911 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001912 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001913 isLink = false
1914 case "ln":
1915 if len(terms) != 3 && len(terms) != 4 {
1916 // ln LINK TARGET or ln -s LINK TARGET
1917 t.Fatal("copyCmds contains invalid ln command", cmd)
1918 }
1919 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001920 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001921 isLink = true
1922 default:
1923 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1924 }
1925 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001926 index := strings.Index(dst, imageApexDir)
1927 if index == -1 {
1928 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1929 }
1930 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001931 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001932 }
1933 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001934 return ret
1935}
1936
Jooyung Hana57af4a2020-01-23 05:36:59 +00001937func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1938 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001939 var failed bool
1940 var surplus []string
1941 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001942 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001943 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001944 for _, expected := range files {
1945 if matched, _ := path.Match(expected, file.path); matched {
1946 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001947 mactchFound = true
1948 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001949 }
1950 }
Jooyung Hane6436d72020-02-27 13:31:56 +09001951 if !mactchFound {
1952 surplus = append(surplus, file.path)
1953 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001954 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001955
Jooyung Han31c470b2019-10-18 16:26:59 +09001956 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001957 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001958 t.Log("surplus files", surplus)
1959 failed = true
1960 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001961
1962 if len(files) > len(filesMatched) {
1963 var missing []string
1964 for _, expected := range files {
1965 if !filesMatched[expected] {
1966 missing = append(missing, expected)
1967 }
1968 }
1969 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001970 t.Log("missing files", missing)
1971 failed = true
1972 }
1973 if failed {
1974 t.Fail()
1975 }
1976}
1977
Jooyung Han344d5432019-08-23 11:17:39 +09001978func TestVndkApexCurrent(t *testing.T) {
1979 ctx, _ := testApex(t, `
1980 apex_vndk {
1981 name: "myapex",
1982 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001983 }
1984
1985 apex_key {
1986 name: "myapex.key",
1987 public_key: "testkey.avbpubkey",
1988 private_key: "testkey.pem",
1989 }
1990
1991 cc_library {
1992 name: "libvndk",
1993 srcs: ["mylib.cpp"],
1994 vendor_available: true,
1995 vndk: {
1996 enabled: true,
1997 },
1998 system_shared_libs: [],
1999 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002000 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002001 }
2002
2003 cc_library {
2004 name: "libvndksp",
2005 srcs: ["mylib.cpp"],
2006 vendor_available: true,
2007 vndk: {
2008 enabled: true,
2009 support_system_process: true,
2010 },
2011 system_shared_libs: [],
2012 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002013 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002014 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002015 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002016
Jooyung Hana57af4a2020-01-23 05:36:59 +00002017 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002018 "lib/libvndk.so",
2019 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002020 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002021 "lib64/libvndk.so",
2022 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002023 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002024 "etc/llndk.libraries.VER.txt",
2025 "etc/vndkcore.libraries.VER.txt",
2026 "etc/vndksp.libraries.VER.txt",
2027 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002028 })
Jooyung Han344d5432019-08-23 11:17:39 +09002029}
2030
2031func TestVndkApexWithPrebuilt(t *testing.T) {
2032 ctx, _ := testApex(t, `
2033 apex_vndk {
2034 name: "myapex",
2035 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002036 }
2037
2038 apex_key {
2039 name: "myapex.key",
2040 public_key: "testkey.avbpubkey",
2041 private_key: "testkey.pem",
2042 }
2043
2044 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002045 name: "libvndk",
2046 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002047 vendor_available: true,
2048 vndk: {
2049 enabled: true,
2050 },
2051 system_shared_libs: [],
2052 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002053 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002054 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002055
2056 cc_prebuilt_library_shared {
2057 name: "libvndk.arm",
2058 srcs: ["libvndk.arm.so"],
2059 vendor_available: true,
2060 vndk: {
2061 enabled: true,
2062 },
2063 enabled: false,
2064 arch: {
2065 arm: {
2066 enabled: true,
2067 },
2068 },
2069 system_shared_libs: [],
2070 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002071 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002072 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002073 `+vndkLibrariesTxtFiles("current"),
2074 withFiles(map[string][]byte{
2075 "libvndk.so": nil,
2076 "libvndk.arm.so": nil,
2077 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002078
Jooyung Hana57af4a2020-01-23 05:36:59 +00002079 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002080 "lib/libvndk.so",
2081 "lib/libvndk.arm.so",
2082 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002083 "lib/libc++.so",
2084 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002085 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002086 })
Jooyung Han344d5432019-08-23 11:17:39 +09002087}
2088
Jooyung Han39edb6c2019-11-06 16:53:07 +09002089func vndkLibrariesTxtFiles(vers ...string) (result string) {
2090 for _, v := range vers {
2091 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002092 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002093 result += `
2094 vndk_libraries_txt {
2095 name: "` + txt + `.libraries.txt",
2096 }
2097 `
2098 }
2099 } else {
2100 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2101 result += `
2102 prebuilt_etc {
2103 name: "` + txt + `.libraries.` + v + `.txt",
2104 src: "dummy.txt",
2105 }
2106 `
2107 }
2108 }
2109 }
2110 return
2111}
2112
Jooyung Han344d5432019-08-23 11:17:39 +09002113func TestVndkApexVersion(t *testing.T) {
2114 ctx, _ := testApex(t, `
2115 apex_vndk {
2116 name: "myapex_v27",
2117 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002118 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002119 vndk_version: "27",
2120 }
2121
2122 apex_key {
2123 name: "myapex.key",
2124 public_key: "testkey.avbpubkey",
2125 private_key: "testkey.pem",
2126 }
2127
Jooyung Han31c470b2019-10-18 16:26:59 +09002128 vndk_prebuilt_shared {
2129 name: "libvndk27",
2130 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002131 vendor_available: true,
2132 vndk: {
2133 enabled: true,
2134 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002135 target_arch: "arm64",
2136 arch: {
2137 arm: {
2138 srcs: ["libvndk27_arm.so"],
2139 },
2140 arm64: {
2141 srcs: ["libvndk27_arm64.so"],
2142 },
2143 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002144 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002145 }
2146
2147 vndk_prebuilt_shared {
2148 name: "libvndk27",
2149 version: "27",
2150 vendor_available: true,
2151 vndk: {
2152 enabled: true,
2153 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002154 target_arch: "x86_64",
2155 arch: {
2156 x86: {
2157 srcs: ["libvndk27_x86.so"],
2158 },
2159 x86_64: {
2160 srcs: ["libvndk27_x86_64.so"],
2161 },
2162 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002163 }
2164 `+vndkLibrariesTxtFiles("27"),
2165 withFiles(map[string][]byte{
2166 "libvndk27_arm.so": nil,
2167 "libvndk27_arm64.so": nil,
2168 "libvndk27_x86.so": nil,
2169 "libvndk27_x86_64.so": nil,
2170 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002171
Jooyung Hana57af4a2020-01-23 05:36:59 +00002172 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002173 "lib/libvndk27_arm.so",
2174 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002175 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002176 })
Jooyung Han344d5432019-08-23 11:17:39 +09002177}
2178
2179func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2180 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2181 apex_vndk {
2182 name: "myapex_v27",
2183 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002184 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002185 vndk_version: "27",
2186 }
2187 apex_vndk {
2188 name: "myapex_v27_other",
2189 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002190 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002191 vndk_version: "27",
2192 }
2193
2194 apex_key {
2195 name: "myapex.key",
2196 public_key: "testkey.avbpubkey",
2197 private_key: "testkey.pem",
2198 }
2199
2200 cc_library {
2201 name: "libvndk",
2202 srcs: ["mylib.cpp"],
2203 vendor_available: true,
2204 vndk: {
2205 enabled: true,
2206 },
2207 system_shared_libs: [],
2208 stl: "none",
2209 }
2210
2211 vndk_prebuilt_shared {
2212 name: "libvndk",
2213 version: "27",
2214 vendor_available: true,
2215 vndk: {
2216 enabled: true,
2217 },
2218 srcs: ["libvndk.so"],
2219 }
2220 `, withFiles(map[string][]byte{
2221 "libvndk.so": nil,
2222 }))
2223}
2224
Jooyung Han90eee022019-10-01 20:02:42 +09002225func TestVndkApexNameRule(t *testing.T) {
2226 ctx, _ := testApex(t, `
2227 apex_vndk {
2228 name: "myapex",
2229 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002230 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002231 }
2232 apex_vndk {
2233 name: "myapex_v28",
2234 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002235 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002236 vndk_version: "28",
2237 }
2238 apex_key {
2239 name: "myapex.key",
2240 public_key: "testkey.avbpubkey",
2241 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002242 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002243
2244 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002245 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002246 actual := proptools.String(bundle.properties.Apex_name)
2247 if !reflect.DeepEqual(actual, expected) {
2248 t.Errorf("Got '%v', expected '%v'", actual, expected)
2249 }
2250 }
2251
2252 assertApexName("com.android.vndk.vVER", "myapex")
2253 assertApexName("com.android.vndk.v28", "myapex_v28")
2254}
2255
Jooyung Han344d5432019-08-23 11:17:39 +09002256func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2257 ctx, _ := testApex(t, `
2258 apex_vndk {
2259 name: "myapex",
2260 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002261 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002262 }
2263
2264 apex_key {
2265 name: "myapex.key",
2266 public_key: "testkey.avbpubkey",
2267 private_key: "testkey.pem",
2268 }
2269
2270 cc_library {
2271 name: "libvndk",
2272 srcs: ["mylib.cpp"],
2273 vendor_available: true,
2274 native_bridge_supported: true,
2275 host_supported: true,
2276 vndk: {
2277 enabled: true,
2278 },
2279 system_shared_libs: [],
2280 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002281 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002282 }
Jooyung Han35155c42020-02-06 17:33:20 +09002283 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09002284
Jooyung Hana57af4a2020-01-23 05:36:59 +00002285 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002286 "lib/libvndk.so",
2287 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002288 "lib/libc++.so",
2289 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002290 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002291 })
Jooyung Han344d5432019-08-23 11:17:39 +09002292}
2293
2294func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2295 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2296 apex_vndk {
2297 name: "myapex",
2298 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002299 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002300 native_bridge_supported: true,
2301 }
2302
2303 apex_key {
2304 name: "myapex.key",
2305 public_key: "testkey.avbpubkey",
2306 private_key: "testkey.pem",
2307 }
2308
2309 cc_library {
2310 name: "libvndk",
2311 srcs: ["mylib.cpp"],
2312 vendor_available: true,
2313 native_bridge_supported: true,
2314 host_supported: true,
2315 vndk: {
2316 enabled: true,
2317 },
2318 system_shared_libs: [],
2319 stl: "none",
2320 }
2321 `)
2322}
2323
Jooyung Han31c470b2019-10-18 16:26:59 +09002324func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002325 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002326 apex_vndk {
2327 name: "myapex_v27",
2328 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002329 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002330 vndk_version: "27",
2331 }
2332
2333 apex_key {
2334 name: "myapex.key",
2335 public_key: "testkey.avbpubkey",
2336 private_key: "testkey.pem",
2337 }
2338
2339 vndk_prebuilt_shared {
2340 name: "libvndk27",
2341 version: "27",
2342 target_arch: "arm",
2343 vendor_available: true,
2344 vndk: {
2345 enabled: true,
2346 },
2347 arch: {
2348 arm: {
2349 srcs: ["libvndk27.so"],
2350 }
2351 },
2352 }
2353
2354 vndk_prebuilt_shared {
2355 name: "libvndk27",
2356 version: "27",
2357 target_arch: "arm",
2358 binder32bit: true,
2359 vendor_available: true,
2360 vndk: {
2361 enabled: true,
2362 },
2363 arch: {
2364 arm: {
2365 srcs: ["libvndk27binder32.so"],
2366 }
2367 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002368 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002369 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002370 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002371 withFiles(map[string][]byte{
2372 "libvndk27.so": nil,
2373 "libvndk27binder32.so": nil,
2374 }),
2375 withBinder32bit,
2376 withTargets(map[android.OsType][]android.Target{
2377 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09002378 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
2379 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09002380 },
2381 }),
2382 )
2383
Jooyung Hana57af4a2020-01-23 05:36:59 +00002384 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002385 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002386 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002387 })
2388}
2389
Jooyung Hane1633032019-08-01 17:41:43 +09002390func TestDependenciesInApexManifest(t *testing.T) {
2391 ctx, _ := testApex(t, `
2392 apex {
2393 name: "myapex_nodep",
2394 key: "myapex.key",
2395 native_shared_libs: ["lib_nodep"],
2396 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002397 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002398 }
2399
2400 apex {
2401 name: "myapex_dep",
2402 key: "myapex.key",
2403 native_shared_libs: ["lib_dep"],
2404 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002405 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002406 }
2407
2408 apex {
2409 name: "myapex_provider",
2410 key: "myapex.key",
2411 native_shared_libs: ["libfoo"],
2412 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002413 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002414 }
2415
2416 apex {
2417 name: "myapex_selfcontained",
2418 key: "myapex.key",
2419 native_shared_libs: ["lib_dep", "libfoo"],
2420 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002421 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002422 }
2423
2424 apex_key {
2425 name: "myapex.key",
2426 public_key: "testkey.avbpubkey",
2427 private_key: "testkey.pem",
2428 }
2429
2430 cc_library {
2431 name: "lib_nodep",
2432 srcs: ["mylib.cpp"],
2433 system_shared_libs: [],
2434 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002435 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002436 }
2437
2438 cc_library {
2439 name: "lib_dep",
2440 srcs: ["mylib.cpp"],
2441 shared_libs: ["libfoo"],
2442 system_shared_libs: [],
2443 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002444 apex_available: [
2445 "myapex_dep",
2446 "myapex_provider",
2447 "myapex_selfcontained",
2448 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002449 }
2450
2451 cc_library {
2452 name: "libfoo",
2453 srcs: ["mytest.cpp"],
2454 stubs: {
2455 versions: ["1"],
2456 },
2457 system_shared_libs: [],
2458 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002459 apex_available: [
2460 "myapex_provider",
2461 "myapex_selfcontained",
2462 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002463 }
2464 `)
2465
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002466 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002467 var provideNativeLibs, requireNativeLibs []string
2468
Sundong Ahnabb64432019-10-22 13:58:29 +09002469 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002470 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2471 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002472 ensureListEmpty(t, provideNativeLibs)
2473 ensureListEmpty(t, requireNativeLibs)
2474
Sundong Ahnabb64432019-10-22 13:58:29 +09002475 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002476 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2477 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002478 ensureListEmpty(t, provideNativeLibs)
2479 ensureListContains(t, requireNativeLibs, "libfoo.so")
2480
Sundong Ahnabb64432019-10-22 13:58:29 +09002481 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002482 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2483 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002484 ensureListContains(t, provideNativeLibs, "libfoo.so")
2485 ensureListEmpty(t, requireNativeLibs)
2486
Sundong Ahnabb64432019-10-22 13:58:29 +09002487 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002488 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2489 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002490 ensureListContains(t, provideNativeLibs, "libfoo.so")
2491 ensureListEmpty(t, requireNativeLibs)
2492}
2493
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002494func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002495 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002496 apex {
2497 name: "myapex",
2498 key: "myapex.key",
2499 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002500 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002501 }
2502
2503 apex_key {
2504 name: "myapex.key",
2505 public_key: "testkey.avbpubkey",
2506 private_key: "testkey.pem",
2507 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002508
2509 cc_library {
2510 name: "mylib",
2511 srcs: ["mylib.cpp"],
2512 system_shared_libs: [],
2513 stl: "none",
2514 apex_available: [
2515 "//apex_available:platform",
2516 "myapex",
2517 ],
2518 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002519 `)
2520
Sundong Ahnabb64432019-10-22 13:58:29 +09002521 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002522 apexManifestRule := module.Rule("apexManifestRule")
2523 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2524 apexRule := module.Rule("apexRule")
2525 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002526
2527 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2528 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2529 name := apexBundle.BaseModuleName()
2530 prefix := "TARGET_"
2531 var builder strings.Builder
2532 data.Custom(&builder, name, prefix, "", data)
2533 androidMk := builder.String()
2534 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2535 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002536}
2537
Alex Light0851b882019-02-07 13:20:53 -08002538func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002539 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002540 apex {
2541 name: "myapex",
2542 key: "myapex.key",
2543 native_shared_libs: ["mylib_common"],
2544 }
2545
2546 apex_key {
2547 name: "myapex.key",
2548 public_key: "testkey.avbpubkey",
2549 private_key: "testkey.pem",
2550 }
2551
2552 cc_library {
2553 name: "mylib_common",
2554 srcs: ["mylib.cpp"],
2555 system_shared_libs: [],
2556 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002557 apex_available: [
2558 "//apex_available:platform",
2559 "myapex",
2560 ],
Alex Light0851b882019-02-07 13:20:53 -08002561 }
2562 `)
2563
Sundong Ahnabb64432019-10-22 13:58:29 +09002564 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002565 apexRule := module.Rule("apexRule")
2566 copyCmds := apexRule.Args["copy_commands"]
2567
2568 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2569 t.Log("Apex was a test apex!")
2570 t.Fail()
2571 }
2572 // Ensure that main rule creates an output
2573 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2574
2575 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002576 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002577
2578 // Ensure that both direct and indirect deps are copied into apex
2579 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2580
Colin Cross7113d202019-11-20 16:39:12 -08002581 // Ensure that the platform variant ends with _shared
2582 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002583
2584 if !android.InAnyApex("mylib_common") {
2585 t.Log("Found mylib_common not in any apex!")
2586 t.Fail()
2587 }
2588}
2589
2590func TestTestApex(t *testing.T) {
2591 if android.InAnyApex("mylib_common_test") {
2592 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!")
2593 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002594 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002595 apex_test {
2596 name: "myapex",
2597 key: "myapex.key",
2598 native_shared_libs: ["mylib_common_test"],
2599 }
2600
2601 apex_key {
2602 name: "myapex.key",
2603 public_key: "testkey.avbpubkey",
2604 private_key: "testkey.pem",
2605 }
2606
2607 cc_library {
2608 name: "mylib_common_test",
2609 srcs: ["mylib.cpp"],
2610 system_shared_libs: [],
2611 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002612 // TODO: remove //apex_available:platform
2613 apex_available: [
2614 "//apex_available:platform",
2615 "myapex",
2616 ],
Alex Light0851b882019-02-07 13:20:53 -08002617 }
2618 `)
2619
Sundong Ahnabb64432019-10-22 13:58:29 +09002620 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002621 apexRule := module.Rule("apexRule")
2622 copyCmds := apexRule.Args["copy_commands"]
2623
2624 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2625 t.Log("Apex was not a test apex!")
2626 t.Fail()
2627 }
2628 // Ensure that main rule creates an output
2629 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2630
2631 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002632 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002633
2634 // Ensure that both direct and indirect deps are copied into apex
2635 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2636
Colin Cross7113d202019-11-20 16:39:12 -08002637 // Ensure that the platform variant ends with _shared
2638 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002639}
2640
Alex Light9670d332019-01-29 18:07:33 -08002641func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002642 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002643 apex {
2644 name: "myapex",
2645 key: "myapex.key",
2646 multilib: {
2647 first: {
2648 native_shared_libs: ["mylib_common"],
2649 }
2650 },
2651 target: {
2652 android: {
2653 multilib: {
2654 first: {
2655 native_shared_libs: ["mylib"],
2656 }
2657 }
2658 },
2659 host: {
2660 multilib: {
2661 first: {
2662 native_shared_libs: ["mylib2"],
2663 }
2664 }
2665 }
2666 }
2667 }
2668
2669 apex_key {
2670 name: "myapex.key",
2671 public_key: "testkey.avbpubkey",
2672 private_key: "testkey.pem",
2673 }
2674
2675 cc_library {
2676 name: "mylib",
2677 srcs: ["mylib.cpp"],
2678 system_shared_libs: [],
2679 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002680 // TODO: remove //apex_available:platform
2681 apex_available: [
2682 "//apex_available:platform",
2683 "myapex",
2684 ],
Alex Light9670d332019-01-29 18:07:33 -08002685 }
2686
2687 cc_library {
2688 name: "mylib_common",
2689 srcs: ["mylib.cpp"],
2690 system_shared_libs: [],
2691 stl: "none",
2692 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002693 // TODO: remove //apex_available:platform
2694 apex_available: [
2695 "//apex_available:platform",
2696 "myapex",
2697 ],
Alex Light9670d332019-01-29 18:07:33 -08002698 }
2699
2700 cc_library {
2701 name: "mylib2",
2702 srcs: ["mylib.cpp"],
2703 system_shared_libs: [],
2704 stl: "none",
2705 compile_multilib: "first",
2706 }
2707 `)
2708
Sundong Ahnabb64432019-10-22 13:58:29 +09002709 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002710 copyCmds := apexRule.Args["copy_commands"]
2711
2712 // Ensure that main rule creates an output
2713 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2714
2715 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002716 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2717 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2718 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002719
2720 // Ensure that both direct and indirect deps are copied into apex
2721 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2722 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2723 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2724
Colin Cross7113d202019-11-20 16:39:12 -08002725 // Ensure that the platform variant ends with _shared
2726 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2727 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2728 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002729}
Jiyong Park04480cf2019-02-06 00:16:29 +09002730
2731func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002732 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002733 apex {
2734 name: "myapex",
2735 key: "myapex.key",
2736 binaries: ["myscript"],
2737 }
2738
2739 apex_key {
2740 name: "myapex.key",
2741 public_key: "testkey.avbpubkey",
2742 private_key: "testkey.pem",
2743 }
2744
2745 sh_binary {
2746 name: "myscript",
2747 src: "mylib.cpp",
2748 filename: "myscript.sh",
2749 sub_dir: "script",
2750 }
2751 `)
2752
Sundong Ahnabb64432019-10-22 13:58:29 +09002753 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002754 copyCmds := apexRule.Args["copy_commands"]
2755
2756 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2757}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002758
Jooyung Han91df2082019-11-20 01:49:42 +09002759func TestApexInVariousPartition(t *testing.T) {
2760 testcases := []struct {
2761 propName, parition, flattenedPartition string
2762 }{
2763 {"", "system", "system_ext"},
2764 {"product_specific: true", "product", "product"},
2765 {"soc_specific: true", "vendor", "vendor"},
2766 {"proprietary: true", "vendor", "vendor"},
2767 {"vendor: true", "vendor", "vendor"},
2768 {"system_ext_specific: true", "system_ext", "system_ext"},
2769 }
2770 for _, tc := range testcases {
2771 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2772 ctx, _ := testApex(t, `
2773 apex {
2774 name: "myapex",
2775 key: "myapex.key",
2776 `+tc.propName+`
2777 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002778
Jooyung Han91df2082019-11-20 01:49:42 +09002779 apex_key {
2780 name: "myapex.key",
2781 public_key: "testkey.avbpubkey",
2782 private_key: "testkey.pem",
2783 }
2784 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002785
Jooyung Han91df2082019-11-20 01:49:42 +09002786 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2787 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2788 actual := apex.installDir.String()
2789 if actual != expected {
2790 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2791 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002792
Jooyung Han91df2082019-11-20 01:49:42 +09002793 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2794 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2795 actual = flattened.installDir.String()
2796 if actual != expected {
2797 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2798 }
2799 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002800 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002801}
Jiyong Park67882562019-03-21 01:11:21 +09002802
Jooyung Han54aca7b2019-11-20 02:26:02 +09002803func TestFileContexts(t *testing.T) {
2804 ctx, _ := testApex(t, `
2805 apex {
2806 name: "myapex",
2807 key: "myapex.key",
2808 }
2809
2810 apex_key {
2811 name: "myapex.key",
2812 public_key: "testkey.avbpubkey",
2813 private_key: "testkey.pem",
2814 }
2815 `)
2816 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2817 apexRule := module.Rule("apexRule")
2818 actual := apexRule.Args["file_contexts"]
2819 expected := "system/sepolicy/apex/myapex-file_contexts"
2820 if actual != expected {
2821 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2822 }
2823
2824 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2825 apex {
2826 name: "myapex",
2827 key: "myapex.key",
2828 file_contexts: "my_own_file_contexts",
2829 }
2830
2831 apex_key {
2832 name: "myapex.key",
2833 public_key: "testkey.avbpubkey",
2834 private_key: "testkey.pem",
2835 }
2836 `, withFiles(map[string][]byte{
2837 "my_own_file_contexts": nil,
2838 }))
2839
2840 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2841 apex {
2842 name: "myapex",
2843 key: "myapex.key",
2844 product_specific: true,
2845 file_contexts: "product_specific_file_contexts",
2846 }
2847
2848 apex_key {
2849 name: "myapex.key",
2850 public_key: "testkey.avbpubkey",
2851 private_key: "testkey.pem",
2852 }
2853 `)
2854
2855 ctx, _ = testApex(t, `
2856 apex {
2857 name: "myapex",
2858 key: "myapex.key",
2859 product_specific: true,
2860 file_contexts: "product_specific_file_contexts",
2861 }
2862
2863 apex_key {
2864 name: "myapex.key",
2865 public_key: "testkey.avbpubkey",
2866 private_key: "testkey.pem",
2867 }
2868 `, withFiles(map[string][]byte{
2869 "product_specific_file_contexts": nil,
2870 }))
2871 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2872 apexRule = module.Rule("apexRule")
2873 actual = apexRule.Args["file_contexts"]
2874 expected = "product_specific_file_contexts"
2875 if actual != expected {
2876 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2877 }
2878
2879 ctx, _ = testApex(t, `
2880 apex {
2881 name: "myapex",
2882 key: "myapex.key",
2883 product_specific: true,
2884 file_contexts: ":my-file-contexts",
2885 }
2886
2887 apex_key {
2888 name: "myapex.key",
2889 public_key: "testkey.avbpubkey",
2890 private_key: "testkey.pem",
2891 }
2892
2893 filegroup {
2894 name: "my-file-contexts",
2895 srcs: ["product_specific_file_contexts"],
2896 }
2897 `, withFiles(map[string][]byte{
2898 "product_specific_file_contexts": nil,
2899 }))
2900 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2901 apexRule = module.Rule("apexRule")
2902 actual = apexRule.Args["file_contexts"]
2903 expected = "product_specific_file_contexts"
2904 if actual != expected {
2905 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2906 }
2907}
2908
Jiyong Park67882562019-03-21 01:11:21 +09002909func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002910 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002911 apex_key {
2912 name: "myapex.key",
2913 public_key: ":my.avbpubkey",
2914 private_key: ":my.pem",
2915 product_specific: true,
2916 }
2917
2918 filegroup {
2919 name: "my.avbpubkey",
2920 srcs: ["testkey2.avbpubkey"],
2921 }
2922
2923 filegroup {
2924 name: "my.pem",
2925 srcs: ["testkey2.pem"],
2926 }
2927 `)
2928
2929 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2930 expected_pubkey := "testkey2.avbpubkey"
2931 actual_pubkey := apex_key.public_key_file.String()
2932 if actual_pubkey != expected_pubkey {
2933 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2934 }
2935 expected_privkey := "testkey2.pem"
2936 actual_privkey := apex_key.private_key_file.String()
2937 if actual_privkey != expected_privkey {
2938 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2939 }
2940}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002941
2942func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002943 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002944 prebuilt_apex {
2945 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002946 arch: {
2947 arm64: {
2948 src: "myapex-arm64.apex",
2949 },
2950 arm: {
2951 src: "myapex-arm.apex",
2952 },
2953 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002954 }
2955 `)
2956
2957 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2958
Jiyong Parkc95714e2019-03-29 14:23:10 +09002959 expectedInput := "myapex-arm64.apex"
2960 if prebuilt.inputApex.String() != expectedInput {
2961 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2962 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002963}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002964
2965func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002966 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002967 prebuilt_apex {
2968 name: "myapex",
2969 src: "myapex-arm.apex",
2970 filename: "notmyapex.apex",
2971 }
2972 `)
2973
2974 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2975
2976 expected := "notmyapex.apex"
2977 if p.installFilename != expected {
2978 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2979 }
2980}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002981
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002982func TestPrebuiltOverrides(t *testing.T) {
2983 ctx, config := testApex(t, `
2984 prebuilt_apex {
2985 name: "myapex.prebuilt",
2986 src: "myapex-arm.apex",
2987 overrides: [
2988 "myapex",
2989 ],
2990 }
2991 `)
2992
2993 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2994
2995 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002996 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002997 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002998 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002999 }
3000}
3001
Roland Levillain630846d2019-06-26 12:48:34 +01003002func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003003 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003004 apex_test {
3005 name: "myapex",
3006 key: "myapex.key",
3007 tests: [
3008 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003009 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003010 ],
3011 }
3012
3013 apex_key {
3014 name: "myapex.key",
3015 public_key: "testkey.avbpubkey",
3016 private_key: "testkey.pem",
3017 }
3018
3019 cc_test {
3020 name: "mytest",
3021 gtest: false,
3022 srcs: ["mytest.cpp"],
3023 relative_install_path: "test",
3024 system_shared_libs: [],
3025 static_executable: true,
3026 stl: "none",
3027 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003028
3029 cc_test {
3030 name: "mytests",
3031 gtest: false,
3032 srcs: [
3033 "mytest1.cpp",
3034 "mytest2.cpp",
3035 "mytest3.cpp",
3036 ],
3037 test_per_src: true,
3038 relative_install_path: "test",
3039 system_shared_libs: [],
3040 static_executable: true,
3041 stl: "none",
3042 }
Roland Levillain630846d2019-06-26 12:48:34 +01003043 `)
3044
Sundong Ahnabb64432019-10-22 13:58:29 +09003045 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003046 copyCmds := apexRule.Args["copy_commands"]
3047
3048 // Ensure that test dep is copied into apex.
3049 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003050
3051 // Ensure that test deps built with `test_per_src` are copied into apex.
3052 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3053 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3054 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003055
3056 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003057 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003058 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3059 name := apexBundle.BaseModuleName()
3060 prefix := "TARGET_"
3061 var builder strings.Builder
3062 data.Custom(&builder, name, prefix, "", data)
3063 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003064 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3065 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3066 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3067 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003068 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003069 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003070 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003071}
3072
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003073func TestInstallExtraFlattenedApexes(t *testing.T) {
3074 ctx, config := testApex(t, `
3075 apex {
3076 name: "myapex",
3077 key: "myapex.key",
3078 }
3079 apex_key {
3080 name: "myapex.key",
3081 public_key: "testkey.avbpubkey",
3082 private_key: "testkey.pem",
3083 }
3084 `, func(fs map[string][]byte, config android.Config) {
3085 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3086 })
3087 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003088 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003089 mk := android.AndroidMkDataForTest(t, config, "", ab)
3090 var builder strings.Builder
3091 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3092 androidMk := builder.String()
3093 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3094}
3095
Jooyung Han5c998b92019-06-27 11:30:33 +09003096func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003097 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003098 apex {
3099 name: "myapex",
3100 key: "myapex.key",
3101 native_shared_libs: ["mylib"],
3102 uses: ["commonapex"],
3103 }
3104
3105 apex {
3106 name: "commonapex",
3107 key: "myapex.key",
3108 native_shared_libs: ["libcommon"],
3109 provide_cpp_shared_libs: true,
3110 }
3111
3112 apex_key {
3113 name: "myapex.key",
3114 public_key: "testkey.avbpubkey",
3115 private_key: "testkey.pem",
3116 }
3117
3118 cc_library {
3119 name: "mylib",
3120 srcs: ["mylib.cpp"],
3121 shared_libs: ["libcommon"],
3122 system_shared_libs: [],
3123 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003124 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003125 }
3126
3127 cc_library {
3128 name: "libcommon",
3129 srcs: ["mylib_common.cpp"],
3130 system_shared_libs: [],
3131 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003132 // TODO: remove //apex_available:platform
3133 apex_available: [
3134 "//apex_available:platform",
3135 "commonapex",
3136 "myapex",
3137 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003138 }
3139 `)
3140
Sundong Ahnabb64432019-10-22 13:58:29 +09003141 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003142 apexRule1 := module1.Rule("apexRule")
3143 copyCmds1 := apexRule1.Args["copy_commands"]
3144
Sundong Ahnabb64432019-10-22 13:58:29 +09003145 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003146 apexRule2 := module2.Rule("apexRule")
3147 copyCmds2 := apexRule2.Args["copy_commands"]
3148
Colin Cross7113d202019-11-20 16:39:12 -08003149 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3150 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003151 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3152 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3153 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3154}
3155
3156func TestApexUsesFailsIfNotProvided(t *testing.T) {
3157 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3158 apex {
3159 name: "myapex",
3160 key: "myapex.key",
3161 uses: ["commonapex"],
3162 }
3163
3164 apex {
3165 name: "commonapex",
3166 key: "myapex.key",
3167 }
3168
3169 apex_key {
3170 name: "myapex.key",
3171 public_key: "testkey.avbpubkey",
3172 private_key: "testkey.pem",
3173 }
3174 `)
3175 testApexError(t, `uses: "commonapex" is not a provider`, `
3176 apex {
3177 name: "myapex",
3178 key: "myapex.key",
3179 uses: ["commonapex"],
3180 }
3181
3182 cc_library {
3183 name: "commonapex",
3184 system_shared_libs: [],
3185 stl: "none",
3186 }
3187
3188 apex_key {
3189 name: "myapex.key",
3190 public_key: "testkey.avbpubkey",
3191 private_key: "testkey.pem",
3192 }
3193 `)
3194}
3195
3196func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3197 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3198 apex {
3199 name: "myapex",
3200 key: "myapex.key",
3201 use_vendor: true,
3202 uses: ["commonapex"],
3203 }
3204
3205 apex {
3206 name: "commonapex",
3207 key: "myapex.key",
3208 provide_cpp_shared_libs: true,
3209 }
3210
3211 apex_key {
3212 name: "myapex.key",
3213 public_key: "testkey.avbpubkey",
3214 private_key: "testkey.pem",
3215 }
Jooyung Handc782442019-11-01 03:14:38 +09003216 `, func(fs map[string][]byte, config android.Config) {
3217 setUseVendorWhitelistForTest(config, []string{"myapex"})
3218 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003219}
3220
Jooyung Hand48f3c32019-08-23 11:18:57 +09003221func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3222 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3223 apex {
3224 name: "myapex",
3225 key: "myapex.key",
3226 native_shared_libs: ["libfoo"],
3227 }
3228
3229 apex_key {
3230 name: "myapex.key",
3231 public_key: "testkey.avbpubkey",
3232 private_key: "testkey.pem",
3233 }
3234
3235 cc_library {
3236 name: "libfoo",
3237 stl: "none",
3238 system_shared_libs: [],
3239 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003240 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003241 }
3242 `)
3243 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3244 apex {
3245 name: "myapex",
3246 key: "myapex.key",
3247 java_libs: ["myjar"],
3248 }
3249
3250 apex_key {
3251 name: "myapex.key",
3252 public_key: "testkey.avbpubkey",
3253 private_key: "testkey.pem",
3254 }
3255
3256 java_library {
3257 name: "myjar",
3258 srcs: ["foo/bar/MyClass.java"],
3259 sdk_version: "none",
3260 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003261 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003262 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003263 }
3264 `)
3265}
3266
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003267func TestApexWithApps(t *testing.T) {
3268 ctx, _ := testApex(t, `
3269 apex {
3270 name: "myapex",
3271 key: "myapex.key",
3272 apps: [
3273 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003274 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003275 ],
3276 }
3277
3278 apex_key {
3279 name: "myapex.key",
3280 public_key: "testkey.avbpubkey",
3281 private_key: "testkey.pem",
3282 }
3283
3284 android_app {
3285 name: "AppFoo",
3286 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003287 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003288 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003289 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08003290 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003291 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003292 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003293
3294 android_app {
3295 name: "AppFooPriv",
3296 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003297 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09003298 system_modules: "none",
3299 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08003300 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003301 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003302 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003303
3304 cc_library_shared {
3305 name: "libjni",
3306 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003307 shared_libs: ["libfoo"],
3308 stl: "none",
3309 system_shared_libs: [],
3310 apex_available: [ "myapex" ],
3311 sdk_version: "current",
3312 }
3313
3314 cc_library_shared {
3315 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09003316 stl: "none",
3317 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003318 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08003319 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003320 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003321 `)
3322
Sundong Ahnabb64432019-10-22 13:58:29 +09003323 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003324 apexRule := module.Rule("apexRule")
3325 copyCmds := apexRule.Args["copy_commands"]
3326
3327 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003328 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003329
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003330 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3331 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003332 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003333 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003334 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003335 // JNI libraries including transitive deps are
3336 for _, jni := range []string{"libjni", "libfoo"} {
3337 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
3338 // ... embedded inside APK (jnilibs.zip)
3339 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3340 // ... and not directly inside the APEX
3341 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3342 }
Dario Frenicde2a032019-10-27 00:29:22 +01003343}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003344
Dario Frenicde2a032019-10-27 00:29:22 +01003345func TestApexWithAppImports(t *testing.T) {
3346 ctx, _ := testApex(t, `
3347 apex {
3348 name: "myapex",
3349 key: "myapex.key",
3350 apps: [
3351 "AppFooPrebuilt",
3352 "AppFooPrivPrebuilt",
3353 ],
3354 }
3355
3356 apex_key {
3357 name: "myapex.key",
3358 public_key: "testkey.avbpubkey",
3359 private_key: "testkey.pem",
3360 }
3361
3362 android_app_import {
3363 name: "AppFooPrebuilt",
3364 apk: "PrebuiltAppFoo.apk",
3365 presigned: true,
3366 dex_preopt: {
3367 enabled: false,
3368 },
3369 }
3370
3371 android_app_import {
3372 name: "AppFooPrivPrebuilt",
3373 apk: "PrebuiltAppFooPriv.apk",
3374 privileged: true,
3375 presigned: true,
3376 dex_preopt: {
3377 enabled: false,
3378 },
3379 }
3380 `)
3381
Sundong Ahnabb64432019-10-22 13:58:29 +09003382 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003383 apexRule := module.Rule("apexRule")
3384 copyCmds := apexRule.Args["copy_commands"]
3385
3386 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3387 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003388}
3389
Dario Freni6f3937c2019-12-20 22:58:03 +00003390func TestApexWithTestHelperApp(t *testing.T) {
3391 ctx, _ := testApex(t, `
3392 apex {
3393 name: "myapex",
3394 key: "myapex.key",
3395 apps: [
3396 "TesterHelpAppFoo",
3397 ],
3398 }
3399
3400 apex_key {
3401 name: "myapex.key",
3402 public_key: "testkey.avbpubkey",
3403 private_key: "testkey.pem",
3404 }
3405
3406 android_test_helper_app {
3407 name: "TesterHelpAppFoo",
3408 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003409 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003410 }
3411
3412 `)
3413
3414 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3415 apexRule := module.Rule("apexRule")
3416 copyCmds := apexRule.Args["copy_commands"]
3417
3418 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3419}
3420
Jooyung Han18020ea2019-11-13 10:50:48 +09003421func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3422 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09003423 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003424 apex {
3425 name: "myapex",
3426 key: "myapex.key",
3427 native_shared_libs: ["libfoo"],
3428 }
3429
3430 apex_key {
3431 name: "myapex.key",
3432 public_key: "testkey.avbpubkey",
3433 private_key: "testkey.pem",
3434 }
3435
3436 apex {
3437 name: "otherapex",
3438 key: "myapex.key",
3439 native_shared_libs: ["libfoo"],
3440 }
3441
3442 cc_defaults {
3443 name: "libfoo-defaults",
3444 apex_available: ["otherapex"],
3445 }
3446
3447 cc_library {
3448 name: "libfoo",
3449 defaults: ["libfoo-defaults"],
3450 stl: "none",
3451 system_shared_libs: [],
3452 }`)
3453}
3454
Jiyong Park127b40b2019-09-30 16:04:35 +09003455func TestApexAvailable(t *testing.T) {
3456 // libfoo is not available to myapex, but only to otherapex
3457 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3458 apex {
3459 name: "myapex",
3460 key: "myapex.key",
3461 native_shared_libs: ["libfoo"],
3462 }
3463
3464 apex_key {
3465 name: "myapex.key",
3466 public_key: "testkey.avbpubkey",
3467 private_key: "testkey.pem",
3468 }
3469
3470 apex {
3471 name: "otherapex",
3472 key: "otherapex.key",
3473 native_shared_libs: ["libfoo"],
3474 }
3475
3476 apex_key {
3477 name: "otherapex.key",
3478 public_key: "testkey.avbpubkey",
3479 private_key: "testkey.pem",
3480 }
3481
3482 cc_library {
3483 name: "libfoo",
3484 stl: "none",
3485 system_shared_libs: [],
3486 apex_available: ["otherapex"],
3487 }`)
3488
Jooyung Han5e9013b2020-03-10 06:23:13 +09003489 // libbbaz is an indirect dep
3490 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003491 apex {
3492 name: "myapex",
3493 key: "myapex.key",
3494 native_shared_libs: ["libfoo"],
3495 }
3496
3497 apex_key {
3498 name: "myapex.key",
3499 public_key: "testkey.avbpubkey",
3500 private_key: "testkey.pem",
3501 }
3502
Jiyong Park127b40b2019-09-30 16:04:35 +09003503 cc_library {
3504 name: "libfoo",
3505 stl: "none",
3506 shared_libs: ["libbar"],
3507 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003508 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003509 }
3510
3511 cc_library {
3512 name: "libbar",
3513 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09003514 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003515 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003516 apex_available: ["myapex"],
3517 }
3518
3519 cc_library {
3520 name: "libbaz",
3521 stl: "none",
3522 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003523 }`)
3524
3525 testApexError(t, "\"otherapex\" is not a valid module name", `
3526 apex {
3527 name: "myapex",
3528 key: "myapex.key",
3529 native_shared_libs: ["libfoo"],
3530 }
3531
3532 apex_key {
3533 name: "myapex.key",
3534 public_key: "testkey.avbpubkey",
3535 private_key: "testkey.pem",
3536 }
3537
3538 cc_library {
3539 name: "libfoo",
3540 stl: "none",
3541 system_shared_libs: [],
3542 apex_available: ["otherapex"],
3543 }`)
3544
3545 ctx, _ := testApex(t, `
3546 apex {
3547 name: "myapex",
3548 key: "myapex.key",
3549 native_shared_libs: ["libfoo", "libbar"],
3550 }
3551
3552 apex_key {
3553 name: "myapex.key",
3554 public_key: "testkey.avbpubkey",
3555 private_key: "testkey.pem",
3556 }
3557
3558 cc_library {
3559 name: "libfoo",
3560 stl: "none",
3561 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09003562 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003563 apex_available: ["myapex"],
3564 }
3565
3566 cc_library {
3567 name: "libbar",
3568 stl: "none",
3569 system_shared_libs: [],
3570 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09003571 }
3572
3573 cc_library {
3574 name: "libbaz",
3575 stl: "none",
3576 system_shared_libs: [],
3577 stubs: {
3578 versions: ["10", "20", "30"],
3579 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003580 }`)
3581
3582 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003583 // TODO(jiyong) the checks for the platform variant are removed because we now create
3584 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3585 // the platform variants are not used from other platform modules. When that is done,
3586 // these checks will be replaced by expecting a specific error message that will be
3587 // emitted when the platform variant is used.
3588 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3589 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3590 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3591 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003592
3593 ctx, _ = testApex(t, `
3594 apex {
3595 name: "myapex",
3596 key: "myapex.key",
3597 }
3598
3599 apex_key {
3600 name: "myapex.key",
3601 public_key: "testkey.avbpubkey",
3602 private_key: "testkey.pem",
3603 }
3604
3605 cc_library {
3606 name: "libfoo",
3607 stl: "none",
3608 system_shared_libs: [],
3609 apex_available: ["//apex_available:platform"],
3610 }`)
3611
3612 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003613 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3614 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003615
3616 ctx, _ = testApex(t, `
3617 apex {
3618 name: "myapex",
3619 key: "myapex.key",
3620 native_shared_libs: ["libfoo"],
3621 }
3622
3623 apex_key {
3624 name: "myapex.key",
3625 public_key: "testkey.avbpubkey",
3626 private_key: "testkey.pem",
3627 }
3628
3629 cc_library {
3630 name: "libfoo",
3631 stl: "none",
3632 system_shared_libs: [],
3633 apex_available: ["myapex"],
3634 static: {
3635 apex_available: ["//apex_available:platform"],
3636 },
3637 }`)
3638
3639 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003640 // TODO(jiyong) the checks for the platform variant are removed because we now create
3641 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3642 // the platform variants are not used from other platform modules. When that is done,
3643 // these checks will be replaced by expecting a specific error message that will be
3644 // emitted when the platform variant is used.
3645 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3646 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3647 // // but the static variant is available to both myapex and the platform
3648 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3649 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003650}
3651
Jiyong Park5d790c32019-11-15 18:40:32 +09003652func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003653 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003654 apex {
3655 name: "myapex",
3656 key: "myapex.key",
3657 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003658 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003659 }
3660
3661 override_apex {
3662 name: "override_myapex",
3663 base: "myapex",
3664 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003665 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003666 logging_parent: "com.foo.bar",
Jiyong Park5d790c32019-11-15 18:40:32 +09003667 }
3668
3669 apex_key {
3670 name: "myapex.key",
3671 public_key: "testkey.avbpubkey",
3672 private_key: "testkey.pem",
3673 }
3674
3675 android_app {
3676 name: "app",
3677 srcs: ["foo/bar/MyClass.java"],
3678 package_name: "foo",
3679 sdk_version: "none",
3680 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003681 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003682 }
3683
3684 override_android_app {
3685 name: "override_app",
3686 base: "app",
3687 package_name: "bar",
3688 }
Jiyong Park20bacab2020-03-03 11:45:41 +09003689 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003690
Jiyong Park317645e2019-12-05 13:20:58 +09003691 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3692 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3693 if originalVariant.GetOverriddenBy() != "" {
3694 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3695 }
3696 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3697 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3698 }
3699
Jiyong Park5d790c32019-11-15 18:40:32 +09003700 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3701 apexRule := module.Rule("apexRule")
3702 copyCmds := apexRule.Args["copy_commands"]
3703
3704 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3705 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003706
3707 apexBundle := module.Module().(*apexBundle)
3708 name := apexBundle.Name()
3709 if name != "override_myapex" {
3710 t.Errorf("name should be \"override_myapex\", but was %q", name)
3711 }
3712
Baligh Uddin004d7172020-02-19 21:29:28 -08003713 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3714 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3715 }
3716
Jiyong Park20bacab2020-03-03 11:45:41 +09003717 optFlags := apexRule.Args["opt_flags"]
3718 ensureContains(t, optFlags, "--override_apk_package_name com.android.myapex")
3719
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003720 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3721 var builder strings.Builder
3722 data.Custom(&builder, name, "TARGET_", "", data)
3723 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003724 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003725 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3726 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003727 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003728 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003729 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003730 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3731 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003732}
3733
Jooyung Han214bf372019-11-12 13:03:50 +09003734func TestLegacyAndroid10Support(t *testing.T) {
3735 ctx, _ := testApex(t, `
3736 apex {
3737 name: "myapex",
3738 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003739 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09003740 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09003741 }
3742
3743 apex_key {
3744 name: "myapex.key",
3745 public_key: "testkey.avbpubkey",
3746 private_key: "testkey.pem",
3747 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003748
3749 cc_library {
3750 name: "mylib",
3751 srcs: ["mylib.cpp"],
3752 stl: "libc++",
3753 system_shared_libs: [],
3754 apex_available: [ "myapex" ],
3755 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003756 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003757
3758 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3759 args := module.Rule("apexRule").Args
3760 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003761 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003762
3763 // The copies of the libraries in the apex should have one more dependency than
3764 // the ones outside the apex, namely the unwinder. Ideally we should check
3765 // the dependency names directly here but for some reason the names are blank in
3766 // this test.
3767 for _, lib := range []string{"libc++", "mylib"} {
3768 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3769 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3770 if len(apexImplicits) != len(nonApexImplicits)+1 {
3771 t.Errorf("%q missing unwinder dep", lib)
3772 }
3773 }
Jooyung Han214bf372019-11-12 13:03:50 +09003774}
3775
Jooyung Han58f26ab2019-12-18 15:34:32 +09003776func TestJavaSDKLibrary(t *testing.T) {
3777 ctx, _ := testApex(t, `
3778 apex {
3779 name: "myapex",
3780 key: "myapex.key",
3781 java_libs: ["foo"],
3782 }
3783
3784 apex_key {
3785 name: "myapex.key",
3786 public_key: "testkey.avbpubkey",
3787 private_key: "testkey.pem",
3788 }
3789
3790 java_sdk_library {
3791 name: "foo",
3792 srcs: ["a.java"],
3793 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003794 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003795 }
3796 `, withFiles(map[string][]byte{
3797 "api/current.txt": nil,
3798 "api/removed.txt": nil,
3799 "api/system-current.txt": nil,
3800 "api/system-removed.txt": nil,
3801 "api/test-current.txt": nil,
3802 "api/test-removed.txt": nil,
3803 }))
3804
3805 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003806 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003807 "javalib/foo.jar",
3808 "etc/permissions/foo.xml",
3809 })
3810 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003811 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3812 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003813}
3814
atrost6e126252020-01-27 17:01:16 +00003815func TestCompatConfig(t *testing.T) {
3816 ctx, _ := testApex(t, `
3817 apex {
3818 name: "myapex",
3819 key: "myapex.key",
3820 prebuilts: ["myjar-platform-compat-config"],
3821 java_libs: ["myjar"],
3822 }
3823
3824 apex_key {
3825 name: "myapex.key",
3826 public_key: "testkey.avbpubkey",
3827 private_key: "testkey.pem",
3828 }
3829
3830 platform_compat_config {
3831 name: "myjar-platform-compat-config",
3832 src: ":myjar",
3833 }
3834
3835 java_library {
3836 name: "myjar",
3837 srcs: ["foo/bar/MyClass.java"],
3838 sdk_version: "none",
3839 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003840 apex_available: [ "myapex" ],
3841 }
3842 `)
3843 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3844 "etc/compatconfig/myjar-platform-compat-config.xml",
3845 "javalib/myjar.jar",
3846 })
3847}
3848
Jiyong Park479321d2019-12-16 11:47:12 +09003849func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3850 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3851 apex {
3852 name: "myapex",
3853 key: "myapex.key",
3854 java_libs: ["myjar"],
3855 }
3856
3857 apex_key {
3858 name: "myapex.key",
3859 public_key: "testkey.avbpubkey",
3860 private_key: "testkey.pem",
3861 }
3862
3863 java_library {
3864 name: "myjar",
3865 srcs: ["foo/bar/MyClass.java"],
3866 sdk_version: "none",
3867 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003868 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003869 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003870 }
3871 `)
3872}
3873
Jiyong Park7afd1072019-12-30 16:56:33 +09003874func TestCarryRequiredModuleNames(t *testing.T) {
3875 ctx, config := testApex(t, `
3876 apex {
3877 name: "myapex",
3878 key: "myapex.key",
3879 native_shared_libs: ["mylib"],
3880 }
3881
3882 apex_key {
3883 name: "myapex.key",
3884 public_key: "testkey.avbpubkey",
3885 private_key: "testkey.pem",
3886 }
3887
3888 cc_library {
3889 name: "mylib",
3890 srcs: ["mylib.cpp"],
3891 system_shared_libs: [],
3892 stl: "none",
3893 required: ["a", "b"],
3894 host_required: ["c", "d"],
3895 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003896 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003897 }
3898 `)
3899
3900 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3901 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3902 name := apexBundle.BaseModuleName()
3903 prefix := "TARGET_"
3904 var builder strings.Builder
3905 data.Custom(&builder, name, prefix, "", data)
3906 androidMk := builder.String()
3907 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3908 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3909 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3910}
3911
Jiyong Park7cd10e32020-01-14 09:22:18 +09003912func TestSymlinksFromApexToSystem(t *testing.T) {
3913 bp := `
3914 apex {
3915 name: "myapex",
3916 key: "myapex.key",
3917 native_shared_libs: ["mylib"],
3918 java_libs: ["myjar"],
3919 }
3920
Jiyong Park9d677202020-02-19 16:29:35 +09003921 apex {
3922 name: "myapex.updatable",
3923 key: "myapex.key",
3924 native_shared_libs: ["mylib"],
3925 java_libs: ["myjar"],
3926 updatable: true,
3927 }
3928
Jiyong Park7cd10e32020-01-14 09:22:18 +09003929 apex_key {
3930 name: "myapex.key",
3931 public_key: "testkey.avbpubkey",
3932 private_key: "testkey.pem",
3933 }
3934
3935 cc_library {
3936 name: "mylib",
3937 srcs: ["mylib.cpp"],
3938 shared_libs: ["myotherlib"],
3939 system_shared_libs: [],
3940 stl: "none",
3941 apex_available: [
3942 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003943 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003944 "//apex_available:platform",
3945 ],
3946 }
3947
3948 cc_library {
3949 name: "myotherlib",
3950 srcs: ["mylib.cpp"],
3951 system_shared_libs: [],
3952 stl: "none",
3953 apex_available: [
3954 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003955 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003956 "//apex_available:platform",
3957 ],
3958 }
3959
3960 java_library {
3961 name: "myjar",
3962 srcs: ["foo/bar/MyClass.java"],
3963 sdk_version: "none",
3964 system_modules: "none",
3965 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003966 apex_available: [
3967 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003968 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003969 "//apex_available:platform",
3970 ],
3971 }
3972
3973 java_library {
3974 name: "myotherjar",
3975 srcs: ["foo/bar/MyClass.java"],
3976 sdk_version: "none",
3977 system_modules: "none",
3978 apex_available: [
3979 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003980 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003981 "//apex_available:platform",
3982 ],
3983 }
3984 `
3985
3986 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3987 for _, f := range files {
3988 if f.path == file {
3989 if f.isLink {
3990 t.Errorf("%q is not a real file", file)
3991 }
3992 return
3993 }
3994 }
3995 t.Errorf("%q is not found", file)
3996 }
3997
3998 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3999 for _, f := range files {
4000 if f.path == file {
4001 if !f.isLink {
4002 t.Errorf("%q is not a symlink", file)
4003 }
4004 return
4005 }
4006 }
4007 t.Errorf("%q is not found", file)
4008 }
4009
Jiyong Park9d677202020-02-19 16:29:35 +09004010 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4011 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004012 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004013 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004014 ensureRealfileExists(t, files, "javalib/myjar.jar")
4015 ensureRealfileExists(t, files, "lib64/mylib.so")
4016 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4017
Jiyong Park9d677202020-02-19 16:29:35 +09004018 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4019 ensureRealfileExists(t, files, "javalib/myjar.jar")
4020 ensureRealfileExists(t, files, "lib64/mylib.so")
4021 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4022
4023 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004024 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004025 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004026 ensureRealfileExists(t, files, "javalib/myjar.jar")
4027 ensureRealfileExists(t, files, "lib64/mylib.so")
4028 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004029
4030 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4031 ensureRealfileExists(t, files, "javalib/myjar.jar")
4032 ensureRealfileExists(t, files, "lib64/mylib.so")
4033 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004034}
4035
Jooyung Han643adc42020-02-27 13:50:06 +09004036func TestApexWithJniLibs(t *testing.T) {
4037 ctx, _ := testApex(t, `
4038 apex {
4039 name: "myapex",
4040 key: "myapex.key",
4041 jni_libs: ["mylib"],
4042 }
4043
4044 apex_key {
4045 name: "myapex.key",
4046 public_key: "testkey.avbpubkey",
4047 private_key: "testkey.pem",
4048 }
4049
4050 cc_library {
4051 name: "mylib",
4052 srcs: ["mylib.cpp"],
4053 shared_libs: ["mylib2"],
4054 system_shared_libs: [],
4055 stl: "none",
4056 apex_available: [ "myapex" ],
4057 }
4058
4059 cc_library {
4060 name: "mylib2",
4061 srcs: ["mylib.cpp"],
4062 system_shared_libs: [],
4063 stl: "none",
4064 apex_available: [ "myapex" ],
4065 }
4066 `)
4067
4068 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
4069 // Notice mylib2.so (transitive dep) is not added as a jni_lib
4070 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
4071 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4072 "lib64/mylib.so",
4073 "lib64/mylib2.so",
4074 })
4075}
4076
4077func TestApexWithJniLibs_Errors(t *testing.T) {
4078 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
4079 apex {
4080 name: "myapex",
4081 key: "myapex.key",
4082 jni_libs: ["xxx"],
4083 }
4084
4085 apex_key {
4086 name: "myapex.key",
4087 public_key: "testkey.avbpubkey",
4088 private_key: "testkey.pem",
4089 }
4090
4091 prebuilt_etc {
4092 name: "xxx",
4093 src: "xxx",
4094 }
4095 `, withFiles(map[string][]byte{
4096 "xxx": nil,
4097 }))
4098}
4099
Jiyong Parkbd159612020-02-28 15:22:21 +09004100func TestAppBundle(t *testing.T) {
4101 ctx, _ := testApex(t, `
4102 apex {
4103 name: "myapex",
4104 key: "myapex.key",
4105 apps: ["AppFoo"],
4106 }
4107
4108 apex_key {
4109 name: "myapex.key",
4110 public_key: "testkey.avbpubkey",
4111 private_key: "testkey.pem",
4112 }
4113
4114 android_app {
4115 name: "AppFoo",
4116 srcs: ["foo/bar/MyClass.java"],
4117 sdk_version: "none",
4118 system_modules: "none",
4119 apex_available: [ "myapex" ],
4120 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004121 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09004122
4123 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4124 content := bundleConfigRule.Args["content"]
4125
4126 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004127 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 +09004128}
4129
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004130func TestMain(m *testing.M) {
4131 run := func() int {
4132 setUp()
4133 defer tearDown()
4134
4135 return m.Run()
4136 }
4137
4138 os.Exit(run())
4139}