blob: 5468c77bf201c3d6483e411b685b1d0fb63284e6 [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 Hanbacf34d2020-03-21 13:58:19 +0000877func TestApexDependencyToLLNDK(t *testing.T) {
878 ctx, _ := testApex(t, `
879 apex {
880 name: "myapex",
881 key: "myapex.key",
882 use_vendor: true,
883 native_shared_libs: ["mylib"],
884 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900885
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000886 apex_key {
887 name: "myapex.key",
888 public_key: "testkey.avbpubkey",
889 private_key: "testkey.pem",
890 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900891
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000892 cc_library {
893 name: "mylib",
894 srcs: ["mylib.cpp"],
895 vendor_available: true,
896 shared_libs: ["libbar"],
897 system_shared_libs: [],
898 stl: "none",
899 apex_available: [ "myapex" ],
900 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900901
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000902 cc_library {
903 name: "libbar",
904 srcs: ["mylib.cpp"],
905 system_shared_libs: [],
906 stl: "none",
907 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900908
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000909 llndk_library {
910 name: "libbar",
911 symbol_file: "",
912 }
913 `, func(fs map[string][]byte, config android.Config) {
914 setUseVendorWhitelistForTest(config, []string{"myapex"})
915 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900916
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000917 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
918 copyCmds := apexRule.Args["copy_commands"]
Jooyung Han9c80bae2019-08-20 17:30:57 +0900919
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000920 // Ensure that LLNDK dep is not included
921 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900922
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000923 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
924 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900925
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000926 // Ensure that LLNDK dep is required
927 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
928
Jooyung Han9c80bae2019-08-20 17:30:57 +0900929}
930
Jiyong Park25fc6a92018-11-18 18:02:45 +0900931func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700932 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900933 apex {
934 name: "myapex",
935 key: "myapex.key",
936 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
937 }
938
939 apex_key {
940 name: "myapex.key",
941 public_key: "testkey.avbpubkey",
942 private_key: "testkey.pem",
943 }
944
945 cc_library {
946 name: "mylib",
947 srcs: ["mylib.cpp"],
948 shared_libs: ["libdl#27"],
949 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000950 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900951 }
952
953 cc_library_shared {
954 name: "mylib_shared",
955 srcs: ["mylib.cpp"],
956 shared_libs: ["libdl#27"],
957 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000958 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900959 }
960
961 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900962 name: "libBootstrap",
963 srcs: ["mylib.cpp"],
964 stl: "none",
965 bootstrap: true,
966 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967 `)
968
Sundong Ahnabb64432019-10-22 13:58:29 +0900969 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900970 copyCmds := apexRule.Args["copy_commands"]
971
972 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800973 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900974 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
975 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900976
977 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900978 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900979
Colin Cross7113d202019-11-20 16:39:12 -0800980 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
981 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
982 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900983
984 // For dependency to libc
985 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900986 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900987 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900988 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900989 // ... Cflags from stub is correctly exported to mylib
990 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
991 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
992
993 // For dependency to libm
994 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800995 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900996 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900997 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900998 // ... and is not compiling with the stub
999 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1000 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1001
1002 // For dependency to libdl
1003 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001004 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001005 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001006 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1007 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001008 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001009 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010 // ... Cflags from stub is correctly exported to mylib
1011 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1012 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001013
1014 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001015 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1016 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1017 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1018 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001019}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001020
Jooyung Han03b51852020-02-26 22:45:42 +09001021func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
1022 // there are three links between liba --> libz
1023 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
1024 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
1025 // 3) (platform) -> liba -> libz : this should be non-stub link
1026 ctx, _ := testApex(t, `
1027 apex {
1028 name: "myapex",
1029 key: "myapex.key",
1030 native_shared_libs: ["libx"],
1031 min_sdk_version: "2",
1032 }
1033
1034 apex {
1035 name: "otherapex",
1036 key: "myapex.key",
1037 native_shared_libs: ["liby"],
1038 min_sdk_version: "3",
1039 }
1040
1041 apex_key {
1042 name: "myapex.key",
1043 public_key: "testkey.avbpubkey",
1044 private_key: "testkey.pem",
1045 }
1046
1047 cc_library {
1048 name: "libx",
1049 shared_libs: ["liba"],
1050 system_shared_libs: [],
1051 stl: "none",
1052 apex_available: [ "myapex" ],
1053 }
1054
1055 cc_library {
1056 name: "liby",
1057 shared_libs: ["liba"],
1058 system_shared_libs: [],
1059 stl: "none",
1060 apex_available: [ "otherapex" ],
1061 }
1062
1063 cc_library {
1064 name: "liba",
1065 shared_libs: ["libz"],
1066 system_shared_libs: [],
1067 stl: "none",
1068 apex_available: [
1069 "//apex_available:anyapex",
1070 "//apex_available:platform",
1071 ],
1072 }
1073
1074 cc_library {
1075 name: "libz",
1076 system_shared_libs: [],
1077 stl: "none",
1078 stubs: {
1079 versions: ["1", "3"],
1080 },
1081 }
1082 `, withUnbundledBuild)
1083
1084 expectLink := func(from, from_variant, to, to_variant string) {
1085 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1086 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1087 }
1088 expectNoLink := func(from, from_variant, to, to_variant string) {
1089 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1090 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1091 }
1092 // platform liba is linked to non-stub version
1093 expectLink("liba", "shared", "libz", "shared")
1094 // liba in myapex is linked to #1
1095 expectLink("liba", "shared_myapex", "libz", "shared_1")
1096 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1097 expectNoLink("liba", "shared_myapex", "libz", "shared")
1098 // liba in otherapex is linked to #3
1099 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1100 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1101 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1102}
1103
1104func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1105 ctx, _ := testApex(t, `
1106 apex {
1107 name: "myapex",
1108 key: "myapex.key",
1109 native_shared_libs: ["libx"],
1110 }
1111
1112 apex_key {
1113 name: "myapex.key",
1114 public_key: "testkey.avbpubkey",
1115 private_key: "testkey.pem",
1116 }
1117
1118 cc_library {
1119 name: "libx",
1120 shared_libs: ["libz"],
1121 system_shared_libs: [],
1122 stl: "none",
1123 apex_available: [ "myapex" ],
1124 }
1125
1126 cc_library {
1127 name: "libz",
1128 system_shared_libs: [],
1129 stl: "none",
1130 stubs: {
1131 versions: ["1", "2"],
1132 },
1133 }
1134 `)
1135
1136 expectLink := func(from, from_variant, to, to_variant string) {
1137 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1138 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1139 }
1140 expectNoLink := func(from, from_variant, to, to_variant string) {
1141 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1142 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1143 }
1144 expectLink("libx", "shared_myapex", "libz", "shared_2")
1145 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1146 expectNoLink("libx", "shared_myapex", "libz", "shared")
1147}
1148
1149func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1150 ctx, _ := testApex(t, `
1151 apex {
1152 name: "myapex",
1153 key: "myapex.key",
1154 native_shared_libs: ["libx"],
1155 }
1156
1157 apex_key {
1158 name: "myapex.key",
1159 public_key: "testkey.avbpubkey",
1160 private_key: "testkey.pem",
1161 }
1162
1163 cc_library {
1164 name: "libx",
1165 system_shared_libs: [],
1166 stl: "none",
1167 apex_available: [ "myapex" ],
1168 stubs: {
1169 versions: ["1", "2"],
1170 },
1171 }
1172
1173 cc_library {
1174 name: "libz",
1175 shared_libs: ["libx"],
1176 system_shared_libs: [],
1177 stl: "none",
1178 }
1179 `)
1180
1181 expectLink := func(from, from_variant, to, to_variant string) {
1182 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1183 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1184 }
1185 expectNoLink := func(from, from_variant, to, to_variant string) {
1186 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1187 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1188 }
1189 expectLink("libz", "shared", "libx", "shared_2")
1190 expectNoLink("libz", "shared", "libz", "shared_1")
1191 expectNoLink("libz", "shared", "libz", "shared")
1192}
1193
1194func TestQApexesUseLatestStubsInBundledBuilds(t *testing.T) {
1195 ctx, _ := testApex(t, `
1196 apex {
1197 name: "myapex",
1198 key: "myapex.key",
1199 native_shared_libs: ["libx"],
1200 min_sdk_version: "29",
1201 }
1202
1203 apex_key {
1204 name: "myapex.key",
1205 public_key: "testkey.avbpubkey",
1206 private_key: "testkey.pem",
1207 }
1208
1209 cc_library {
1210 name: "libx",
1211 shared_libs: ["libbar"],
1212 apex_available: [ "myapex" ],
1213 }
1214
1215 cc_library {
1216 name: "libbar",
1217 stubs: {
1218 versions: ["29", "30"],
1219 },
1220 }
1221 `)
1222 expectLink := func(from, from_variant, to, to_variant string) {
1223 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1224 libFlags := ld.Args["libFlags"]
1225 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1226 }
1227 expectLink("libx", "shared_myapex", "libbar", "shared_30")
1228}
1229
1230func TestQTargetApexUseStaticUnwinder(t *testing.T) {
1231 ctx, _ := testApex(t, `
1232 apex {
1233 name: "myapex",
1234 key: "myapex.key",
1235 native_shared_libs: ["libx"],
1236 min_sdk_version: "29",
1237 }
1238
1239 apex_key {
1240 name: "myapex.key",
1241 public_key: "testkey.avbpubkey",
1242 private_key: "testkey.pem",
1243 }
1244
1245 cc_library {
1246 name: "libx",
1247 apex_available: [ "myapex" ],
1248 }
1249
1250 `, withUnbundledBuild)
1251
1252 // ensure apex variant of c++ is linked with static unwinder
1253 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1254 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1255 // note that platform variant is not.
1256 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1257 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Hanbacf34d2020-03-21 13:58:19 +00001258
1259 libFlags := ctx.ModuleForTests("libx", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1260 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_myapex/libc++.so")
1261 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_29/libc.so") // min_sdk_version applied
Jooyung Han03b51852020-02-26 22:45:42 +09001262}
1263
1264func TestInvalidMinSdkVersion(t *testing.T) {
1265 testApexError(t, `"libz" .*: min_sdk_version is set 29.*`, `
1266 apex {
1267 name: "myapex",
1268 key: "myapex.key",
1269 native_shared_libs: ["libx"],
1270 min_sdk_version: "29",
1271 }
1272
1273 apex_key {
1274 name: "myapex.key",
1275 public_key: "testkey.avbpubkey",
1276 private_key: "testkey.pem",
1277 }
1278
1279 cc_library {
1280 name: "libx",
1281 shared_libs: ["libz"],
1282 system_shared_libs: [],
1283 stl: "none",
1284 apex_available: [ "myapex" ],
1285 }
1286
1287 cc_library {
1288 name: "libz",
1289 system_shared_libs: [],
1290 stl: "none",
1291 stubs: {
1292 versions: ["30"],
1293 },
1294 }
1295 `, withUnbundledBuild)
1296
1297 testApexError(t, `"myapex" .*: min_sdk_version: should be .*`, `
1298 apex {
1299 name: "myapex",
1300 key: "myapex.key",
1301 min_sdk_version: "R",
1302 }
1303
1304 apex_key {
1305 name: "myapex.key",
1306 public_key: "testkey.avbpubkey",
1307 private_key: "testkey.pem",
1308 }
1309 `)
1310}
1311
Jiyong Park7c2ee712018-12-07 00:42:25 +09001312func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001313 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001314 apex {
1315 name: "myapex",
1316 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001317 native_shared_libs: ["mylib"],
1318 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001319 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001320 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001321 }
1322
1323 apex_key {
1324 name: "myapex.key",
1325 public_key: "testkey.avbpubkey",
1326 private_key: "testkey.pem",
1327 }
1328
1329 prebuilt_etc {
1330 name: "myetc",
1331 src: "myprebuilt",
1332 sub_dir: "foo/bar",
1333 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001334
1335 cc_library {
1336 name: "mylib",
1337 srcs: ["mylib.cpp"],
1338 relative_install_path: "foo/bar",
1339 system_shared_libs: [],
1340 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001341 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001342 }
1343
1344 cc_binary {
1345 name: "mybin",
1346 srcs: ["mylib.cpp"],
1347 relative_install_path: "foo/bar",
1348 system_shared_libs: [],
1349 static_executable: true,
1350 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001351 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001352 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001353 `)
1354
Sundong Ahnabb64432019-10-22 13:58:29 +09001355 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001356 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1357
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001358 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001359 ensureListContains(t, dirs, "etc")
1360 ensureListContains(t, dirs, "etc/foo")
1361 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001362 ensureListContains(t, dirs, "lib64")
1363 ensureListContains(t, dirs, "lib64/foo")
1364 ensureListContains(t, dirs, "lib64/foo/bar")
1365 ensureListContains(t, dirs, "lib")
1366 ensureListContains(t, dirs, "lib/foo")
1367 ensureListContains(t, dirs, "lib/foo/bar")
1368
Jiyong Parkbd13e442019-03-15 18:10:35 +09001369 ensureListContains(t, dirs, "bin")
1370 ensureListContains(t, dirs, "bin/foo")
1371 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001372}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001373
Jooyung Han35155c42020-02-06 17:33:20 +09001374func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
1375 ctx, _ := testApex(t, `
1376 apex {
1377 name: "myapex",
1378 key: "myapex.key",
1379 multilib: {
1380 both: {
1381 native_shared_libs: ["mylib"],
1382 binaries: ["mybin"],
1383 },
1384 },
1385 compile_multilib: "both",
1386 native_bridge_supported: true,
1387 }
1388
1389 apex_key {
1390 name: "myapex.key",
1391 public_key: "testkey.avbpubkey",
1392 private_key: "testkey.pem",
1393 }
1394
1395 cc_library {
1396 name: "mylib",
1397 relative_install_path: "foo/bar",
1398 system_shared_libs: [],
1399 stl: "none",
1400 apex_available: [ "myapex" ],
1401 native_bridge_supported: true,
1402 }
1403
1404 cc_binary {
1405 name: "mybin",
1406 relative_install_path: "foo/bar",
1407 system_shared_libs: [],
1408 static_executable: true,
1409 stl: "none",
1410 apex_available: [ "myapex" ],
1411 native_bridge_supported: true,
1412 compile_multilib: "both", // default is "first" for binary
1413 multilib: {
1414 lib64: {
1415 suffix: "64",
1416 },
1417 },
1418 }
1419 `, withNativeBridgeEnabled)
1420 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1421 "bin/foo/bar/mybin",
1422 "bin/foo/bar/mybin64",
1423 "bin/arm/foo/bar/mybin",
1424 "bin/arm64/foo/bar/mybin64",
1425 "lib/foo/bar/mylib.so",
1426 "lib/arm/foo/bar/mylib.so",
1427 "lib64/foo/bar/mylib.so",
1428 "lib64/arm64/foo/bar/mylib.so",
1429 })
1430}
1431
Jiyong Parkda6eb592018-12-19 17:12:36 +09001432func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001433 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001434 apex {
1435 name: "myapex",
1436 key: "myapex.key",
1437 native_shared_libs: ["mylib"],
1438 use_vendor: true,
1439 }
1440
1441 apex_key {
1442 name: "myapex.key",
1443 public_key: "testkey.avbpubkey",
1444 private_key: "testkey.pem",
1445 }
1446
1447 cc_library {
1448 name: "mylib",
1449 srcs: ["mylib.cpp"],
1450 shared_libs: ["mylib2"],
1451 system_shared_libs: [],
1452 vendor_available: true,
1453 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001454 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001455 }
1456
1457 cc_library {
1458 name: "mylib2",
1459 srcs: ["mylib.cpp"],
1460 system_shared_libs: [],
1461 vendor_available: true,
1462 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001463 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001464 }
Jooyung Handc782442019-11-01 03:14:38 +09001465 `, func(fs map[string][]byte, config android.Config) {
1466 setUseVendorWhitelistForTest(config, []string{"myapex"})
1467 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001468
1469 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001470 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001471 for _, implicit := range i.Implicits {
1472 inputsList = append(inputsList, implicit.String())
1473 }
1474 }
1475 inputsString := strings.Join(inputsList, " ")
1476
1477 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001478 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1479 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001480
1481 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001482 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1483 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001484}
Jiyong Park16e91a02018-12-20 18:18:08 +09001485
Jooyung Handc782442019-11-01 03:14:38 +09001486func TestUseVendorRestriction(t *testing.T) {
1487 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1488 apex {
1489 name: "myapex",
1490 key: "myapex.key",
1491 use_vendor: true,
1492 }
1493 apex_key {
1494 name: "myapex.key",
1495 public_key: "testkey.avbpubkey",
1496 private_key: "testkey.pem",
1497 }
1498 `, func(fs map[string][]byte, config android.Config) {
1499 setUseVendorWhitelistForTest(config, []string{""})
1500 })
1501 // no error with whitelist
1502 testApex(t, `
1503 apex {
1504 name: "myapex",
1505 key: "myapex.key",
1506 use_vendor: true,
1507 }
1508 apex_key {
1509 name: "myapex.key",
1510 public_key: "testkey.avbpubkey",
1511 private_key: "testkey.pem",
1512 }
1513 `, func(fs map[string][]byte, config android.Config) {
1514 setUseVendorWhitelistForTest(config, []string{"myapex"})
1515 })
1516}
1517
Jooyung Han5c998b92019-06-27 11:30:33 +09001518func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1519 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1520 apex {
1521 name: "myapex",
1522 key: "myapex.key",
1523 native_shared_libs: ["mylib"],
1524 use_vendor: true,
1525 }
1526
1527 apex_key {
1528 name: "myapex.key",
1529 public_key: "testkey.avbpubkey",
1530 private_key: "testkey.pem",
1531 }
1532
1533 cc_library {
1534 name: "mylib",
1535 srcs: ["mylib.cpp"],
1536 system_shared_libs: [],
1537 stl: "none",
1538 }
1539 `)
1540}
1541
Jiyong Park16e91a02018-12-20 18:18:08 +09001542func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001543 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001544 apex {
1545 name: "myapex",
1546 key: "myapex.key",
1547 native_shared_libs: ["mylib"],
1548 }
1549
1550 apex_key {
1551 name: "myapex.key",
1552 public_key: "testkey.avbpubkey",
1553 private_key: "testkey.pem",
1554 }
1555
1556 cc_library {
1557 name: "mylib",
1558 srcs: ["mylib.cpp"],
1559 system_shared_libs: [],
1560 stl: "none",
1561 stubs: {
1562 versions: ["1", "2", "3"],
1563 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001564 apex_available: [
1565 "//apex_available:platform",
1566 "myapex",
1567 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001568 }
1569
1570 cc_binary {
1571 name: "not_in_apex",
1572 srcs: ["mylib.cpp"],
1573 static_libs: ["mylib"],
1574 static_executable: true,
1575 system_shared_libs: [],
1576 stl: "none",
1577 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001578 `)
1579
Colin Cross7113d202019-11-20 16:39:12 -08001580 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001581
1582 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001583 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001584}
Jiyong Park9335a262018-12-24 11:31:58 +09001585
1586func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001587 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001588 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001589 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001590 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001591 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001592 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001593 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001594 }
1595
1596 cc_library {
1597 name: "mylib",
1598 srcs: ["mylib.cpp"],
1599 system_shared_libs: [],
1600 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001601 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001602 }
1603
1604 apex_key {
1605 name: "myapex.key",
1606 public_key: "testkey.avbpubkey",
1607 private_key: "testkey.pem",
1608 }
1609
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001610 android_app_certificate {
1611 name: "myapex.certificate",
1612 certificate: "testkey",
1613 }
1614
1615 android_app_certificate {
1616 name: "myapex.certificate.override",
1617 certificate: "testkey.override",
1618 }
1619
Jiyong Park9335a262018-12-24 11:31:58 +09001620 `)
1621
1622 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001623 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001624
1625 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1626 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1627 "vendor/foo/devkeys/testkey.avbpubkey")
1628 }
1629 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1630 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1631 "vendor/foo/devkeys/testkey.pem")
1632 }
1633
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001634 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001635 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001636 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001637 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001638 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001639 }
1640}
Jiyong Park58e364a2019-01-19 19:24:06 +09001641
Jooyung Hanf121a652019-12-17 14:30:11 +09001642func TestCertificate(t *testing.T) {
1643 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1644 ctx, _ := testApex(t, `
1645 apex {
1646 name: "myapex",
1647 key: "myapex.key",
1648 }
1649 apex_key {
1650 name: "myapex.key",
1651 public_key: "testkey.avbpubkey",
1652 private_key: "testkey.pem",
1653 }`)
1654 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1655 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1656 if actual := rule.Args["certificates"]; actual != expected {
1657 t.Errorf("certificates should be %q, not %q", expected, actual)
1658 }
1659 })
1660 t.Run("override when unspecified", func(t *testing.T) {
1661 ctx, _ := testApex(t, `
1662 apex {
1663 name: "myapex_keytest",
1664 key: "myapex.key",
1665 file_contexts: ":myapex-file_contexts",
1666 }
1667 apex_key {
1668 name: "myapex.key",
1669 public_key: "testkey.avbpubkey",
1670 private_key: "testkey.pem",
1671 }
1672 android_app_certificate {
1673 name: "myapex.certificate.override",
1674 certificate: "testkey.override",
1675 }`)
1676 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1677 expected := "testkey.override.x509.pem testkey.override.pk8"
1678 if actual := rule.Args["certificates"]; actual != expected {
1679 t.Errorf("certificates should be %q, not %q", expected, actual)
1680 }
1681 })
1682 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1683 ctx, _ := testApex(t, `
1684 apex {
1685 name: "myapex",
1686 key: "myapex.key",
1687 certificate: ":myapex.certificate",
1688 }
1689 apex_key {
1690 name: "myapex.key",
1691 public_key: "testkey.avbpubkey",
1692 private_key: "testkey.pem",
1693 }
1694 android_app_certificate {
1695 name: "myapex.certificate",
1696 certificate: "testkey",
1697 }`)
1698 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1699 expected := "testkey.x509.pem testkey.pk8"
1700 if actual := rule.Args["certificates"]; actual != expected {
1701 t.Errorf("certificates should be %q, not %q", expected, actual)
1702 }
1703 })
1704 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1705 ctx, _ := testApex(t, `
1706 apex {
1707 name: "myapex_keytest",
1708 key: "myapex.key",
1709 file_contexts: ":myapex-file_contexts",
1710 certificate: ":myapex.certificate",
1711 }
1712 apex_key {
1713 name: "myapex.key",
1714 public_key: "testkey.avbpubkey",
1715 private_key: "testkey.pem",
1716 }
1717 android_app_certificate {
1718 name: "myapex.certificate.override",
1719 certificate: "testkey.override",
1720 }`)
1721 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1722 expected := "testkey.override.x509.pem testkey.override.pk8"
1723 if actual := rule.Args["certificates"]; actual != expected {
1724 t.Errorf("certificates should be %q, not %q", expected, actual)
1725 }
1726 })
1727 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1728 ctx, _ := testApex(t, `
1729 apex {
1730 name: "myapex",
1731 key: "myapex.key",
1732 certificate: "testkey",
1733 }
1734 apex_key {
1735 name: "myapex.key",
1736 public_key: "testkey.avbpubkey",
1737 private_key: "testkey.pem",
1738 }`)
1739 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1740 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1741 if actual := rule.Args["certificates"]; actual != expected {
1742 t.Errorf("certificates should be %q, not %q", expected, actual)
1743 }
1744 })
1745 t.Run("override when specified as <name>", func(t *testing.T) {
1746 ctx, _ := testApex(t, `
1747 apex {
1748 name: "myapex_keytest",
1749 key: "myapex.key",
1750 file_contexts: ":myapex-file_contexts",
1751 certificate: "testkey",
1752 }
1753 apex_key {
1754 name: "myapex.key",
1755 public_key: "testkey.avbpubkey",
1756 private_key: "testkey.pem",
1757 }
1758 android_app_certificate {
1759 name: "myapex.certificate.override",
1760 certificate: "testkey.override",
1761 }`)
1762 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1763 expected := "testkey.override.x509.pem testkey.override.pk8"
1764 if actual := rule.Args["certificates"]; actual != expected {
1765 t.Errorf("certificates should be %q, not %q", expected, actual)
1766 }
1767 })
1768}
1769
Jiyong Park58e364a2019-01-19 19:24:06 +09001770func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001771 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001772 apex {
1773 name: "myapex",
1774 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001775 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001776 }
1777
1778 apex {
1779 name: "otherapex",
1780 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001781 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09001782 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09001783 }
1784
1785 apex_key {
1786 name: "myapex.key",
1787 public_key: "testkey.avbpubkey",
1788 private_key: "testkey.pem",
1789 }
1790
1791 cc_library {
1792 name: "mylib",
1793 srcs: ["mylib.cpp"],
1794 system_shared_libs: [],
1795 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001796 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001797 "myapex",
1798 "otherapex",
1799 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001800 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09001801 cc_library {
1802 name: "mylib2",
1803 srcs: ["mylib.cpp"],
1804 system_shared_libs: [],
1805 stl: "none",
1806 apex_available: [
1807 "myapex",
1808 "otherapex",
1809 ],
1810 use_apex_name_macro: true,
1811 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001812 `)
1813
Jooyung Hanc87a0592020-03-02 17:44:33 +09001814 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001815 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001816 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001817 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001818
Jooyung Hanccce2f22020-03-07 03:45:53 +09001819 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001820 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1821 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001822 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09001823 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001824
Jooyung Hanccce2f22020-03-07 03:45:53 +09001825 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001826 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1827 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001828 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09001829 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001830
Jooyung Hanc87a0592020-03-02 17:44:33 +09001831 // When cc_library sets use_apex_name_macro: true
1832 // apex variants define additional macro to distinguish which apex variant it is built for
1833
1834 // non-APEX variant does not have __ANDROID_APEX__ defined
1835 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1836 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1837
1838 // APEX variant has __ANDROID_APEX__ defined
1839 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001840 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001841 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1842 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001843
Jooyung Hanc87a0592020-03-02 17:44:33 +09001844 // APEX variant has __ANDROID_APEX__ defined
1845 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001846 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001847 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1848 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001849}
Jiyong Park7e636d02019-01-28 16:16:54 +09001850
1851func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001852 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001853 apex {
1854 name: "myapex",
1855 key: "myapex.key",
1856 native_shared_libs: ["mylib"],
1857 }
1858
1859 apex_key {
1860 name: "myapex.key",
1861 public_key: "testkey.avbpubkey",
1862 private_key: "testkey.pem",
1863 }
1864
1865 cc_library_headers {
1866 name: "mylib_headers",
1867 export_include_dirs: ["my_include"],
1868 system_shared_libs: [],
1869 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001870 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001871 }
1872
1873 cc_library {
1874 name: "mylib",
1875 srcs: ["mylib.cpp"],
1876 system_shared_libs: [],
1877 stl: "none",
1878 header_libs: ["mylib_headers"],
1879 export_header_lib_headers: ["mylib_headers"],
1880 stubs: {
1881 versions: ["1", "2", "3"],
1882 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001883 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001884 }
1885
1886 cc_library {
1887 name: "otherlib",
1888 srcs: ["mylib.cpp"],
1889 system_shared_libs: [],
1890 stl: "none",
1891 shared_libs: ["mylib"],
1892 }
1893 `)
1894
Colin Cross7113d202019-11-20 16:39:12 -08001895 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001896
1897 // Ensure that the include path of the header lib is exported to 'otherlib'
1898 ensureContains(t, cFlags, "-Imy_include")
1899}
Alex Light9670d332019-01-29 18:07:33 -08001900
Jiyong Park7cd10e32020-01-14 09:22:18 +09001901type fileInApex struct {
1902 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001903 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001904 isLink bool
1905}
1906
Jooyung Hana57af4a2020-01-23 05:36:59 +00001907func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001908 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001909 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001910 copyCmds := apexRule.Args["copy_commands"]
1911 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001912 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001913 for _, cmd := range strings.Split(copyCmds, "&&") {
1914 cmd = strings.TrimSpace(cmd)
1915 if cmd == "" {
1916 continue
1917 }
1918 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001919 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001920 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001921 switch terms[0] {
1922 case "mkdir":
1923 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001924 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001925 t.Fatal("copyCmds contains invalid cp command", cmd)
1926 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001927 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001928 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001929 isLink = false
1930 case "ln":
1931 if len(terms) != 3 && len(terms) != 4 {
1932 // ln LINK TARGET or ln -s LINK TARGET
1933 t.Fatal("copyCmds contains invalid ln command", cmd)
1934 }
1935 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001936 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001937 isLink = true
1938 default:
1939 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1940 }
1941 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001942 index := strings.Index(dst, imageApexDir)
1943 if index == -1 {
1944 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1945 }
1946 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001947 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001948 }
1949 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001950 return ret
1951}
1952
Jooyung Hana57af4a2020-01-23 05:36:59 +00001953func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1954 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001955 var failed bool
1956 var surplus []string
1957 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001958 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001959 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001960 for _, expected := range files {
1961 if matched, _ := path.Match(expected, file.path); matched {
1962 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001963 mactchFound = true
1964 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001965 }
1966 }
Jooyung Hane6436d72020-02-27 13:31:56 +09001967 if !mactchFound {
1968 surplus = append(surplus, file.path)
1969 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001970 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001971
Jooyung Han31c470b2019-10-18 16:26:59 +09001972 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001973 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001974 t.Log("surplus files", surplus)
1975 failed = true
1976 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001977
1978 if len(files) > len(filesMatched) {
1979 var missing []string
1980 for _, expected := range files {
1981 if !filesMatched[expected] {
1982 missing = append(missing, expected)
1983 }
1984 }
1985 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001986 t.Log("missing files", missing)
1987 failed = true
1988 }
1989 if failed {
1990 t.Fail()
1991 }
1992}
1993
Jooyung Han344d5432019-08-23 11:17:39 +09001994func TestVndkApexCurrent(t *testing.T) {
1995 ctx, _ := testApex(t, `
1996 apex_vndk {
1997 name: "myapex",
1998 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001999 }
2000
2001 apex_key {
2002 name: "myapex.key",
2003 public_key: "testkey.avbpubkey",
2004 private_key: "testkey.pem",
2005 }
2006
2007 cc_library {
2008 name: "libvndk",
2009 srcs: ["mylib.cpp"],
2010 vendor_available: true,
2011 vndk: {
2012 enabled: true,
2013 },
2014 system_shared_libs: [],
2015 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002016 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002017 }
2018
2019 cc_library {
2020 name: "libvndksp",
2021 srcs: ["mylib.cpp"],
2022 vendor_available: true,
2023 vndk: {
2024 enabled: true,
2025 support_system_process: true,
2026 },
2027 system_shared_libs: [],
2028 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002029 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002030 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002031 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002032
Jooyung Hana57af4a2020-01-23 05:36:59 +00002033 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002034 "lib/libvndk.so",
2035 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002036 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002037 "lib64/libvndk.so",
2038 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002039 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002040 "etc/llndk.libraries.VER.txt",
2041 "etc/vndkcore.libraries.VER.txt",
2042 "etc/vndksp.libraries.VER.txt",
2043 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002044 })
Jooyung Han344d5432019-08-23 11:17:39 +09002045}
2046
2047func TestVndkApexWithPrebuilt(t *testing.T) {
2048 ctx, _ := testApex(t, `
2049 apex_vndk {
2050 name: "myapex",
2051 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002052 }
2053
2054 apex_key {
2055 name: "myapex.key",
2056 public_key: "testkey.avbpubkey",
2057 private_key: "testkey.pem",
2058 }
2059
2060 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002061 name: "libvndk",
2062 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002063 vendor_available: true,
2064 vndk: {
2065 enabled: true,
2066 },
2067 system_shared_libs: [],
2068 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002069 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002070 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002071
2072 cc_prebuilt_library_shared {
2073 name: "libvndk.arm",
2074 srcs: ["libvndk.arm.so"],
2075 vendor_available: true,
2076 vndk: {
2077 enabled: true,
2078 },
2079 enabled: false,
2080 arch: {
2081 arm: {
2082 enabled: true,
2083 },
2084 },
2085 system_shared_libs: [],
2086 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002087 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002088 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002089 `+vndkLibrariesTxtFiles("current"),
2090 withFiles(map[string][]byte{
2091 "libvndk.so": nil,
2092 "libvndk.arm.so": nil,
2093 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002094
Jooyung Hana57af4a2020-01-23 05:36:59 +00002095 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002096 "lib/libvndk.so",
2097 "lib/libvndk.arm.so",
2098 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002099 "lib/libc++.so",
2100 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002101 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002102 })
Jooyung Han344d5432019-08-23 11:17:39 +09002103}
2104
Jooyung Han39edb6c2019-11-06 16:53:07 +09002105func vndkLibrariesTxtFiles(vers ...string) (result string) {
2106 for _, v := range vers {
2107 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002108 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002109 result += `
2110 vndk_libraries_txt {
2111 name: "` + txt + `.libraries.txt",
2112 }
2113 `
2114 }
2115 } else {
2116 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2117 result += `
2118 prebuilt_etc {
2119 name: "` + txt + `.libraries.` + v + `.txt",
2120 src: "dummy.txt",
2121 }
2122 `
2123 }
2124 }
2125 }
2126 return
2127}
2128
Jooyung Han344d5432019-08-23 11:17:39 +09002129func TestVndkApexVersion(t *testing.T) {
2130 ctx, _ := testApex(t, `
2131 apex_vndk {
2132 name: "myapex_v27",
2133 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002134 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002135 vndk_version: "27",
2136 }
2137
2138 apex_key {
2139 name: "myapex.key",
2140 public_key: "testkey.avbpubkey",
2141 private_key: "testkey.pem",
2142 }
2143
Jooyung Han31c470b2019-10-18 16:26:59 +09002144 vndk_prebuilt_shared {
2145 name: "libvndk27",
2146 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002147 vendor_available: true,
2148 vndk: {
2149 enabled: true,
2150 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002151 target_arch: "arm64",
2152 arch: {
2153 arm: {
2154 srcs: ["libvndk27_arm.so"],
2155 },
2156 arm64: {
2157 srcs: ["libvndk27_arm64.so"],
2158 },
2159 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002160 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002161 }
2162
2163 vndk_prebuilt_shared {
2164 name: "libvndk27",
2165 version: "27",
2166 vendor_available: true,
2167 vndk: {
2168 enabled: true,
2169 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002170 target_arch: "x86_64",
2171 arch: {
2172 x86: {
2173 srcs: ["libvndk27_x86.so"],
2174 },
2175 x86_64: {
2176 srcs: ["libvndk27_x86_64.so"],
2177 },
2178 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002179 }
2180 `+vndkLibrariesTxtFiles("27"),
2181 withFiles(map[string][]byte{
2182 "libvndk27_arm.so": nil,
2183 "libvndk27_arm64.so": nil,
2184 "libvndk27_x86.so": nil,
2185 "libvndk27_x86_64.so": nil,
2186 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002187
Jooyung Hana57af4a2020-01-23 05:36:59 +00002188 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002189 "lib/libvndk27_arm.so",
2190 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002191 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002192 })
Jooyung Han344d5432019-08-23 11:17:39 +09002193}
2194
2195func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2196 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2197 apex_vndk {
2198 name: "myapex_v27",
2199 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002200 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002201 vndk_version: "27",
2202 }
2203 apex_vndk {
2204 name: "myapex_v27_other",
2205 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002206 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002207 vndk_version: "27",
2208 }
2209
2210 apex_key {
2211 name: "myapex.key",
2212 public_key: "testkey.avbpubkey",
2213 private_key: "testkey.pem",
2214 }
2215
2216 cc_library {
2217 name: "libvndk",
2218 srcs: ["mylib.cpp"],
2219 vendor_available: true,
2220 vndk: {
2221 enabled: true,
2222 },
2223 system_shared_libs: [],
2224 stl: "none",
2225 }
2226
2227 vndk_prebuilt_shared {
2228 name: "libvndk",
2229 version: "27",
2230 vendor_available: true,
2231 vndk: {
2232 enabled: true,
2233 },
2234 srcs: ["libvndk.so"],
2235 }
2236 `, withFiles(map[string][]byte{
2237 "libvndk.so": nil,
2238 }))
2239}
2240
Jooyung Han90eee022019-10-01 20:02:42 +09002241func TestVndkApexNameRule(t *testing.T) {
2242 ctx, _ := testApex(t, `
2243 apex_vndk {
2244 name: "myapex",
2245 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002246 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002247 }
2248 apex_vndk {
2249 name: "myapex_v28",
2250 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002251 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002252 vndk_version: "28",
2253 }
2254 apex_key {
2255 name: "myapex.key",
2256 public_key: "testkey.avbpubkey",
2257 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002258 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002259
2260 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002261 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002262 actual := proptools.String(bundle.properties.Apex_name)
2263 if !reflect.DeepEqual(actual, expected) {
2264 t.Errorf("Got '%v', expected '%v'", actual, expected)
2265 }
2266 }
2267
2268 assertApexName("com.android.vndk.vVER", "myapex")
2269 assertApexName("com.android.vndk.v28", "myapex_v28")
2270}
2271
Jooyung Han344d5432019-08-23 11:17:39 +09002272func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2273 ctx, _ := testApex(t, `
2274 apex_vndk {
2275 name: "myapex",
2276 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002277 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002278 }
2279
2280 apex_key {
2281 name: "myapex.key",
2282 public_key: "testkey.avbpubkey",
2283 private_key: "testkey.pem",
2284 }
2285
2286 cc_library {
2287 name: "libvndk",
2288 srcs: ["mylib.cpp"],
2289 vendor_available: true,
2290 native_bridge_supported: true,
2291 host_supported: true,
2292 vndk: {
2293 enabled: true,
2294 },
2295 system_shared_libs: [],
2296 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002297 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002298 }
Jooyung Han35155c42020-02-06 17:33:20 +09002299 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09002300
Jooyung Hana57af4a2020-01-23 05:36:59 +00002301 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002302 "lib/libvndk.so",
2303 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002304 "lib/libc++.so",
2305 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002306 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002307 })
Jooyung Han344d5432019-08-23 11:17:39 +09002308}
2309
2310func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2311 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2312 apex_vndk {
2313 name: "myapex",
2314 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002315 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002316 native_bridge_supported: true,
2317 }
2318
2319 apex_key {
2320 name: "myapex.key",
2321 public_key: "testkey.avbpubkey",
2322 private_key: "testkey.pem",
2323 }
2324
2325 cc_library {
2326 name: "libvndk",
2327 srcs: ["mylib.cpp"],
2328 vendor_available: true,
2329 native_bridge_supported: true,
2330 host_supported: true,
2331 vndk: {
2332 enabled: true,
2333 },
2334 system_shared_libs: [],
2335 stl: "none",
2336 }
2337 `)
2338}
2339
Jooyung Han31c470b2019-10-18 16:26:59 +09002340func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002341 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002342 apex_vndk {
2343 name: "myapex_v27",
2344 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002345 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002346 vndk_version: "27",
2347 }
2348
2349 apex_key {
2350 name: "myapex.key",
2351 public_key: "testkey.avbpubkey",
2352 private_key: "testkey.pem",
2353 }
2354
2355 vndk_prebuilt_shared {
2356 name: "libvndk27",
2357 version: "27",
2358 target_arch: "arm",
2359 vendor_available: true,
2360 vndk: {
2361 enabled: true,
2362 },
2363 arch: {
2364 arm: {
2365 srcs: ["libvndk27.so"],
2366 }
2367 },
2368 }
2369
2370 vndk_prebuilt_shared {
2371 name: "libvndk27",
2372 version: "27",
2373 target_arch: "arm",
2374 binder32bit: true,
2375 vendor_available: true,
2376 vndk: {
2377 enabled: true,
2378 },
2379 arch: {
2380 arm: {
2381 srcs: ["libvndk27binder32.so"],
2382 }
2383 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002384 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002385 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002386 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002387 withFiles(map[string][]byte{
2388 "libvndk27.so": nil,
2389 "libvndk27binder32.so": nil,
2390 }),
2391 withBinder32bit,
2392 withTargets(map[android.OsType][]android.Target{
2393 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09002394 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
2395 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09002396 },
2397 }),
2398 )
2399
Jooyung Hana57af4a2020-01-23 05:36:59 +00002400 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002401 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002402 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002403 })
2404}
2405
Jooyung Hane1633032019-08-01 17:41:43 +09002406func TestDependenciesInApexManifest(t *testing.T) {
2407 ctx, _ := testApex(t, `
2408 apex {
2409 name: "myapex_nodep",
2410 key: "myapex.key",
2411 native_shared_libs: ["lib_nodep"],
2412 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002413 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002414 }
2415
2416 apex {
2417 name: "myapex_dep",
2418 key: "myapex.key",
2419 native_shared_libs: ["lib_dep"],
2420 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002421 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002422 }
2423
2424 apex {
2425 name: "myapex_provider",
2426 key: "myapex.key",
2427 native_shared_libs: ["libfoo"],
2428 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002429 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002430 }
2431
2432 apex {
2433 name: "myapex_selfcontained",
2434 key: "myapex.key",
2435 native_shared_libs: ["lib_dep", "libfoo"],
2436 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002437 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002438 }
2439
2440 apex_key {
2441 name: "myapex.key",
2442 public_key: "testkey.avbpubkey",
2443 private_key: "testkey.pem",
2444 }
2445
2446 cc_library {
2447 name: "lib_nodep",
2448 srcs: ["mylib.cpp"],
2449 system_shared_libs: [],
2450 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002451 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002452 }
2453
2454 cc_library {
2455 name: "lib_dep",
2456 srcs: ["mylib.cpp"],
2457 shared_libs: ["libfoo"],
2458 system_shared_libs: [],
2459 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002460 apex_available: [
2461 "myapex_dep",
2462 "myapex_provider",
2463 "myapex_selfcontained",
2464 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002465 }
2466
2467 cc_library {
2468 name: "libfoo",
2469 srcs: ["mytest.cpp"],
2470 stubs: {
2471 versions: ["1"],
2472 },
2473 system_shared_libs: [],
2474 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002475 apex_available: [
2476 "myapex_provider",
2477 "myapex_selfcontained",
2478 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002479 }
2480 `)
2481
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002482 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002483 var provideNativeLibs, requireNativeLibs []string
2484
Sundong Ahnabb64432019-10-22 13:58:29 +09002485 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002486 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2487 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002488 ensureListEmpty(t, provideNativeLibs)
2489 ensureListEmpty(t, requireNativeLibs)
2490
Sundong Ahnabb64432019-10-22 13:58:29 +09002491 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002492 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2493 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002494 ensureListEmpty(t, provideNativeLibs)
2495 ensureListContains(t, requireNativeLibs, "libfoo.so")
2496
Sundong Ahnabb64432019-10-22 13:58:29 +09002497 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002498 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2499 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002500 ensureListContains(t, provideNativeLibs, "libfoo.so")
2501 ensureListEmpty(t, requireNativeLibs)
2502
Sundong Ahnabb64432019-10-22 13:58:29 +09002503 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002504 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2505 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002506 ensureListContains(t, provideNativeLibs, "libfoo.so")
2507 ensureListEmpty(t, requireNativeLibs)
2508}
2509
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002510func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002511 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002512 apex {
2513 name: "myapex",
2514 key: "myapex.key",
2515 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002516 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002517 }
2518
2519 apex_key {
2520 name: "myapex.key",
2521 public_key: "testkey.avbpubkey",
2522 private_key: "testkey.pem",
2523 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002524
2525 cc_library {
2526 name: "mylib",
2527 srcs: ["mylib.cpp"],
2528 system_shared_libs: [],
2529 stl: "none",
2530 apex_available: [
2531 "//apex_available:platform",
2532 "myapex",
2533 ],
2534 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002535 `)
2536
Sundong Ahnabb64432019-10-22 13:58:29 +09002537 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002538 apexManifestRule := module.Rule("apexManifestRule")
2539 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2540 apexRule := module.Rule("apexRule")
2541 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002542
2543 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2544 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2545 name := apexBundle.BaseModuleName()
2546 prefix := "TARGET_"
2547 var builder strings.Builder
2548 data.Custom(&builder, name, prefix, "", data)
2549 androidMk := builder.String()
2550 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2551 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002552}
2553
Alex Light0851b882019-02-07 13:20:53 -08002554func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002555 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002556 apex {
2557 name: "myapex",
2558 key: "myapex.key",
2559 native_shared_libs: ["mylib_common"],
2560 }
2561
2562 apex_key {
2563 name: "myapex.key",
2564 public_key: "testkey.avbpubkey",
2565 private_key: "testkey.pem",
2566 }
2567
2568 cc_library {
2569 name: "mylib_common",
2570 srcs: ["mylib.cpp"],
2571 system_shared_libs: [],
2572 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002573 apex_available: [
2574 "//apex_available:platform",
2575 "myapex",
2576 ],
Alex Light0851b882019-02-07 13:20:53 -08002577 }
2578 `)
2579
Sundong Ahnabb64432019-10-22 13:58:29 +09002580 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002581 apexRule := module.Rule("apexRule")
2582 copyCmds := apexRule.Args["copy_commands"]
2583
2584 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2585 t.Log("Apex was a test apex!")
2586 t.Fail()
2587 }
2588 // Ensure that main rule creates an output
2589 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2590
2591 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002592 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002593
2594 // Ensure that both direct and indirect deps are copied into apex
2595 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2596
Colin Cross7113d202019-11-20 16:39:12 -08002597 // Ensure that the platform variant ends with _shared
2598 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002599
2600 if !android.InAnyApex("mylib_common") {
2601 t.Log("Found mylib_common not in any apex!")
2602 t.Fail()
2603 }
2604}
2605
2606func TestTestApex(t *testing.T) {
2607 if android.InAnyApex("mylib_common_test") {
2608 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!")
2609 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002610 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002611 apex_test {
2612 name: "myapex",
2613 key: "myapex.key",
2614 native_shared_libs: ["mylib_common_test"],
2615 }
2616
2617 apex_key {
2618 name: "myapex.key",
2619 public_key: "testkey.avbpubkey",
2620 private_key: "testkey.pem",
2621 }
2622
2623 cc_library {
2624 name: "mylib_common_test",
2625 srcs: ["mylib.cpp"],
2626 system_shared_libs: [],
2627 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002628 // TODO: remove //apex_available:platform
2629 apex_available: [
2630 "//apex_available:platform",
2631 "myapex",
2632 ],
Alex Light0851b882019-02-07 13:20:53 -08002633 }
2634 `)
2635
Sundong Ahnabb64432019-10-22 13:58:29 +09002636 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002637 apexRule := module.Rule("apexRule")
2638 copyCmds := apexRule.Args["copy_commands"]
2639
2640 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2641 t.Log("Apex was not a test apex!")
2642 t.Fail()
2643 }
2644 // Ensure that main rule creates an output
2645 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2646
2647 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002648 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002649
2650 // Ensure that both direct and indirect deps are copied into apex
2651 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2652
Colin Cross7113d202019-11-20 16:39:12 -08002653 // Ensure that the platform variant ends with _shared
2654 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002655}
2656
Alex Light9670d332019-01-29 18:07:33 -08002657func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002658 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002659 apex {
2660 name: "myapex",
2661 key: "myapex.key",
2662 multilib: {
2663 first: {
2664 native_shared_libs: ["mylib_common"],
2665 }
2666 },
2667 target: {
2668 android: {
2669 multilib: {
2670 first: {
2671 native_shared_libs: ["mylib"],
2672 }
2673 }
2674 },
2675 host: {
2676 multilib: {
2677 first: {
2678 native_shared_libs: ["mylib2"],
2679 }
2680 }
2681 }
2682 }
2683 }
2684
2685 apex_key {
2686 name: "myapex.key",
2687 public_key: "testkey.avbpubkey",
2688 private_key: "testkey.pem",
2689 }
2690
2691 cc_library {
2692 name: "mylib",
2693 srcs: ["mylib.cpp"],
2694 system_shared_libs: [],
2695 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002696 // TODO: remove //apex_available:platform
2697 apex_available: [
2698 "//apex_available:platform",
2699 "myapex",
2700 ],
Alex Light9670d332019-01-29 18:07:33 -08002701 }
2702
2703 cc_library {
2704 name: "mylib_common",
2705 srcs: ["mylib.cpp"],
2706 system_shared_libs: [],
2707 stl: "none",
2708 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002709 // TODO: remove //apex_available:platform
2710 apex_available: [
2711 "//apex_available:platform",
2712 "myapex",
2713 ],
Alex Light9670d332019-01-29 18:07:33 -08002714 }
2715
2716 cc_library {
2717 name: "mylib2",
2718 srcs: ["mylib.cpp"],
2719 system_shared_libs: [],
2720 stl: "none",
2721 compile_multilib: "first",
2722 }
2723 `)
2724
Sundong Ahnabb64432019-10-22 13:58:29 +09002725 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002726 copyCmds := apexRule.Args["copy_commands"]
2727
2728 // Ensure that main rule creates an output
2729 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2730
2731 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002732 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2733 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2734 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002735
2736 // Ensure that both direct and indirect deps are copied into apex
2737 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2738 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2739 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2740
Colin Cross7113d202019-11-20 16:39:12 -08002741 // Ensure that the platform variant ends with _shared
2742 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2743 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2744 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002745}
Jiyong Park04480cf2019-02-06 00:16:29 +09002746
2747func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002748 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002749 apex {
2750 name: "myapex",
2751 key: "myapex.key",
2752 binaries: ["myscript"],
2753 }
2754
2755 apex_key {
2756 name: "myapex.key",
2757 public_key: "testkey.avbpubkey",
2758 private_key: "testkey.pem",
2759 }
2760
2761 sh_binary {
2762 name: "myscript",
2763 src: "mylib.cpp",
2764 filename: "myscript.sh",
2765 sub_dir: "script",
2766 }
2767 `)
2768
Sundong Ahnabb64432019-10-22 13:58:29 +09002769 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002770 copyCmds := apexRule.Args["copy_commands"]
2771
2772 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2773}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002774
Jooyung Han91df2082019-11-20 01:49:42 +09002775func TestApexInVariousPartition(t *testing.T) {
2776 testcases := []struct {
2777 propName, parition, flattenedPartition string
2778 }{
2779 {"", "system", "system_ext"},
2780 {"product_specific: true", "product", "product"},
2781 {"soc_specific: true", "vendor", "vendor"},
2782 {"proprietary: true", "vendor", "vendor"},
2783 {"vendor: true", "vendor", "vendor"},
2784 {"system_ext_specific: true", "system_ext", "system_ext"},
2785 }
2786 for _, tc := range testcases {
2787 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2788 ctx, _ := testApex(t, `
2789 apex {
2790 name: "myapex",
2791 key: "myapex.key",
2792 `+tc.propName+`
2793 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002794
Jooyung Han91df2082019-11-20 01:49:42 +09002795 apex_key {
2796 name: "myapex.key",
2797 public_key: "testkey.avbpubkey",
2798 private_key: "testkey.pem",
2799 }
2800 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002801
Jooyung Han91df2082019-11-20 01:49:42 +09002802 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2803 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2804 actual := apex.installDir.String()
2805 if actual != expected {
2806 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2807 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002808
Jooyung Han91df2082019-11-20 01:49:42 +09002809 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2810 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2811 actual = flattened.installDir.String()
2812 if actual != expected {
2813 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2814 }
2815 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002816 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002817}
Jiyong Park67882562019-03-21 01:11:21 +09002818
Jooyung Han54aca7b2019-11-20 02:26:02 +09002819func TestFileContexts(t *testing.T) {
2820 ctx, _ := testApex(t, `
2821 apex {
2822 name: "myapex",
2823 key: "myapex.key",
2824 }
2825
2826 apex_key {
2827 name: "myapex.key",
2828 public_key: "testkey.avbpubkey",
2829 private_key: "testkey.pem",
2830 }
2831 `)
2832 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2833 apexRule := module.Rule("apexRule")
2834 actual := apexRule.Args["file_contexts"]
2835 expected := "system/sepolicy/apex/myapex-file_contexts"
2836 if actual != expected {
2837 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2838 }
2839
2840 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2841 apex {
2842 name: "myapex",
2843 key: "myapex.key",
2844 file_contexts: "my_own_file_contexts",
2845 }
2846
2847 apex_key {
2848 name: "myapex.key",
2849 public_key: "testkey.avbpubkey",
2850 private_key: "testkey.pem",
2851 }
2852 `, withFiles(map[string][]byte{
2853 "my_own_file_contexts": nil,
2854 }))
2855
2856 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2857 apex {
2858 name: "myapex",
2859 key: "myapex.key",
2860 product_specific: true,
2861 file_contexts: "product_specific_file_contexts",
2862 }
2863
2864 apex_key {
2865 name: "myapex.key",
2866 public_key: "testkey.avbpubkey",
2867 private_key: "testkey.pem",
2868 }
2869 `)
2870
2871 ctx, _ = testApex(t, `
2872 apex {
2873 name: "myapex",
2874 key: "myapex.key",
2875 product_specific: true,
2876 file_contexts: "product_specific_file_contexts",
2877 }
2878
2879 apex_key {
2880 name: "myapex.key",
2881 public_key: "testkey.avbpubkey",
2882 private_key: "testkey.pem",
2883 }
2884 `, withFiles(map[string][]byte{
2885 "product_specific_file_contexts": nil,
2886 }))
2887 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2888 apexRule = module.Rule("apexRule")
2889 actual = apexRule.Args["file_contexts"]
2890 expected = "product_specific_file_contexts"
2891 if actual != expected {
2892 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2893 }
2894
2895 ctx, _ = testApex(t, `
2896 apex {
2897 name: "myapex",
2898 key: "myapex.key",
2899 product_specific: true,
2900 file_contexts: ":my-file-contexts",
2901 }
2902
2903 apex_key {
2904 name: "myapex.key",
2905 public_key: "testkey.avbpubkey",
2906 private_key: "testkey.pem",
2907 }
2908
2909 filegroup {
2910 name: "my-file-contexts",
2911 srcs: ["product_specific_file_contexts"],
2912 }
2913 `, withFiles(map[string][]byte{
2914 "product_specific_file_contexts": nil,
2915 }))
2916 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2917 apexRule = module.Rule("apexRule")
2918 actual = apexRule.Args["file_contexts"]
2919 expected = "product_specific_file_contexts"
2920 if actual != expected {
2921 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2922 }
2923}
2924
Jiyong Park67882562019-03-21 01:11:21 +09002925func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002926 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002927 apex_key {
2928 name: "myapex.key",
2929 public_key: ":my.avbpubkey",
2930 private_key: ":my.pem",
2931 product_specific: true,
2932 }
2933
2934 filegroup {
2935 name: "my.avbpubkey",
2936 srcs: ["testkey2.avbpubkey"],
2937 }
2938
2939 filegroup {
2940 name: "my.pem",
2941 srcs: ["testkey2.pem"],
2942 }
2943 `)
2944
2945 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2946 expected_pubkey := "testkey2.avbpubkey"
2947 actual_pubkey := apex_key.public_key_file.String()
2948 if actual_pubkey != expected_pubkey {
2949 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2950 }
2951 expected_privkey := "testkey2.pem"
2952 actual_privkey := apex_key.private_key_file.String()
2953 if actual_privkey != expected_privkey {
2954 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2955 }
2956}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002957
2958func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002959 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002960 prebuilt_apex {
2961 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002962 arch: {
2963 arm64: {
2964 src: "myapex-arm64.apex",
2965 },
2966 arm: {
2967 src: "myapex-arm.apex",
2968 },
2969 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002970 }
2971 `)
2972
2973 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2974
Jiyong Parkc95714e2019-03-29 14:23:10 +09002975 expectedInput := "myapex-arm64.apex"
2976 if prebuilt.inputApex.String() != expectedInput {
2977 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2978 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002979}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002980
2981func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002982 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002983 prebuilt_apex {
2984 name: "myapex",
2985 src: "myapex-arm.apex",
2986 filename: "notmyapex.apex",
2987 }
2988 `)
2989
2990 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2991
2992 expected := "notmyapex.apex"
2993 if p.installFilename != expected {
2994 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2995 }
2996}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002997
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002998func TestPrebuiltOverrides(t *testing.T) {
2999 ctx, config := testApex(t, `
3000 prebuilt_apex {
3001 name: "myapex.prebuilt",
3002 src: "myapex-arm.apex",
3003 overrides: [
3004 "myapex",
3005 ],
3006 }
3007 `)
3008
3009 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3010
3011 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003012 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003013 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003014 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003015 }
3016}
3017
Roland Levillain630846d2019-06-26 12:48:34 +01003018func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003019 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003020 apex_test {
3021 name: "myapex",
3022 key: "myapex.key",
3023 tests: [
3024 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003025 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003026 ],
3027 }
3028
3029 apex_key {
3030 name: "myapex.key",
3031 public_key: "testkey.avbpubkey",
3032 private_key: "testkey.pem",
3033 }
3034
3035 cc_test {
3036 name: "mytest",
3037 gtest: false,
3038 srcs: ["mytest.cpp"],
3039 relative_install_path: "test",
3040 system_shared_libs: [],
3041 static_executable: true,
3042 stl: "none",
3043 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003044
3045 cc_test {
3046 name: "mytests",
3047 gtest: false,
3048 srcs: [
3049 "mytest1.cpp",
3050 "mytest2.cpp",
3051 "mytest3.cpp",
3052 ],
3053 test_per_src: true,
3054 relative_install_path: "test",
3055 system_shared_libs: [],
3056 static_executable: true,
3057 stl: "none",
3058 }
Roland Levillain630846d2019-06-26 12:48:34 +01003059 `)
3060
Sundong Ahnabb64432019-10-22 13:58:29 +09003061 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003062 copyCmds := apexRule.Args["copy_commands"]
3063
3064 // Ensure that test dep is copied into apex.
3065 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003066
3067 // Ensure that test deps built with `test_per_src` are copied into apex.
3068 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3069 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3070 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003071
3072 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003073 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003074 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3075 name := apexBundle.BaseModuleName()
3076 prefix := "TARGET_"
3077 var builder strings.Builder
3078 data.Custom(&builder, name, prefix, "", data)
3079 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003080 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3081 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3082 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3083 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003084 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003085 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003086 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003087}
3088
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003089func TestInstallExtraFlattenedApexes(t *testing.T) {
3090 ctx, config := testApex(t, `
3091 apex {
3092 name: "myapex",
3093 key: "myapex.key",
3094 }
3095 apex_key {
3096 name: "myapex.key",
3097 public_key: "testkey.avbpubkey",
3098 private_key: "testkey.pem",
3099 }
3100 `, func(fs map[string][]byte, config android.Config) {
3101 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3102 })
3103 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003104 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003105 mk := android.AndroidMkDataForTest(t, config, "", ab)
3106 var builder strings.Builder
3107 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3108 androidMk := builder.String()
3109 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3110}
3111
Jooyung Han5c998b92019-06-27 11:30:33 +09003112func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003113 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003114 apex {
3115 name: "myapex",
3116 key: "myapex.key",
3117 native_shared_libs: ["mylib"],
3118 uses: ["commonapex"],
3119 }
3120
3121 apex {
3122 name: "commonapex",
3123 key: "myapex.key",
3124 native_shared_libs: ["libcommon"],
3125 provide_cpp_shared_libs: true,
3126 }
3127
3128 apex_key {
3129 name: "myapex.key",
3130 public_key: "testkey.avbpubkey",
3131 private_key: "testkey.pem",
3132 }
3133
3134 cc_library {
3135 name: "mylib",
3136 srcs: ["mylib.cpp"],
3137 shared_libs: ["libcommon"],
3138 system_shared_libs: [],
3139 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003140 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003141 }
3142
3143 cc_library {
3144 name: "libcommon",
3145 srcs: ["mylib_common.cpp"],
3146 system_shared_libs: [],
3147 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003148 // TODO: remove //apex_available:platform
3149 apex_available: [
3150 "//apex_available:platform",
3151 "commonapex",
3152 "myapex",
3153 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003154 }
3155 `)
3156
Sundong Ahnabb64432019-10-22 13:58:29 +09003157 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003158 apexRule1 := module1.Rule("apexRule")
3159 copyCmds1 := apexRule1.Args["copy_commands"]
3160
Sundong Ahnabb64432019-10-22 13:58:29 +09003161 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003162 apexRule2 := module2.Rule("apexRule")
3163 copyCmds2 := apexRule2.Args["copy_commands"]
3164
Colin Cross7113d202019-11-20 16:39:12 -08003165 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3166 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003167 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3168 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3169 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3170}
3171
3172func TestApexUsesFailsIfNotProvided(t *testing.T) {
3173 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3174 apex {
3175 name: "myapex",
3176 key: "myapex.key",
3177 uses: ["commonapex"],
3178 }
3179
3180 apex {
3181 name: "commonapex",
3182 key: "myapex.key",
3183 }
3184
3185 apex_key {
3186 name: "myapex.key",
3187 public_key: "testkey.avbpubkey",
3188 private_key: "testkey.pem",
3189 }
3190 `)
3191 testApexError(t, `uses: "commonapex" is not a provider`, `
3192 apex {
3193 name: "myapex",
3194 key: "myapex.key",
3195 uses: ["commonapex"],
3196 }
3197
3198 cc_library {
3199 name: "commonapex",
3200 system_shared_libs: [],
3201 stl: "none",
3202 }
3203
3204 apex_key {
3205 name: "myapex.key",
3206 public_key: "testkey.avbpubkey",
3207 private_key: "testkey.pem",
3208 }
3209 `)
3210}
3211
3212func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3213 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3214 apex {
3215 name: "myapex",
3216 key: "myapex.key",
3217 use_vendor: true,
3218 uses: ["commonapex"],
3219 }
3220
3221 apex {
3222 name: "commonapex",
3223 key: "myapex.key",
3224 provide_cpp_shared_libs: true,
3225 }
3226
3227 apex_key {
3228 name: "myapex.key",
3229 public_key: "testkey.avbpubkey",
3230 private_key: "testkey.pem",
3231 }
Jooyung Handc782442019-11-01 03:14:38 +09003232 `, func(fs map[string][]byte, config android.Config) {
3233 setUseVendorWhitelistForTest(config, []string{"myapex"})
3234 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003235}
3236
Jooyung Hand48f3c32019-08-23 11:18:57 +09003237func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3238 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3239 apex {
3240 name: "myapex",
3241 key: "myapex.key",
3242 native_shared_libs: ["libfoo"],
3243 }
3244
3245 apex_key {
3246 name: "myapex.key",
3247 public_key: "testkey.avbpubkey",
3248 private_key: "testkey.pem",
3249 }
3250
3251 cc_library {
3252 name: "libfoo",
3253 stl: "none",
3254 system_shared_libs: [],
3255 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003256 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003257 }
3258 `)
3259 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3260 apex {
3261 name: "myapex",
3262 key: "myapex.key",
3263 java_libs: ["myjar"],
3264 }
3265
3266 apex_key {
3267 name: "myapex.key",
3268 public_key: "testkey.avbpubkey",
3269 private_key: "testkey.pem",
3270 }
3271
3272 java_library {
3273 name: "myjar",
3274 srcs: ["foo/bar/MyClass.java"],
3275 sdk_version: "none",
3276 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003277 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003278 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003279 }
3280 `)
3281}
3282
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003283func TestApexWithApps(t *testing.T) {
3284 ctx, _ := testApex(t, `
3285 apex {
3286 name: "myapex",
3287 key: "myapex.key",
3288 apps: [
3289 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003290 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003291 ],
3292 }
3293
3294 apex_key {
3295 name: "myapex.key",
3296 public_key: "testkey.avbpubkey",
3297 private_key: "testkey.pem",
3298 }
3299
3300 android_app {
3301 name: "AppFoo",
3302 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003303 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003304 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003305 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08003306 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003307 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003308 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003309
3310 android_app {
3311 name: "AppFooPriv",
3312 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003313 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09003314 system_modules: "none",
3315 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08003316 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003317 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003318 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003319
3320 cc_library_shared {
3321 name: "libjni",
3322 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003323 shared_libs: ["libfoo"],
3324 stl: "none",
3325 system_shared_libs: [],
3326 apex_available: [ "myapex" ],
3327 sdk_version: "current",
3328 }
3329
3330 cc_library_shared {
3331 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09003332 stl: "none",
3333 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003334 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08003335 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003336 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003337 `)
3338
Sundong Ahnabb64432019-10-22 13:58:29 +09003339 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003340 apexRule := module.Rule("apexRule")
3341 copyCmds := apexRule.Args["copy_commands"]
3342
3343 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003344 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003345
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003346 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3347 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003348 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003349 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003350 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003351 // JNI libraries including transitive deps are
3352 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossf8e80222020-04-07 04:21:21 +00003353 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003354 // ... embedded inside APK (jnilibs.zip)
3355 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3356 // ... and not directly inside the APEX
3357 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3358 }
Dario Frenicde2a032019-10-27 00:29:22 +01003359}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003360
Dario Frenicde2a032019-10-27 00:29:22 +01003361func TestApexWithAppImports(t *testing.T) {
3362 ctx, _ := testApex(t, `
3363 apex {
3364 name: "myapex",
3365 key: "myapex.key",
3366 apps: [
3367 "AppFooPrebuilt",
3368 "AppFooPrivPrebuilt",
3369 ],
3370 }
3371
3372 apex_key {
3373 name: "myapex.key",
3374 public_key: "testkey.avbpubkey",
3375 private_key: "testkey.pem",
3376 }
3377
3378 android_app_import {
3379 name: "AppFooPrebuilt",
3380 apk: "PrebuiltAppFoo.apk",
3381 presigned: true,
3382 dex_preopt: {
3383 enabled: false,
3384 },
3385 }
3386
3387 android_app_import {
3388 name: "AppFooPrivPrebuilt",
3389 apk: "PrebuiltAppFooPriv.apk",
3390 privileged: true,
3391 presigned: true,
3392 dex_preopt: {
3393 enabled: false,
3394 },
3395 }
3396 `)
3397
Sundong Ahnabb64432019-10-22 13:58:29 +09003398 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003399 apexRule := module.Rule("apexRule")
3400 copyCmds := apexRule.Args["copy_commands"]
3401
3402 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3403 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003404}
3405
Dario Freni6f3937c2019-12-20 22:58:03 +00003406func TestApexWithTestHelperApp(t *testing.T) {
3407 ctx, _ := testApex(t, `
3408 apex {
3409 name: "myapex",
3410 key: "myapex.key",
3411 apps: [
3412 "TesterHelpAppFoo",
3413 ],
3414 }
3415
3416 apex_key {
3417 name: "myapex.key",
3418 public_key: "testkey.avbpubkey",
3419 private_key: "testkey.pem",
3420 }
3421
3422 android_test_helper_app {
3423 name: "TesterHelpAppFoo",
3424 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003425 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003426 }
3427
3428 `)
3429
3430 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3431 apexRule := module.Rule("apexRule")
3432 copyCmds := apexRule.Args["copy_commands"]
3433
3434 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3435}
3436
Jooyung Han18020ea2019-11-13 10:50:48 +09003437func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3438 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09003439 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003440 apex {
3441 name: "myapex",
3442 key: "myapex.key",
3443 native_shared_libs: ["libfoo"],
3444 }
3445
3446 apex_key {
3447 name: "myapex.key",
3448 public_key: "testkey.avbpubkey",
3449 private_key: "testkey.pem",
3450 }
3451
3452 apex {
3453 name: "otherapex",
3454 key: "myapex.key",
3455 native_shared_libs: ["libfoo"],
3456 }
3457
3458 cc_defaults {
3459 name: "libfoo-defaults",
3460 apex_available: ["otherapex"],
3461 }
3462
3463 cc_library {
3464 name: "libfoo",
3465 defaults: ["libfoo-defaults"],
3466 stl: "none",
3467 system_shared_libs: [],
3468 }`)
3469}
3470
Jiyong Park127b40b2019-09-30 16:04:35 +09003471func TestApexAvailable(t *testing.T) {
3472 // libfoo is not available to myapex, but only to otherapex
3473 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3474 apex {
3475 name: "myapex",
3476 key: "myapex.key",
3477 native_shared_libs: ["libfoo"],
3478 }
3479
3480 apex_key {
3481 name: "myapex.key",
3482 public_key: "testkey.avbpubkey",
3483 private_key: "testkey.pem",
3484 }
3485
3486 apex {
3487 name: "otherapex",
3488 key: "otherapex.key",
3489 native_shared_libs: ["libfoo"],
3490 }
3491
3492 apex_key {
3493 name: "otherapex.key",
3494 public_key: "testkey.avbpubkey",
3495 private_key: "testkey.pem",
3496 }
3497
3498 cc_library {
3499 name: "libfoo",
3500 stl: "none",
3501 system_shared_libs: [],
3502 apex_available: ["otherapex"],
3503 }`)
3504
Jooyung Han5e9013b2020-03-10 06:23:13 +09003505 // libbbaz is an indirect dep
3506 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003507 apex {
3508 name: "myapex",
3509 key: "myapex.key",
3510 native_shared_libs: ["libfoo"],
3511 }
3512
3513 apex_key {
3514 name: "myapex.key",
3515 public_key: "testkey.avbpubkey",
3516 private_key: "testkey.pem",
3517 }
3518
Jiyong Park127b40b2019-09-30 16:04:35 +09003519 cc_library {
3520 name: "libfoo",
3521 stl: "none",
3522 shared_libs: ["libbar"],
3523 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003524 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003525 }
3526
3527 cc_library {
3528 name: "libbar",
3529 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09003530 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003531 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003532 apex_available: ["myapex"],
3533 }
3534
3535 cc_library {
3536 name: "libbaz",
3537 stl: "none",
3538 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003539 }`)
3540
3541 testApexError(t, "\"otherapex\" is not a valid module name", `
3542 apex {
3543 name: "myapex",
3544 key: "myapex.key",
3545 native_shared_libs: ["libfoo"],
3546 }
3547
3548 apex_key {
3549 name: "myapex.key",
3550 public_key: "testkey.avbpubkey",
3551 private_key: "testkey.pem",
3552 }
3553
3554 cc_library {
3555 name: "libfoo",
3556 stl: "none",
3557 system_shared_libs: [],
3558 apex_available: ["otherapex"],
3559 }`)
3560
3561 ctx, _ := testApex(t, `
3562 apex {
3563 name: "myapex",
3564 key: "myapex.key",
3565 native_shared_libs: ["libfoo", "libbar"],
3566 }
3567
3568 apex_key {
3569 name: "myapex.key",
3570 public_key: "testkey.avbpubkey",
3571 private_key: "testkey.pem",
3572 }
3573
3574 cc_library {
3575 name: "libfoo",
3576 stl: "none",
3577 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09003578 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003579 apex_available: ["myapex"],
3580 }
3581
3582 cc_library {
3583 name: "libbar",
3584 stl: "none",
3585 system_shared_libs: [],
3586 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09003587 }
3588
3589 cc_library {
3590 name: "libbaz",
3591 stl: "none",
3592 system_shared_libs: [],
3593 stubs: {
3594 versions: ["10", "20", "30"],
3595 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003596 }`)
3597
3598 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003599 // TODO(jiyong) the checks for the platform variant are removed because we now create
3600 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3601 // the platform variants are not used from other platform modules. When that is done,
3602 // these checks will be replaced by expecting a specific error message that will be
3603 // emitted when the platform variant is used.
3604 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3605 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3606 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3607 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003608
3609 ctx, _ = testApex(t, `
3610 apex {
3611 name: "myapex",
3612 key: "myapex.key",
3613 }
3614
3615 apex_key {
3616 name: "myapex.key",
3617 public_key: "testkey.avbpubkey",
3618 private_key: "testkey.pem",
3619 }
3620
3621 cc_library {
3622 name: "libfoo",
3623 stl: "none",
3624 system_shared_libs: [],
3625 apex_available: ["//apex_available:platform"],
3626 }`)
3627
3628 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003629 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3630 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003631
3632 ctx, _ = testApex(t, `
3633 apex {
3634 name: "myapex",
3635 key: "myapex.key",
3636 native_shared_libs: ["libfoo"],
3637 }
3638
3639 apex_key {
3640 name: "myapex.key",
3641 public_key: "testkey.avbpubkey",
3642 private_key: "testkey.pem",
3643 }
3644
3645 cc_library {
3646 name: "libfoo",
3647 stl: "none",
3648 system_shared_libs: [],
3649 apex_available: ["myapex"],
3650 static: {
3651 apex_available: ["//apex_available:platform"],
3652 },
3653 }`)
3654
3655 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003656 // TODO(jiyong) the checks for the platform variant are removed because we now create
3657 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3658 // the platform variants are not used from other platform modules. When that is done,
3659 // these checks will be replaced by expecting a specific error message that will be
3660 // emitted when the platform variant is used.
3661 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3662 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3663 // // but the static variant is available to both myapex and the platform
3664 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3665 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003666}
3667
Jiyong Park5d790c32019-11-15 18:40:32 +09003668func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003669 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003670 apex {
3671 name: "myapex",
3672 key: "myapex.key",
3673 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003674 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003675 }
3676
3677 override_apex {
3678 name: "override_myapex",
3679 base: "myapex",
3680 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003681 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003682 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003683 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09003684 }
3685
3686 apex_key {
3687 name: "myapex.key",
3688 public_key: "testkey.avbpubkey",
3689 private_key: "testkey.pem",
3690 }
3691
3692 android_app {
3693 name: "app",
3694 srcs: ["foo/bar/MyClass.java"],
3695 package_name: "foo",
3696 sdk_version: "none",
3697 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003698 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003699 }
3700
3701 override_android_app {
3702 name: "override_app",
3703 base: "app",
3704 package_name: "bar",
3705 }
Jiyong Park20bacab2020-03-03 11:45:41 +09003706 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003707
Jiyong Park317645e2019-12-05 13:20:58 +09003708 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3709 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3710 if originalVariant.GetOverriddenBy() != "" {
3711 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3712 }
3713 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3714 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3715 }
3716
Jiyong Park5d790c32019-11-15 18:40:32 +09003717 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3718 apexRule := module.Rule("apexRule")
3719 copyCmds := apexRule.Args["copy_commands"]
3720
3721 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3722 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003723
3724 apexBundle := module.Module().(*apexBundle)
3725 name := apexBundle.Name()
3726 if name != "override_myapex" {
3727 t.Errorf("name should be \"override_myapex\", but was %q", name)
3728 }
3729
Baligh Uddin004d7172020-02-19 21:29:28 -08003730 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3731 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3732 }
3733
Jiyong Park20bacab2020-03-03 11:45:41 +09003734 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003735 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09003736
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003737 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3738 var builder strings.Builder
3739 data.Custom(&builder, name, "TARGET_", "", data)
3740 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003741 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003742 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3743 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003744 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003745 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003746 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003747 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3748 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003749}
3750
Jooyung Han214bf372019-11-12 13:03:50 +09003751func TestLegacyAndroid10Support(t *testing.T) {
3752 ctx, _ := testApex(t, `
3753 apex {
3754 name: "myapex",
3755 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003756 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09003757 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09003758 }
3759
3760 apex_key {
3761 name: "myapex.key",
3762 public_key: "testkey.avbpubkey",
3763 private_key: "testkey.pem",
3764 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003765
3766 cc_library {
3767 name: "mylib",
3768 srcs: ["mylib.cpp"],
3769 stl: "libc++",
3770 system_shared_libs: [],
3771 apex_available: [ "myapex" ],
3772 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003773 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003774
3775 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3776 args := module.Rule("apexRule").Args
3777 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003778 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003779
3780 // The copies of the libraries in the apex should have one more dependency than
3781 // the ones outside the apex, namely the unwinder. Ideally we should check
3782 // the dependency names directly here but for some reason the names are blank in
3783 // this test.
3784 for _, lib := range []string{"libc++", "mylib"} {
3785 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3786 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3787 if len(apexImplicits) != len(nonApexImplicits)+1 {
3788 t.Errorf("%q missing unwinder dep", lib)
3789 }
3790 }
Jooyung Han214bf372019-11-12 13:03:50 +09003791}
3792
Jooyung Han58f26ab2019-12-18 15:34:32 +09003793func TestJavaSDKLibrary(t *testing.T) {
3794 ctx, _ := testApex(t, `
3795 apex {
3796 name: "myapex",
3797 key: "myapex.key",
3798 java_libs: ["foo"],
3799 }
3800
3801 apex_key {
3802 name: "myapex.key",
3803 public_key: "testkey.avbpubkey",
3804 private_key: "testkey.pem",
3805 }
3806
3807 java_sdk_library {
3808 name: "foo",
3809 srcs: ["a.java"],
3810 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003811 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003812 }
3813 `, withFiles(map[string][]byte{
3814 "api/current.txt": nil,
3815 "api/removed.txt": nil,
3816 "api/system-current.txt": nil,
3817 "api/system-removed.txt": nil,
3818 "api/test-current.txt": nil,
3819 "api/test-removed.txt": nil,
3820 }))
3821
3822 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003823 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003824 "javalib/foo.jar",
3825 "etc/permissions/foo.xml",
3826 })
3827 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003828 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3829 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003830}
3831
atrost6e126252020-01-27 17:01:16 +00003832func TestCompatConfig(t *testing.T) {
3833 ctx, _ := testApex(t, `
3834 apex {
3835 name: "myapex",
3836 key: "myapex.key",
3837 prebuilts: ["myjar-platform-compat-config"],
3838 java_libs: ["myjar"],
3839 }
3840
3841 apex_key {
3842 name: "myapex.key",
3843 public_key: "testkey.avbpubkey",
3844 private_key: "testkey.pem",
3845 }
3846
3847 platform_compat_config {
3848 name: "myjar-platform-compat-config",
3849 src: ":myjar",
3850 }
3851
3852 java_library {
3853 name: "myjar",
3854 srcs: ["foo/bar/MyClass.java"],
3855 sdk_version: "none",
3856 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003857 apex_available: [ "myapex" ],
3858 }
3859 `)
3860 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3861 "etc/compatconfig/myjar-platform-compat-config.xml",
3862 "javalib/myjar.jar",
3863 })
3864}
3865
Jiyong Park479321d2019-12-16 11:47:12 +09003866func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3867 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3868 apex {
3869 name: "myapex",
3870 key: "myapex.key",
3871 java_libs: ["myjar"],
3872 }
3873
3874 apex_key {
3875 name: "myapex.key",
3876 public_key: "testkey.avbpubkey",
3877 private_key: "testkey.pem",
3878 }
3879
3880 java_library {
3881 name: "myjar",
3882 srcs: ["foo/bar/MyClass.java"],
3883 sdk_version: "none",
3884 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003885 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003886 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003887 }
3888 `)
3889}
3890
Jiyong Park7afd1072019-12-30 16:56:33 +09003891func TestCarryRequiredModuleNames(t *testing.T) {
3892 ctx, config := testApex(t, `
3893 apex {
3894 name: "myapex",
3895 key: "myapex.key",
3896 native_shared_libs: ["mylib"],
3897 }
3898
3899 apex_key {
3900 name: "myapex.key",
3901 public_key: "testkey.avbpubkey",
3902 private_key: "testkey.pem",
3903 }
3904
3905 cc_library {
3906 name: "mylib",
3907 srcs: ["mylib.cpp"],
3908 system_shared_libs: [],
3909 stl: "none",
3910 required: ["a", "b"],
3911 host_required: ["c", "d"],
3912 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003913 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003914 }
3915 `)
3916
3917 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3918 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3919 name := apexBundle.BaseModuleName()
3920 prefix := "TARGET_"
3921 var builder strings.Builder
3922 data.Custom(&builder, name, prefix, "", data)
3923 androidMk := builder.String()
3924 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3925 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3926 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3927}
3928
Jiyong Park7cd10e32020-01-14 09:22:18 +09003929func TestSymlinksFromApexToSystem(t *testing.T) {
3930 bp := `
3931 apex {
3932 name: "myapex",
3933 key: "myapex.key",
3934 native_shared_libs: ["mylib"],
3935 java_libs: ["myjar"],
3936 }
3937
Jiyong Park9d677202020-02-19 16:29:35 +09003938 apex {
3939 name: "myapex.updatable",
3940 key: "myapex.key",
3941 native_shared_libs: ["mylib"],
3942 java_libs: ["myjar"],
3943 updatable: true,
3944 }
3945
Jiyong Park7cd10e32020-01-14 09:22:18 +09003946 apex_key {
3947 name: "myapex.key",
3948 public_key: "testkey.avbpubkey",
3949 private_key: "testkey.pem",
3950 }
3951
3952 cc_library {
3953 name: "mylib",
3954 srcs: ["mylib.cpp"],
3955 shared_libs: ["myotherlib"],
3956 system_shared_libs: [],
3957 stl: "none",
3958 apex_available: [
3959 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003960 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003961 "//apex_available:platform",
3962 ],
3963 }
3964
3965 cc_library {
3966 name: "myotherlib",
3967 srcs: ["mylib.cpp"],
3968 system_shared_libs: [],
3969 stl: "none",
3970 apex_available: [
3971 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003972 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003973 "//apex_available:platform",
3974 ],
3975 }
3976
3977 java_library {
3978 name: "myjar",
3979 srcs: ["foo/bar/MyClass.java"],
3980 sdk_version: "none",
3981 system_modules: "none",
3982 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003983 apex_available: [
3984 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003985 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003986 "//apex_available:platform",
3987 ],
3988 }
3989
3990 java_library {
3991 name: "myotherjar",
3992 srcs: ["foo/bar/MyClass.java"],
3993 sdk_version: "none",
3994 system_modules: "none",
3995 apex_available: [
3996 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003997 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003998 "//apex_available:platform",
3999 ],
4000 }
4001 `
4002
4003 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
4004 for _, f := range files {
4005 if f.path == file {
4006 if f.isLink {
4007 t.Errorf("%q is not a real file", file)
4008 }
4009 return
4010 }
4011 }
4012 t.Errorf("%q is not found", file)
4013 }
4014
4015 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
4016 for _, f := range files {
4017 if f.path == file {
4018 if !f.isLink {
4019 t.Errorf("%q is not a symlink", file)
4020 }
4021 return
4022 }
4023 }
4024 t.Errorf("%q is not found", file)
4025 }
4026
Jiyong Park9d677202020-02-19 16:29:35 +09004027 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4028 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004029 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004030 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004031 ensureRealfileExists(t, files, "javalib/myjar.jar")
4032 ensureRealfileExists(t, files, "lib64/mylib.so")
4033 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4034
Jiyong Park9d677202020-02-19 16:29:35 +09004035 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4036 ensureRealfileExists(t, files, "javalib/myjar.jar")
4037 ensureRealfileExists(t, files, "lib64/mylib.so")
4038 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4039
4040 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004041 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004042 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004043 ensureRealfileExists(t, files, "javalib/myjar.jar")
4044 ensureRealfileExists(t, files, "lib64/mylib.so")
4045 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004046
4047 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4048 ensureRealfileExists(t, files, "javalib/myjar.jar")
4049 ensureRealfileExists(t, files, "lib64/mylib.so")
4050 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004051}
4052
Jooyung Han643adc42020-02-27 13:50:06 +09004053func TestApexWithJniLibs(t *testing.T) {
4054 ctx, _ := testApex(t, `
4055 apex {
4056 name: "myapex",
4057 key: "myapex.key",
4058 jni_libs: ["mylib"],
4059 }
4060
4061 apex_key {
4062 name: "myapex.key",
4063 public_key: "testkey.avbpubkey",
4064 private_key: "testkey.pem",
4065 }
4066
4067 cc_library {
4068 name: "mylib",
4069 srcs: ["mylib.cpp"],
4070 shared_libs: ["mylib2"],
4071 system_shared_libs: [],
4072 stl: "none",
4073 apex_available: [ "myapex" ],
4074 }
4075
4076 cc_library {
4077 name: "mylib2",
4078 srcs: ["mylib.cpp"],
4079 system_shared_libs: [],
4080 stl: "none",
4081 apex_available: [ "myapex" ],
4082 }
4083 `)
4084
4085 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
4086 // Notice mylib2.so (transitive dep) is not added as a jni_lib
4087 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
4088 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4089 "lib64/mylib.so",
4090 "lib64/mylib2.so",
4091 })
4092}
4093
4094func TestApexWithJniLibs_Errors(t *testing.T) {
4095 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
4096 apex {
4097 name: "myapex",
4098 key: "myapex.key",
4099 jni_libs: ["xxx"],
4100 }
4101
4102 apex_key {
4103 name: "myapex.key",
4104 public_key: "testkey.avbpubkey",
4105 private_key: "testkey.pem",
4106 }
4107
4108 prebuilt_etc {
4109 name: "xxx",
4110 src: "xxx",
4111 }
4112 `, withFiles(map[string][]byte{
4113 "xxx": nil,
4114 }))
4115}
4116
Jiyong Parkbd159612020-02-28 15:22:21 +09004117func TestAppBundle(t *testing.T) {
4118 ctx, _ := testApex(t, `
4119 apex {
4120 name: "myapex",
4121 key: "myapex.key",
4122 apps: ["AppFoo"],
4123 }
4124
4125 apex_key {
4126 name: "myapex.key",
4127 public_key: "testkey.avbpubkey",
4128 private_key: "testkey.pem",
4129 }
4130
4131 android_app {
4132 name: "AppFoo",
4133 srcs: ["foo/bar/MyClass.java"],
4134 sdk_version: "none",
4135 system_modules: "none",
4136 apex_available: [ "myapex" ],
4137 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004138 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09004139
4140 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4141 content := bundleConfigRule.Args["content"]
4142
4143 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004144 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 +09004145}
4146
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004147func TestMain(m *testing.M) {
4148 run := func() int {
4149 setUp()
4150 defer tearDown()
4151
4152 return m.Run()
4153 }
4154
4155 os.Exit(run())
4156}