blob: 2bdecb73cc1bc3aed7be31b797a3ea0008f90157 [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,
Jiyong Park9918e1a2020-03-17 19:16:40 +0900171 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800172 "testkey2.avbpubkey": nil,
173 "testkey2.pem": nil,
174 "myapex-arm64.apex": nil,
175 "myapex-arm.apex": nil,
176 "frameworks/base/api/current.txt": nil,
177 "framework/aidl/a.aidl": nil,
178 "build/make/core/proguard.flags": nil,
179 "build/make/core/proguard_basic_keeps.flags": nil,
180 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900181 }
182
Colin Crossf9aabd72020-02-15 11:29:50 -0800183 cc.GatherRequiredFilesForTest(fs)
184
Jooyung Han344d5432019-08-23 11:17:39 +0900185 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800186 // The fs now needs to be populated before creating the config, call handlers twice
187 // for now, once to get any fs changes, and later after the config was created to
188 // set product variables or targets.
189 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
190 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900191 }
192
Colin Cross98be1bb2019-12-13 20:41:13 -0800193 config := android.TestArchConfig(buildDir, nil, bp, fs)
194 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
195 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
196 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
197 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
198 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
199 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
200
201 for _, handler := range handlers {
202 // The fs now needs to be populated before creating the config, call handlers twice
203 // for now, earlier to get any fs changes, and now after the config was created to
204 // set product variables or targets.
205 tempFS := map[string][]byte{}
206 handler(tempFS, config)
207 }
208
209 ctx := android.NewTestArchContext()
210 ctx.RegisterModuleType("apex", BundleFactory)
211 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
212 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
213 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
214 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
215 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
216 ctx.RegisterModuleType("override_apex", overrideApexFactory)
217
Jooyung Hana57af4a2020-01-23 05:36:59 +0000218 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
219 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
220
Paul Duffin77980a82019-12-19 16:01:36 +0000221 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800222 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800223 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
224 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800225 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000226 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800227 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800228 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000229 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000230 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000231 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900232 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800233
Colin Cross98be1bb2019-12-13 20:41:13 -0800234 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800235 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800236
237 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238
Jooyung Han5c998b92019-06-27 11:30:33 +0900239 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900240}
241
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700242func setUp() {
243 var err error
244 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700246 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900248}
249
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700250func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900251 os.RemoveAll(buildDir)
252}
253
Jooyung Han643adc42020-02-27 13:50:06 +0900254// ensure that 'result' equals 'expected'
255func ensureEquals(t *testing.T, result string, expected string) {
256 t.Helper()
257 if result != expected {
258 t.Errorf("%q != %q", expected, result)
259 }
260}
261
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262// ensure that 'result' contains 'expected'
263func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900264 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900265 if !strings.Contains(result, expected) {
266 t.Errorf("%q is not found in %q", expected, result)
267 }
268}
269
270// ensures that 'result' does not contain 'notExpected'
271func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900272 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 if strings.Contains(result, notExpected) {
274 t.Errorf("%q is found in %q", notExpected, result)
275 }
276}
277
278func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if !android.InList(expected, result) {
281 t.Errorf("%q is not found in %v", expected, result)
282 }
283}
284
285func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900286 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 if android.InList(notExpected, result) {
288 t.Errorf("%q is found in %v", notExpected, result)
289 }
290}
291
Jooyung Hane1633032019-08-01 17:41:43 +0900292func ensureListEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) > 0 {
295 t.Errorf("%q is expected to be empty", result)
296 }
297}
298
Jiyong Park25fc6a92018-11-18 18:02:45 +0900299// Minimal test
300func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700301 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900302 apex_defaults {
303 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900304 manifest: ":myapex.manifest",
305 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306 key: "myapex.key",
307 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800308 multilib: {
309 both: {
310 binaries: ["foo",],
311 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900312 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900313 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314 }
315
Jiyong Park30ca9372019-02-07 16:27:23 +0900316 apex {
317 name: "myapex",
318 defaults: ["myapex-defaults"],
319 }
320
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 apex_key {
322 name: "myapex.key",
323 public_key: "testkey.avbpubkey",
324 private_key: "testkey.pem",
325 }
326
Jiyong Park809bb722019-02-13 21:33:49 +0900327 filegroup {
328 name: "myapex.manifest",
329 srcs: ["apex_manifest.json"],
330 }
331
332 filegroup {
333 name: "myapex.androidmanifest",
334 srcs: ["AndroidManifest.xml"],
335 }
336
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 cc_library {
338 name: "mylib",
339 srcs: ["mylib.cpp"],
340 shared_libs: ["mylib2"],
341 system_shared_libs: [],
342 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000343 // TODO: remove //apex_available:platform
344 apex_available: [
345 "//apex_available:platform",
346 "myapex",
347 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 }
349
Alex Light3d673592019-01-18 14:37:31 -0800350 cc_binary {
351 name: "foo",
352 srcs: ["mylib.cpp"],
353 compile_multilib: "both",
354 multilib: {
355 lib32: {
356 suffix: "32",
357 },
358 lib64: {
359 suffix: "64",
360 },
361 },
362 symlinks: ["foo_link_"],
363 symlink_preferred_arch: true,
364 system_shared_libs: [],
365 static_executable: true,
366 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000367 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800368 }
369
Jiyong Park25fc6a92018-11-18 18:02:45 +0900370 cc_library {
371 name: "mylib2",
372 srcs: ["mylib.cpp"],
373 system_shared_libs: [],
374 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900375 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900376 static_libs: ["libstatic"],
377 // TODO: remove //apex_available:platform
378 apex_available: [
379 "//apex_available:platform",
380 "myapex",
381 ],
382 }
383
384 cc_library_static {
385 name: "libstatic",
386 srcs: ["mylib.cpp"],
387 system_shared_libs: [],
388 stl: "none",
389 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000390 // TODO: remove //apex_available:platform
391 apex_available: [
392 "//apex_available:platform",
393 "myapex",
394 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900395 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900396
397 java_library {
398 name: "myjar",
399 srcs: ["foo/bar/MyClass.java"],
400 sdk_version: "none",
401 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900402 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900403 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000404 // TODO: remove //apex_available:platform
405 apex_available: [
406 "//apex_available:platform",
407 "myapex",
408 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900409 }
410
411 java_library {
412 name: "myotherjar",
413 srcs: ["foo/bar/MyClass.java"],
414 sdk_version: "none",
415 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900416 // TODO: remove //apex_available:platform
417 apex_available: [
418 "//apex_available:platform",
419 "myapex",
420 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900421 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900422
423 java_library {
424 name: "mysharedjar",
425 srcs: ["foo/bar/MyClass.java"],
426 sdk_version: "none",
427 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900428 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900429 `)
430
Sundong Ahnabb64432019-10-22 13:58:29 +0900431 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900432
433 optFlags := apexRule.Args["opt_flags"]
434 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700435 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900436 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900437
Jiyong Park25fc6a92018-11-18 18:02:45 +0900438 copyCmds := apexRule.Args["copy_commands"]
439
440 // Ensure that main rule creates an output
441 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
442
443 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800444 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900445 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900446
447 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800448 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900449 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900450
451 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800452 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
453 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900454 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
455 // .. but not for java libs
456 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900457 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800458
Colin Cross7113d202019-11-20 16:39:12 -0800459 // Ensure that the platform variant ends with _shared or _common
460 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
461 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900462 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
463 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900464 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
465
466 // Ensure that dynamic dependency to java libs are not included
467 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800468
469 // Ensure that all symlinks are present.
470 found_foo_link_64 := false
471 found_foo := false
472 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900473 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800474 if strings.HasSuffix(cmd, "bin/foo") {
475 found_foo = true
476 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
477 found_foo_link_64 = true
478 }
479 }
480 }
481 good := found_foo && found_foo_link_64
482 if !good {
483 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
484 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900485
Sundong Ahnabb64432019-10-22 13:58:29 +0900486 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700487 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900488 if len(noticeInputs) != 3 {
489 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900490 }
491 ensureListContains(t, noticeInputs, "NOTICE")
492 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900493 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900494
495 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 +0900496 ensureListContains(t, depsInfo, "myjar <- myapex")
497 ensureListContains(t, depsInfo, "mylib <- myapex")
498 ensureListContains(t, depsInfo, "mylib2 <- mylib")
499 ensureListContains(t, depsInfo, "myotherjar <- myjar")
500 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800501}
502
Jooyung Hanf21c7972019-12-16 22:32:06 +0900503func TestDefaults(t *testing.T) {
504 ctx, _ := testApex(t, `
505 apex_defaults {
506 name: "myapex-defaults",
507 key: "myapex.key",
508 prebuilts: ["myetc"],
509 native_shared_libs: ["mylib"],
510 java_libs: ["myjar"],
511 apps: ["AppFoo"],
512 }
513
514 prebuilt_etc {
515 name: "myetc",
516 src: "myprebuilt",
517 }
518
519 apex {
520 name: "myapex",
521 defaults: ["myapex-defaults"],
522 }
523
524 apex_key {
525 name: "myapex.key",
526 public_key: "testkey.avbpubkey",
527 private_key: "testkey.pem",
528 }
529
530 cc_library {
531 name: "mylib",
532 system_shared_libs: [],
533 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000534 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900535 }
536
537 java_library {
538 name: "myjar",
539 srcs: ["foo/bar/MyClass.java"],
540 sdk_version: "none",
541 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000542 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900543 }
544
545 android_app {
546 name: "AppFoo",
547 srcs: ["foo/bar/MyClass.java"],
548 sdk_version: "none",
549 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000550 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900551 }
552 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000553 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900554 "etc/myetc",
555 "javalib/myjar.jar",
556 "lib64/mylib.so",
557 "app/AppFoo/AppFoo.apk",
558 })
559}
560
Jooyung Han01a3ee22019-11-02 02:52:25 +0900561func TestApexManifest(t *testing.T) {
562 ctx, _ := testApex(t, `
563 apex {
564 name: "myapex",
565 key: "myapex.key",
566 }
567
568 apex_key {
569 name: "myapex.key",
570 public_key: "testkey.avbpubkey",
571 private_key: "testkey.pem",
572 }
573 `)
574
575 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900576 args := module.Rule("apexRule").Args
577 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
578 t.Error("manifest should be apex_manifest.pb, but " + manifest)
579 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900580}
581
Alex Light5098a612018-11-29 17:12:15 -0800582func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700583 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800584 apex {
585 name: "myapex",
586 key: "myapex.key",
587 payload_type: "zip",
588 native_shared_libs: ["mylib"],
589 }
590
591 apex_key {
592 name: "myapex.key",
593 public_key: "testkey.avbpubkey",
594 private_key: "testkey.pem",
595 }
596
597 cc_library {
598 name: "mylib",
599 srcs: ["mylib.cpp"],
600 shared_libs: ["mylib2"],
601 system_shared_libs: [],
602 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000603 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800604 }
605
606 cc_library {
607 name: "mylib2",
608 srcs: ["mylib.cpp"],
609 system_shared_libs: [],
610 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000611 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800612 }
613 `)
614
Sundong Ahnabb64432019-10-22 13:58:29 +0900615 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800616 copyCmds := zipApexRule.Args["copy_commands"]
617
618 // Ensure that main rule creates an output
619 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
620
621 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800622 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800623
624 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800625 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800626
627 // Ensure that both direct and indirect deps are copied into apex
628 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
629 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900630}
631
632func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700633 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900634 apex {
635 name: "myapex",
636 key: "myapex.key",
637 native_shared_libs: ["mylib", "mylib3"],
638 }
639
640 apex_key {
641 name: "myapex.key",
642 public_key: "testkey.avbpubkey",
643 private_key: "testkey.pem",
644 }
645
646 cc_library {
647 name: "mylib",
648 srcs: ["mylib.cpp"],
649 shared_libs: ["mylib2", "mylib3"],
650 system_shared_libs: [],
651 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000652 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900653 }
654
655 cc_library {
656 name: "mylib2",
657 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900658 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900659 system_shared_libs: [],
660 stl: "none",
661 stubs: {
662 versions: ["1", "2", "3"],
663 },
664 }
665
666 cc_library {
667 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900668 srcs: ["mylib.cpp"],
669 shared_libs: ["mylib4"],
670 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900671 stl: "none",
672 stubs: {
673 versions: ["10", "11", "12"],
674 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000675 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900676 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900677
678 cc_library {
679 name: "mylib4",
680 srcs: ["mylib.cpp"],
681 system_shared_libs: [],
682 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000683 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900684 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900685 `)
686
Sundong Ahnabb64432019-10-22 13:58:29 +0900687 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900688 copyCmds := apexRule.Args["copy_commands"]
689
690 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800691 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900692
693 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800694 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900695
696 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800697 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900698
Colin Cross7113d202019-11-20 16:39:12 -0800699 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900700
701 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900702 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900703 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900704 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900705
706 // 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 -0800707 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900708 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800709 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900710
711 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900712 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900713 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900714
715 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900716 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900717
Jooyung Hana57af4a2020-01-23 05:36:59 +0000718 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900719 "lib64/mylib.so",
720 "lib64/mylib3.so",
721 "lib64/mylib4.so",
722 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900723}
724
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900725func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700726 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900727 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900728 name: "myapex2",
729 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900730 native_shared_libs: ["mylib"],
731 }
732
733 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900734 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900735 public_key: "testkey.avbpubkey",
736 private_key: "testkey.pem",
737 }
738
739 cc_library {
740 name: "mylib",
741 srcs: ["mylib.cpp"],
742 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900743 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900744 system_shared_libs: [],
745 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000746 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900747 }
748
749 cc_library {
750 name: "libfoo",
751 srcs: ["mylib.cpp"],
752 shared_libs: ["libbar"],
753 system_shared_libs: [],
754 stl: "none",
755 stubs: {
756 versions: ["10", "20", "30"],
757 },
758 }
759
760 cc_library {
761 name: "libbar",
762 srcs: ["mylib.cpp"],
763 system_shared_libs: [],
764 stl: "none",
765 }
766
Jiyong Park678c8812020-02-07 17:25:49 +0900767 cc_library_static {
768 name: "libbaz",
769 srcs: ["mylib.cpp"],
770 system_shared_libs: [],
771 stl: "none",
772 apex_available: [ "myapex2" ],
773 }
774
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900775 `)
776
Jiyong Park83dc74b2020-01-14 18:38:44 +0900777 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900778 copyCmds := apexRule.Args["copy_commands"]
779
780 // Ensure that direct non-stubs dep is always included
781 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
782
783 // Ensure that indirect stubs dep is not included
784 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
785
786 // Ensure that dependency of stubs is not included
787 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
788
Jiyong Park83dc74b2020-01-14 18:38:44 +0900789 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900790
791 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900792 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900793 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900794 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900795
Jiyong Park3ff16992019-12-27 14:11:47 +0900796 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900797
798 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
799 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900800
801 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 +0900802
803 ensureListContains(t, depsInfo, "mylib <- myapex2")
804 ensureListContains(t, depsInfo, "libbaz <- mylib")
805 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900806}
807
Jooyung Hand3639552019-08-09 12:57:43 +0900808func TestApexWithRuntimeLibsDependency(t *testing.T) {
809 /*
810 myapex
811 |
812 v (runtime_libs)
813 mylib ------+------> libfoo [provides stub]
814 |
815 `------> libbar
816 */
817 ctx, _ := testApex(t, `
818 apex {
819 name: "myapex",
820 key: "myapex.key",
821 native_shared_libs: ["mylib"],
822 }
823
824 apex_key {
825 name: "myapex.key",
826 public_key: "testkey.avbpubkey",
827 private_key: "testkey.pem",
828 }
829
830 cc_library {
831 name: "mylib",
832 srcs: ["mylib.cpp"],
833 runtime_libs: ["libfoo", "libbar"],
834 system_shared_libs: [],
835 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000836 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900837 }
838
839 cc_library {
840 name: "libfoo",
841 srcs: ["mylib.cpp"],
842 system_shared_libs: [],
843 stl: "none",
844 stubs: {
845 versions: ["10", "20", "30"],
846 },
847 }
848
849 cc_library {
850 name: "libbar",
851 srcs: ["mylib.cpp"],
852 system_shared_libs: [],
853 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000854 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900855 }
856
857 `)
858
Sundong Ahnabb64432019-10-22 13:58:29 +0900859 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900860 copyCmds := apexRule.Args["copy_commands"]
861
862 // Ensure that direct non-stubs dep is always included
863 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
864
865 // Ensure that indirect stubs dep is not included
866 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
867
868 // Ensure that runtime_libs dep in included
869 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
870
Sundong Ahnabb64432019-10-22 13:58:29 +0900871 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900872 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
873 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900874
875}
876
Jooyung Han61b66e92020-03-21 14:21:46 +0000877func TestApexDependsOnLLNDKTransitively(t *testing.T) {
878 testcases := []struct {
879 name string
880 minSdkVersion string
881 shouldLink string
882 shouldNotLink []string
883 }{
884 {
885 name: "should link to test latest",
886 minSdkVersion: "current",
887 shouldLink: "30",
888 shouldNotLink: []string{"29"},
889 },
890 {
891 name: "should link to llndk#29",
892 minSdkVersion: "29",
893 shouldLink: "29",
894 shouldNotLink: []string{"30"},
895 },
896 }
897 for _, tc := range testcases {
898 t.Run(tc.name, func(t *testing.T) {
899 ctx, _ := testApex(t, `
900 apex {
901 name: "myapex",
902 key: "myapex.key",
903 use_vendor: true,
904 native_shared_libs: ["mylib"],
905 min_sdk_version: "`+tc.minSdkVersion+`",
906 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900907
Jooyung Han61b66e92020-03-21 14:21:46 +0000908 apex_key {
909 name: "myapex.key",
910 public_key: "testkey.avbpubkey",
911 private_key: "testkey.pem",
912 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900913
Jooyung Han61b66e92020-03-21 14:21:46 +0000914 cc_library {
915 name: "mylib",
916 srcs: ["mylib.cpp"],
917 vendor_available: true,
918 shared_libs: ["libbar"],
919 system_shared_libs: [],
920 stl: "none",
921 apex_available: [ "myapex" ],
922 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900923
Jooyung Han61b66e92020-03-21 14:21:46 +0000924 cc_library {
925 name: "libbar",
926 srcs: ["mylib.cpp"],
927 system_shared_libs: [],
928 stl: "none",
929 stubs: { versions: ["29","30"] },
930 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900931
Jooyung Han61b66e92020-03-21 14:21:46 +0000932 llndk_library {
933 name: "libbar",
934 symbol_file: "",
935 }
936 `, func(fs map[string][]byte, config android.Config) {
937 setUseVendorWhitelistForTest(config, []string{"myapex"})
938 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +0900939
Jooyung Han61b66e92020-03-21 14:21:46 +0000940 // Ensure that LLNDK dep is not included
941 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
942 "lib64/mylib.so",
943 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900944
Jooyung Han61b66e92020-03-21 14:21:46 +0000945 // Ensure that LLNDK dep is required
946 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
947 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
948 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900949
Jooyung Han61b66e92020-03-21 14:21:46 +0000950 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
951 ensureContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
952 for _, ver := range tc.shouldNotLink {
953 ensureNotContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
954 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900955
Jooyung Han61b66e92020-03-21 14:21:46 +0000956 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
957 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
958 })
959 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900960}
961
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700963 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964 apex {
965 name: "myapex",
966 key: "myapex.key",
967 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
968 }
969
970 apex_key {
971 name: "myapex.key",
972 public_key: "testkey.avbpubkey",
973 private_key: "testkey.pem",
974 }
975
976 cc_library {
977 name: "mylib",
978 srcs: ["mylib.cpp"],
979 shared_libs: ["libdl#27"],
980 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000981 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900982 }
983
984 cc_library_shared {
985 name: "mylib_shared",
986 srcs: ["mylib.cpp"],
987 shared_libs: ["libdl#27"],
988 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000989 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900990 }
991
992 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900993 name: "libBootstrap",
994 srcs: ["mylib.cpp"],
995 stl: "none",
996 bootstrap: true,
997 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900998 `)
999
Sundong Ahnabb64432019-10-22 13:58:29 +09001000 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001001 copyCmds := apexRule.Args["copy_commands"]
1002
1003 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001004 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001005 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1006 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001007
1008 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001009 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010
Colin Cross7113d202019-11-20 16:39:12 -08001011 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1012 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1013 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001014
1015 // For dependency to libc
1016 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001017 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001018 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001019 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001020 // ... Cflags from stub is correctly exported to mylib
1021 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1022 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1023
1024 // For dependency to libm
1025 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001026 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001027 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001028 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001029 // ... and is not compiling with the stub
1030 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1031 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1032
1033 // For dependency to libdl
1034 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001035 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001036 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001037 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1038 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001039 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001040 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001041 // ... Cflags from stub is correctly exported to mylib
1042 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1043 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001044
1045 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001046 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1047 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1048 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1049 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001050}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001051
Jooyung Han03b51852020-02-26 22:45:42 +09001052func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
1053 // there are three links between liba --> libz
1054 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
1055 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
1056 // 3) (platform) -> liba -> libz : this should be non-stub link
1057 ctx, _ := testApex(t, `
1058 apex {
1059 name: "myapex",
1060 key: "myapex.key",
1061 native_shared_libs: ["libx"],
1062 min_sdk_version: "2",
1063 }
1064
1065 apex {
1066 name: "otherapex",
1067 key: "myapex.key",
1068 native_shared_libs: ["liby"],
1069 min_sdk_version: "3",
1070 }
1071
1072 apex_key {
1073 name: "myapex.key",
1074 public_key: "testkey.avbpubkey",
1075 private_key: "testkey.pem",
1076 }
1077
1078 cc_library {
1079 name: "libx",
1080 shared_libs: ["liba"],
1081 system_shared_libs: [],
1082 stl: "none",
1083 apex_available: [ "myapex" ],
1084 }
1085
1086 cc_library {
1087 name: "liby",
1088 shared_libs: ["liba"],
1089 system_shared_libs: [],
1090 stl: "none",
1091 apex_available: [ "otherapex" ],
1092 }
1093
1094 cc_library {
1095 name: "liba",
1096 shared_libs: ["libz"],
1097 system_shared_libs: [],
1098 stl: "none",
1099 apex_available: [
1100 "//apex_available:anyapex",
1101 "//apex_available:platform",
1102 ],
1103 }
1104
1105 cc_library {
1106 name: "libz",
1107 system_shared_libs: [],
1108 stl: "none",
1109 stubs: {
1110 versions: ["1", "3"],
1111 },
1112 }
1113 `, withUnbundledBuild)
1114
1115 expectLink := func(from, from_variant, to, to_variant string) {
1116 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1117 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1118 }
1119 expectNoLink := func(from, from_variant, to, to_variant string) {
1120 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1121 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1122 }
1123 // platform liba is linked to non-stub version
1124 expectLink("liba", "shared", "libz", "shared")
1125 // liba in myapex is linked to #1
1126 expectLink("liba", "shared_myapex", "libz", "shared_1")
1127 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1128 expectNoLink("liba", "shared_myapex", "libz", "shared")
1129 // liba in otherapex is linked to #3
1130 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1131 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1132 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1133}
1134
1135func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1136 ctx, _ := testApex(t, `
1137 apex {
1138 name: "myapex",
1139 key: "myapex.key",
1140 native_shared_libs: ["libx"],
1141 }
1142
1143 apex_key {
1144 name: "myapex.key",
1145 public_key: "testkey.avbpubkey",
1146 private_key: "testkey.pem",
1147 }
1148
1149 cc_library {
1150 name: "libx",
1151 shared_libs: ["libz"],
1152 system_shared_libs: [],
1153 stl: "none",
1154 apex_available: [ "myapex" ],
1155 }
1156
1157 cc_library {
1158 name: "libz",
1159 system_shared_libs: [],
1160 stl: "none",
1161 stubs: {
1162 versions: ["1", "2"],
1163 },
1164 }
1165 `)
1166
1167 expectLink := func(from, from_variant, to, to_variant string) {
1168 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1169 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1170 }
1171 expectNoLink := func(from, from_variant, to, to_variant string) {
1172 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1173 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1174 }
1175 expectLink("libx", "shared_myapex", "libz", "shared_2")
1176 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1177 expectNoLink("libx", "shared_myapex", "libz", "shared")
1178}
1179
1180func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1181 ctx, _ := testApex(t, `
1182 apex {
1183 name: "myapex",
1184 key: "myapex.key",
1185 native_shared_libs: ["libx"],
1186 }
1187
1188 apex_key {
1189 name: "myapex.key",
1190 public_key: "testkey.avbpubkey",
1191 private_key: "testkey.pem",
1192 }
1193
1194 cc_library {
1195 name: "libx",
1196 system_shared_libs: [],
1197 stl: "none",
1198 apex_available: [ "myapex" ],
1199 stubs: {
1200 versions: ["1", "2"],
1201 },
1202 }
1203
1204 cc_library {
1205 name: "libz",
1206 shared_libs: ["libx"],
1207 system_shared_libs: [],
1208 stl: "none",
1209 }
1210 `)
1211
1212 expectLink := func(from, from_variant, to, to_variant string) {
1213 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1214 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1215 }
1216 expectNoLink := func(from, from_variant, to, to_variant string) {
1217 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1218 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1219 }
1220 expectLink("libz", "shared", "libx", "shared_2")
1221 expectNoLink("libz", "shared", "libz", "shared_1")
1222 expectNoLink("libz", "shared", "libz", "shared")
1223}
1224
1225func TestQApexesUseLatestStubsInBundledBuilds(t *testing.T) {
1226 ctx, _ := testApex(t, `
1227 apex {
1228 name: "myapex",
1229 key: "myapex.key",
1230 native_shared_libs: ["libx"],
1231 min_sdk_version: "29",
1232 }
1233
1234 apex_key {
1235 name: "myapex.key",
1236 public_key: "testkey.avbpubkey",
1237 private_key: "testkey.pem",
1238 }
1239
1240 cc_library {
1241 name: "libx",
1242 shared_libs: ["libbar"],
1243 apex_available: [ "myapex" ],
1244 }
1245
1246 cc_library {
1247 name: "libbar",
1248 stubs: {
1249 versions: ["29", "30"],
1250 },
1251 }
1252 `)
1253 expectLink := func(from, from_variant, to, to_variant string) {
1254 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1255 libFlags := ld.Args["libFlags"]
1256 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1257 }
1258 expectLink("libx", "shared_myapex", "libbar", "shared_30")
1259}
1260
1261func TestQTargetApexUseStaticUnwinder(t *testing.T) {
1262 ctx, _ := testApex(t, `
1263 apex {
1264 name: "myapex",
1265 key: "myapex.key",
1266 native_shared_libs: ["libx"],
1267 min_sdk_version: "29",
1268 }
1269
1270 apex_key {
1271 name: "myapex.key",
1272 public_key: "testkey.avbpubkey",
1273 private_key: "testkey.pem",
1274 }
1275
1276 cc_library {
1277 name: "libx",
1278 apex_available: [ "myapex" ],
1279 }
1280
1281 `, withUnbundledBuild)
1282
1283 // ensure apex variant of c++ is linked with static unwinder
1284 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1285 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1286 // note that platform variant is not.
1287 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1288 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Han03b51852020-02-26 22:45:42 +09001289}
1290
1291func TestInvalidMinSdkVersion(t *testing.T) {
1292 testApexError(t, `"libz" .*: min_sdk_version is set 29.*`, `
1293 apex {
1294 name: "myapex",
1295 key: "myapex.key",
1296 native_shared_libs: ["libx"],
1297 min_sdk_version: "29",
1298 }
1299
1300 apex_key {
1301 name: "myapex.key",
1302 public_key: "testkey.avbpubkey",
1303 private_key: "testkey.pem",
1304 }
1305
1306 cc_library {
1307 name: "libx",
1308 shared_libs: ["libz"],
1309 system_shared_libs: [],
1310 stl: "none",
1311 apex_available: [ "myapex" ],
1312 }
1313
1314 cc_library {
1315 name: "libz",
1316 system_shared_libs: [],
1317 stl: "none",
1318 stubs: {
1319 versions: ["30"],
1320 },
1321 }
1322 `, withUnbundledBuild)
1323
1324 testApexError(t, `"myapex" .*: min_sdk_version: should be .*`, `
1325 apex {
1326 name: "myapex",
1327 key: "myapex.key",
1328 min_sdk_version: "R",
1329 }
1330
1331 apex_key {
1332 name: "myapex.key",
1333 public_key: "testkey.avbpubkey",
1334 private_key: "testkey.pem",
1335 }
1336 `)
1337}
1338
Jiyong Park7c2ee712018-12-07 00:42:25 +09001339func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001340 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001341 apex {
1342 name: "myapex",
1343 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001344 native_shared_libs: ["mylib"],
1345 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001346 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001347 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001348 }
1349
1350 apex_key {
1351 name: "myapex.key",
1352 public_key: "testkey.avbpubkey",
1353 private_key: "testkey.pem",
1354 }
1355
1356 prebuilt_etc {
1357 name: "myetc",
1358 src: "myprebuilt",
1359 sub_dir: "foo/bar",
1360 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001361
1362 cc_library {
1363 name: "mylib",
1364 srcs: ["mylib.cpp"],
1365 relative_install_path: "foo/bar",
1366 system_shared_libs: [],
1367 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001368 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001369 }
1370
1371 cc_binary {
1372 name: "mybin",
1373 srcs: ["mylib.cpp"],
1374 relative_install_path: "foo/bar",
1375 system_shared_libs: [],
1376 static_executable: true,
1377 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001378 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001379 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001380 `)
1381
Sundong Ahnabb64432019-10-22 13:58:29 +09001382 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001383 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1384
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001385 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001386 ensureListContains(t, dirs, "etc")
1387 ensureListContains(t, dirs, "etc/foo")
1388 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001389 ensureListContains(t, dirs, "lib64")
1390 ensureListContains(t, dirs, "lib64/foo")
1391 ensureListContains(t, dirs, "lib64/foo/bar")
1392 ensureListContains(t, dirs, "lib")
1393 ensureListContains(t, dirs, "lib/foo")
1394 ensureListContains(t, dirs, "lib/foo/bar")
1395
Jiyong Parkbd13e442019-03-15 18:10:35 +09001396 ensureListContains(t, dirs, "bin")
1397 ensureListContains(t, dirs, "bin/foo")
1398 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001399}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001400
Jooyung Han35155c42020-02-06 17:33:20 +09001401func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
1402 ctx, _ := testApex(t, `
1403 apex {
1404 name: "myapex",
1405 key: "myapex.key",
1406 multilib: {
1407 both: {
1408 native_shared_libs: ["mylib"],
1409 binaries: ["mybin"],
1410 },
1411 },
1412 compile_multilib: "both",
1413 native_bridge_supported: true,
1414 }
1415
1416 apex_key {
1417 name: "myapex.key",
1418 public_key: "testkey.avbpubkey",
1419 private_key: "testkey.pem",
1420 }
1421
1422 cc_library {
1423 name: "mylib",
1424 relative_install_path: "foo/bar",
1425 system_shared_libs: [],
1426 stl: "none",
1427 apex_available: [ "myapex" ],
1428 native_bridge_supported: true,
1429 }
1430
1431 cc_binary {
1432 name: "mybin",
1433 relative_install_path: "foo/bar",
1434 system_shared_libs: [],
1435 static_executable: true,
1436 stl: "none",
1437 apex_available: [ "myapex" ],
1438 native_bridge_supported: true,
1439 compile_multilib: "both", // default is "first" for binary
1440 multilib: {
1441 lib64: {
1442 suffix: "64",
1443 },
1444 },
1445 }
1446 `, withNativeBridgeEnabled)
1447 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1448 "bin/foo/bar/mybin",
1449 "bin/foo/bar/mybin64",
1450 "bin/arm/foo/bar/mybin",
1451 "bin/arm64/foo/bar/mybin64",
1452 "lib/foo/bar/mylib.so",
1453 "lib/arm/foo/bar/mylib.so",
1454 "lib64/foo/bar/mylib.so",
1455 "lib64/arm64/foo/bar/mylib.so",
1456 })
1457}
1458
Jiyong Parkda6eb592018-12-19 17:12:36 +09001459func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001460 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001461 apex {
1462 name: "myapex",
1463 key: "myapex.key",
1464 native_shared_libs: ["mylib"],
1465 use_vendor: true,
1466 }
1467
1468 apex_key {
1469 name: "myapex.key",
1470 public_key: "testkey.avbpubkey",
1471 private_key: "testkey.pem",
1472 }
1473
1474 cc_library {
1475 name: "mylib",
1476 srcs: ["mylib.cpp"],
1477 shared_libs: ["mylib2"],
1478 system_shared_libs: [],
1479 vendor_available: true,
1480 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001481 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001482 }
1483
1484 cc_library {
1485 name: "mylib2",
1486 srcs: ["mylib.cpp"],
1487 system_shared_libs: [],
1488 vendor_available: true,
1489 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001490 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001491 }
Jooyung Handc782442019-11-01 03:14:38 +09001492 `, func(fs map[string][]byte, config android.Config) {
1493 setUseVendorWhitelistForTest(config, []string{"myapex"})
1494 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001495
1496 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001497 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001498 for _, implicit := range i.Implicits {
1499 inputsList = append(inputsList, implicit.String())
1500 }
1501 }
1502 inputsString := strings.Join(inputsList, " ")
1503
1504 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001505 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1506 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001507
1508 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001509 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1510 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001511}
Jiyong Park16e91a02018-12-20 18:18:08 +09001512
Jooyung Handc782442019-11-01 03:14:38 +09001513func TestUseVendorRestriction(t *testing.T) {
1514 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1515 apex {
1516 name: "myapex",
1517 key: "myapex.key",
1518 use_vendor: true,
1519 }
1520 apex_key {
1521 name: "myapex.key",
1522 public_key: "testkey.avbpubkey",
1523 private_key: "testkey.pem",
1524 }
1525 `, func(fs map[string][]byte, config android.Config) {
1526 setUseVendorWhitelistForTest(config, []string{""})
1527 })
1528 // no error with whitelist
1529 testApex(t, `
1530 apex {
1531 name: "myapex",
1532 key: "myapex.key",
1533 use_vendor: true,
1534 }
1535 apex_key {
1536 name: "myapex.key",
1537 public_key: "testkey.avbpubkey",
1538 private_key: "testkey.pem",
1539 }
1540 `, func(fs map[string][]byte, config android.Config) {
1541 setUseVendorWhitelistForTest(config, []string{"myapex"})
1542 })
1543}
1544
Jooyung Han5c998b92019-06-27 11:30:33 +09001545func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1546 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1547 apex {
1548 name: "myapex",
1549 key: "myapex.key",
1550 native_shared_libs: ["mylib"],
1551 use_vendor: true,
1552 }
1553
1554 apex_key {
1555 name: "myapex.key",
1556 public_key: "testkey.avbpubkey",
1557 private_key: "testkey.pem",
1558 }
1559
1560 cc_library {
1561 name: "mylib",
1562 srcs: ["mylib.cpp"],
1563 system_shared_libs: [],
1564 stl: "none",
1565 }
1566 `)
1567}
1568
Jiyong Park16e91a02018-12-20 18:18:08 +09001569func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001570 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001571 apex {
1572 name: "myapex",
1573 key: "myapex.key",
1574 native_shared_libs: ["mylib"],
1575 }
1576
1577 apex_key {
1578 name: "myapex.key",
1579 public_key: "testkey.avbpubkey",
1580 private_key: "testkey.pem",
1581 }
1582
1583 cc_library {
1584 name: "mylib",
1585 srcs: ["mylib.cpp"],
1586 system_shared_libs: [],
1587 stl: "none",
1588 stubs: {
1589 versions: ["1", "2", "3"],
1590 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001591 apex_available: [
1592 "//apex_available:platform",
1593 "myapex",
1594 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001595 }
1596
1597 cc_binary {
1598 name: "not_in_apex",
1599 srcs: ["mylib.cpp"],
1600 static_libs: ["mylib"],
1601 static_executable: true,
1602 system_shared_libs: [],
1603 stl: "none",
1604 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001605 `)
1606
Colin Cross7113d202019-11-20 16:39:12 -08001607 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001608
1609 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001610 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001611}
Jiyong Park9335a262018-12-24 11:31:58 +09001612
1613func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001614 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001615 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001616 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001617 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001618 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001619 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001620 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001621 }
1622
1623 cc_library {
1624 name: "mylib",
1625 srcs: ["mylib.cpp"],
1626 system_shared_libs: [],
1627 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001628 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001629 }
1630
1631 apex_key {
1632 name: "myapex.key",
1633 public_key: "testkey.avbpubkey",
1634 private_key: "testkey.pem",
1635 }
1636
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001637 android_app_certificate {
1638 name: "myapex.certificate",
1639 certificate: "testkey",
1640 }
1641
1642 android_app_certificate {
1643 name: "myapex.certificate.override",
1644 certificate: "testkey.override",
1645 }
1646
Jiyong Park9335a262018-12-24 11:31:58 +09001647 `)
1648
1649 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001650 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001651
1652 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1653 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1654 "vendor/foo/devkeys/testkey.avbpubkey")
1655 }
1656 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1657 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1658 "vendor/foo/devkeys/testkey.pem")
1659 }
1660
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001661 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001662 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001663 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001664 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001665 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001666 }
1667}
Jiyong Park58e364a2019-01-19 19:24:06 +09001668
Jooyung Hanf121a652019-12-17 14:30:11 +09001669func TestCertificate(t *testing.T) {
1670 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1671 ctx, _ := testApex(t, `
1672 apex {
1673 name: "myapex",
1674 key: "myapex.key",
1675 }
1676 apex_key {
1677 name: "myapex.key",
1678 public_key: "testkey.avbpubkey",
1679 private_key: "testkey.pem",
1680 }`)
1681 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1682 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1683 if actual := rule.Args["certificates"]; actual != expected {
1684 t.Errorf("certificates should be %q, not %q", expected, actual)
1685 }
1686 })
1687 t.Run("override when unspecified", func(t *testing.T) {
1688 ctx, _ := testApex(t, `
1689 apex {
1690 name: "myapex_keytest",
1691 key: "myapex.key",
1692 file_contexts: ":myapex-file_contexts",
1693 }
1694 apex_key {
1695 name: "myapex.key",
1696 public_key: "testkey.avbpubkey",
1697 private_key: "testkey.pem",
1698 }
1699 android_app_certificate {
1700 name: "myapex.certificate.override",
1701 certificate: "testkey.override",
1702 }`)
1703 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1704 expected := "testkey.override.x509.pem testkey.override.pk8"
1705 if actual := rule.Args["certificates"]; actual != expected {
1706 t.Errorf("certificates should be %q, not %q", expected, actual)
1707 }
1708 })
1709 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1710 ctx, _ := testApex(t, `
1711 apex {
1712 name: "myapex",
1713 key: "myapex.key",
1714 certificate: ":myapex.certificate",
1715 }
1716 apex_key {
1717 name: "myapex.key",
1718 public_key: "testkey.avbpubkey",
1719 private_key: "testkey.pem",
1720 }
1721 android_app_certificate {
1722 name: "myapex.certificate",
1723 certificate: "testkey",
1724 }`)
1725 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1726 expected := "testkey.x509.pem testkey.pk8"
1727 if actual := rule.Args["certificates"]; actual != expected {
1728 t.Errorf("certificates should be %q, not %q", expected, actual)
1729 }
1730 })
1731 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1732 ctx, _ := testApex(t, `
1733 apex {
1734 name: "myapex_keytest",
1735 key: "myapex.key",
1736 file_contexts: ":myapex-file_contexts",
1737 certificate: ":myapex.certificate",
1738 }
1739 apex_key {
1740 name: "myapex.key",
1741 public_key: "testkey.avbpubkey",
1742 private_key: "testkey.pem",
1743 }
1744 android_app_certificate {
1745 name: "myapex.certificate.override",
1746 certificate: "testkey.override",
1747 }`)
1748 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1749 expected := "testkey.override.x509.pem testkey.override.pk8"
1750 if actual := rule.Args["certificates"]; actual != expected {
1751 t.Errorf("certificates should be %q, not %q", expected, actual)
1752 }
1753 })
1754 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1755 ctx, _ := testApex(t, `
1756 apex {
1757 name: "myapex",
1758 key: "myapex.key",
1759 certificate: "testkey",
1760 }
1761 apex_key {
1762 name: "myapex.key",
1763 public_key: "testkey.avbpubkey",
1764 private_key: "testkey.pem",
1765 }`)
1766 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1767 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1768 if actual := rule.Args["certificates"]; actual != expected {
1769 t.Errorf("certificates should be %q, not %q", expected, actual)
1770 }
1771 })
1772 t.Run("override when specified as <name>", func(t *testing.T) {
1773 ctx, _ := testApex(t, `
1774 apex {
1775 name: "myapex_keytest",
1776 key: "myapex.key",
1777 file_contexts: ":myapex-file_contexts",
1778 certificate: "testkey",
1779 }
1780 apex_key {
1781 name: "myapex.key",
1782 public_key: "testkey.avbpubkey",
1783 private_key: "testkey.pem",
1784 }
1785 android_app_certificate {
1786 name: "myapex.certificate.override",
1787 certificate: "testkey.override",
1788 }`)
1789 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1790 expected := "testkey.override.x509.pem testkey.override.pk8"
1791 if actual := rule.Args["certificates"]; actual != expected {
1792 t.Errorf("certificates should be %q, not %q", expected, actual)
1793 }
1794 })
1795}
1796
Jiyong Park58e364a2019-01-19 19:24:06 +09001797func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001798 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001799 apex {
1800 name: "myapex",
1801 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001802 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001803 }
1804
1805 apex {
1806 name: "otherapex",
1807 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001808 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09001809 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09001810 }
1811
1812 apex_key {
1813 name: "myapex.key",
1814 public_key: "testkey.avbpubkey",
1815 private_key: "testkey.pem",
1816 }
1817
1818 cc_library {
1819 name: "mylib",
1820 srcs: ["mylib.cpp"],
1821 system_shared_libs: [],
1822 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001823 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001824 "myapex",
1825 "otherapex",
1826 ],
Jooyung Han24282772020-03-21 23:20:55 +09001827 recovery_available: true,
Jiyong Park58e364a2019-01-19 19:24:06 +09001828 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09001829 cc_library {
1830 name: "mylib2",
1831 srcs: ["mylib.cpp"],
1832 system_shared_libs: [],
1833 stl: "none",
1834 apex_available: [
1835 "myapex",
1836 "otherapex",
1837 ],
1838 use_apex_name_macro: true,
1839 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001840 `)
1841
Jooyung Hanc87a0592020-03-02 17:44:33 +09001842 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001843 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001844 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han24282772020-03-21 23:20:55 +09001845 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001846
Jooyung Hanccce2f22020-03-07 03:45:53 +09001847 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001848 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1849 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001850 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09001851 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001852
Jooyung Hanccce2f22020-03-07 03:45:53 +09001853 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001854 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1855 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001856 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09001857 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001858
Jooyung Hanc87a0592020-03-02 17:44:33 +09001859 // When cc_library sets use_apex_name_macro: true
1860 // apex variants define additional macro to distinguish which apex variant it is built for
1861
1862 // non-APEX variant does not have __ANDROID_APEX__ defined
1863 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1864 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1865
1866 // APEX variant has __ANDROID_APEX__ defined
1867 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001868 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001869 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1870 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001871
Jooyung Hanc87a0592020-03-02 17:44:33 +09001872 // APEX variant has __ANDROID_APEX__ defined
1873 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001874 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001875 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1876 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09001877
1878 // recovery variant does not set __ANDROID_SDK_VERSION__
1879 mylibCFlags = ctx.ModuleForTests("mylib", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1880 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1881 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001882}
Jiyong Park7e636d02019-01-28 16:16:54 +09001883
1884func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001885 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001886 apex {
1887 name: "myapex",
1888 key: "myapex.key",
1889 native_shared_libs: ["mylib"],
1890 }
1891
1892 apex_key {
1893 name: "myapex.key",
1894 public_key: "testkey.avbpubkey",
1895 private_key: "testkey.pem",
1896 }
1897
1898 cc_library_headers {
1899 name: "mylib_headers",
1900 export_include_dirs: ["my_include"],
1901 system_shared_libs: [],
1902 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001903 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001904 }
1905
1906 cc_library {
1907 name: "mylib",
1908 srcs: ["mylib.cpp"],
1909 system_shared_libs: [],
1910 stl: "none",
1911 header_libs: ["mylib_headers"],
1912 export_header_lib_headers: ["mylib_headers"],
1913 stubs: {
1914 versions: ["1", "2", "3"],
1915 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001916 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001917 }
1918
1919 cc_library {
1920 name: "otherlib",
1921 srcs: ["mylib.cpp"],
1922 system_shared_libs: [],
1923 stl: "none",
1924 shared_libs: ["mylib"],
1925 }
1926 `)
1927
Colin Cross7113d202019-11-20 16:39:12 -08001928 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001929
1930 // Ensure that the include path of the header lib is exported to 'otherlib'
1931 ensureContains(t, cFlags, "-Imy_include")
1932}
Alex Light9670d332019-01-29 18:07:33 -08001933
Jiyong Park7cd10e32020-01-14 09:22:18 +09001934type fileInApex struct {
1935 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001936 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001937 isLink bool
1938}
1939
Jooyung Hana57af4a2020-01-23 05:36:59 +00001940func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001941 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001942 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001943 copyCmds := apexRule.Args["copy_commands"]
1944 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001945 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001946 for _, cmd := range strings.Split(copyCmds, "&&") {
1947 cmd = strings.TrimSpace(cmd)
1948 if cmd == "" {
1949 continue
1950 }
1951 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001952 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001953 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001954 switch terms[0] {
1955 case "mkdir":
1956 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001957 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001958 t.Fatal("copyCmds contains invalid cp command", cmd)
1959 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001960 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001961 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001962 isLink = false
1963 case "ln":
1964 if len(terms) != 3 && len(terms) != 4 {
1965 // ln LINK TARGET or ln -s LINK TARGET
1966 t.Fatal("copyCmds contains invalid ln command", cmd)
1967 }
1968 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001969 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001970 isLink = true
1971 default:
1972 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1973 }
1974 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001975 index := strings.Index(dst, imageApexDir)
1976 if index == -1 {
1977 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1978 }
1979 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001980 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001981 }
1982 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001983 return ret
1984}
1985
Jooyung Hana57af4a2020-01-23 05:36:59 +00001986func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1987 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001988 var failed bool
1989 var surplus []string
1990 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001991 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001992 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001993 for _, expected := range files {
1994 if matched, _ := path.Match(expected, file.path); matched {
1995 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001996 mactchFound = true
1997 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001998 }
1999 }
Jooyung Hane6436d72020-02-27 13:31:56 +09002000 if !mactchFound {
2001 surplus = append(surplus, file.path)
2002 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002003 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002004
Jooyung Han31c470b2019-10-18 16:26:59 +09002005 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002006 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09002007 t.Log("surplus files", surplus)
2008 failed = true
2009 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002010
2011 if len(files) > len(filesMatched) {
2012 var missing []string
2013 for _, expected := range files {
2014 if !filesMatched[expected] {
2015 missing = append(missing, expected)
2016 }
2017 }
2018 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09002019 t.Log("missing files", missing)
2020 failed = true
2021 }
2022 if failed {
2023 t.Fail()
2024 }
2025}
2026
Jooyung Han344d5432019-08-23 11:17:39 +09002027func TestVndkApexCurrent(t *testing.T) {
2028 ctx, _ := testApex(t, `
2029 apex_vndk {
2030 name: "myapex",
2031 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002032 }
2033
2034 apex_key {
2035 name: "myapex.key",
2036 public_key: "testkey.avbpubkey",
2037 private_key: "testkey.pem",
2038 }
2039
2040 cc_library {
2041 name: "libvndk",
2042 srcs: ["mylib.cpp"],
2043 vendor_available: true,
2044 vndk: {
2045 enabled: true,
2046 },
2047 system_shared_libs: [],
2048 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002049 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002050 }
2051
2052 cc_library {
2053 name: "libvndksp",
2054 srcs: ["mylib.cpp"],
2055 vendor_available: true,
2056 vndk: {
2057 enabled: true,
2058 support_system_process: true,
2059 },
2060 system_shared_libs: [],
2061 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002062 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002063 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002064 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002065
Jooyung Hana57af4a2020-01-23 05:36:59 +00002066 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002067 "lib/libvndk.so",
2068 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002069 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002070 "lib64/libvndk.so",
2071 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002072 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002073 "etc/llndk.libraries.VER.txt",
2074 "etc/vndkcore.libraries.VER.txt",
2075 "etc/vndksp.libraries.VER.txt",
2076 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002077 })
Jooyung Han344d5432019-08-23 11:17:39 +09002078}
2079
2080func TestVndkApexWithPrebuilt(t *testing.T) {
2081 ctx, _ := testApex(t, `
2082 apex_vndk {
2083 name: "myapex",
2084 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002085 }
2086
2087 apex_key {
2088 name: "myapex.key",
2089 public_key: "testkey.avbpubkey",
2090 private_key: "testkey.pem",
2091 }
2092
2093 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002094 name: "libvndk",
2095 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002096 vendor_available: true,
2097 vndk: {
2098 enabled: true,
2099 },
2100 system_shared_libs: [],
2101 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002102 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002103 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002104
2105 cc_prebuilt_library_shared {
2106 name: "libvndk.arm",
2107 srcs: ["libvndk.arm.so"],
2108 vendor_available: true,
2109 vndk: {
2110 enabled: true,
2111 },
2112 enabled: false,
2113 arch: {
2114 arm: {
2115 enabled: true,
2116 },
2117 },
2118 system_shared_libs: [],
2119 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002120 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002121 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002122 `+vndkLibrariesTxtFiles("current"),
2123 withFiles(map[string][]byte{
2124 "libvndk.so": nil,
2125 "libvndk.arm.so": nil,
2126 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002127
Jooyung Hana57af4a2020-01-23 05:36:59 +00002128 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002129 "lib/libvndk.so",
2130 "lib/libvndk.arm.so",
2131 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002132 "lib/libc++.so",
2133 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002134 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002135 })
Jooyung Han344d5432019-08-23 11:17:39 +09002136}
2137
Jooyung Han39edb6c2019-11-06 16:53:07 +09002138func vndkLibrariesTxtFiles(vers ...string) (result string) {
2139 for _, v := range vers {
2140 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002141 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002142 result += `
2143 vndk_libraries_txt {
2144 name: "` + txt + `.libraries.txt",
2145 }
2146 `
2147 }
2148 } else {
2149 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2150 result += `
2151 prebuilt_etc {
2152 name: "` + txt + `.libraries.` + v + `.txt",
2153 src: "dummy.txt",
2154 }
2155 `
2156 }
2157 }
2158 }
2159 return
2160}
2161
Jooyung Han344d5432019-08-23 11:17:39 +09002162func TestVndkApexVersion(t *testing.T) {
2163 ctx, _ := testApex(t, `
2164 apex_vndk {
2165 name: "myapex_v27",
2166 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002167 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002168 vndk_version: "27",
2169 }
2170
2171 apex_key {
2172 name: "myapex.key",
2173 public_key: "testkey.avbpubkey",
2174 private_key: "testkey.pem",
2175 }
2176
Jooyung Han31c470b2019-10-18 16:26:59 +09002177 vndk_prebuilt_shared {
2178 name: "libvndk27",
2179 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002180 vendor_available: true,
2181 vndk: {
2182 enabled: true,
2183 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002184 target_arch: "arm64",
2185 arch: {
2186 arm: {
2187 srcs: ["libvndk27_arm.so"],
2188 },
2189 arm64: {
2190 srcs: ["libvndk27_arm64.so"],
2191 },
2192 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002193 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002194 }
2195
2196 vndk_prebuilt_shared {
2197 name: "libvndk27",
2198 version: "27",
2199 vendor_available: true,
2200 vndk: {
2201 enabled: true,
2202 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002203 target_arch: "x86_64",
2204 arch: {
2205 x86: {
2206 srcs: ["libvndk27_x86.so"],
2207 },
2208 x86_64: {
2209 srcs: ["libvndk27_x86_64.so"],
2210 },
2211 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002212 }
2213 `+vndkLibrariesTxtFiles("27"),
2214 withFiles(map[string][]byte{
2215 "libvndk27_arm.so": nil,
2216 "libvndk27_arm64.so": nil,
2217 "libvndk27_x86.so": nil,
2218 "libvndk27_x86_64.so": nil,
2219 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002220
Jooyung Hana57af4a2020-01-23 05:36:59 +00002221 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002222 "lib/libvndk27_arm.so",
2223 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002224 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002225 })
Jooyung Han344d5432019-08-23 11:17:39 +09002226}
2227
2228func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2229 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2230 apex_vndk {
2231 name: "myapex_v27",
2232 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002233 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002234 vndk_version: "27",
2235 }
2236 apex_vndk {
2237 name: "myapex_v27_other",
2238 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002239 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002240 vndk_version: "27",
2241 }
2242
2243 apex_key {
2244 name: "myapex.key",
2245 public_key: "testkey.avbpubkey",
2246 private_key: "testkey.pem",
2247 }
2248
2249 cc_library {
2250 name: "libvndk",
2251 srcs: ["mylib.cpp"],
2252 vendor_available: true,
2253 vndk: {
2254 enabled: true,
2255 },
2256 system_shared_libs: [],
2257 stl: "none",
2258 }
2259
2260 vndk_prebuilt_shared {
2261 name: "libvndk",
2262 version: "27",
2263 vendor_available: true,
2264 vndk: {
2265 enabled: true,
2266 },
2267 srcs: ["libvndk.so"],
2268 }
2269 `, withFiles(map[string][]byte{
2270 "libvndk.so": nil,
2271 }))
2272}
2273
Jooyung Han90eee022019-10-01 20:02:42 +09002274func TestVndkApexNameRule(t *testing.T) {
2275 ctx, _ := testApex(t, `
2276 apex_vndk {
2277 name: "myapex",
2278 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002279 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002280 }
2281 apex_vndk {
2282 name: "myapex_v28",
2283 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002284 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002285 vndk_version: "28",
2286 }
2287 apex_key {
2288 name: "myapex.key",
2289 public_key: "testkey.avbpubkey",
2290 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002291 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002292
2293 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002294 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002295 actual := proptools.String(bundle.properties.Apex_name)
2296 if !reflect.DeepEqual(actual, expected) {
2297 t.Errorf("Got '%v', expected '%v'", actual, expected)
2298 }
2299 }
2300
2301 assertApexName("com.android.vndk.vVER", "myapex")
2302 assertApexName("com.android.vndk.v28", "myapex_v28")
2303}
2304
Jooyung Han344d5432019-08-23 11:17:39 +09002305func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2306 ctx, _ := testApex(t, `
2307 apex_vndk {
2308 name: "myapex",
2309 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002310 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002311 }
2312
2313 apex_key {
2314 name: "myapex.key",
2315 public_key: "testkey.avbpubkey",
2316 private_key: "testkey.pem",
2317 }
2318
2319 cc_library {
2320 name: "libvndk",
2321 srcs: ["mylib.cpp"],
2322 vendor_available: true,
2323 native_bridge_supported: true,
2324 host_supported: true,
2325 vndk: {
2326 enabled: true,
2327 },
2328 system_shared_libs: [],
2329 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002330 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002331 }
Jooyung Han35155c42020-02-06 17:33:20 +09002332 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09002333
Jooyung Hana57af4a2020-01-23 05:36:59 +00002334 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002335 "lib/libvndk.so",
2336 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002337 "lib/libc++.so",
2338 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002339 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002340 })
Jooyung Han344d5432019-08-23 11:17:39 +09002341}
2342
2343func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2344 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2345 apex_vndk {
2346 name: "myapex",
2347 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002348 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002349 native_bridge_supported: true,
2350 }
2351
2352 apex_key {
2353 name: "myapex.key",
2354 public_key: "testkey.avbpubkey",
2355 private_key: "testkey.pem",
2356 }
2357
2358 cc_library {
2359 name: "libvndk",
2360 srcs: ["mylib.cpp"],
2361 vendor_available: true,
2362 native_bridge_supported: true,
2363 host_supported: true,
2364 vndk: {
2365 enabled: true,
2366 },
2367 system_shared_libs: [],
2368 stl: "none",
2369 }
2370 `)
2371}
2372
Jooyung Han31c470b2019-10-18 16:26:59 +09002373func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002374 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002375 apex_vndk {
2376 name: "myapex_v27",
2377 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002378 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002379 vndk_version: "27",
2380 }
2381
2382 apex_key {
2383 name: "myapex.key",
2384 public_key: "testkey.avbpubkey",
2385 private_key: "testkey.pem",
2386 }
2387
2388 vndk_prebuilt_shared {
2389 name: "libvndk27",
2390 version: "27",
2391 target_arch: "arm",
2392 vendor_available: true,
2393 vndk: {
2394 enabled: true,
2395 },
2396 arch: {
2397 arm: {
2398 srcs: ["libvndk27.so"],
2399 }
2400 },
2401 }
2402
2403 vndk_prebuilt_shared {
2404 name: "libvndk27",
2405 version: "27",
2406 target_arch: "arm",
2407 binder32bit: true,
2408 vendor_available: true,
2409 vndk: {
2410 enabled: true,
2411 },
2412 arch: {
2413 arm: {
2414 srcs: ["libvndk27binder32.so"],
2415 }
2416 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002417 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002418 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002419 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002420 withFiles(map[string][]byte{
2421 "libvndk27.so": nil,
2422 "libvndk27binder32.so": nil,
2423 }),
2424 withBinder32bit,
2425 withTargets(map[android.OsType][]android.Target{
2426 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09002427 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
2428 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09002429 },
2430 }),
2431 )
2432
Jooyung Hana57af4a2020-01-23 05:36:59 +00002433 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002434 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002435 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002436 })
2437}
2438
Jooyung Hane1633032019-08-01 17:41:43 +09002439func TestDependenciesInApexManifest(t *testing.T) {
2440 ctx, _ := testApex(t, `
2441 apex {
2442 name: "myapex_nodep",
2443 key: "myapex.key",
2444 native_shared_libs: ["lib_nodep"],
2445 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002446 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002447 }
2448
2449 apex {
2450 name: "myapex_dep",
2451 key: "myapex.key",
2452 native_shared_libs: ["lib_dep"],
2453 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002454 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002455 }
2456
2457 apex {
2458 name: "myapex_provider",
2459 key: "myapex.key",
2460 native_shared_libs: ["libfoo"],
2461 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002462 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002463 }
2464
2465 apex {
2466 name: "myapex_selfcontained",
2467 key: "myapex.key",
2468 native_shared_libs: ["lib_dep", "libfoo"],
2469 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002470 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002471 }
2472
2473 apex_key {
2474 name: "myapex.key",
2475 public_key: "testkey.avbpubkey",
2476 private_key: "testkey.pem",
2477 }
2478
2479 cc_library {
2480 name: "lib_nodep",
2481 srcs: ["mylib.cpp"],
2482 system_shared_libs: [],
2483 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002484 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002485 }
2486
2487 cc_library {
2488 name: "lib_dep",
2489 srcs: ["mylib.cpp"],
2490 shared_libs: ["libfoo"],
2491 system_shared_libs: [],
2492 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002493 apex_available: [
2494 "myapex_dep",
2495 "myapex_provider",
2496 "myapex_selfcontained",
2497 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002498 }
2499
2500 cc_library {
2501 name: "libfoo",
2502 srcs: ["mytest.cpp"],
2503 stubs: {
2504 versions: ["1"],
2505 },
2506 system_shared_libs: [],
2507 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002508 apex_available: [
2509 "myapex_provider",
2510 "myapex_selfcontained",
2511 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002512 }
2513 `)
2514
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002515 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002516 var provideNativeLibs, requireNativeLibs []string
2517
Sundong Ahnabb64432019-10-22 13:58:29 +09002518 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002519 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2520 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002521 ensureListEmpty(t, provideNativeLibs)
2522 ensureListEmpty(t, requireNativeLibs)
2523
Sundong Ahnabb64432019-10-22 13:58:29 +09002524 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002525 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2526 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002527 ensureListEmpty(t, provideNativeLibs)
2528 ensureListContains(t, requireNativeLibs, "libfoo.so")
2529
Sundong Ahnabb64432019-10-22 13:58:29 +09002530 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002531 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2532 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002533 ensureListContains(t, provideNativeLibs, "libfoo.so")
2534 ensureListEmpty(t, requireNativeLibs)
2535
Sundong Ahnabb64432019-10-22 13:58:29 +09002536 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002537 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2538 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002539 ensureListContains(t, provideNativeLibs, "libfoo.so")
2540 ensureListEmpty(t, requireNativeLibs)
2541}
2542
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002543func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002544 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002545 apex {
2546 name: "myapex",
2547 key: "myapex.key",
2548 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002549 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002550 }
2551
2552 apex_key {
2553 name: "myapex.key",
2554 public_key: "testkey.avbpubkey",
2555 private_key: "testkey.pem",
2556 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002557
2558 cc_library {
2559 name: "mylib",
2560 srcs: ["mylib.cpp"],
2561 system_shared_libs: [],
2562 stl: "none",
2563 apex_available: [
2564 "//apex_available:platform",
2565 "myapex",
2566 ],
2567 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002568 `)
2569
Sundong Ahnabb64432019-10-22 13:58:29 +09002570 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002571 apexManifestRule := module.Rule("apexManifestRule")
2572 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2573 apexRule := module.Rule("apexRule")
2574 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002575
2576 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2577 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2578 name := apexBundle.BaseModuleName()
2579 prefix := "TARGET_"
2580 var builder strings.Builder
2581 data.Custom(&builder, name, prefix, "", data)
2582 androidMk := builder.String()
2583 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2584 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002585}
2586
Alex Light0851b882019-02-07 13:20:53 -08002587func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002588 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002589 apex {
2590 name: "myapex",
2591 key: "myapex.key",
2592 native_shared_libs: ["mylib_common"],
2593 }
2594
2595 apex_key {
2596 name: "myapex.key",
2597 public_key: "testkey.avbpubkey",
2598 private_key: "testkey.pem",
2599 }
2600
2601 cc_library {
2602 name: "mylib_common",
2603 srcs: ["mylib.cpp"],
2604 system_shared_libs: [],
2605 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002606 apex_available: [
2607 "//apex_available:platform",
2608 "myapex",
2609 ],
Alex Light0851b882019-02-07 13:20:53 -08002610 }
2611 `)
2612
Sundong Ahnabb64432019-10-22 13:58:29 +09002613 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002614 apexRule := module.Rule("apexRule")
2615 copyCmds := apexRule.Args["copy_commands"]
2616
2617 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2618 t.Log("Apex was a test apex!")
2619 t.Fail()
2620 }
2621 // Ensure that main rule creates an output
2622 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2623
2624 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002625 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002626
2627 // Ensure that both direct and indirect deps are copied into apex
2628 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2629
Colin Cross7113d202019-11-20 16:39:12 -08002630 // Ensure that the platform variant ends with _shared
2631 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002632
2633 if !android.InAnyApex("mylib_common") {
2634 t.Log("Found mylib_common not in any apex!")
2635 t.Fail()
2636 }
2637}
2638
2639func TestTestApex(t *testing.T) {
2640 if android.InAnyApex("mylib_common_test") {
2641 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!")
2642 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002643 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002644 apex_test {
2645 name: "myapex",
2646 key: "myapex.key",
2647 native_shared_libs: ["mylib_common_test"],
2648 }
2649
2650 apex_key {
2651 name: "myapex.key",
2652 public_key: "testkey.avbpubkey",
2653 private_key: "testkey.pem",
2654 }
2655
2656 cc_library {
2657 name: "mylib_common_test",
2658 srcs: ["mylib.cpp"],
2659 system_shared_libs: [],
2660 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002661 // TODO: remove //apex_available:platform
2662 apex_available: [
2663 "//apex_available:platform",
2664 "myapex",
2665 ],
Alex Light0851b882019-02-07 13:20:53 -08002666 }
2667 `)
2668
Sundong Ahnabb64432019-10-22 13:58:29 +09002669 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002670 apexRule := module.Rule("apexRule")
2671 copyCmds := apexRule.Args["copy_commands"]
2672
2673 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2674 t.Log("Apex was not a test apex!")
2675 t.Fail()
2676 }
2677 // Ensure that main rule creates an output
2678 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2679
2680 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002681 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002682
2683 // Ensure that both direct and indirect deps are copied into apex
2684 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2685
Colin Cross7113d202019-11-20 16:39:12 -08002686 // Ensure that the platform variant ends with _shared
2687 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002688}
2689
Alex Light9670d332019-01-29 18:07:33 -08002690func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002691 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002692 apex {
2693 name: "myapex",
2694 key: "myapex.key",
2695 multilib: {
2696 first: {
2697 native_shared_libs: ["mylib_common"],
2698 }
2699 },
2700 target: {
2701 android: {
2702 multilib: {
2703 first: {
2704 native_shared_libs: ["mylib"],
2705 }
2706 }
2707 },
2708 host: {
2709 multilib: {
2710 first: {
2711 native_shared_libs: ["mylib2"],
2712 }
2713 }
2714 }
2715 }
2716 }
2717
2718 apex_key {
2719 name: "myapex.key",
2720 public_key: "testkey.avbpubkey",
2721 private_key: "testkey.pem",
2722 }
2723
2724 cc_library {
2725 name: "mylib",
2726 srcs: ["mylib.cpp"],
2727 system_shared_libs: [],
2728 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002729 // TODO: remove //apex_available:platform
2730 apex_available: [
2731 "//apex_available:platform",
2732 "myapex",
2733 ],
Alex Light9670d332019-01-29 18:07:33 -08002734 }
2735
2736 cc_library {
2737 name: "mylib_common",
2738 srcs: ["mylib.cpp"],
2739 system_shared_libs: [],
2740 stl: "none",
2741 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002742 // TODO: remove //apex_available:platform
2743 apex_available: [
2744 "//apex_available:platform",
2745 "myapex",
2746 ],
Alex Light9670d332019-01-29 18:07:33 -08002747 }
2748
2749 cc_library {
2750 name: "mylib2",
2751 srcs: ["mylib.cpp"],
2752 system_shared_libs: [],
2753 stl: "none",
2754 compile_multilib: "first",
2755 }
2756 `)
2757
Sundong Ahnabb64432019-10-22 13:58:29 +09002758 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002759 copyCmds := apexRule.Args["copy_commands"]
2760
2761 // Ensure that main rule creates an output
2762 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2763
2764 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002765 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2766 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2767 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002768
2769 // Ensure that both direct and indirect deps are copied into apex
2770 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2771 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2772 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2773
Colin Cross7113d202019-11-20 16:39:12 -08002774 // Ensure that the platform variant ends with _shared
2775 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2776 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2777 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002778}
Jiyong Park04480cf2019-02-06 00:16:29 +09002779
2780func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002781 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002782 apex {
2783 name: "myapex",
2784 key: "myapex.key",
2785 binaries: ["myscript"],
2786 }
2787
2788 apex_key {
2789 name: "myapex.key",
2790 public_key: "testkey.avbpubkey",
2791 private_key: "testkey.pem",
2792 }
2793
2794 sh_binary {
2795 name: "myscript",
2796 src: "mylib.cpp",
2797 filename: "myscript.sh",
2798 sub_dir: "script",
2799 }
2800 `)
2801
Sundong Ahnabb64432019-10-22 13:58:29 +09002802 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002803 copyCmds := apexRule.Args["copy_commands"]
2804
2805 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2806}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002807
Jooyung Han91df2082019-11-20 01:49:42 +09002808func TestApexInVariousPartition(t *testing.T) {
2809 testcases := []struct {
2810 propName, parition, flattenedPartition string
2811 }{
2812 {"", "system", "system_ext"},
2813 {"product_specific: true", "product", "product"},
2814 {"soc_specific: true", "vendor", "vendor"},
2815 {"proprietary: true", "vendor", "vendor"},
2816 {"vendor: true", "vendor", "vendor"},
2817 {"system_ext_specific: true", "system_ext", "system_ext"},
2818 }
2819 for _, tc := range testcases {
2820 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2821 ctx, _ := testApex(t, `
2822 apex {
2823 name: "myapex",
2824 key: "myapex.key",
2825 `+tc.propName+`
2826 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002827
Jooyung Han91df2082019-11-20 01:49:42 +09002828 apex_key {
2829 name: "myapex.key",
2830 public_key: "testkey.avbpubkey",
2831 private_key: "testkey.pem",
2832 }
2833 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002834
Jooyung Han91df2082019-11-20 01:49:42 +09002835 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2836 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2837 actual := apex.installDir.String()
2838 if actual != expected {
2839 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2840 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002841
Jooyung Han91df2082019-11-20 01:49:42 +09002842 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2843 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2844 actual = flattened.installDir.String()
2845 if actual != expected {
2846 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2847 }
2848 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002849 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002850}
Jiyong Park67882562019-03-21 01:11:21 +09002851
Jooyung Han54aca7b2019-11-20 02:26:02 +09002852func TestFileContexts(t *testing.T) {
2853 ctx, _ := testApex(t, `
2854 apex {
2855 name: "myapex",
2856 key: "myapex.key",
2857 }
2858
2859 apex_key {
2860 name: "myapex.key",
2861 public_key: "testkey.avbpubkey",
2862 private_key: "testkey.pem",
2863 }
2864 `)
2865 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2866 apexRule := module.Rule("apexRule")
2867 actual := apexRule.Args["file_contexts"]
2868 expected := "system/sepolicy/apex/myapex-file_contexts"
2869 if actual != expected {
2870 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2871 }
2872
2873 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2874 apex {
2875 name: "myapex",
2876 key: "myapex.key",
2877 file_contexts: "my_own_file_contexts",
2878 }
2879
2880 apex_key {
2881 name: "myapex.key",
2882 public_key: "testkey.avbpubkey",
2883 private_key: "testkey.pem",
2884 }
2885 `, withFiles(map[string][]byte{
2886 "my_own_file_contexts": nil,
2887 }))
2888
2889 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2890 apex {
2891 name: "myapex",
2892 key: "myapex.key",
2893 product_specific: true,
2894 file_contexts: "product_specific_file_contexts",
2895 }
2896
2897 apex_key {
2898 name: "myapex.key",
2899 public_key: "testkey.avbpubkey",
2900 private_key: "testkey.pem",
2901 }
2902 `)
2903
2904 ctx, _ = testApex(t, `
2905 apex {
2906 name: "myapex",
2907 key: "myapex.key",
2908 product_specific: true,
2909 file_contexts: "product_specific_file_contexts",
2910 }
2911
2912 apex_key {
2913 name: "myapex.key",
2914 public_key: "testkey.avbpubkey",
2915 private_key: "testkey.pem",
2916 }
2917 `, withFiles(map[string][]byte{
2918 "product_specific_file_contexts": nil,
2919 }))
2920 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2921 apexRule = module.Rule("apexRule")
2922 actual = apexRule.Args["file_contexts"]
2923 expected = "product_specific_file_contexts"
2924 if actual != expected {
2925 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2926 }
2927
2928 ctx, _ = testApex(t, `
2929 apex {
2930 name: "myapex",
2931 key: "myapex.key",
2932 product_specific: true,
2933 file_contexts: ":my-file-contexts",
2934 }
2935
2936 apex_key {
2937 name: "myapex.key",
2938 public_key: "testkey.avbpubkey",
2939 private_key: "testkey.pem",
2940 }
2941
2942 filegroup {
2943 name: "my-file-contexts",
2944 srcs: ["product_specific_file_contexts"],
2945 }
2946 `, withFiles(map[string][]byte{
2947 "product_specific_file_contexts": nil,
2948 }))
2949 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2950 apexRule = module.Rule("apexRule")
2951 actual = apexRule.Args["file_contexts"]
2952 expected = "product_specific_file_contexts"
2953 if actual != expected {
2954 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2955 }
2956}
2957
Jiyong Park67882562019-03-21 01:11:21 +09002958func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002959 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002960 apex_key {
2961 name: "myapex.key",
2962 public_key: ":my.avbpubkey",
2963 private_key: ":my.pem",
2964 product_specific: true,
2965 }
2966
2967 filegroup {
2968 name: "my.avbpubkey",
2969 srcs: ["testkey2.avbpubkey"],
2970 }
2971
2972 filegroup {
2973 name: "my.pem",
2974 srcs: ["testkey2.pem"],
2975 }
2976 `)
2977
2978 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2979 expected_pubkey := "testkey2.avbpubkey"
2980 actual_pubkey := apex_key.public_key_file.String()
2981 if actual_pubkey != expected_pubkey {
2982 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2983 }
2984 expected_privkey := "testkey2.pem"
2985 actual_privkey := apex_key.private_key_file.String()
2986 if actual_privkey != expected_privkey {
2987 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2988 }
2989}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002990
2991func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002992 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002993 prebuilt_apex {
2994 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002995 arch: {
2996 arm64: {
2997 src: "myapex-arm64.apex",
2998 },
2999 arm: {
3000 src: "myapex-arm.apex",
3001 },
3002 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003003 }
3004 `)
3005
3006 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3007
Jiyong Parkc95714e2019-03-29 14:23:10 +09003008 expectedInput := "myapex-arm64.apex"
3009 if prebuilt.inputApex.String() != expectedInput {
3010 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
3011 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003012}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003013
3014func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003015 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003016 prebuilt_apex {
3017 name: "myapex",
3018 src: "myapex-arm.apex",
3019 filename: "notmyapex.apex",
3020 }
3021 `)
3022
3023 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3024
3025 expected := "notmyapex.apex"
3026 if p.installFilename != expected {
3027 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
3028 }
3029}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003030
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003031func TestPrebuiltOverrides(t *testing.T) {
3032 ctx, config := testApex(t, `
3033 prebuilt_apex {
3034 name: "myapex.prebuilt",
3035 src: "myapex-arm.apex",
3036 overrides: [
3037 "myapex",
3038 ],
3039 }
3040 `)
3041
3042 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3043
3044 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003045 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003046 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003047 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003048 }
3049}
3050
Roland Levillain630846d2019-06-26 12:48:34 +01003051func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003052 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003053 apex_test {
3054 name: "myapex",
3055 key: "myapex.key",
3056 tests: [
3057 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003058 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003059 ],
3060 }
3061
3062 apex_key {
3063 name: "myapex.key",
3064 public_key: "testkey.avbpubkey",
3065 private_key: "testkey.pem",
3066 }
3067
3068 cc_test {
3069 name: "mytest",
3070 gtest: false,
3071 srcs: ["mytest.cpp"],
3072 relative_install_path: "test",
3073 system_shared_libs: [],
3074 static_executable: true,
3075 stl: "none",
3076 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003077
3078 cc_test {
3079 name: "mytests",
3080 gtest: false,
3081 srcs: [
3082 "mytest1.cpp",
3083 "mytest2.cpp",
3084 "mytest3.cpp",
3085 ],
3086 test_per_src: true,
3087 relative_install_path: "test",
3088 system_shared_libs: [],
3089 static_executable: true,
3090 stl: "none",
3091 }
Roland Levillain630846d2019-06-26 12:48:34 +01003092 `)
3093
Sundong Ahnabb64432019-10-22 13:58:29 +09003094 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003095 copyCmds := apexRule.Args["copy_commands"]
3096
3097 // Ensure that test dep is copied into apex.
3098 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003099
3100 // Ensure that test deps built with `test_per_src` are copied into apex.
3101 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3102 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3103 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003104
3105 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003106 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003107 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3108 name := apexBundle.BaseModuleName()
3109 prefix := "TARGET_"
3110 var builder strings.Builder
3111 data.Custom(&builder, name, prefix, "", data)
3112 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003113 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3114 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3115 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3116 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003117 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003118 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003119 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003120}
3121
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003122func TestInstallExtraFlattenedApexes(t *testing.T) {
3123 ctx, config := testApex(t, `
3124 apex {
3125 name: "myapex",
3126 key: "myapex.key",
3127 }
3128 apex_key {
3129 name: "myapex.key",
3130 public_key: "testkey.avbpubkey",
3131 private_key: "testkey.pem",
3132 }
3133 `, func(fs map[string][]byte, config android.Config) {
3134 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3135 })
3136 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003137 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003138 mk := android.AndroidMkDataForTest(t, config, "", ab)
3139 var builder strings.Builder
3140 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3141 androidMk := builder.String()
3142 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3143}
3144
Jooyung Han5c998b92019-06-27 11:30:33 +09003145func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003146 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003147 apex {
3148 name: "myapex",
3149 key: "myapex.key",
3150 native_shared_libs: ["mylib"],
3151 uses: ["commonapex"],
3152 }
3153
3154 apex {
3155 name: "commonapex",
3156 key: "myapex.key",
3157 native_shared_libs: ["libcommon"],
3158 provide_cpp_shared_libs: true,
3159 }
3160
3161 apex_key {
3162 name: "myapex.key",
3163 public_key: "testkey.avbpubkey",
3164 private_key: "testkey.pem",
3165 }
3166
3167 cc_library {
3168 name: "mylib",
3169 srcs: ["mylib.cpp"],
3170 shared_libs: ["libcommon"],
3171 system_shared_libs: [],
3172 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003173 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003174 }
3175
3176 cc_library {
3177 name: "libcommon",
3178 srcs: ["mylib_common.cpp"],
3179 system_shared_libs: [],
3180 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003181 // TODO: remove //apex_available:platform
3182 apex_available: [
3183 "//apex_available:platform",
3184 "commonapex",
3185 "myapex",
3186 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003187 }
3188 `)
3189
Sundong Ahnabb64432019-10-22 13:58:29 +09003190 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003191 apexRule1 := module1.Rule("apexRule")
3192 copyCmds1 := apexRule1.Args["copy_commands"]
3193
Sundong Ahnabb64432019-10-22 13:58:29 +09003194 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003195 apexRule2 := module2.Rule("apexRule")
3196 copyCmds2 := apexRule2.Args["copy_commands"]
3197
Colin Cross7113d202019-11-20 16:39:12 -08003198 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3199 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003200 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3201 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3202 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3203}
3204
3205func TestApexUsesFailsIfNotProvided(t *testing.T) {
3206 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3207 apex {
3208 name: "myapex",
3209 key: "myapex.key",
3210 uses: ["commonapex"],
3211 }
3212
3213 apex {
3214 name: "commonapex",
3215 key: "myapex.key",
3216 }
3217
3218 apex_key {
3219 name: "myapex.key",
3220 public_key: "testkey.avbpubkey",
3221 private_key: "testkey.pem",
3222 }
3223 `)
3224 testApexError(t, `uses: "commonapex" is not a provider`, `
3225 apex {
3226 name: "myapex",
3227 key: "myapex.key",
3228 uses: ["commonapex"],
3229 }
3230
3231 cc_library {
3232 name: "commonapex",
3233 system_shared_libs: [],
3234 stl: "none",
3235 }
3236
3237 apex_key {
3238 name: "myapex.key",
3239 public_key: "testkey.avbpubkey",
3240 private_key: "testkey.pem",
3241 }
3242 `)
3243}
3244
3245func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3246 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3247 apex {
3248 name: "myapex",
3249 key: "myapex.key",
3250 use_vendor: true,
3251 uses: ["commonapex"],
3252 }
3253
3254 apex {
3255 name: "commonapex",
3256 key: "myapex.key",
3257 provide_cpp_shared_libs: true,
3258 }
3259
3260 apex_key {
3261 name: "myapex.key",
3262 public_key: "testkey.avbpubkey",
3263 private_key: "testkey.pem",
3264 }
Jooyung Handc782442019-11-01 03:14:38 +09003265 `, func(fs map[string][]byte, config android.Config) {
3266 setUseVendorWhitelistForTest(config, []string{"myapex"})
3267 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003268}
3269
Jooyung Hand48f3c32019-08-23 11:18:57 +09003270func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3271 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3272 apex {
3273 name: "myapex",
3274 key: "myapex.key",
3275 native_shared_libs: ["libfoo"],
3276 }
3277
3278 apex_key {
3279 name: "myapex.key",
3280 public_key: "testkey.avbpubkey",
3281 private_key: "testkey.pem",
3282 }
3283
3284 cc_library {
3285 name: "libfoo",
3286 stl: "none",
3287 system_shared_libs: [],
3288 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003289 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003290 }
3291 `)
3292 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3293 apex {
3294 name: "myapex",
3295 key: "myapex.key",
3296 java_libs: ["myjar"],
3297 }
3298
3299 apex_key {
3300 name: "myapex.key",
3301 public_key: "testkey.avbpubkey",
3302 private_key: "testkey.pem",
3303 }
3304
3305 java_library {
3306 name: "myjar",
3307 srcs: ["foo/bar/MyClass.java"],
3308 sdk_version: "none",
3309 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003310 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003311 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003312 }
3313 `)
3314}
3315
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003316func TestApexWithApps(t *testing.T) {
3317 ctx, _ := testApex(t, `
3318 apex {
3319 name: "myapex",
3320 key: "myapex.key",
3321 apps: [
3322 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003323 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003324 ],
3325 }
3326
3327 apex_key {
3328 name: "myapex.key",
3329 public_key: "testkey.avbpubkey",
3330 private_key: "testkey.pem",
3331 }
3332
3333 android_app {
3334 name: "AppFoo",
3335 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003336 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003337 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003338 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08003339 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003340 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003341 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003342
3343 android_app {
3344 name: "AppFooPriv",
3345 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003346 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09003347 system_modules: "none",
3348 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08003349 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003350 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003351 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003352
3353 cc_library_shared {
3354 name: "libjni",
3355 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003356 shared_libs: ["libfoo"],
3357 stl: "none",
3358 system_shared_libs: [],
3359 apex_available: [ "myapex" ],
3360 sdk_version: "current",
3361 }
3362
3363 cc_library_shared {
3364 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09003365 stl: "none",
3366 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003367 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08003368 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003369 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003370 `)
3371
Sundong Ahnabb64432019-10-22 13:58:29 +09003372 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003373 apexRule := module.Rule("apexRule")
3374 copyCmds := apexRule.Args["copy_commands"]
3375
3376 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003377 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003378
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003379 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3380 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003381 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003382 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003383 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003384 // JNI libraries including transitive deps are
3385 for _, jni := range []string{"libjni", "libfoo"} {
3386 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
3387 // ... embedded inside APK (jnilibs.zip)
3388 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3389 // ... and not directly inside the APEX
3390 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3391 }
Dario Frenicde2a032019-10-27 00:29:22 +01003392}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003393
Dario Frenicde2a032019-10-27 00:29:22 +01003394func TestApexWithAppImports(t *testing.T) {
3395 ctx, _ := testApex(t, `
3396 apex {
3397 name: "myapex",
3398 key: "myapex.key",
3399 apps: [
3400 "AppFooPrebuilt",
3401 "AppFooPrivPrebuilt",
3402 ],
3403 }
3404
3405 apex_key {
3406 name: "myapex.key",
3407 public_key: "testkey.avbpubkey",
3408 private_key: "testkey.pem",
3409 }
3410
3411 android_app_import {
3412 name: "AppFooPrebuilt",
3413 apk: "PrebuiltAppFoo.apk",
3414 presigned: true,
3415 dex_preopt: {
3416 enabled: false,
3417 },
3418 }
3419
3420 android_app_import {
3421 name: "AppFooPrivPrebuilt",
3422 apk: "PrebuiltAppFooPriv.apk",
3423 privileged: true,
3424 presigned: true,
3425 dex_preopt: {
3426 enabled: false,
3427 },
3428 }
3429 `)
3430
Sundong Ahnabb64432019-10-22 13:58:29 +09003431 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003432 apexRule := module.Rule("apexRule")
3433 copyCmds := apexRule.Args["copy_commands"]
3434
3435 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3436 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003437}
3438
Dario Freni6f3937c2019-12-20 22:58:03 +00003439func TestApexWithTestHelperApp(t *testing.T) {
3440 ctx, _ := testApex(t, `
3441 apex {
3442 name: "myapex",
3443 key: "myapex.key",
3444 apps: [
3445 "TesterHelpAppFoo",
3446 ],
3447 }
3448
3449 apex_key {
3450 name: "myapex.key",
3451 public_key: "testkey.avbpubkey",
3452 private_key: "testkey.pem",
3453 }
3454
3455 android_test_helper_app {
3456 name: "TesterHelpAppFoo",
3457 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003458 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003459 }
3460
3461 `)
3462
3463 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3464 apexRule := module.Rule("apexRule")
3465 copyCmds := apexRule.Args["copy_commands"]
3466
3467 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3468}
3469
Jooyung Han18020ea2019-11-13 10:50:48 +09003470func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3471 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09003472 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003473 apex {
3474 name: "myapex",
3475 key: "myapex.key",
3476 native_shared_libs: ["libfoo"],
3477 }
3478
3479 apex_key {
3480 name: "myapex.key",
3481 public_key: "testkey.avbpubkey",
3482 private_key: "testkey.pem",
3483 }
3484
3485 apex {
3486 name: "otherapex",
3487 key: "myapex.key",
3488 native_shared_libs: ["libfoo"],
3489 }
3490
3491 cc_defaults {
3492 name: "libfoo-defaults",
3493 apex_available: ["otherapex"],
3494 }
3495
3496 cc_library {
3497 name: "libfoo",
3498 defaults: ["libfoo-defaults"],
3499 stl: "none",
3500 system_shared_libs: [],
3501 }`)
3502}
3503
Paul Duffine52e66f2020-03-30 17:54:29 +01003504func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003505 // libfoo is not available to myapex, but only to otherapex
3506 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3507 apex {
3508 name: "myapex",
3509 key: "myapex.key",
3510 native_shared_libs: ["libfoo"],
3511 }
3512
3513 apex_key {
3514 name: "myapex.key",
3515 public_key: "testkey.avbpubkey",
3516 private_key: "testkey.pem",
3517 }
3518
3519 apex {
3520 name: "otherapex",
3521 key: "otherapex.key",
3522 native_shared_libs: ["libfoo"],
3523 }
3524
3525 apex_key {
3526 name: "otherapex.key",
3527 public_key: "testkey.avbpubkey",
3528 private_key: "testkey.pem",
3529 }
3530
3531 cc_library {
3532 name: "libfoo",
3533 stl: "none",
3534 system_shared_libs: [],
3535 apex_available: ["otherapex"],
3536 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01003537}
Jiyong Park127b40b2019-09-30 16:04:35 +09003538
Paul Duffine52e66f2020-03-30 17:54:29 +01003539func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09003540 // libbbaz is an indirect dep
3541 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003542 apex {
3543 name: "myapex",
3544 key: "myapex.key",
3545 native_shared_libs: ["libfoo"],
3546 }
3547
3548 apex_key {
3549 name: "myapex.key",
3550 public_key: "testkey.avbpubkey",
3551 private_key: "testkey.pem",
3552 }
3553
Jiyong Park127b40b2019-09-30 16:04:35 +09003554 cc_library {
3555 name: "libfoo",
3556 stl: "none",
3557 shared_libs: ["libbar"],
3558 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003559 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003560 }
3561
3562 cc_library {
3563 name: "libbar",
3564 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09003565 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003566 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003567 apex_available: ["myapex"],
3568 }
3569
3570 cc_library {
3571 name: "libbaz",
3572 stl: "none",
3573 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003574 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01003575}
Jiyong Park127b40b2019-09-30 16:04:35 +09003576
Paul Duffine52e66f2020-03-30 17:54:29 +01003577func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003578 testApexError(t, "\"otherapex\" is not a valid module name", `
3579 apex {
3580 name: "myapex",
3581 key: "myapex.key",
3582 native_shared_libs: ["libfoo"],
3583 }
3584
3585 apex_key {
3586 name: "myapex.key",
3587 public_key: "testkey.avbpubkey",
3588 private_key: "testkey.pem",
3589 }
3590
3591 cc_library {
3592 name: "libfoo",
3593 stl: "none",
3594 system_shared_libs: [],
3595 apex_available: ["otherapex"],
3596 }`)
3597
Paul Duffine52e66f2020-03-30 17:54:29 +01003598 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003599 apex {
3600 name: "myapex",
3601 key: "myapex.key",
3602 native_shared_libs: ["libfoo", "libbar"],
3603 }
3604
3605 apex_key {
3606 name: "myapex.key",
3607 public_key: "testkey.avbpubkey",
3608 private_key: "testkey.pem",
3609 }
3610
3611 cc_library {
3612 name: "libfoo",
3613 stl: "none",
3614 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09003615 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003616 apex_available: ["myapex"],
3617 }
3618
3619 cc_library {
3620 name: "libbar",
3621 stl: "none",
3622 system_shared_libs: [],
3623 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09003624 }
3625
3626 cc_library {
3627 name: "libbaz",
3628 stl: "none",
3629 system_shared_libs: [],
3630 stubs: {
3631 versions: ["10", "20", "30"],
3632 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003633 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01003634}
Jiyong Park127b40b2019-09-30 16:04:35 +09003635
Paul Duffine52e66f2020-03-30 17:54:29 +01003636func TestApexAvailable_CreatedForPlatform(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003637 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003638 // TODO(jiyong) the checks for the platform variant are removed because we now create
3639 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3640 // the platform variants are not used from other platform modules. When that is done,
3641 // these checks will be replaced by expecting a specific error message that will be
3642 // emitted when the platform variant is used.
3643 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3644 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3645 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3646 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003647
Paul Duffine52e66f2020-03-30 17:54:29 +01003648 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003649 apex {
3650 name: "myapex",
3651 key: "myapex.key",
3652 }
3653
3654 apex_key {
3655 name: "myapex.key",
3656 public_key: "testkey.avbpubkey",
3657 private_key: "testkey.pem",
3658 }
3659
3660 cc_library {
3661 name: "libfoo",
3662 stl: "none",
3663 system_shared_libs: [],
3664 apex_available: ["//apex_available:platform"],
3665 }`)
3666
3667 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003668 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3669 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Paul Duffine52e66f2020-03-30 17:54:29 +01003670}
Jiyong Parka90ca002019-10-07 15:47:24 +09003671
Paul Duffine52e66f2020-03-30 17:54:29 +01003672func TestApexAvailable_CreatedForApex(t *testing.T) {
3673 testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09003674 apex {
3675 name: "myapex",
3676 key: "myapex.key",
3677 native_shared_libs: ["libfoo"],
3678 }
3679
3680 apex_key {
3681 name: "myapex.key",
3682 public_key: "testkey.avbpubkey",
3683 private_key: "testkey.pem",
3684 }
3685
3686 cc_library {
3687 name: "libfoo",
3688 stl: "none",
3689 system_shared_libs: [],
3690 apex_available: ["myapex"],
3691 static: {
3692 apex_available: ["//apex_available:platform"],
3693 },
3694 }`)
3695
3696 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003697 // TODO(jiyong) the checks for the platform variant are removed because we now create
3698 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3699 // the platform variants are not used from other platform modules. When that is done,
3700 // these checks will be replaced by expecting a specific error message that will be
3701 // emitted when the platform variant is used.
3702 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3703 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3704 // // but the static variant is available to both myapex and the platform
3705 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3706 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003707}
3708
Jiyong Park5d790c32019-11-15 18:40:32 +09003709func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003710 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003711 apex {
3712 name: "myapex",
3713 key: "myapex.key",
3714 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003715 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003716 }
3717
3718 override_apex {
3719 name: "override_myapex",
3720 base: "myapex",
3721 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003722 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003723 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003724 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09003725 }
3726
3727 apex_key {
3728 name: "myapex.key",
3729 public_key: "testkey.avbpubkey",
3730 private_key: "testkey.pem",
3731 }
3732
3733 android_app {
3734 name: "app",
3735 srcs: ["foo/bar/MyClass.java"],
3736 package_name: "foo",
3737 sdk_version: "none",
3738 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003739 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003740 }
3741
3742 override_android_app {
3743 name: "override_app",
3744 base: "app",
3745 package_name: "bar",
3746 }
Jiyong Park20bacab2020-03-03 11:45:41 +09003747 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003748
Jiyong Park317645e2019-12-05 13:20:58 +09003749 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3750 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3751 if originalVariant.GetOverriddenBy() != "" {
3752 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3753 }
3754 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3755 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3756 }
3757
Jiyong Park5d790c32019-11-15 18:40:32 +09003758 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3759 apexRule := module.Rule("apexRule")
3760 copyCmds := apexRule.Args["copy_commands"]
3761
3762 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3763 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003764
3765 apexBundle := module.Module().(*apexBundle)
3766 name := apexBundle.Name()
3767 if name != "override_myapex" {
3768 t.Errorf("name should be \"override_myapex\", but was %q", name)
3769 }
3770
Baligh Uddin004d7172020-02-19 21:29:28 -08003771 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3772 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3773 }
3774
Jiyong Park20bacab2020-03-03 11:45:41 +09003775 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003776 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09003777
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003778 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3779 var builder strings.Builder
3780 data.Custom(&builder, name, "TARGET_", "", data)
3781 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003782 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003783 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3784 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003785 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003786 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003787 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003788 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3789 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003790}
3791
Jooyung Han214bf372019-11-12 13:03:50 +09003792func TestLegacyAndroid10Support(t *testing.T) {
3793 ctx, _ := testApex(t, `
3794 apex {
3795 name: "myapex",
3796 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003797 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09003798 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09003799 }
3800
3801 apex_key {
3802 name: "myapex.key",
3803 public_key: "testkey.avbpubkey",
3804 private_key: "testkey.pem",
3805 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003806
3807 cc_library {
3808 name: "mylib",
3809 srcs: ["mylib.cpp"],
3810 stl: "libc++",
3811 system_shared_libs: [],
3812 apex_available: [ "myapex" ],
3813 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003814 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003815
3816 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3817 args := module.Rule("apexRule").Args
3818 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003819 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003820
3821 // The copies of the libraries in the apex should have one more dependency than
3822 // the ones outside the apex, namely the unwinder. Ideally we should check
3823 // the dependency names directly here but for some reason the names are blank in
3824 // this test.
3825 for _, lib := range []string{"libc++", "mylib"} {
3826 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3827 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3828 if len(apexImplicits) != len(nonApexImplicits)+1 {
3829 t.Errorf("%q missing unwinder dep", lib)
3830 }
3831 }
Jooyung Han214bf372019-11-12 13:03:50 +09003832}
3833
Jooyung Han58f26ab2019-12-18 15:34:32 +09003834func TestJavaSDKLibrary(t *testing.T) {
3835 ctx, _ := testApex(t, `
3836 apex {
3837 name: "myapex",
3838 key: "myapex.key",
3839 java_libs: ["foo"],
3840 }
3841
3842 apex_key {
3843 name: "myapex.key",
3844 public_key: "testkey.avbpubkey",
3845 private_key: "testkey.pem",
3846 }
3847
3848 java_sdk_library {
3849 name: "foo",
3850 srcs: ["a.java"],
3851 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003852 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003853 }
3854 `, withFiles(map[string][]byte{
3855 "api/current.txt": nil,
3856 "api/removed.txt": nil,
3857 "api/system-current.txt": nil,
3858 "api/system-removed.txt": nil,
3859 "api/test-current.txt": nil,
3860 "api/test-removed.txt": nil,
3861 }))
3862
3863 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003864 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003865 "javalib/foo.jar",
3866 "etc/permissions/foo.xml",
3867 })
3868 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003869 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3870 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003871}
3872
atrost6e126252020-01-27 17:01:16 +00003873func TestCompatConfig(t *testing.T) {
3874 ctx, _ := testApex(t, `
3875 apex {
3876 name: "myapex",
3877 key: "myapex.key",
3878 prebuilts: ["myjar-platform-compat-config"],
3879 java_libs: ["myjar"],
3880 }
3881
3882 apex_key {
3883 name: "myapex.key",
3884 public_key: "testkey.avbpubkey",
3885 private_key: "testkey.pem",
3886 }
3887
3888 platform_compat_config {
3889 name: "myjar-platform-compat-config",
3890 src: ":myjar",
3891 }
3892
3893 java_library {
3894 name: "myjar",
3895 srcs: ["foo/bar/MyClass.java"],
3896 sdk_version: "none",
3897 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003898 apex_available: [ "myapex" ],
3899 }
3900 `)
3901 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3902 "etc/compatconfig/myjar-platform-compat-config.xml",
3903 "javalib/myjar.jar",
3904 })
3905}
3906
Jiyong Park479321d2019-12-16 11:47:12 +09003907func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3908 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3909 apex {
3910 name: "myapex",
3911 key: "myapex.key",
3912 java_libs: ["myjar"],
3913 }
3914
3915 apex_key {
3916 name: "myapex.key",
3917 public_key: "testkey.avbpubkey",
3918 private_key: "testkey.pem",
3919 }
3920
3921 java_library {
3922 name: "myjar",
3923 srcs: ["foo/bar/MyClass.java"],
3924 sdk_version: "none",
3925 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003926 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003927 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003928 }
3929 `)
3930}
3931
Jiyong Park7afd1072019-12-30 16:56:33 +09003932func TestCarryRequiredModuleNames(t *testing.T) {
3933 ctx, config := testApex(t, `
3934 apex {
3935 name: "myapex",
3936 key: "myapex.key",
3937 native_shared_libs: ["mylib"],
3938 }
3939
3940 apex_key {
3941 name: "myapex.key",
3942 public_key: "testkey.avbpubkey",
3943 private_key: "testkey.pem",
3944 }
3945
3946 cc_library {
3947 name: "mylib",
3948 srcs: ["mylib.cpp"],
3949 system_shared_libs: [],
3950 stl: "none",
3951 required: ["a", "b"],
3952 host_required: ["c", "d"],
3953 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003954 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003955 }
3956 `)
3957
3958 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3959 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3960 name := apexBundle.BaseModuleName()
3961 prefix := "TARGET_"
3962 var builder strings.Builder
3963 data.Custom(&builder, name, prefix, "", data)
3964 androidMk := builder.String()
3965 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3966 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3967 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3968}
3969
Jiyong Park7cd10e32020-01-14 09:22:18 +09003970func TestSymlinksFromApexToSystem(t *testing.T) {
3971 bp := `
3972 apex {
3973 name: "myapex",
3974 key: "myapex.key",
3975 native_shared_libs: ["mylib"],
3976 java_libs: ["myjar"],
3977 }
3978
Jiyong Park9d677202020-02-19 16:29:35 +09003979 apex {
3980 name: "myapex.updatable",
3981 key: "myapex.key",
3982 native_shared_libs: ["mylib"],
3983 java_libs: ["myjar"],
3984 updatable: true,
3985 }
3986
Jiyong Park7cd10e32020-01-14 09:22:18 +09003987 apex_key {
3988 name: "myapex.key",
3989 public_key: "testkey.avbpubkey",
3990 private_key: "testkey.pem",
3991 }
3992
3993 cc_library {
3994 name: "mylib",
3995 srcs: ["mylib.cpp"],
3996 shared_libs: ["myotherlib"],
3997 system_shared_libs: [],
3998 stl: "none",
3999 apex_available: [
4000 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004001 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004002 "//apex_available:platform",
4003 ],
4004 }
4005
4006 cc_library {
4007 name: "myotherlib",
4008 srcs: ["mylib.cpp"],
4009 system_shared_libs: [],
4010 stl: "none",
4011 apex_available: [
4012 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004013 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004014 "//apex_available:platform",
4015 ],
4016 }
4017
4018 java_library {
4019 name: "myjar",
4020 srcs: ["foo/bar/MyClass.java"],
4021 sdk_version: "none",
4022 system_modules: "none",
4023 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09004024 apex_available: [
4025 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004026 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004027 "//apex_available:platform",
4028 ],
4029 }
4030
4031 java_library {
4032 name: "myotherjar",
4033 srcs: ["foo/bar/MyClass.java"],
4034 sdk_version: "none",
4035 system_modules: "none",
4036 apex_available: [
4037 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004038 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004039 "//apex_available:platform",
4040 ],
4041 }
4042 `
4043
4044 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
4045 for _, f := range files {
4046 if f.path == file {
4047 if f.isLink {
4048 t.Errorf("%q is not a real file", file)
4049 }
4050 return
4051 }
4052 }
4053 t.Errorf("%q is not found", file)
4054 }
4055
4056 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
4057 for _, f := range files {
4058 if f.path == file {
4059 if !f.isLink {
4060 t.Errorf("%q is not a symlink", file)
4061 }
4062 return
4063 }
4064 }
4065 t.Errorf("%q is not found", file)
4066 }
4067
Jiyong Park9d677202020-02-19 16:29:35 +09004068 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4069 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004070 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004071 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004072 ensureRealfileExists(t, files, "javalib/myjar.jar")
4073 ensureRealfileExists(t, files, "lib64/mylib.so")
4074 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4075
Jiyong Park9d677202020-02-19 16:29:35 +09004076 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4077 ensureRealfileExists(t, files, "javalib/myjar.jar")
4078 ensureRealfileExists(t, files, "lib64/mylib.so")
4079 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4080
4081 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004082 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004083 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004084 ensureRealfileExists(t, files, "javalib/myjar.jar")
4085 ensureRealfileExists(t, files, "lib64/mylib.so")
4086 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004087
4088 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4089 ensureRealfileExists(t, files, "javalib/myjar.jar")
4090 ensureRealfileExists(t, files, "lib64/mylib.so")
4091 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004092}
4093
Jooyung Han643adc42020-02-27 13:50:06 +09004094func TestApexWithJniLibs(t *testing.T) {
4095 ctx, _ := testApex(t, `
4096 apex {
4097 name: "myapex",
4098 key: "myapex.key",
4099 jni_libs: ["mylib"],
4100 }
4101
4102 apex_key {
4103 name: "myapex.key",
4104 public_key: "testkey.avbpubkey",
4105 private_key: "testkey.pem",
4106 }
4107
4108 cc_library {
4109 name: "mylib",
4110 srcs: ["mylib.cpp"],
4111 shared_libs: ["mylib2"],
4112 system_shared_libs: [],
4113 stl: "none",
4114 apex_available: [ "myapex" ],
4115 }
4116
4117 cc_library {
4118 name: "mylib2",
4119 srcs: ["mylib.cpp"],
4120 system_shared_libs: [],
4121 stl: "none",
4122 apex_available: [ "myapex" ],
4123 }
4124 `)
4125
4126 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
4127 // Notice mylib2.so (transitive dep) is not added as a jni_lib
4128 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
4129 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4130 "lib64/mylib.so",
4131 "lib64/mylib2.so",
4132 })
4133}
4134
4135func TestApexWithJniLibs_Errors(t *testing.T) {
4136 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
4137 apex {
4138 name: "myapex",
4139 key: "myapex.key",
4140 jni_libs: ["xxx"],
4141 }
4142
4143 apex_key {
4144 name: "myapex.key",
4145 public_key: "testkey.avbpubkey",
4146 private_key: "testkey.pem",
4147 }
4148
4149 prebuilt_etc {
4150 name: "xxx",
4151 src: "xxx",
4152 }
4153 `, withFiles(map[string][]byte{
4154 "xxx": nil,
4155 }))
4156}
4157
Jiyong Parkbd159612020-02-28 15:22:21 +09004158func TestAppBundle(t *testing.T) {
4159 ctx, _ := testApex(t, `
4160 apex {
4161 name: "myapex",
4162 key: "myapex.key",
4163 apps: ["AppFoo"],
4164 }
4165
4166 apex_key {
4167 name: "myapex.key",
4168 public_key: "testkey.avbpubkey",
4169 private_key: "testkey.pem",
4170 }
4171
4172 android_app {
4173 name: "AppFoo",
4174 srcs: ["foo/bar/MyClass.java"],
4175 sdk_version: "none",
4176 system_modules: "none",
4177 apex_available: [ "myapex" ],
4178 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004179 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09004180
4181 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4182 content := bundleConfigRule.Args["content"]
4183
4184 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004185 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 +09004186}
4187
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004188func TestMain(m *testing.M) {
4189 run := func() int {
4190 setUp()
4191 defer tearDown()
4192
4193 return m.Run()
4194 }
4195
4196 os.Exit(run())
4197}