blob: 968efb58fe348c6e6d60c40c68cd4ec7faf09756 [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 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001827 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09001828 cc_library {
1829 name: "mylib2",
1830 srcs: ["mylib.cpp"],
1831 system_shared_libs: [],
1832 stl: "none",
1833 apex_available: [
1834 "myapex",
1835 "otherapex",
1836 ],
1837 use_apex_name_macro: true,
1838 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001839 `)
1840
Jooyung Hanc87a0592020-03-02 17:44:33 +09001841 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001842 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001843 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001844 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001845
Jooyung Hanccce2f22020-03-07 03:45:53 +09001846 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001847 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1848 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001849 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09001850 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001851
Jooyung Hanccce2f22020-03-07 03:45:53 +09001852 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001853 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1854 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001855 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09001856 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001857
Jooyung Hanc87a0592020-03-02 17:44:33 +09001858 // When cc_library sets use_apex_name_macro: true
1859 // apex variants define additional macro to distinguish which apex variant it is built for
1860
1861 // non-APEX variant does not have __ANDROID_APEX__ defined
1862 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1863 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1864
1865 // APEX variant has __ANDROID_APEX__ defined
1866 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001867 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001868 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1869 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001870
Jooyung Hanc87a0592020-03-02 17:44:33 +09001871 // APEX variant has __ANDROID_APEX__ defined
1872 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001873 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001874 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1875 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001876}
Jiyong Park7e636d02019-01-28 16:16:54 +09001877
1878func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001879 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001880 apex {
1881 name: "myapex",
1882 key: "myapex.key",
1883 native_shared_libs: ["mylib"],
1884 }
1885
1886 apex_key {
1887 name: "myapex.key",
1888 public_key: "testkey.avbpubkey",
1889 private_key: "testkey.pem",
1890 }
1891
1892 cc_library_headers {
1893 name: "mylib_headers",
1894 export_include_dirs: ["my_include"],
1895 system_shared_libs: [],
1896 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001897 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001898 }
1899
1900 cc_library {
1901 name: "mylib",
1902 srcs: ["mylib.cpp"],
1903 system_shared_libs: [],
1904 stl: "none",
1905 header_libs: ["mylib_headers"],
1906 export_header_lib_headers: ["mylib_headers"],
1907 stubs: {
1908 versions: ["1", "2", "3"],
1909 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001910 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001911 }
1912
1913 cc_library {
1914 name: "otherlib",
1915 srcs: ["mylib.cpp"],
1916 system_shared_libs: [],
1917 stl: "none",
1918 shared_libs: ["mylib"],
1919 }
1920 `)
1921
Colin Cross7113d202019-11-20 16:39:12 -08001922 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001923
1924 // Ensure that the include path of the header lib is exported to 'otherlib'
1925 ensureContains(t, cFlags, "-Imy_include")
1926}
Alex Light9670d332019-01-29 18:07:33 -08001927
Jiyong Park7cd10e32020-01-14 09:22:18 +09001928type fileInApex struct {
1929 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001930 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001931 isLink bool
1932}
1933
Jooyung Hana57af4a2020-01-23 05:36:59 +00001934func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001935 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001936 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001937 copyCmds := apexRule.Args["copy_commands"]
1938 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001939 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001940 for _, cmd := range strings.Split(copyCmds, "&&") {
1941 cmd = strings.TrimSpace(cmd)
1942 if cmd == "" {
1943 continue
1944 }
1945 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001946 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001947 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001948 switch terms[0] {
1949 case "mkdir":
1950 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001951 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001952 t.Fatal("copyCmds contains invalid cp command", cmd)
1953 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001954 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001955 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001956 isLink = false
1957 case "ln":
1958 if len(terms) != 3 && len(terms) != 4 {
1959 // ln LINK TARGET or ln -s LINK TARGET
1960 t.Fatal("copyCmds contains invalid ln command", cmd)
1961 }
1962 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001963 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001964 isLink = true
1965 default:
1966 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1967 }
1968 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001969 index := strings.Index(dst, imageApexDir)
1970 if index == -1 {
1971 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1972 }
1973 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001974 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001975 }
1976 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001977 return ret
1978}
1979
Jooyung Hana57af4a2020-01-23 05:36:59 +00001980func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1981 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001982 var failed bool
1983 var surplus []string
1984 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001985 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001986 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001987 for _, expected := range files {
1988 if matched, _ := path.Match(expected, file.path); matched {
1989 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001990 mactchFound = true
1991 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001992 }
1993 }
Jooyung Hane6436d72020-02-27 13:31:56 +09001994 if !mactchFound {
1995 surplus = append(surplus, file.path)
1996 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001997 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001998
Jooyung Han31c470b2019-10-18 16:26:59 +09001999 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002000 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09002001 t.Log("surplus files", surplus)
2002 failed = true
2003 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002004
2005 if len(files) > len(filesMatched) {
2006 var missing []string
2007 for _, expected := range files {
2008 if !filesMatched[expected] {
2009 missing = append(missing, expected)
2010 }
2011 }
2012 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09002013 t.Log("missing files", missing)
2014 failed = true
2015 }
2016 if failed {
2017 t.Fail()
2018 }
2019}
2020
Jooyung Han344d5432019-08-23 11:17:39 +09002021func TestVndkApexCurrent(t *testing.T) {
2022 ctx, _ := testApex(t, `
2023 apex_vndk {
2024 name: "myapex",
2025 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002026 }
2027
2028 apex_key {
2029 name: "myapex.key",
2030 public_key: "testkey.avbpubkey",
2031 private_key: "testkey.pem",
2032 }
2033
2034 cc_library {
2035 name: "libvndk",
2036 srcs: ["mylib.cpp"],
2037 vendor_available: true,
2038 vndk: {
2039 enabled: true,
2040 },
2041 system_shared_libs: [],
2042 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002043 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002044 }
2045
2046 cc_library {
2047 name: "libvndksp",
2048 srcs: ["mylib.cpp"],
2049 vendor_available: true,
2050 vndk: {
2051 enabled: true,
2052 support_system_process: true,
2053 },
2054 system_shared_libs: [],
2055 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002056 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002057 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002058 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002059
Jooyung Hana57af4a2020-01-23 05:36:59 +00002060 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002061 "lib/libvndk.so",
2062 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002063 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002064 "lib64/libvndk.so",
2065 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002066 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002067 "etc/llndk.libraries.VER.txt",
2068 "etc/vndkcore.libraries.VER.txt",
2069 "etc/vndksp.libraries.VER.txt",
2070 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002071 })
Jooyung Han344d5432019-08-23 11:17:39 +09002072}
2073
2074func TestVndkApexWithPrebuilt(t *testing.T) {
2075 ctx, _ := testApex(t, `
2076 apex_vndk {
2077 name: "myapex",
2078 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002079 }
2080
2081 apex_key {
2082 name: "myapex.key",
2083 public_key: "testkey.avbpubkey",
2084 private_key: "testkey.pem",
2085 }
2086
2087 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002088 name: "libvndk",
2089 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002090 vendor_available: true,
2091 vndk: {
2092 enabled: true,
2093 },
2094 system_shared_libs: [],
2095 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002096 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002097 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002098
2099 cc_prebuilt_library_shared {
2100 name: "libvndk.arm",
2101 srcs: ["libvndk.arm.so"],
2102 vendor_available: true,
2103 vndk: {
2104 enabled: true,
2105 },
2106 enabled: false,
2107 arch: {
2108 arm: {
2109 enabled: true,
2110 },
2111 },
2112 system_shared_libs: [],
2113 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002114 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002115 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002116 `+vndkLibrariesTxtFiles("current"),
2117 withFiles(map[string][]byte{
2118 "libvndk.so": nil,
2119 "libvndk.arm.so": nil,
2120 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002121
Jooyung Hana57af4a2020-01-23 05:36:59 +00002122 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002123 "lib/libvndk.so",
2124 "lib/libvndk.arm.so",
2125 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002126 "lib/libc++.so",
2127 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002128 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002129 })
Jooyung Han344d5432019-08-23 11:17:39 +09002130}
2131
Jooyung Han39edb6c2019-11-06 16:53:07 +09002132func vndkLibrariesTxtFiles(vers ...string) (result string) {
2133 for _, v := range vers {
2134 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002135 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002136 result += `
2137 vndk_libraries_txt {
2138 name: "` + txt + `.libraries.txt",
2139 }
2140 `
2141 }
2142 } else {
2143 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2144 result += `
2145 prebuilt_etc {
2146 name: "` + txt + `.libraries.` + v + `.txt",
2147 src: "dummy.txt",
2148 }
2149 `
2150 }
2151 }
2152 }
2153 return
2154}
2155
Jooyung Han344d5432019-08-23 11:17:39 +09002156func TestVndkApexVersion(t *testing.T) {
2157 ctx, _ := testApex(t, `
2158 apex_vndk {
2159 name: "myapex_v27",
2160 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002161 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002162 vndk_version: "27",
2163 }
2164
2165 apex_key {
2166 name: "myapex.key",
2167 public_key: "testkey.avbpubkey",
2168 private_key: "testkey.pem",
2169 }
2170
Jooyung Han31c470b2019-10-18 16:26:59 +09002171 vndk_prebuilt_shared {
2172 name: "libvndk27",
2173 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002174 vendor_available: true,
2175 vndk: {
2176 enabled: true,
2177 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002178 target_arch: "arm64",
2179 arch: {
2180 arm: {
2181 srcs: ["libvndk27_arm.so"],
2182 },
2183 arm64: {
2184 srcs: ["libvndk27_arm64.so"],
2185 },
2186 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002187 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002188 }
2189
2190 vndk_prebuilt_shared {
2191 name: "libvndk27",
2192 version: "27",
2193 vendor_available: true,
2194 vndk: {
2195 enabled: true,
2196 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002197 target_arch: "x86_64",
2198 arch: {
2199 x86: {
2200 srcs: ["libvndk27_x86.so"],
2201 },
2202 x86_64: {
2203 srcs: ["libvndk27_x86_64.so"],
2204 },
2205 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002206 }
2207 `+vndkLibrariesTxtFiles("27"),
2208 withFiles(map[string][]byte{
2209 "libvndk27_arm.so": nil,
2210 "libvndk27_arm64.so": nil,
2211 "libvndk27_x86.so": nil,
2212 "libvndk27_x86_64.so": nil,
2213 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002214
Jooyung Hana57af4a2020-01-23 05:36:59 +00002215 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002216 "lib/libvndk27_arm.so",
2217 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002218 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002219 })
Jooyung Han344d5432019-08-23 11:17:39 +09002220}
2221
2222func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2223 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2224 apex_vndk {
2225 name: "myapex_v27",
2226 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002227 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002228 vndk_version: "27",
2229 }
2230 apex_vndk {
2231 name: "myapex_v27_other",
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
2237 apex_key {
2238 name: "myapex.key",
2239 public_key: "testkey.avbpubkey",
2240 private_key: "testkey.pem",
2241 }
2242
2243 cc_library {
2244 name: "libvndk",
2245 srcs: ["mylib.cpp"],
2246 vendor_available: true,
2247 vndk: {
2248 enabled: true,
2249 },
2250 system_shared_libs: [],
2251 stl: "none",
2252 }
2253
2254 vndk_prebuilt_shared {
2255 name: "libvndk",
2256 version: "27",
2257 vendor_available: true,
2258 vndk: {
2259 enabled: true,
2260 },
2261 srcs: ["libvndk.so"],
2262 }
2263 `, withFiles(map[string][]byte{
2264 "libvndk.so": nil,
2265 }))
2266}
2267
Jooyung Han90eee022019-10-01 20:02:42 +09002268func TestVndkApexNameRule(t *testing.T) {
2269 ctx, _ := testApex(t, `
2270 apex_vndk {
2271 name: "myapex",
2272 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002273 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002274 }
2275 apex_vndk {
2276 name: "myapex_v28",
2277 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002278 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002279 vndk_version: "28",
2280 }
2281 apex_key {
2282 name: "myapex.key",
2283 public_key: "testkey.avbpubkey",
2284 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002285 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002286
2287 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002288 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002289 actual := proptools.String(bundle.properties.Apex_name)
2290 if !reflect.DeepEqual(actual, expected) {
2291 t.Errorf("Got '%v', expected '%v'", actual, expected)
2292 }
2293 }
2294
2295 assertApexName("com.android.vndk.vVER", "myapex")
2296 assertApexName("com.android.vndk.v28", "myapex_v28")
2297}
2298
Jooyung Han344d5432019-08-23 11:17:39 +09002299func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2300 ctx, _ := testApex(t, `
2301 apex_vndk {
2302 name: "myapex",
2303 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002304 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002305 }
2306
2307 apex_key {
2308 name: "myapex.key",
2309 public_key: "testkey.avbpubkey",
2310 private_key: "testkey.pem",
2311 }
2312
2313 cc_library {
2314 name: "libvndk",
2315 srcs: ["mylib.cpp"],
2316 vendor_available: true,
2317 native_bridge_supported: true,
2318 host_supported: true,
2319 vndk: {
2320 enabled: true,
2321 },
2322 system_shared_libs: [],
2323 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002324 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002325 }
Jooyung Han35155c42020-02-06 17:33:20 +09002326 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09002327
Jooyung Hana57af4a2020-01-23 05:36:59 +00002328 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002329 "lib/libvndk.so",
2330 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002331 "lib/libc++.so",
2332 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002333 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002334 })
Jooyung Han344d5432019-08-23 11:17:39 +09002335}
2336
2337func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2338 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2339 apex_vndk {
2340 name: "myapex",
2341 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002342 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002343 native_bridge_supported: true,
2344 }
2345
2346 apex_key {
2347 name: "myapex.key",
2348 public_key: "testkey.avbpubkey",
2349 private_key: "testkey.pem",
2350 }
2351
2352 cc_library {
2353 name: "libvndk",
2354 srcs: ["mylib.cpp"],
2355 vendor_available: true,
2356 native_bridge_supported: true,
2357 host_supported: true,
2358 vndk: {
2359 enabled: true,
2360 },
2361 system_shared_libs: [],
2362 stl: "none",
2363 }
2364 `)
2365}
2366
Jooyung Han31c470b2019-10-18 16:26:59 +09002367func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002368 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002369 apex_vndk {
2370 name: "myapex_v27",
2371 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002372 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002373 vndk_version: "27",
2374 }
2375
2376 apex_key {
2377 name: "myapex.key",
2378 public_key: "testkey.avbpubkey",
2379 private_key: "testkey.pem",
2380 }
2381
2382 vndk_prebuilt_shared {
2383 name: "libvndk27",
2384 version: "27",
2385 target_arch: "arm",
2386 vendor_available: true,
2387 vndk: {
2388 enabled: true,
2389 },
2390 arch: {
2391 arm: {
2392 srcs: ["libvndk27.so"],
2393 }
2394 },
2395 }
2396
2397 vndk_prebuilt_shared {
2398 name: "libvndk27",
2399 version: "27",
2400 target_arch: "arm",
2401 binder32bit: true,
2402 vendor_available: true,
2403 vndk: {
2404 enabled: true,
2405 },
2406 arch: {
2407 arm: {
2408 srcs: ["libvndk27binder32.so"],
2409 }
2410 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002411 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002412 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002413 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002414 withFiles(map[string][]byte{
2415 "libvndk27.so": nil,
2416 "libvndk27binder32.so": nil,
2417 }),
2418 withBinder32bit,
2419 withTargets(map[android.OsType][]android.Target{
2420 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09002421 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
2422 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09002423 },
2424 }),
2425 )
2426
Jooyung Hana57af4a2020-01-23 05:36:59 +00002427 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002428 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002429 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002430 })
2431}
2432
Jooyung Hane1633032019-08-01 17:41:43 +09002433func TestDependenciesInApexManifest(t *testing.T) {
2434 ctx, _ := testApex(t, `
2435 apex {
2436 name: "myapex_nodep",
2437 key: "myapex.key",
2438 native_shared_libs: ["lib_nodep"],
2439 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002440 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002441 }
2442
2443 apex {
2444 name: "myapex_dep",
2445 key: "myapex.key",
2446 native_shared_libs: ["lib_dep"],
2447 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002448 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002449 }
2450
2451 apex {
2452 name: "myapex_provider",
2453 key: "myapex.key",
2454 native_shared_libs: ["libfoo"],
2455 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002456 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002457 }
2458
2459 apex {
2460 name: "myapex_selfcontained",
2461 key: "myapex.key",
2462 native_shared_libs: ["lib_dep", "libfoo"],
2463 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002464 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002465 }
2466
2467 apex_key {
2468 name: "myapex.key",
2469 public_key: "testkey.avbpubkey",
2470 private_key: "testkey.pem",
2471 }
2472
2473 cc_library {
2474 name: "lib_nodep",
2475 srcs: ["mylib.cpp"],
2476 system_shared_libs: [],
2477 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002478 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002479 }
2480
2481 cc_library {
2482 name: "lib_dep",
2483 srcs: ["mylib.cpp"],
2484 shared_libs: ["libfoo"],
2485 system_shared_libs: [],
2486 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002487 apex_available: [
2488 "myapex_dep",
2489 "myapex_provider",
2490 "myapex_selfcontained",
2491 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002492 }
2493
2494 cc_library {
2495 name: "libfoo",
2496 srcs: ["mytest.cpp"],
2497 stubs: {
2498 versions: ["1"],
2499 },
2500 system_shared_libs: [],
2501 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002502 apex_available: [
2503 "myapex_provider",
2504 "myapex_selfcontained",
2505 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002506 }
2507 `)
2508
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002509 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002510 var provideNativeLibs, requireNativeLibs []string
2511
Sundong Ahnabb64432019-10-22 13:58:29 +09002512 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002513 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2514 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002515 ensureListEmpty(t, provideNativeLibs)
2516 ensureListEmpty(t, requireNativeLibs)
2517
Sundong Ahnabb64432019-10-22 13:58:29 +09002518 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_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 ensureListContains(t, requireNativeLibs, "libfoo.so")
2523
Sundong Ahnabb64432019-10-22 13:58:29 +09002524 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_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 ensureListContains(t, provideNativeLibs, "libfoo.so")
2528 ensureListEmpty(t, requireNativeLibs)
2529
Sundong Ahnabb64432019-10-22 13:58:29 +09002530 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_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}
2536
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002537func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002538 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002539 apex {
2540 name: "myapex",
2541 key: "myapex.key",
2542 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002543 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002544 }
2545
2546 apex_key {
2547 name: "myapex.key",
2548 public_key: "testkey.avbpubkey",
2549 private_key: "testkey.pem",
2550 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002551
2552 cc_library {
2553 name: "mylib",
2554 srcs: ["mylib.cpp"],
2555 system_shared_libs: [],
2556 stl: "none",
2557 apex_available: [
2558 "//apex_available:platform",
2559 "myapex",
2560 ],
2561 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002562 `)
2563
Sundong Ahnabb64432019-10-22 13:58:29 +09002564 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002565 apexManifestRule := module.Rule("apexManifestRule")
2566 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2567 apexRule := module.Rule("apexRule")
2568 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002569
2570 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2571 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2572 name := apexBundle.BaseModuleName()
2573 prefix := "TARGET_"
2574 var builder strings.Builder
2575 data.Custom(&builder, name, prefix, "", data)
2576 androidMk := builder.String()
2577 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2578 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002579}
2580
Alex Light0851b882019-02-07 13:20:53 -08002581func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002582 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002583 apex {
2584 name: "myapex",
2585 key: "myapex.key",
2586 native_shared_libs: ["mylib_common"],
2587 }
2588
2589 apex_key {
2590 name: "myapex.key",
2591 public_key: "testkey.avbpubkey",
2592 private_key: "testkey.pem",
2593 }
2594
2595 cc_library {
2596 name: "mylib_common",
2597 srcs: ["mylib.cpp"],
2598 system_shared_libs: [],
2599 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002600 apex_available: [
2601 "//apex_available:platform",
2602 "myapex",
2603 ],
Alex Light0851b882019-02-07 13:20:53 -08002604 }
2605 `)
2606
Sundong Ahnabb64432019-10-22 13:58:29 +09002607 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002608 apexRule := module.Rule("apexRule")
2609 copyCmds := apexRule.Args["copy_commands"]
2610
2611 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2612 t.Log("Apex was a test apex!")
2613 t.Fail()
2614 }
2615 // Ensure that main rule creates an output
2616 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2617
2618 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002619 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002620
2621 // Ensure that both direct and indirect deps are copied into apex
2622 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2623
Colin Cross7113d202019-11-20 16:39:12 -08002624 // Ensure that the platform variant ends with _shared
2625 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002626
2627 if !android.InAnyApex("mylib_common") {
2628 t.Log("Found mylib_common not in any apex!")
2629 t.Fail()
2630 }
2631}
2632
2633func TestTestApex(t *testing.T) {
2634 if android.InAnyApex("mylib_common_test") {
2635 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!")
2636 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002637 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002638 apex_test {
2639 name: "myapex",
2640 key: "myapex.key",
2641 native_shared_libs: ["mylib_common_test"],
2642 }
2643
2644 apex_key {
2645 name: "myapex.key",
2646 public_key: "testkey.avbpubkey",
2647 private_key: "testkey.pem",
2648 }
2649
2650 cc_library {
2651 name: "mylib_common_test",
2652 srcs: ["mylib.cpp"],
2653 system_shared_libs: [],
2654 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002655 // TODO: remove //apex_available:platform
2656 apex_available: [
2657 "//apex_available:platform",
2658 "myapex",
2659 ],
Alex Light0851b882019-02-07 13:20:53 -08002660 }
2661 `)
2662
Sundong Ahnabb64432019-10-22 13:58:29 +09002663 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002664 apexRule := module.Rule("apexRule")
2665 copyCmds := apexRule.Args["copy_commands"]
2666
2667 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2668 t.Log("Apex was not a test apex!")
2669 t.Fail()
2670 }
2671 // Ensure that main rule creates an output
2672 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2673
2674 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002675 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002676
2677 // Ensure that both direct and indirect deps are copied into apex
2678 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2679
Colin Cross7113d202019-11-20 16:39:12 -08002680 // Ensure that the platform variant ends with _shared
2681 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002682}
2683
Alex Light9670d332019-01-29 18:07:33 -08002684func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002685 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002686 apex {
2687 name: "myapex",
2688 key: "myapex.key",
2689 multilib: {
2690 first: {
2691 native_shared_libs: ["mylib_common"],
2692 }
2693 },
2694 target: {
2695 android: {
2696 multilib: {
2697 first: {
2698 native_shared_libs: ["mylib"],
2699 }
2700 }
2701 },
2702 host: {
2703 multilib: {
2704 first: {
2705 native_shared_libs: ["mylib2"],
2706 }
2707 }
2708 }
2709 }
2710 }
2711
2712 apex_key {
2713 name: "myapex.key",
2714 public_key: "testkey.avbpubkey",
2715 private_key: "testkey.pem",
2716 }
2717
2718 cc_library {
2719 name: "mylib",
2720 srcs: ["mylib.cpp"],
2721 system_shared_libs: [],
2722 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002723 // TODO: remove //apex_available:platform
2724 apex_available: [
2725 "//apex_available:platform",
2726 "myapex",
2727 ],
Alex Light9670d332019-01-29 18:07:33 -08002728 }
2729
2730 cc_library {
2731 name: "mylib_common",
2732 srcs: ["mylib.cpp"],
2733 system_shared_libs: [],
2734 stl: "none",
2735 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002736 // TODO: remove //apex_available:platform
2737 apex_available: [
2738 "//apex_available:platform",
2739 "myapex",
2740 ],
Alex Light9670d332019-01-29 18:07:33 -08002741 }
2742
2743 cc_library {
2744 name: "mylib2",
2745 srcs: ["mylib.cpp"],
2746 system_shared_libs: [],
2747 stl: "none",
2748 compile_multilib: "first",
2749 }
2750 `)
2751
Sundong Ahnabb64432019-10-22 13:58:29 +09002752 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002753 copyCmds := apexRule.Args["copy_commands"]
2754
2755 // Ensure that main rule creates an output
2756 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2757
2758 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002759 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2760 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2761 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002762
2763 // Ensure that both direct and indirect deps are copied into apex
2764 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2765 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2766 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2767
Colin Cross7113d202019-11-20 16:39:12 -08002768 // Ensure that the platform variant ends with _shared
2769 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2770 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2771 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002772}
Jiyong Park04480cf2019-02-06 00:16:29 +09002773
2774func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002775 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002776 apex {
2777 name: "myapex",
2778 key: "myapex.key",
2779 binaries: ["myscript"],
2780 }
2781
2782 apex_key {
2783 name: "myapex.key",
2784 public_key: "testkey.avbpubkey",
2785 private_key: "testkey.pem",
2786 }
2787
2788 sh_binary {
2789 name: "myscript",
2790 src: "mylib.cpp",
2791 filename: "myscript.sh",
2792 sub_dir: "script",
2793 }
2794 `)
2795
Sundong Ahnabb64432019-10-22 13:58:29 +09002796 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002797 copyCmds := apexRule.Args["copy_commands"]
2798
2799 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2800}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002801
Jooyung Han91df2082019-11-20 01:49:42 +09002802func TestApexInVariousPartition(t *testing.T) {
2803 testcases := []struct {
2804 propName, parition, flattenedPartition string
2805 }{
2806 {"", "system", "system_ext"},
2807 {"product_specific: true", "product", "product"},
2808 {"soc_specific: true", "vendor", "vendor"},
2809 {"proprietary: true", "vendor", "vendor"},
2810 {"vendor: true", "vendor", "vendor"},
2811 {"system_ext_specific: true", "system_ext", "system_ext"},
2812 }
2813 for _, tc := range testcases {
2814 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2815 ctx, _ := testApex(t, `
2816 apex {
2817 name: "myapex",
2818 key: "myapex.key",
2819 `+tc.propName+`
2820 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002821
Jooyung Han91df2082019-11-20 01:49:42 +09002822 apex_key {
2823 name: "myapex.key",
2824 public_key: "testkey.avbpubkey",
2825 private_key: "testkey.pem",
2826 }
2827 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002828
Jooyung Han91df2082019-11-20 01:49:42 +09002829 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2830 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2831 actual := apex.installDir.String()
2832 if actual != expected {
2833 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2834 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002835
Jooyung Han91df2082019-11-20 01:49:42 +09002836 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2837 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2838 actual = flattened.installDir.String()
2839 if actual != expected {
2840 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2841 }
2842 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002843 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002844}
Jiyong Park67882562019-03-21 01:11:21 +09002845
Jooyung Han54aca7b2019-11-20 02:26:02 +09002846func TestFileContexts(t *testing.T) {
2847 ctx, _ := testApex(t, `
2848 apex {
2849 name: "myapex",
2850 key: "myapex.key",
2851 }
2852
2853 apex_key {
2854 name: "myapex.key",
2855 public_key: "testkey.avbpubkey",
2856 private_key: "testkey.pem",
2857 }
2858 `)
2859 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2860 apexRule := module.Rule("apexRule")
2861 actual := apexRule.Args["file_contexts"]
2862 expected := "system/sepolicy/apex/myapex-file_contexts"
2863 if actual != expected {
2864 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2865 }
2866
2867 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2868 apex {
2869 name: "myapex",
2870 key: "myapex.key",
2871 file_contexts: "my_own_file_contexts",
2872 }
2873
2874 apex_key {
2875 name: "myapex.key",
2876 public_key: "testkey.avbpubkey",
2877 private_key: "testkey.pem",
2878 }
2879 `, withFiles(map[string][]byte{
2880 "my_own_file_contexts": nil,
2881 }))
2882
2883 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2884 apex {
2885 name: "myapex",
2886 key: "myapex.key",
2887 product_specific: true,
2888 file_contexts: "product_specific_file_contexts",
2889 }
2890
2891 apex_key {
2892 name: "myapex.key",
2893 public_key: "testkey.avbpubkey",
2894 private_key: "testkey.pem",
2895 }
2896 `)
2897
2898 ctx, _ = testApex(t, `
2899 apex {
2900 name: "myapex",
2901 key: "myapex.key",
2902 product_specific: true,
2903 file_contexts: "product_specific_file_contexts",
2904 }
2905
2906 apex_key {
2907 name: "myapex.key",
2908 public_key: "testkey.avbpubkey",
2909 private_key: "testkey.pem",
2910 }
2911 `, withFiles(map[string][]byte{
2912 "product_specific_file_contexts": nil,
2913 }))
2914 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2915 apexRule = module.Rule("apexRule")
2916 actual = apexRule.Args["file_contexts"]
2917 expected = "product_specific_file_contexts"
2918 if actual != expected {
2919 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2920 }
2921
2922 ctx, _ = testApex(t, `
2923 apex {
2924 name: "myapex",
2925 key: "myapex.key",
2926 product_specific: true,
2927 file_contexts: ":my-file-contexts",
2928 }
2929
2930 apex_key {
2931 name: "myapex.key",
2932 public_key: "testkey.avbpubkey",
2933 private_key: "testkey.pem",
2934 }
2935
2936 filegroup {
2937 name: "my-file-contexts",
2938 srcs: ["product_specific_file_contexts"],
2939 }
2940 `, withFiles(map[string][]byte{
2941 "product_specific_file_contexts": nil,
2942 }))
2943 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2944 apexRule = module.Rule("apexRule")
2945 actual = apexRule.Args["file_contexts"]
2946 expected = "product_specific_file_contexts"
2947 if actual != expected {
2948 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2949 }
2950}
2951
Jiyong Park67882562019-03-21 01:11:21 +09002952func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002953 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002954 apex_key {
2955 name: "myapex.key",
2956 public_key: ":my.avbpubkey",
2957 private_key: ":my.pem",
2958 product_specific: true,
2959 }
2960
2961 filegroup {
2962 name: "my.avbpubkey",
2963 srcs: ["testkey2.avbpubkey"],
2964 }
2965
2966 filegroup {
2967 name: "my.pem",
2968 srcs: ["testkey2.pem"],
2969 }
2970 `)
2971
2972 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2973 expected_pubkey := "testkey2.avbpubkey"
2974 actual_pubkey := apex_key.public_key_file.String()
2975 if actual_pubkey != expected_pubkey {
2976 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2977 }
2978 expected_privkey := "testkey2.pem"
2979 actual_privkey := apex_key.private_key_file.String()
2980 if actual_privkey != expected_privkey {
2981 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2982 }
2983}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002984
2985func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002986 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002987 prebuilt_apex {
2988 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002989 arch: {
2990 arm64: {
2991 src: "myapex-arm64.apex",
2992 },
2993 arm: {
2994 src: "myapex-arm.apex",
2995 },
2996 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002997 }
2998 `)
2999
3000 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3001
Jiyong Parkc95714e2019-03-29 14:23:10 +09003002 expectedInput := "myapex-arm64.apex"
3003 if prebuilt.inputApex.String() != expectedInput {
3004 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
3005 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003006}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003007
3008func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003009 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003010 prebuilt_apex {
3011 name: "myapex",
3012 src: "myapex-arm.apex",
3013 filename: "notmyapex.apex",
3014 }
3015 `)
3016
3017 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3018
3019 expected := "notmyapex.apex"
3020 if p.installFilename != expected {
3021 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
3022 }
3023}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003024
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003025func TestPrebuiltOverrides(t *testing.T) {
3026 ctx, config := testApex(t, `
3027 prebuilt_apex {
3028 name: "myapex.prebuilt",
3029 src: "myapex-arm.apex",
3030 overrides: [
3031 "myapex",
3032 ],
3033 }
3034 `)
3035
3036 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3037
3038 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003039 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003040 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003041 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003042 }
3043}
3044
Roland Levillain630846d2019-06-26 12:48:34 +01003045func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003046 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003047 apex_test {
3048 name: "myapex",
3049 key: "myapex.key",
3050 tests: [
3051 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003052 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003053 ],
3054 }
3055
3056 apex_key {
3057 name: "myapex.key",
3058 public_key: "testkey.avbpubkey",
3059 private_key: "testkey.pem",
3060 }
3061
3062 cc_test {
3063 name: "mytest",
3064 gtest: false,
3065 srcs: ["mytest.cpp"],
3066 relative_install_path: "test",
3067 system_shared_libs: [],
3068 static_executable: true,
3069 stl: "none",
3070 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003071
3072 cc_test {
3073 name: "mytests",
3074 gtest: false,
3075 srcs: [
3076 "mytest1.cpp",
3077 "mytest2.cpp",
3078 "mytest3.cpp",
3079 ],
3080 test_per_src: true,
3081 relative_install_path: "test",
3082 system_shared_libs: [],
3083 static_executable: true,
3084 stl: "none",
3085 }
Roland Levillain630846d2019-06-26 12:48:34 +01003086 `)
3087
Sundong Ahnabb64432019-10-22 13:58:29 +09003088 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003089 copyCmds := apexRule.Args["copy_commands"]
3090
3091 // Ensure that test dep is copied into apex.
3092 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003093
3094 // Ensure that test deps built with `test_per_src` are copied into apex.
3095 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3096 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3097 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003098
3099 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003100 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003101 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3102 name := apexBundle.BaseModuleName()
3103 prefix := "TARGET_"
3104 var builder strings.Builder
3105 data.Custom(&builder, name, prefix, "", data)
3106 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003107 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3108 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3109 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3110 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003111 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003112 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003113 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003114}
3115
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003116func TestInstallExtraFlattenedApexes(t *testing.T) {
3117 ctx, config := testApex(t, `
3118 apex {
3119 name: "myapex",
3120 key: "myapex.key",
3121 }
3122 apex_key {
3123 name: "myapex.key",
3124 public_key: "testkey.avbpubkey",
3125 private_key: "testkey.pem",
3126 }
3127 `, func(fs map[string][]byte, config android.Config) {
3128 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3129 })
3130 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003131 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003132 mk := android.AndroidMkDataForTest(t, config, "", ab)
3133 var builder strings.Builder
3134 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3135 androidMk := builder.String()
3136 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3137}
3138
Jooyung Han5c998b92019-06-27 11:30:33 +09003139func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003140 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003141 apex {
3142 name: "myapex",
3143 key: "myapex.key",
3144 native_shared_libs: ["mylib"],
3145 uses: ["commonapex"],
3146 }
3147
3148 apex {
3149 name: "commonapex",
3150 key: "myapex.key",
3151 native_shared_libs: ["libcommon"],
3152 provide_cpp_shared_libs: true,
3153 }
3154
3155 apex_key {
3156 name: "myapex.key",
3157 public_key: "testkey.avbpubkey",
3158 private_key: "testkey.pem",
3159 }
3160
3161 cc_library {
3162 name: "mylib",
3163 srcs: ["mylib.cpp"],
3164 shared_libs: ["libcommon"],
3165 system_shared_libs: [],
3166 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003167 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003168 }
3169
3170 cc_library {
3171 name: "libcommon",
3172 srcs: ["mylib_common.cpp"],
3173 system_shared_libs: [],
3174 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003175 // TODO: remove //apex_available:platform
3176 apex_available: [
3177 "//apex_available:platform",
3178 "commonapex",
3179 "myapex",
3180 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003181 }
3182 `)
3183
Sundong Ahnabb64432019-10-22 13:58:29 +09003184 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003185 apexRule1 := module1.Rule("apexRule")
3186 copyCmds1 := apexRule1.Args["copy_commands"]
3187
Sundong Ahnabb64432019-10-22 13:58:29 +09003188 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003189 apexRule2 := module2.Rule("apexRule")
3190 copyCmds2 := apexRule2.Args["copy_commands"]
3191
Colin Cross7113d202019-11-20 16:39:12 -08003192 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3193 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003194 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3195 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3196 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3197}
3198
3199func TestApexUsesFailsIfNotProvided(t *testing.T) {
3200 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3201 apex {
3202 name: "myapex",
3203 key: "myapex.key",
3204 uses: ["commonapex"],
3205 }
3206
3207 apex {
3208 name: "commonapex",
3209 key: "myapex.key",
3210 }
3211
3212 apex_key {
3213 name: "myapex.key",
3214 public_key: "testkey.avbpubkey",
3215 private_key: "testkey.pem",
3216 }
3217 `)
3218 testApexError(t, `uses: "commonapex" is not a provider`, `
3219 apex {
3220 name: "myapex",
3221 key: "myapex.key",
3222 uses: ["commonapex"],
3223 }
3224
3225 cc_library {
3226 name: "commonapex",
3227 system_shared_libs: [],
3228 stl: "none",
3229 }
3230
3231 apex_key {
3232 name: "myapex.key",
3233 public_key: "testkey.avbpubkey",
3234 private_key: "testkey.pem",
3235 }
3236 `)
3237}
3238
3239func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3240 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3241 apex {
3242 name: "myapex",
3243 key: "myapex.key",
3244 use_vendor: true,
3245 uses: ["commonapex"],
3246 }
3247
3248 apex {
3249 name: "commonapex",
3250 key: "myapex.key",
3251 provide_cpp_shared_libs: true,
3252 }
3253
3254 apex_key {
3255 name: "myapex.key",
3256 public_key: "testkey.avbpubkey",
3257 private_key: "testkey.pem",
3258 }
Jooyung Handc782442019-11-01 03:14:38 +09003259 `, func(fs map[string][]byte, config android.Config) {
3260 setUseVendorWhitelistForTest(config, []string{"myapex"})
3261 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003262}
3263
Jooyung Hand48f3c32019-08-23 11:18:57 +09003264func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3265 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3266 apex {
3267 name: "myapex",
3268 key: "myapex.key",
3269 native_shared_libs: ["libfoo"],
3270 }
3271
3272 apex_key {
3273 name: "myapex.key",
3274 public_key: "testkey.avbpubkey",
3275 private_key: "testkey.pem",
3276 }
3277
3278 cc_library {
3279 name: "libfoo",
3280 stl: "none",
3281 system_shared_libs: [],
3282 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003283 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003284 }
3285 `)
3286 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3287 apex {
3288 name: "myapex",
3289 key: "myapex.key",
3290 java_libs: ["myjar"],
3291 }
3292
3293 apex_key {
3294 name: "myapex.key",
3295 public_key: "testkey.avbpubkey",
3296 private_key: "testkey.pem",
3297 }
3298
3299 java_library {
3300 name: "myjar",
3301 srcs: ["foo/bar/MyClass.java"],
3302 sdk_version: "none",
3303 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003304 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003305 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003306 }
3307 `)
3308}
3309
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003310func TestApexWithApps(t *testing.T) {
3311 ctx, _ := testApex(t, `
3312 apex {
3313 name: "myapex",
3314 key: "myapex.key",
3315 apps: [
3316 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003317 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003318 ],
3319 }
3320
3321 apex_key {
3322 name: "myapex.key",
3323 public_key: "testkey.avbpubkey",
3324 private_key: "testkey.pem",
3325 }
3326
3327 android_app {
3328 name: "AppFoo",
3329 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003330 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003331 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003332 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08003333 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003334 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003335 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003336
3337 android_app {
3338 name: "AppFooPriv",
3339 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003340 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09003341 system_modules: "none",
3342 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08003343 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003344 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003345 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003346
3347 cc_library_shared {
3348 name: "libjni",
3349 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003350 shared_libs: ["libfoo"],
3351 stl: "none",
3352 system_shared_libs: [],
3353 apex_available: [ "myapex" ],
3354 sdk_version: "current",
3355 }
3356
3357 cc_library_shared {
3358 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09003359 stl: "none",
3360 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003361 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08003362 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003363 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003364 `)
3365
Sundong Ahnabb64432019-10-22 13:58:29 +09003366 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003367 apexRule := module.Rule("apexRule")
3368 copyCmds := apexRule.Args["copy_commands"]
3369
3370 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003371 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003372
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003373 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3374 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003375 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003376 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003377 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003378 // JNI libraries including transitive deps are
3379 for _, jni := range []string{"libjni", "libfoo"} {
3380 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
3381 // ... embedded inside APK (jnilibs.zip)
3382 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3383 // ... and not directly inside the APEX
3384 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3385 }
Dario Frenicde2a032019-10-27 00:29:22 +01003386}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003387
Dario Frenicde2a032019-10-27 00:29:22 +01003388func TestApexWithAppImports(t *testing.T) {
3389 ctx, _ := testApex(t, `
3390 apex {
3391 name: "myapex",
3392 key: "myapex.key",
3393 apps: [
3394 "AppFooPrebuilt",
3395 "AppFooPrivPrebuilt",
3396 ],
3397 }
3398
3399 apex_key {
3400 name: "myapex.key",
3401 public_key: "testkey.avbpubkey",
3402 private_key: "testkey.pem",
3403 }
3404
3405 android_app_import {
3406 name: "AppFooPrebuilt",
3407 apk: "PrebuiltAppFoo.apk",
3408 presigned: true,
3409 dex_preopt: {
3410 enabled: false,
3411 },
3412 }
3413
3414 android_app_import {
3415 name: "AppFooPrivPrebuilt",
3416 apk: "PrebuiltAppFooPriv.apk",
3417 privileged: true,
3418 presigned: true,
3419 dex_preopt: {
3420 enabled: false,
3421 },
3422 }
3423 `)
3424
Sundong Ahnabb64432019-10-22 13:58:29 +09003425 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003426 apexRule := module.Rule("apexRule")
3427 copyCmds := apexRule.Args["copy_commands"]
3428
3429 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3430 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003431}
3432
Dario Freni6f3937c2019-12-20 22:58:03 +00003433func TestApexWithTestHelperApp(t *testing.T) {
3434 ctx, _ := testApex(t, `
3435 apex {
3436 name: "myapex",
3437 key: "myapex.key",
3438 apps: [
3439 "TesterHelpAppFoo",
3440 ],
3441 }
3442
3443 apex_key {
3444 name: "myapex.key",
3445 public_key: "testkey.avbpubkey",
3446 private_key: "testkey.pem",
3447 }
3448
3449 android_test_helper_app {
3450 name: "TesterHelpAppFoo",
3451 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003452 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003453 }
3454
3455 `)
3456
3457 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3458 apexRule := module.Rule("apexRule")
3459 copyCmds := apexRule.Args["copy_commands"]
3460
3461 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3462}
3463
Jooyung Han18020ea2019-11-13 10:50:48 +09003464func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3465 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09003466 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003467 apex {
3468 name: "myapex",
3469 key: "myapex.key",
3470 native_shared_libs: ["libfoo"],
3471 }
3472
3473 apex_key {
3474 name: "myapex.key",
3475 public_key: "testkey.avbpubkey",
3476 private_key: "testkey.pem",
3477 }
3478
3479 apex {
3480 name: "otherapex",
3481 key: "myapex.key",
3482 native_shared_libs: ["libfoo"],
3483 }
3484
3485 cc_defaults {
3486 name: "libfoo-defaults",
3487 apex_available: ["otherapex"],
3488 }
3489
3490 cc_library {
3491 name: "libfoo",
3492 defaults: ["libfoo-defaults"],
3493 stl: "none",
3494 system_shared_libs: [],
3495 }`)
3496}
3497
Jiyong Park127b40b2019-09-30 16:04:35 +09003498func TestApexAvailable(t *testing.T) {
3499 // libfoo is not available to myapex, but only to otherapex
3500 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3501 apex {
3502 name: "myapex",
3503 key: "myapex.key",
3504 native_shared_libs: ["libfoo"],
3505 }
3506
3507 apex_key {
3508 name: "myapex.key",
3509 public_key: "testkey.avbpubkey",
3510 private_key: "testkey.pem",
3511 }
3512
3513 apex {
3514 name: "otherapex",
3515 key: "otherapex.key",
3516 native_shared_libs: ["libfoo"],
3517 }
3518
3519 apex_key {
3520 name: "otherapex.key",
3521 public_key: "testkey.avbpubkey",
3522 private_key: "testkey.pem",
3523 }
3524
3525 cc_library {
3526 name: "libfoo",
3527 stl: "none",
3528 system_shared_libs: [],
3529 apex_available: ["otherapex"],
3530 }`)
3531
Jooyung Han5e9013b2020-03-10 06:23:13 +09003532 // libbbaz is an indirect dep
3533 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003534 apex {
3535 name: "myapex",
3536 key: "myapex.key",
3537 native_shared_libs: ["libfoo"],
3538 }
3539
3540 apex_key {
3541 name: "myapex.key",
3542 public_key: "testkey.avbpubkey",
3543 private_key: "testkey.pem",
3544 }
3545
Jiyong Park127b40b2019-09-30 16:04:35 +09003546 cc_library {
3547 name: "libfoo",
3548 stl: "none",
3549 shared_libs: ["libbar"],
3550 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003551 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003552 }
3553
3554 cc_library {
3555 name: "libbar",
3556 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09003557 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003558 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003559 apex_available: ["myapex"],
3560 }
3561
3562 cc_library {
3563 name: "libbaz",
3564 stl: "none",
3565 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003566 }`)
3567
3568 testApexError(t, "\"otherapex\" is not a valid module name", `
3569 apex {
3570 name: "myapex",
3571 key: "myapex.key",
3572 native_shared_libs: ["libfoo"],
3573 }
3574
3575 apex_key {
3576 name: "myapex.key",
3577 public_key: "testkey.avbpubkey",
3578 private_key: "testkey.pem",
3579 }
3580
3581 cc_library {
3582 name: "libfoo",
3583 stl: "none",
3584 system_shared_libs: [],
3585 apex_available: ["otherapex"],
3586 }`)
3587
3588 ctx, _ := testApex(t, `
3589 apex {
3590 name: "myapex",
3591 key: "myapex.key",
3592 native_shared_libs: ["libfoo", "libbar"],
3593 }
3594
3595 apex_key {
3596 name: "myapex.key",
3597 public_key: "testkey.avbpubkey",
3598 private_key: "testkey.pem",
3599 }
3600
3601 cc_library {
3602 name: "libfoo",
3603 stl: "none",
3604 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09003605 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003606 apex_available: ["myapex"],
3607 }
3608
3609 cc_library {
3610 name: "libbar",
3611 stl: "none",
3612 system_shared_libs: [],
3613 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09003614 }
3615
3616 cc_library {
3617 name: "libbaz",
3618 stl: "none",
3619 system_shared_libs: [],
3620 stubs: {
3621 versions: ["10", "20", "30"],
3622 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003623 }`)
3624
3625 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003626 // TODO(jiyong) the checks for the platform variant are removed because we now create
3627 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3628 // the platform variants are not used from other platform modules. When that is done,
3629 // these checks will be replaced by expecting a specific error message that will be
3630 // emitted when the platform variant is used.
3631 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3632 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3633 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3634 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003635
3636 ctx, _ = testApex(t, `
3637 apex {
3638 name: "myapex",
3639 key: "myapex.key",
3640 }
3641
3642 apex_key {
3643 name: "myapex.key",
3644 public_key: "testkey.avbpubkey",
3645 private_key: "testkey.pem",
3646 }
3647
3648 cc_library {
3649 name: "libfoo",
3650 stl: "none",
3651 system_shared_libs: [],
3652 apex_available: ["//apex_available:platform"],
3653 }`)
3654
3655 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003656 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3657 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003658
3659 ctx, _ = testApex(t, `
3660 apex {
3661 name: "myapex",
3662 key: "myapex.key",
3663 native_shared_libs: ["libfoo"],
3664 }
3665
3666 apex_key {
3667 name: "myapex.key",
3668 public_key: "testkey.avbpubkey",
3669 private_key: "testkey.pem",
3670 }
3671
3672 cc_library {
3673 name: "libfoo",
3674 stl: "none",
3675 system_shared_libs: [],
3676 apex_available: ["myapex"],
3677 static: {
3678 apex_available: ["//apex_available:platform"],
3679 },
3680 }`)
3681
3682 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003683 // TODO(jiyong) the checks for the platform variant are removed because we now create
3684 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3685 // the platform variants are not used from other platform modules. When that is done,
3686 // these checks will be replaced by expecting a specific error message that will be
3687 // emitted when the platform variant is used.
3688 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3689 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3690 // // but the static variant is available to both myapex and the platform
3691 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3692 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003693}
3694
Jiyong Park5d790c32019-11-15 18:40:32 +09003695func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003696 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003697 apex {
3698 name: "myapex",
3699 key: "myapex.key",
3700 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003701 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003702 }
3703
3704 override_apex {
3705 name: "override_myapex",
3706 base: "myapex",
3707 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003708 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003709 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003710 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09003711 }
3712
3713 apex_key {
3714 name: "myapex.key",
3715 public_key: "testkey.avbpubkey",
3716 private_key: "testkey.pem",
3717 }
3718
3719 android_app {
3720 name: "app",
3721 srcs: ["foo/bar/MyClass.java"],
3722 package_name: "foo",
3723 sdk_version: "none",
3724 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003725 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003726 }
3727
3728 override_android_app {
3729 name: "override_app",
3730 base: "app",
3731 package_name: "bar",
3732 }
Jiyong Park20bacab2020-03-03 11:45:41 +09003733 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003734
Jiyong Park317645e2019-12-05 13:20:58 +09003735 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3736 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3737 if originalVariant.GetOverriddenBy() != "" {
3738 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3739 }
3740 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3741 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3742 }
3743
Jiyong Park5d790c32019-11-15 18:40:32 +09003744 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3745 apexRule := module.Rule("apexRule")
3746 copyCmds := apexRule.Args["copy_commands"]
3747
3748 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3749 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003750
3751 apexBundle := module.Module().(*apexBundle)
3752 name := apexBundle.Name()
3753 if name != "override_myapex" {
3754 t.Errorf("name should be \"override_myapex\", but was %q", name)
3755 }
3756
Baligh Uddin004d7172020-02-19 21:29:28 -08003757 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3758 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3759 }
3760
Jiyong Park20bacab2020-03-03 11:45:41 +09003761 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003762 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09003763
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003764 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3765 var builder strings.Builder
3766 data.Custom(&builder, name, "TARGET_", "", data)
3767 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003768 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003769 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3770 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003771 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003772 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003773 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003774 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3775 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003776}
3777
Jooyung Han214bf372019-11-12 13:03:50 +09003778func TestLegacyAndroid10Support(t *testing.T) {
3779 ctx, _ := testApex(t, `
3780 apex {
3781 name: "myapex",
3782 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003783 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09003784 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09003785 }
3786
3787 apex_key {
3788 name: "myapex.key",
3789 public_key: "testkey.avbpubkey",
3790 private_key: "testkey.pem",
3791 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003792
3793 cc_library {
3794 name: "mylib",
3795 srcs: ["mylib.cpp"],
3796 stl: "libc++",
3797 system_shared_libs: [],
3798 apex_available: [ "myapex" ],
3799 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003800 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003801
3802 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3803 args := module.Rule("apexRule").Args
3804 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003805 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003806
3807 // The copies of the libraries in the apex should have one more dependency than
3808 // the ones outside the apex, namely the unwinder. Ideally we should check
3809 // the dependency names directly here but for some reason the names are blank in
3810 // this test.
3811 for _, lib := range []string{"libc++", "mylib"} {
3812 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3813 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3814 if len(apexImplicits) != len(nonApexImplicits)+1 {
3815 t.Errorf("%q missing unwinder dep", lib)
3816 }
3817 }
Jooyung Han214bf372019-11-12 13:03:50 +09003818}
3819
Jooyung Han58f26ab2019-12-18 15:34:32 +09003820func TestJavaSDKLibrary(t *testing.T) {
3821 ctx, _ := testApex(t, `
3822 apex {
3823 name: "myapex",
3824 key: "myapex.key",
3825 java_libs: ["foo"],
3826 }
3827
3828 apex_key {
3829 name: "myapex.key",
3830 public_key: "testkey.avbpubkey",
3831 private_key: "testkey.pem",
3832 }
3833
3834 java_sdk_library {
3835 name: "foo",
3836 srcs: ["a.java"],
3837 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003838 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003839 }
3840 `, withFiles(map[string][]byte{
3841 "api/current.txt": nil,
3842 "api/removed.txt": nil,
3843 "api/system-current.txt": nil,
3844 "api/system-removed.txt": nil,
3845 "api/test-current.txt": nil,
3846 "api/test-removed.txt": nil,
3847 }))
3848
3849 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003850 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003851 "javalib/foo.jar",
3852 "etc/permissions/foo.xml",
3853 })
3854 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003855 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3856 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003857}
3858
atrost6e126252020-01-27 17:01:16 +00003859func TestCompatConfig(t *testing.T) {
3860 ctx, _ := testApex(t, `
3861 apex {
3862 name: "myapex",
3863 key: "myapex.key",
3864 prebuilts: ["myjar-platform-compat-config"],
3865 java_libs: ["myjar"],
3866 }
3867
3868 apex_key {
3869 name: "myapex.key",
3870 public_key: "testkey.avbpubkey",
3871 private_key: "testkey.pem",
3872 }
3873
3874 platform_compat_config {
3875 name: "myjar-platform-compat-config",
3876 src: ":myjar",
3877 }
3878
3879 java_library {
3880 name: "myjar",
3881 srcs: ["foo/bar/MyClass.java"],
3882 sdk_version: "none",
3883 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003884 apex_available: [ "myapex" ],
3885 }
3886 `)
3887 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3888 "etc/compatconfig/myjar-platform-compat-config.xml",
3889 "javalib/myjar.jar",
3890 })
3891}
3892
Jiyong Park479321d2019-12-16 11:47:12 +09003893func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3894 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3895 apex {
3896 name: "myapex",
3897 key: "myapex.key",
3898 java_libs: ["myjar"],
3899 }
3900
3901 apex_key {
3902 name: "myapex.key",
3903 public_key: "testkey.avbpubkey",
3904 private_key: "testkey.pem",
3905 }
3906
3907 java_library {
3908 name: "myjar",
3909 srcs: ["foo/bar/MyClass.java"],
3910 sdk_version: "none",
3911 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003912 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003913 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003914 }
3915 `)
3916}
3917
Jiyong Park7afd1072019-12-30 16:56:33 +09003918func TestCarryRequiredModuleNames(t *testing.T) {
3919 ctx, config := testApex(t, `
3920 apex {
3921 name: "myapex",
3922 key: "myapex.key",
3923 native_shared_libs: ["mylib"],
3924 }
3925
3926 apex_key {
3927 name: "myapex.key",
3928 public_key: "testkey.avbpubkey",
3929 private_key: "testkey.pem",
3930 }
3931
3932 cc_library {
3933 name: "mylib",
3934 srcs: ["mylib.cpp"],
3935 system_shared_libs: [],
3936 stl: "none",
3937 required: ["a", "b"],
3938 host_required: ["c", "d"],
3939 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003940 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003941 }
3942 `)
3943
3944 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3945 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3946 name := apexBundle.BaseModuleName()
3947 prefix := "TARGET_"
3948 var builder strings.Builder
3949 data.Custom(&builder, name, prefix, "", data)
3950 androidMk := builder.String()
3951 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3952 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3953 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3954}
3955
Jiyong Park7cd10e32020-01-14 09:22:18 +09003956func TestSymlinksFromApexToSystem(t *testing.T) {
3957 bp := `
3958 apex {
3959 name: "myapex",
3960 key: "myapex.key",
3961 native_shared_libs: ["mylib"],
3962 java_libs: ["myjar"],
3963 }
3964
Jiyong Park9d677202020-02-19 16:29:35 +09003965 apex {
3966 name: "myapex.updatable",
3967 key: "myapex.key",
3968 native_shared_libs: ["mylib"],
3969 java_libs: ["myjar"],
3970 updatable: true,
3971 }
3972
Jiyong Park7cd10e32020-01-14 09:22:18 +09003973 apex_key {
3974 name: "myapex.key",
3975 public_key: "testkey.avbpubkey",
3976 private_key: "testkey.pem",
3977 }
3978
3979 cc_library {
3980 name: "mylib",
3981 srcs: ["mylib.cpp"],
3982 shared_libs: ["myotherlib"],
3983 system_shared_libs: [],
3984 stl: "none",
3985 apex_available: [
3986 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003987 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003988 "//apex_available:platform",
3989 ],
3990 }
3991
3992 cc_library {
3993 name: "myotherlib",
3994 srcs: ["mylib.cpp"],
3995 system_shared_libs: [],
3996 stl: "none",
3997 apex_available: [
3998 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003999 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004000 "//apex_available:platform",
4001 ],
4002 }
4003
4004 java_library {
4005 name: "myjar",
4006 srcs: ["foo/bar/MyClass.java"],
4007 sdk_version: "none",
4008 system_modules: "none",
4009 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09004010 apex_available: [
4011 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004012 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004013 "//apex_available:platform",
4014 ],
4015 }
4016
4017 java_library {
4018 name: "myotherjar",
4019 srcs: ["foo/bar/MyClass.java"],
4020 sdk_version: "none",
4021 system_modules: "none",
4022 apex_available: [
4023 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004024 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004025 "//apex_available:platform",
4026 ],
4027 }
4028 `
4029
4030 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
4031 for _, f := range files {
4032 if f.path == file {
4033 if f.isLink {
4034 t.Errorf("%q is not a real file", file)
4035 }
4036 return
4037 }
4038 }
4039 t.Errorf("%q is not found", file)
4040 }
4041
4042 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
4043 for _, f := range files {
4044 if f.path == file {
4045 if !f.isLink {
4046 t.Errorf("%q is not a symlink", file)
4047 }
4048 return
4049 }
4050 }
4051 t.Errorf("%q is not found", file)
4052 }
4053
Jiyong Park9d677202020-02-19 16:29:35 +09004054 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4055 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004056 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004057 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004058 ensureRealfileExists(t, files, "javalib/myjar.jar")
4059 ensureRealfileExists(t, files, "lib64/mylib.so")
4060 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4061
Jiyong Park9d677202020-02-19 16:29:35 +09004062 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4063 ensureRealfileExists(t, files, "javalib/myjar.jar")
4064 ensureRealfileExists(t, files, "lib64/mylib.so")
4065 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4066
4067 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004068 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004069 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004070 ensureRealfileExists(t, files, "javalib/myjar.jar")
4071 ensureRealfileExists(t, files, "lib64/mylib.so")
4072 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004073
4074 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4075 ensureRealfileExists(t, files, "javalib/myjar.jar")
4076 ensureRealfileExists(t, files, "lib64/mylib.so")
4077 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004078}
4079
Jooyung Han643adc42020-02-27 13:50:06 +09004080func TestApexWithJniLibs(t *testing.T) {
4081 ctx, _ := testApex(t, `
4082 apex {
4083 name: "myapex",
4084 key: "myapex.key",
4085 jni_libs: ["mylib"],
4086 }
4087
4088 apex_key {
4089 name: "myapex.key",
4090 public_key: "testkey.avbpubkey",
4091 private_key: "testkey.pem",
4092 }
4093
4094 cc_library {
4095 name: "mylib",
4096 srcs: ["mylib.cpp"],
4097 shared_libs: ["mylib2"],
4098 system_shared_libs: [],
4099 stl: "none",
4100 apex_available: [ "myapex" ],
4101 }
4102
4103 cc_library {
4104 name: "mylib2",
4105 srcs: ["mylib.cpp"],
4106 system_shared_libs: [],
4107 stl: "none",
4108 apex_available: [ "myapex" ],
4109 }
4110 `)
4111
4112 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
4113 // Notice mylib2.so (transitive dep) is not added as a jni_lib
4114 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
4115 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4116 "lib64/mylib.so",
4117 "lib64/mylib2.so",
4118 })
4119}
4120
4121func TestApexWithJniLibs_Errors(t *testing.T) {
4122 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
4123 apex {
4124 name: "myapex",
4125 key: "myapex.key",
4126 jni_libs: ["xxx"],
4127 }
4128
4129 apex_key {
4130 name: "myapex.key",
4131 public_key: "testkey.avbpubkey",
4132 private_key: "testkey.pem",
4133 }
4134
4135 prebuilt_etc {
4136 name: "xxx",
4137 src: "xxx",
4138 }
4139 `, withFiles(map[string][]byte{
4140 "xxx": nil,
4141 }))
4142}
4143
Jiyong Parkbd159612020-02-28 15:22:21 +09004144func TestAppBundle(t *testing.T) {
4145 ctx, _ := testApex(t, `
4146 apex {
4147 name: "myapex",
4148 key: "myapex.key",
4149 apps: ["AppFoo"],
4150 }
4151
4152 apex_key {
4153 name: "myapex.key",
4154 public_key: "testkey.avbpubkey",
4155 private_key: "testkey.pem",
4156 }
4157
4158 android_app {
4159 name: "AppFoo",
4160 srcs: ["foo/bar/MyClass.java"],
4161 sdk_version: "none",
4162 system_modules: "none",
4163 apex_available: [ "myapex" ],
4164 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004165 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09004166
4167 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4168 content := bundleConfigRule.Args["content"]
4169
4170 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004171 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 +09004172}
4173
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004174func TestMain(m *testing.M) {
4175 run := func() int {
4176 setUp()
4177 defer tearDown()
4178
4179 return m.Run()
4180 }
4181
4182 os.Exit(run())
4183}