blob: 5d08f37ab95ea654131634e58d0a07527b282125 [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"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090024 "strings"
25 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090026
27 "github.com/google/blueprint/proptools"
28
29 "android/soong/android"
30 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000031 "android/soong/dexpreopt"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090032 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090033)
34
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070035var buildDir string
36
Jooyung Hand3639552019-08-09 12:57:43 +090037// names returns name list from white space separated string
38func names(s string) (ns []string) {
39 for _, n := range strings.Split(s, " ") {
40 if len(n) > 0 {
41 ns = append(ns, n)
42 }
43 }
44 return
45}
46
Jooyung Han344d5432019-08-23 11:17:39 +090047func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
48 t.Helper()
49 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090050 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
51 if len(errs) > 0 {
52 android.FailIfNoMatchingErrors(t, pattern, errs)
53 return
54 }
55 _, errs = ctx.PrepareBuildActions(config)
56 if len(errs) > 0 {
57 android.FailIfNoMatchingErrors(t, pattern, errs)
58 return
59 }
60
61 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
62}
63
Jooyung Han344d5432019-08-23 11:17:39 +090064func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
65 t.Helper()
66 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090067 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
68 android.FailIfErrored(t, errs)
69 _, errs = ctx.PrepareBuildActions(config)
70 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070071 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090072}
73
Jooyung Han344d5432019-08-23 11:17:39 +090074type testCustomizer func(fs map[string][]byte, config android.Config)
75
76func withFiles(files map[string][]byte) testCustomizer {
77 return func(fs map[string][]byte, config android.Config) {
78 for k, v := range files {
79 fs[k] = v
80 }
81 }
82}
83
84func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
85 return func(fs map[string][]byte, config android.Config) {
86 for k, v := range targets {
87 config.Targets[k] = v
88 }
89 }
90}
91
Jooyung Han35155c42020-02-06 17:33:20 +090092// withNativeBridgeTargets sets configuration with targets including:
93// - X86_64 (primary)
94// - X86 (secondary)
95// - Arm64 on X86_64 (native bridge)
96// - Arm on X86 (native bridge)
97func withNativeBridgeEnabled(fs map[string][]byte, config android.Config) {
98 config.Targets[android.Android] = []android.Target{
99 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
100 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
101 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
102 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
103 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
104 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
105 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
106 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
107 }
108}
109
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900110func withManifestPackageNameOverrides(specs []string) testCustomizer {
111 return func(fs map[string][]byte, config android.Config) {
112 config.TestProductVariables.ManifestPackageNameOverrides = specs
113 }
114}
115
Jooyung Han31c470b2019-10-18 16:26:59 +0900116func withBinder32bit(fs map[string][]byte, config android.Config) {
117 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
118}
119
Jiyong Park7cd10e32020-01-14 09:22:18 +0900120func withUnbundledBuild(fs map[string][]byte, config android.Config) {
121 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
122}
123
Jooyung Han344d5432019-08-23 11:17:39 +0900124func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +0900125 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900126
127 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900128 filegroup {
129 name: "myapex-file_contexts",
130 srcs: [
131 "system/sepolicy/apex/myapex-file_contexts",
132 ],
133 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900134 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800135
Colin Crossf9aabd72020-02-15 11:29:50 -0800136 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
137
Dario Frenicde2a032019-10-27 00:29:22 +0100138 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900139
Jooyung Han344d5432019-08-23 11:17:39 +0900140 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900141 "a.java": nil,
142 "PrebuiltAppFoo.apk": nil,
143 "PrebuiltAppFooPriv.apk": nil,
144 "build/make/target/product/security": nil,
145 "apex_manifest.json": nil,
146 "AndroidManifest.xml": nil,
147 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900148 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900149 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900150 "system/sepolicy/apex/otherapex-file_contexts": nil,
151 "system/sepolicy/apex/commonapex-file_contexts": nil,
152 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800153 "mylib.cpp": nil,
154 "mylib_common.cpp": nil,
155 "mytest.cpp": nil,
156 "mytest1.cpp": nil,
157 "mytest2.cpp": nil,
158 "mytest3.cpp": nil,
159 "myprebuilt": nil,
160 "my_include": nil,
161 "foo/bar/MyClass.java": nil,
162 "prebuilt.jar": nil,
Paul Duffindddd5462020-04-07 15:25:44 +0100163 "prebuilt.so": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800164 "vendor/foo/devkeys/test.x509.pem": nil,
165 "vendor/foo/devkeys/test.pk8": nil,
166 "testkey.x509.pem": nil,
167 "testkey.pk8": nil,
168 "testkey.override.x509.pem": nil,
169 "testkey.override.pk8": nil,
170 "vendor/foo/devkeys/testkey.avbpubkey": nil,
171 "vendor/foo/devkeys/testkey.pem": nil,
172 "NOTICE": nil,
173 "custom_notice": nil,
Jiyong Park9918e1a2020-03-17 19:16:40 +0900174 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800175 "testkey2.avbpubkey": nil,
176 "testkey2.pem": nil,
177 "myapex-arm64.apex": nil,
178 "myapex-arm.apex": nil,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700179 "myapex.apks": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800180 "frameworks/base/api/current.txt": nil,
181 "framework/aidl/a.aidl": nil,
182 "build/make/core/proguard.flags": nil,
183 "build/make/core/proguard_basic_keeps.flags": nil,
184 "dummy.txt": nil,
Liz Kammer1c14a212020-05-12 15:26:55 -0700185 "baz": nil,
186 "bar/baz": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900187 }
188
Colin Crossf9aabd72020-02-15 11:29:50 -0800189 cc.GatherRequiredFilesForTest(fs)
190
Jooyung Han344d5432019-08-23 11:17:39 +0900191 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800192 // The fs now needs to be populated before creating the config, call handlers twice
193 // for now, once to get any fs changes, and later after the config was created to
194 // set product variables or targets.
195 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
196 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900197 }
198
Colin Cross98be1bb2019-12-13 20:41:13 -0800199 config := android.TestArchConfig(buildDir, nil, bp, fs)
200 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
201 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
202 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
203 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
204 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
205 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
206
207 for _, handler := range handlers {
208 // The fs now needs to be populated before creating the config, call handlers twice
209 // for now, earlier to get any fs changes, and now after the config was created to
210 // set product variables or targets.
211 tempFS := map[string][]byte{}
212 handler(tempFS, config)
213 }
214
215 ctx := android.NewTestArchContext()
216 ctx.RegisterModuleType("apex", BundleFactory)
217 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
218 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
219 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
220 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
221 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
222 ctx.RegisterModuleType("override_apex", overrideApexFactory)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700223 ctx.RegisterModuleType("apex_set", apexSetFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800224
Jooyung Hana57af4a2020-01-23 05:36:59 +0000225 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
226 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
227
Paul Duffin77980a82019-12-19 16:01:36 +0000228 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800229 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800230 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
231 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800232 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000233 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800234 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800235 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000236 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000237 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000238 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900239 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800240
Colin Cross98be1bb2019-12-13 20:41:13 -0800241 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800242 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800243
244 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245
Jooyung Han5c998b92019-06-27 11:30:33 +0900246 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247}
248
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700249func setUp() {
250 var err error
251 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900252 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700253 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900254 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255}
256
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700257func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900258 os.RemoveAll(buildDir)
259}
260
Jooyung Han643adc42020-02-27 13:50:06 +0900261// ensure that 'result' equals 'expected'
262func ensureEquals(t *testing.T, result string, expected string) {
263 t.Helper()
264 if result != expected {
265 t.Errorf("%q != %q", expected, result)
266 }
267}
268
Jiyong Park25fc6a92018-11-18 18:02:45 +0900269// ensure that 'result' contains 'expected'
270func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900271 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900272 if !strings.Contains(result, expected) {
273 t.Errorf("%q is not found in %q", expected, result)
274 }
275}
276
277// ensures that 'result' does not contain 'notExpected'
278func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if strings.Contains(result, notExpected) {
281 t.Errorf("%q is found in %q", notExpected, result)
282 }
283}
284
285func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900286 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 if !android.InList(expected, result) {
288 t.Errorf("%q is not found in %v", expected, result)
289 }
290}
291
292func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900293 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900294 if android.InList(notExpected, result) {
295 t.Errorf("%q is found in %v", notExpected, result)
296 }
297}
298
Jooyung Hane1633032019-08-01 17:41:43 +0900299func ensureListEmpty(t *testing.T, result []string) {
300 t.Helper()
301 if len(result) > 0 {
302 t.Errorf("%q is expected to be empty", result)
303 }
304}
305
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306// Minimal test
307func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700308 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900309 apex_defaults {
310 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900311 manifest: ":myapex.manifest",
312 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313 key: "myapex.key",
314 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800315 multilib: {
316 both: {
317 binaries: ["foo",],
318 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900319 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900320 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 }
322
Jiyong Park30ca9372019-02-07 16:27:23 +0900323 apex {
324 name: "myapex",
325 defaults: ["myapex-defaults"],
326 }
327
Jiyong Park25fc6a92018-11-18 18:02:45 +0900328 apex_key {
329 name: "myapex.key",
330 public_key: "testkey.avbpubkey",
331 private_key: "testkey.pem",
332 }
333
Jiyong Park809bb722019-02-13 21:33:49 +0900334 filegroup {
335 name: "myapex.manifest",
336 srcs: ["apex_manifest.json"],
337 }
338
339 filegroup {
340 name: "myapex.androidmanifest",
341 srcs: ["AndroidManifest.xml"],
342 }
343
Jiyong Park25fc6a92018-11-18 18:02:45 +0900344 cc_library {
345 name: "mylib",
346 srcs: ["mylib.cpp"],
347 shared_libs: ["mylib2"],
348 system_shared_libs: [],
349 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000350 // TODO: remove //apex_available:platform
351 apex_available: [
352 "//apex_available:platform",
353 "myapex",
354 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 }
356
Alex Light3d673592019-01-18 14:37:31 -0800357 cc_binary {
358 name: "foo",
359 srcs: ["mylib.cpp"],
360 compile_multilib: "both",
361 multilib: {
362 lib32: {
363 suffix: "32",
364 },
365 lib64: {
366 suffix: "64",
367 },
368 },
369 symlinks: ["foo_link_"],
370 symlink_preferred_arch: true,
371 system_shared_libs: [],
372 static_executable: true,
373 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000374 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800375 }
376
Paul Duffindddd5462020-04-07 15:25:44 +0100377 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900378 name: "mylib2",
379 srcs: ["mylib.cpp"],
380 system_shared_libs: [],
381 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900382 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900383 static_libs: ["libstatic"],
384 // TODO: remove //apex_available:platform
385 apex_available: [
386 "//apex_available:platform",
387 "myapex",
388 ],
389 }
390
Paul Duffindddd5462020-04-07 15:25:44 +0100391 cc_prebuilt_library_shared {
392 name: "mylib2",
393 srcs: ["prebuilt.so"],
394 // TODO: remove //apex_available:platform
395 apex_available: [
396 "//apex_available:platform",
397 "myapex",
398 ],
399 }
400
Jiyong Park9918e1a2020-03-17 19:16:40 +0900401 cc_library_static {
402 name: "libstatic",
403 srcs: ["mylib.cpp"],
404 system_shared_libs: [],
405 stl: "none",
406 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000407 // TODO: remove //apex_available:platform
408 apex_available: [
409 "//apex_available:platform",
410 "myapex",
411 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900412 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900413
414 java_library {
415 name: "myjar",
416 srcs: ["foo/bar/MyClass.java"],
417 sdk_version: "none",
418 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900419 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900420 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000421 // TODO: remove //apex_available:platform
422 apex_available: [
423 "//apex_available:platform",
424 "myapex",
425 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900426 }
427
428 java_library {
429 name: "myotherjar",
430 srcs: ["foo/bar/MyClass.java"],
431 sdk_version: "none",
432 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900433 // TODO: remove //apex_available:platform
434 apex_available: [
435 "//apex_available:platform",
436 "myapex",
437 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900438 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900439
440 java_library {
441 name: "mysharedjar",
442 srcs: ["foo/bar/MyClass.java"],
443 sdk_version: "none",
444 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900445 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900446 `)
447
Sundong Ahnabb64432019-10-22 13:58:29 +0900448 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900449
450 optFlags := apexRule.Args["opt_flags"]
451 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700452 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900453 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900454
Jiyong Park25fc6a92018-11-18 18:02:45 +0900455 copyCmds := apexRule.Args["copy_commands"]
456
457 // Ensure that main rule creates an output
458 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
459
460 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800461 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900462 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900463
464 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800465 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900466 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900467
468 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800469 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
470 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900471 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
472 // .. but not for java libs
473 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900474 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800475
Colin Cross7113d202019-11-20 16:39:12 -0800476 // Ensure that the platform variant ends with _shared or _common
477 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
478 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900479 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
480 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900481 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
482
483 // Ensure that dynamic dependency to java libs are not included
484 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800485
486 // Ensure that all symlinks are present.
487 found_foo_link_64 := false
488 found_foo := false
489 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900490 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800491 if strings.HasSuffix(cmd, "bin/foo") {
492 found_foo = true
493 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
494 found_foo_link_64 = true
495 }
496 }
497 }
498 good := found_foo && found_foo_link_64
499 if !good {
500 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
501 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900502
Sundong Ahnabb64432019-10-22 13:58:29 +0900503 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700504 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900505 if len(noticeInputs) != 3 {
506 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900507 }
508 ensureListContains(t, noticeInputs, "NOTICE")
509 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900510 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900511
Artur Satayeva8bd1132020-04-27 18:07:06 +0100512 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev480e25b2020-04-27 18:53:18 +0100513 ensureListContains(t, fullDepsInfo, "myjar(minSdkVersion:(no version)) <- myapex")
514 ensureListContains(t, fullDepsInfo, "mylib(minSdkVersion:(no version)) <- myapex")
515 ensureListContains(t, fullDepsInfo, "mylib2(minSdkVersion:(no version)) <- mylib")
516 ensureListContains(t, fullDepsInfo, "myotherjar(minSdkVersion:(no version)) <- myjar")
517 ensureListContains(t, fullDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100518
519 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev480e25b2020-04-27 18:53:18 +0100520 ensureListContains(t, flatDepsInfo, " myjar(minSdkVersion:(no version))")
521 ensureListContains(t, flatDepsInfo, " mylib(minSdkVersion:(no version))")
522 ensureListContains(t, flatDepsInfo, " mylib2(minSdkVersion:(no version))")
523 ensureListContains(t, flatDepsInfo, " myotherjar(minSdkVersion:(no version))")
524 ensureListContains(t, flatDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800525}
526
Jooyung Hanf21c7972019-12-16 22:32:06 +0900527func TestDefaults(t *testing.T) {
528 ctx, _ := testApex(t, `
529 apex_defaults {
530 name: "myapex-defaults",
531 key: "myapex.key",
532 prebuilts: ["myetc"],
533 native_shared_libs: ["mylib"],
534 java_libs: ["myjar"],
535 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900536 rros: ["rro"],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900537 }
538
539 prebuilt_etc {
540 name: "myetc",
541 src: "myprebuilt",
542 }
543
544 apex {
545 name: "myapex",
546 defaults: ["myapex-defaults"],
547 }
548
549 apex_key {
550 name: "myapex.key",
551 public_key: "testkey.avbpubkey",
552 private_key: "testkey.pem",
553 }
554
555 cc_library {
556 name: "mylib",
557 system_shared_libs: [],
558 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000559 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900560 }
561
562 java_library {
563 name: "myjar",
564 srcs: ["foo/bar/MyClass.java"],
565 sdk_version: "none",
566 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000567 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900568 }
569
570 android_app {
571 name: "AppFoo",
572 srcs: ["foo/bar/MyClass.java"],
573 sdk_version: "none",
574 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000575 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900576 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900577
578 runtime_resource_overlay {
579 name: "rro",
580 theme: "blue",
581 }
582
Jooyung Hanf21c7972019-12-16 22:32:06 +0900583 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000584 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900585 "etc/myetc",
586 "javalib/myjar.jar",
587 "lib64/mylib.so",
588 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900589 "overlay/blue/rro.apk",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900590 })
591}
592
Jooyung Han01a3ee22019-11-02 02:52:25 +0900593func TestApexManifest(t *testing.T) {
594 ctx, _ := testApex(t, `
595 apex {
596 name: "myapex",
597 key: "myapex.key",
598 }
599
600 apex_key {
601 name: "myapex.key",
602 public_key: "testkey.avbpubkey",
603 private_key: "testkey.pem",
604 }
605 `)
606
607 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900608 args := module.Rule("apexRule").Args
609 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
610 t.Error("manifest should be apex_manifest.pb, but " + manifest)
611 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900612}
613
Alex Light5098a612018-11-29 17:12:15 -0800614func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700615 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800616 apex {
617 name: "myapex",
618 key: "myapex.key",
619 payload_type: "zip",
620 native_shared_libs: ["mylib"],
621 }
622
623 apex_key {
624 name: "myapex.key",
625 public_key: "testkey.avbpubkey",
626 private_key: "testkey.pem",
627 }
628
629 cc_library {
630 name: "mylib",
631 srcs: ["mylib.cpp"],
632 shared_libs: ["mylib2"],
633 system_shared_libs: [],
634 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000635 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800636 }
637
638 cc_library {
639 name: "mylib2",
640 srcs: ["mylib.cpp"],
641 system_shared_libs: [],
642 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000643 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800644 }
645 `)
646
Sundong Ahnabb64432019-10-22 13:58:29 +0900647 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800648 copyCmds := zipApexRule.Args["copy_commands"]
649
650 // Ensure that main rule creates an output
651 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
652
653 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800654 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800655
656 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800657 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800658
659 // Ensure that both direct and indirect deps are copied into apex
660 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
661 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900662}
663
664func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700665 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900666 apex {
667 name: "myapex",
668 key: "myapex.key",
669 native_shared_libs: ["mylib", "mylib3"],
670 }
671
672 apex_key {
673 name: "myapex.key",
674 public_key: "testkey.avbpubkey",
675 private_key: "testkey.pem",
676 }
677
678 cc_library {
679 name: "mylib",
680 srcs: ["mylib.cpp"],
681 shared_libs: ["mylib2", "mylib3"],
682 system_shared_libs: [],
683 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000684 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900685 }
686
687 cc_library {
688 name: "mylib2",
689 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900690 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900691 system_shared_libs: [],
692 stl: "none",
693 stubs: {
694 versions: ["1", "2", "3"],
695 },
696 }
697
698 cc_library {
699 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900700 srcs: ["mylib.cpp"],
701 shared_libs: ["mylib4"],
702 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900703 stl: "none",
704 stubs: {
705 versions: ["10", "11", "12"],
706 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000707 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900708 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900709
710 cc_library {
711 name: "mylib4",
712 srcs: ["mylib.cpp"],
713 system_shared_libs: [],
714 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000715 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900716 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900717 `)
718
Sundong Ahnabb64432019-10-22 13:58:29 +0900719 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900720 copyCmds := apexRule.Args["copy_commands"]
721
722 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800723 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900724
725 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800726 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900727
728 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800729 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900730
Colin Cross7113d202019-11-20 16:39:12 -0800731 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900732
733 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900734 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900735 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900736 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900737
738 // 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 -0800739 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900740 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800741 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900742
743 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900744 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900745 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900746
747 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900748 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900749
Jooyung Hana57af4a2020-01-23 05:36:59 +0000750 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900751 "lib64/mylib.so",
752 "lib64/mylib3.so",
753 "lib64/mylib4.so",
754 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900755}
756
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900757func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700758 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900759 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900760 name: "myapex2",
761 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900762 native_shared_libs: ["mylib"],
763 }
764
765 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900766 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900767 public_key: "testkey.avbpubkey",
768 private_key: "testkey.pem",
769 }
770
771 cc_library {
772 name: "mylib",
773 srcs: ["mylib.cpp"],
774 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900775 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900776 system_shared_libs: [],
777 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000778 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900779 }
780
781 cc_library {
782 name: "libfoo",
783 srcs: ["mylib.cpp"],
784 shared_libs: ["libbar"],
785 system_shared_libs: [],
786 stl: "none",
787 stubs: {
788 versions: ["10", "20", "30"],
789 },
790 }
791
792 cc_library {
793 name: "libbar",
794 srcs: ["mylib.cpp"],
795 system_shared_libs: [],
796 stl: "none",
797 }
798
Jiyong Park678c8812020-02-07 17:25:49 +0900799 cc_library_static {
800 name: "libbaz",
801 srcs: ["mylib.cpp"],
802 system_shared_libs: [],
803 stl: "none",
804 apex_available: [ "myapex2" ],
805 }
806
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900807 `)
808
Jiyong Park83dc74b2020-01-14 18:38:44 +0900809 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900810 copyCmds := apexRule.Args["copy_commands"]
811
812 // Ensure that direct non-stubs dep is always included
813 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
814
815 // Ensure that indirect stubs dep is not included
816 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
817
818 // Ensure that dependency of stubs is not included
819 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
820
Jiyong Park83dc74b2020-01-14 18:38:44 +0900821 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900822
823 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900824 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900825 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900826 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900827
Jiyong Park3ff16992019-12-27 14:11:47 +0900828 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900829
830 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
831 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900832
Artur Satayeva8bd1132020-04-27 18:07:06 +0100833 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev480e25b2020-04-27 18:53:18 +0100834 ensureListContains(t, fullDepsInfo, "mylib(minSdkVersion:(no version)) <- myapex2")
835 ensureListContains(t, fullDepsInfo, "libbaz(minSdkVersion:(no version)) <- mylib")
836 ensureListContains(t, fullDepsInfo, "libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +0900837
Artur Satayeva8bd1132020-04-27 18:07:06 +0100838 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev480e25b2020-04-27 18:53:18 +0100839 ensureListContains(t, flatDepsInfo, " mylib(minSdkVersion:(no version))")
840 ensureListContains(t, flatDepsInfo, " libbaz(minSdkVersion:(no version))")
841 ensureListContains(t, flatDepsInfo, " libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900842}
843
Jooyung Hand3639552019-08-09 12:57:43 +0900844func TestApexWithRuntimeLibsDependency(t *testing.T) {
845 /*
846 myapex
847 |
848 v (runtime_libs)
849 mylib ------+------> libfoo [provides stub]
850 |
851 `------> libbar
852 */
853 ctx, _ := testApex(t, `
854 apex {
855 name: "myapex",
856 key: "myapex.key",
857 native_shared_libs: ["mylib"],
858 }
859
860 apex_key {
861 name: "myapex.key",
862 public_key: "testkey.avbpubkey",
863 private_key: "testkey.pem",
864 }
865
866 cc_library {
867 name: "mylib",
868 srcs: ["mylib.cpp"],
869 runtime_libs: ["libfoo", "libbar"],
870 system_shared_libs: [],
871 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000872 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900873 }
874
875 cc_library {
876 name: "libfoo",
877 srcs: ["mylib.cpp"],
878 system_shared_libs: [],
879 stl: "none",
880 stubs: {
881 versions: ["10", "20", "30"],
882 },
883 }
884
885 cc_library {
886 name: "libbar",
887 srcs: ["mylib.cpp"],
888 system_shared_libs: [],
889 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000890 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900891 }
892
893 `)
894
Sundong Ahnabb64432019-10-22 13:58:29 +0900895 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900896 copyCmds := apexRule.Args["copy_commands"]
897
898 // Ensure that direct non-stubs dep is always included
899 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
900
901 // Ensure that indirect stubs dep is not included
902 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
903
904 // Ensure that runtime_libs dep in included
905 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
906
Sundong Ahnabb64432019-10-22 13:58:29 +0900907 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900908 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
909 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900910
911}
912
Jooyung Han8ce8db92020-05-15 19:05:05 +0900913func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
914 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
915 bp := `
916 apex {
917 name: "com.android.runtime",
918 key: "com.android.runtime.key",
919 native_shared_libs: ["libc"],
920 }
921
922 apex_key {
923 name: "com.android.runtime.key",
924 public_key: "testkey.avbpubkey",
925 private_key: "testkey.pem",
926 }
927
928 cc_library {
929 name: "libc",
930 no_libcrt: true,
931 nocrt: true,
932 stl: "none",
933 system_shared_libs: [],
934 stubs: { versions: ["1"] },
935 apex_available: ["com.android.runtime"],
936
937 sanitize: {
938 hwaddress: true,
939 }
940 }
941
942 cc_prebuilt_library_shared {
943 name: "libclang_rt.hwasan-aarch64-android",
944 no_libcrt: true,
945 nocrt: true,
946 stl: "none",
947 system_shared_libs: [],
948 srcs: [""],
949 stubs: { versions: ["1"] },
950
951 sanitize: {
952 never: true,
953 },
954 }
955 `
956 // override bp to use hard-coded names: com.android.runtime and libc
957 fs["Android.bp"] = []byte(bp)
958 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
959 })
960
961 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
962 "lib64/bionic/libc.so",
963 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
964 })
965
966 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
967
968 installed := hwasan.Description("install libclang_rt.hwasan")
969 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
970
971 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
972 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
973 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
974}
975
976func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
977 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
978 bp := `
979 apex {
980 name: "com.android.runtime",
981 key: "com.android.runtime.key",
982 native_shared_libs: ["libc"],
983 }
984
985 apex_key {
986 name: "com.android.runtime.key",
987 public_key: "testkey.avbpubkey",
988 private_key: "testkey.pem",
989 }
990
991 cc_library {
992 name: "libc",
993 no_libcrt: true,
994 nocrt: true,
995 stl: "none",
996 system_shared_libs: [],
997 stubs: { versions: ["1"] },
998 apex_available: ["com.android.runtime"],
999 }
1000
1001 cc_prebuilt_library_shared {
1002 name: "libclang_rt.hwasan-aarch64-android",
1003 no_libcrt: true,
1004 nocrt: true,
1005 stl: "none",
1006 system_shared_libs: [],
1007 srcs: [""],
1008 stubs: { versions: ["1"] },
1009
1010 sanitize: {
1011 never: true,
1012 },
1013 }
1014 `
1015 // override bp to use hard-coded names: com.android.runtime and libc
1016 fs["Android.bp"] = []byte(bp)
1017 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
1018
1019 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1020 })
1021
1022 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1023 "lib64/bionic/libc.so",
1024 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1025 })
1026
1027 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1028
1029 installed := hwasan.Description("install libclang_rt.hwasan")
1030 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1031
1032 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1033 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1034 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1035}
1036
Jooyung Han61b66e92020-03-21 14:21:46 +00001037func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1038 testcases := []struct {
1039 name string
1040 minSdkVersion string
1041 shouldLink string
1042 shouldNotLink []string
1043 }{
1044 {
Jooyung Han75568392020-03-20 04:29:24 +09001045 name: "should link to the latest",
Jooyung Han61b66e92020-03-21 14:21:46 +00001046 minSdkVersion: "current",
1047 shouldLink: "30",
1048 shouldNotLink: []string{"29"},
1049 },
1050 {
1051 name: "should link to llndk#29",
1052 minSdkVersion: "29",
1053 shouldLink: "29",
1054 shouldNotLink: []string{"30"},
1055 },
1056 }
1057 for _, tc := range testcases {
1058 t.Run(tc.name, func(t *testing.T) {
1059 ctx, _ := testApex(t, `
1060 apex {
1061 name: "myapex",
1062 key: "myapex.key",
1063 use_vendor: true,
1064 native_shared_libs: ["mylib"],
1065 min_sdk_version: "`+tc.minSdkVersion+`",
1066 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001067
Jooyung Han61b66e92020-03-21 14:21:46 +00001068 apex_key {
1069 name: "myapex.key",
1070 public_key: "testkey.avbpubkey",
1071 private_key: "testkey.pem",
1072 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001073
Jooyung Han61b66e92020-03-21 14:21:46 +00001074 cc_library {
1075 name: "mylib",
1076 srcs: ["mylib.cpp"],
1077 vendor_available: true,
1078 shared_libs: ["libbar"],
1079 system_shared_libs: [],
1080 stl: "none",
1081 apex_available: [ "myapex" ],
1082 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001083
Jooyung Han61b66e92020-03-21 14:21:46 +00001084 cc_library {
1085 name: "libbar",
1086 srcs: ["mylib.cpp"],
1087 system_shared_libs: [],
1088 stl: "none",
1089 stubs: { versions: ["29","30"] },
1090 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001091
Jooyung Han61b66e92020-03-21 14:21:46 +00001092 llndk_library {
1093 name: "libbar",
1094 symbol_file: "",
1095 }
1096 `, func(fs map[string][]byte, config android.Config) {
1097 setUseVendorWhitelistForTest(config, []string{"myapex"})
1098 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +09001099
Jooyung Han61b66e92020-03-21 14:21:46 +00001100 // Ensure that LLNDK dep is not included
1101 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1102 "lib64/mylib.so",
1103 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001104
Jooyung Han61b66e92020-03-21 14:21:46 +00001105 // Ensure that LLNDK dep is required
1106 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1107 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1108 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001109
Jooyung Han61b66e92020-03-21 14:21:46 +00001110 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1111 ensureContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
1112 for _, ver := range tc.shouldNotLink {
1113 ensureNotContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
1114 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001115
Jooyung Han61b66e92020-03-21 14:21:46 +00001116 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1117 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1118 })
1119 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001120}
1121
Jiyong Park25fc6a92018-11-18 18:02:45 +09001122func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001123 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001124 apex {
1125 name: "myapex",
1126 key: "myapex.key",
1127 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
1128 }
1129
1130 apex_key {
1131 name: "myapex.key",
1132 public_key: "testkey.avbpubkey",
1133 private_key: "testkey.pem",
1134 }
1135
1136 cc_library {
1137 name: "mylib",
1138 srcs: ["mylib.cpp"],
1139 shared_libs: ["libdl#27"],
1140 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001141 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001142 }
1143
1144 cc_library_shared {
1145 name: "mylib_shared",
1146 srcs: ["mylib.cpp"],
1147 shared_libs: ["libdl#27"],
1148 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001149 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001150 }
1151
1152 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001153 name: "libBootstrap",
1154 srcs: ["mylib.cpp"],
1155 stl: "none",
1156 bootstrap: true,
1157 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001158 `)
1159
Sundong Ahnabb64432019-10-22 13:58:29 +09001160 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001161 copyCmds := apexRule.Args["copy_commands"]
1162
1163 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001164 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001165 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1166 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001167
1168 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001169 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001170
Colin Cross7113d202019-11-20 16:39:12 -08001171 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1172 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1173 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001174
1175 // For dependency to libc
1176 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001177 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001178 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001179 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001180 // ... Cflags from stub is correctly exported to mylib
1181 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1182 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1183
1184 // For dependency to libm
1185 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001186 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001187 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001188 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001189 // ... and is not compiling with the stub
1190 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1191 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1192
1193 // For dependency to libdl
1194 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001195 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001196 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001197 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1198 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001199 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001200 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001201 // ... Cflags from stub is correctly exported to mylib
1202 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1203 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001204
1205 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001206 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1207 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1208 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1209 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001210}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001211
Jooyung Han03b51852020-02-26 22:45:42 +09001212func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
1213 // there are three links between liba --> libz
1214 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
1215 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
1216 // 3) (platform) -> liba -> libz : this should be non-stub link
1217 ctx, _ := testApex(t, `
1218 apex {
1219 name: "myapex",
1220 key: "myapex.key",
1221 native_shared_libs: ["libx"],
1222 min_sdk_version: "2",
1223 }
1224
1225 apex {
1226 name: "otherapex",
1227 key: "myapex.key",
1228 native_shared_libs: ["liby"],
1229 min_sdk_version: "3",
1230 }
1231
1232 apex_key {
1233 name: "myapex.key",
1234 public_key: "testkey.avbpubkey",
1235 private_key: "testkey.pem",
1236 }
1237
1238 cc_library {
1239 name: "libx",
1240 shared_libs: ["liba"],
1241 system_shared_libs: [],
1242 stl: "none",
1243 apex_available: [ "myapex" ],
1244 }
1245
1246 cc_library {
1247 name: "liby",
1248 shared_libs: ["liba"],
1249 system_shared_libs: [],
1250 stl: "none",
1251 apex_available: [ "otherapex" ],
1252 }
1253
1254 cc_library {
1255 name: "liba",
1256 shared_libs: ["libz"],
1257 system_shared_libs: [],
1258 stl: "none",
1259 apex_available: [
1260 "//apex_available:anyapex",
1261 "//apex_available:platform",
1262 ],
1263 }
1264
1265 cc_library {
1266 name: "libz",
1267 system_shared_libs: [],
1268 stl: "none",
1269 stubs: {
1270 versions: ["1", "3"],
1271 },
1272 }
1273 `, withUnbundledBuild)
1274
1275 expectLink := func(from, from_variant, to, to_variant string) {
1276 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1277 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1278 }
1279 expectNoLink := func(from, from_variant, to, to_variant string) {
1280 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1281 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1282 }
1283 // platform liba is linked to non-stub version
1284 expectLink("liba", "shared", "libz", "shared")
1285 // liba in myapex is linked to #1
1286 expectLink("liba", "shared_myapex", "libz", "shared_1")
1287 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1288 expectNoLink("liba", "shared_myapex", "libz", "shared")
1289 // liba in otherapex is linked to #3
1290 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1291 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1292 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1293}
1294
Jooyung Hanaed150d2020-04-02 01:41:41 +09001295func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
1296 ctx, _ := testApex(t, `
1297 apex {
1298 name: "myapex",
1299 key: "myapex.key",
1300 native_shared_libs: ["libx"],
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 cc_library {
1311 name: "libx",
1312 shared_libs: ["libz"],
1313 system_shared_libs: [],
1314 stl: "none",
1315 apex_available: [ "myapex" ],
1316 }
1317
1318 cc_library {
1319 name: "libz",
1320 system_shared_libs: [],
1321 stl: "none",
1322 stubs: {
1323 versions: ["29", "R"],
1324 },
1325 }
1326 `, func(fs map[string][]byte, config android.Config) {
1327 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
1328 })
1329
1330 expectLink := func(from, from_variant, to, to_variant string) {
1331 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1332 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1333 }
1334 expectNoLink := func(from, from_variant, to, to_variant string) {
1335 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1336 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1337 }
1338 // 9000 is quite a magic number.
1339 // Finalized SDK codenames are mapped as P(28), Q(29), ...
1340 // And, codenames which are not finalized yet(active_codenames + future_codenames) are numbered from 9000, 9001, ...
1341 // to distinguish them from finalized and future_api(10000)
1342 // In this test, "R" is assumed not finalized yet( listed in Platform_version_active_codenames) and translated into 9000
1343 // (refer android/api_levels.go)
1344 expectLink("libx", "shared_myapex", "libz", "shared_9000")
1345 expectNoLink("libx", "shared_myapex", "libz", "shared_29")
1346 expectNoLink("libx", "shared_myapex", "libz", "shared")
1347}
1348
Jooyung Han03b51852020-02-26 22:45:42 +09001349func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1350 ctx, _ := testApex(t, `
1351 apex {
1352 name: "myapex",
1353 key: "myapex.key",
1354 native_shared_libs: ["libx"],
1355 }
1356
1357 apex_key {
1358 name: "myapex.key",
1359 public_key: "testkey.avbpubkey",
1360 private_key: "testkey.pem",
1361 }
1362
1363 cc_library {
1364 name: "libx",
1365 shared_libs: ["libz"],
1366 system_shared_libs: [],
1367 stl: "none",
1368 apex_available: [ "myapex" ],
1369 }
1370
1371 cc_library {
1372 name: "libz",
1373 system_shared_libs: [],
1374 stl: "none",
1375 stubs: {
1376 versions: ["1", "2"],
1377 },
1378 }
1379 `)
1380
1381 expectLink := func(from, from_variant, to, to_variant string) {
1382 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1383 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1384 }
1385 expectNoLink := func(from, from_variant, to, to_variant string) {
1386 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1387 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1388 }
1389 expectLink("libx", "shared_myapex", "libz", "shared_2")
1390 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1391 expectNoLink("libx", "shared_myapex", "libz", "shared")
1392}
1393
1394func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1395 ctx, _ := testApex(t, `
1396 apex {
1397 name: "myapex",
1398 key: "myapex.key",
1399 native_shared_libs: ["libx"],
1400 }
1401
1402 apex_key {
1403 name: "myapex.key",
1404 public_key: "testkey.avbpubkey",
1405 private_key: "testkey.pem",
1406 }
1407
1408 cc_library {
1409 name: "libx",
1410 system_shared_libs: [],
1411 stl: "none",
1412 apex_available: [ "myapex" ],
1413 stubs: {
1414 versions: ["1", "2"],
1415 },
1416 }
1417
1418 cc_library {
1419 name: "libz",
1420 shared_libs: ["libx"],
1421 system_shared_libs: [],
1422 stl: "none",
1423 }
1424 `)
1425
1426 expectLink := func(from, from_variant, to, to_variant string) {
1427 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1428 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1429 }
1430 expectNoLink := func(from, from_variant, to, to_variant string) {
1431 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1432 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1433 }
1434 expectLink("libz", "shared", "libx", "shared_2")
1435 expectNoLink("libz", "shared", "libz", "shared_1")
1436 expectNoLink("libz", "shared", "libz", "shared")
1437}
1438
Jooyung Han75568392020-03-20 04:29:24 +09001439func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001440 ctx, _ := testApex(t, `
1441 apex {
1442 name: "myapex",
1443 key: "myapex.key",
1444 native_shared_libs: ["libx"],
1445 min_sdk_version: "29",
1446 }
1447
1448 apex_key {
1449 name: "myapex.key",
1450 public_key: "testkey.avbpubkey",
1451 private_key: "testkey.pem",
1452 }
1453
1454 cc_library {
1455 name: "libx",
1456 shared_libs: ["libbar"],
1457 apex_available: [ "myapex" ],
1458 }
1459
1460 cc_library {
1461 name: "libbar",
1462 stubs: {
1463 versions: ["29", "30"],
1464 },
1465 }
Jooyung Han75568392020-03-20 04:29:24 +09001466 `, func(fs map[string][]byte, config android.Config) {
1467 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1468 })
Jooyung Han03b51852020-02-26 22:45:42 +09001469 expectLink := func(from, from_variant, to, to_variant string) {
1470 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1471 libFlags := ld.Args["libFlags"]
1472 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1473 }
Jooyung Han75568392020-03-20 04:29:24 +09001474 expectLink("libx", "shared_hwasan_myapex", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001475}
1476
Jooyung Han75568392020-03-20 04:29:24 +09001477func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001478 ctx, _ := testApex(t, `
1479 apex {
1480 name: "myapex",
1481 key: "myapex.key",
1482 native_shared_libs: ["libx"],
1483 min_sdk_version: "29",
1484 }
1485
1486 apex_key {
1487 name: "myapex.key",
1488 public_key: "testkey.avbpubkey",
1489 private_key: "testkey.pem",
1490 }
1491
1492 cc_library {
1493 name: "libx",
1494 apex_available: [ "myapex" ],
1495 }
Jooyung Han75568392020-03-20 04:29:24 +09001496 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001497
1498 // ensure apex variant of c++ is linked with static unwinder
1499 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1500 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1501 // note that platform variant is not.
1502 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1503 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Han03b51852020-02-26 22:45:42 +09001504}
1505
1506func TestInvalidMinSdkVersion(t *testing.T) {
Jooyung Han75568392020-03-20 04:29:24 +09001507 testApexError(t, `"libz" .*: not found a version\(<=29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001508 apex {
1509 name: "myapex",
1510 key: "myapex.key",
1511 native_shared_libs: ["libx"],
1512 min_sdk_version: "29",
1513 }
1514
1515 apex_key {
1516 name: "myapex.key",
1517 public_key: "testkey.avbpubkey",
1518 private_key: "testkey.pem",
1519 }
1520
1521 cc_library {
1522 name: "libx",
1523 shared_libs: ["libz"],
1524 system_shared_libs: [],
1525 stl: "none",
1526 apex_available: [ "myapex" ],
1527 }
1528
1529 cc_library {
1530 name: "libz",
1531 system_shared_libs: [],
1532 stl: "none",
1533 stubs: {
1534 versions: ["30"],
1535 },
1536 }
Jooyung Han75568392020-03-20 04:29:24 +09001537 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001538
Jooyung Hanaed150d2020-04-02 01:41:41 +09001539 testApexError(t, `"myapex" .*: min_sdk_version: SDK version should be .*`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001540 apex {
1541 name: "myapex",
1542 key: "myapex.key",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001543 min_sdk_version: "abc",
Jooyung Han03b51852020-02-26 22:45:42 +09001544 }
1545
1546 apex_key {
1547 name: "myapex.key",
1548 public_key: "testkey.avbpubkey",
1549 private_key: "testkey.pem",
1550 }
1551 `)
1552}
1553
Artur Satayev8cf899a2020-04-15 17:29:42 +01001554func TestJavaStableSdkVersion(t *testing.T) {
1555 testCases := []struct {
1556 name string
1557 expectedError string
1558 bp string
1559 }{
1560 {
1561 name: "Non-updatable apex with non-stable dep",
1562 bp: `
1563 apex {
1564 name: "myapex",
1565 java_libs: ["myjar"],
1566 key: "myapex.key",
1567 }
1568 apex_key {
1569 name: "myapex.key",
1570 public_key: "testkey.avbpubkey",
1571 private_key: "testkey.pem",
1572 }
1573 java_library {
1574 name: "myjar",
1575 srcs: ["foo/bar/MyClass.java"],
1576 sdk_version: "core_platform",
1577 apex_available: ["myapex"],
1578 }
1579 `,
1580 },
1581 {
1582 name: "Updatable apex with stable dep",
1583 bp: `
1584 apex {
1585 name: "myapex",
1586 java_libs: ["myjar"],
1587 key: "myapex.key",
1588 updatable: true,
1589 min_sdk_version: "29",
1590 }
1591 apex_key {
1592 name: "myapex.key",
1593 public_key: "testkey.avbpubkey",
1594 private_key: "testkey.pem",
1595 }
1596 java_library {
1597 name: "myjar",
1598 srcs: ["foo/bar/MyClass.java"],
1599 sdk_version: "current",
1600 apex_available: ["myapex"],
1601 }
1602 `,
1603 },
1604 {
1605 name: "Updatable apex with non-stable dep",
1606 expectedError: "cannot depend on \"myjar\"",
1607 bp: `
1608 apex {
1609 name: "myapex",
1610 java_libs: ["myjar"],
1611 key: "myapex.key",
1612 updatable: true,
1613 }
1614 apex_key {
1615 name: "myapex.key",
1616 public_key: "testkey.avbpubkey",
1617 private_key: "testkey.pem",
1618 }
1619 java_library {
1620 name: "myjar",
1621 srcs: ["foo/bar/MyClass.java"],
1622 sdk_version: "core_platform",
1623 apex_available: ["myapex"],
1624 }
1625 `,
1626 },
1627 {
1628 name: "Updatable apex with non-stable transitive dep",
1629 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against non-public Android API.",
1630 bp: `
1631 apex {
1632 name: "myapex",
1633 java_libs: ["myjar"],
1634 key: "myapex.key",
1635 updatable: true,
1636 }
1637 apex_key {
1638 name: "myapex.key",
1639 public_key: "testkey.avbpubkey",
1640 private_key: "testkey.pem",
1641 }
1642 java_library {
1643 name: "myjar",
1644 srcs: ["foo/bar/MyClass.java"],
1645 sdk_version: "current",
1646 apex_available: ["myapex"],
1647 static_libs: ["transitive-jar"],
1648 }
1649 java_library {
1650 name: "transitive-jar",
1651 srcs: ["foo/bar/MyClass.java"],
1652 sdk_version: "core_platform",
1653 apex_available: ["myapex"],
1654 }
1655 `,
1656 },
1657 }
1658
1659 for _, test := range testCases {
1660 t.Run(test.name, func(t *testing.T) {
1661 if test.expectedError == "" {
1662 testApex(t, test.bp)
1663 } else {
1664 testApexError(t, test.expectedError, test.bp)
1665 }
1666 })
1667 }
1668}
1669
Jiyong Park7c2ee712018-12-07 00:42:25 +09001670func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001671 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001672 apex {
1673 name: "myapex",
1674 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001675 native_shared_libs: ["mylib"],
1676 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001677 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001678 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001679 }
1680
1681 apex_key {
1682 name: "myapex.key",
1683 public_key: "testkey.avbpubkey",
1684 private_key: "testkey.pem",
1685 }
1686
1687 prebuilt_etc {
1688 name: "myetc",
1689 src: "myprebuilt",
1690 sub_dir: "foo/bar",
1691 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001692
1693 cc_library {
1694 name: "mylib",
1695 srcs: ["mylib.cpp"],
1696 relative_install_path: "foo/bar",
1697 system_shared_libs: [],
1698 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001699 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001700 }
1701
1702 cc_binary {
1703 name: "mybin",
1704 srcs: ["mylib.cpp"],
1705 relative_install_path: "foo/bar",
1706 system_shared_libs: [],
1707 static_executable: true,
1708 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001709 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001710 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001711 `)
1712
Sundong Ahnabb64432019-10-22 13:58:29 +09001713 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001714 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1715
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001716 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001717 ensureListContains(t, dirs, "etc")
1718 ensureListContains(t, dirs, "etc/foo")
1719 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001720 ensureListContains(t, dirs, "lib64")
1721 ensureListContains(t, dirs, "lib64/foo")
1722 ensureListContains(t, dirs, "lib64/foo/bar")
1723 ensureListContains(t, dirs, "lib")
1724 ensureListContains(t, dirs, "lib/foo")
1725 ensureListContains(t, dirs, "lib/foo/bar")
1726
Jiyong Parkbd13e442019-03-15 18:10:35 +09001727 ensureListContains(t, dirs, "bin")
1728 ensureListContains(t, dirs, "bin/foo")
1729 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001730}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001731
Jooyung Han35155c42020-02-06 17:33:20 +09001732func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
1733 ctx, _ := testApex(t, `
1734 apex {
1735 name: "myapex",
1736 key: "myapex.key",
1737 multilib: {
1738 both: {
1739 native_shared_libs: ["mylib"],
1740 binaries: ["mybin"],
1741 },
1742 },
1743 compile_multilib: "both",
1744 native_bridge_supported: true,
1745 }
1746
1747 apex_key {
1748 name: "myapex.key",
1749 public_key: "testkey.avbpubkey",
1750 private_key: "testkey.pem",
1751 }
1752
1753 cc_library {
1754 name: "mylib",
1755 relative_install_path: "foo/bar",
1756 system_shared_libs: [],
1757 stl: "none",
1758 apex_available: [ "myapex" ],
1759 native_bridge_supported: true,
1760 }
1761
1762 cc_binary {
1763 name: "mybin",
1764 relative_install_path: "foo/bar",
1765 system_shared_libs: [],
1766 static_executable: true,
1767 stl: "none",
1768 apex_available: [ "myapex" ],
1769 native_bridge_supported: true,
1770 compile_multilib: "both", // default is "first" for binary
1771 multilib: {
1772 lib64: {
1773 suffix: "64",
1774 },
1775 },
1776 }
1777 `, withNativeBridgeEnabled)
1778 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1779 "bin/foo/bar/mybin",
1780 "bin/foo/bar/mybin64",
1781 "bin/arm/foo/bar/mybin",
1782 "bin/arm64/foo/bar/mybin64",
1783 "lib/foo/bar/mylib.so",
1784 "lib/arm/foo/bar/mylib.so",
1785 "lib64/foo/bar/mylib.so",
1786 "lib64/arm64/foo/bar/mylib.so",
1787 })
1788}
1789
Jiyong Parkda6eb592018-12-19 17:12:36 +09001790func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001791 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001792 apex {
1793 name: "myapex",
1794 key: "myapex.key",
1795 native_shared_libs: ["mylib"],
1796 use_vendor: true,
1797 }
1798
1799 apex_key {
1800 name: "myapex.key",
1801 public_key: "testkey.avbpubkey",
1802 private_key: "testkey.pem",
1803 }
1804
1805 cc_library {
1806 name: "mylib",
1807 srcs: ["mylib.cpp"],
1808 shared_libs: ["mylib2"],
1809 system_shared_libs: [],
1810 vendor_available: true,
1811 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001812 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001813 }
1814
1815 cc_library {
1816 name: "mylib2",
1817 srcs: ["mylib.cpp"],
1818 system_shared_libs: [],
1819 vendor_available: true,
1820 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001821 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001822 }
Jooyung Handc782442019-11-01 03:14:38 +09001823 `, func(fs map[string][]byte, config android.Config) {
1824 setUseVendorWhitelistForTest(config, []string{"myapex"})
1825 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001826
1827 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001828 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001829 for _, implicit := range i.Implicits {
1830 inputsList = append(inputsList, implicit.String())
1831 }
1832 }
1833 inputsString := strings.Join(inputsList, " ")
1834
1835 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001836 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1837 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001838
1839 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001840 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1841 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001842}
Jiyong Park16e91a02018-12-20 18:18:08 +09001843
Jooyung Handc782442019-11-01 03:14:38 +09001844func TestUseVendorRestriction(t *testing.T) {
1845 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1846 apex {
1847 name: "myapex",
1848 key: "myapex.key",
1849 use_vendor: true,
1850 }
1851 apex_key {
1852 name: "myapex.key",
1853 public_key: "testkey.avbpubkey",
1854 private_key: "testkey.pem",
1855 }
1856 `, func(fs map[string][]byte, config android.Config) {
1857 setUseVendorWhitelistForTest(config, []string{""})
1858 })
1859 // no error with whitelist
1860 testApex(t, `
1861 apex {
1862 name: "myapex",
1863 key: "myapex.key",
1864 use_vendor: true,
1865 }
1866 apex_key {
1867 name: "myapex.key",
1868 public_key: "testkey.avbpubkey",
1869 private_key: "testkey.pem",
1870 }
1871 `, func(fs map[string][]byte, config android.Config) {
1872 setUseVendorWhitelistForTest(config, []string{"myapex"})
1873 })
1874}
1875
Jooyung Han5c998b92019-06-27 11:30:33 +09001876func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1877 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1878 apex {
1879 name: "myapex",
1880 key: "myapex.key",
1881 native_shared_libs: ["mylib"],
1882 use_vendor: true,
1883 }
1884
1885 apex_key {
1886 name: "myapex.key",
1887 public_key: "testkey.avbpubkey",
1888 private_key: "testkey.pem",
1889 }
1890
1891 cc_library {
1892 name: "mylib",
1893 srcs: ["mylib.cpp"],
1894 system_shared_libs: [],
1895 stl: "none",
1896 }
1897 `)
1898}
1899
Jiyong Park16e91a02018-12-20 18:18:08 +09001900func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001901 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001902 apex {
1903 name: "myapex",
1904 key: "myapex.key",
1905 native_shared_libs: ["mylib"],
1906 }
1907
1908 apex_key {
1909 name: "myapex.key",
1910 public_key: "testkey.avbpubkey",
1911 private_key: "testkey.pem",
1912 }
1913
1914 cc_library {
1915 name: "mylib",
1916 srcs: ["mylib.cpp"],
1917 system_shared_libs: [],
1918 stl: "none",
1919 stubs: {
1920 versions: ["1", "2", "3"],
1921 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001922 apex_available: [
1923 "//apex_available:platform",
1924 "myapex",
1925 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001926 }
1927
1928 cc_binary {
1929 name: "not_in_apex",
1930 srcs: ["mylib.cpp"],
1931 static_libs: ["mylib"],
1932 static_executable: true,
1933 system_shared_libs: [],
1934 stl: "none",
1935 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001936 `)
1937
Colin Cross7113d202019-11-20 16:39:12 -08001938 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001939
1940 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001941 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001942}
Jiyong Park9335a262018-12-24 11:31:58 +09001943
1944func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001945 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001946 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001947 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001948 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001949 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001950 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001951 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001952 }
1953
1954 cc_library {
1955 name: "mylib",
1956 srcs: ["mylib.cpp"],
1957 system_shared_libs: [],
1958 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001959 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001960 }
1961
1962 apex_key {
1963 name: "myapex.key",
1964 public_key: "testkey.avbpubkey",
1965 private_key: "testkey.pem",
1966 }
1967
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001968 android_app_certificate {
1969 name: "myapex.certificate",
1970 certificate: "testkey",
1971 }
1972
1973 android_app_certificate {
1974 name: "myapex.certificate.override",
1975 certificate: "testkey.override",
1976 }
1977
Jiyong Park9335a262018-12-24 11:31:58 +09001978 `)
1979
1980 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001981 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001982
1983 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1984 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1985 "vendor/foo/devkeys/testkey.avbpubkey")
1986 }
1987 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1988 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1989 "vendor/foo/devkeys/testkey.pem")
1990 }
1991
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001992 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001993 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001994 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001995 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001996 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001997 }
1998}
Jiyong Park58e364a2019-01-19 19:24:06 +09001999
Jooyung Hanf121a652019-12-17 14:30:11 +09002000func TestCertificate(t *testing.T) {
2001 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
2002 ctx, _ := testApex(t, `
2003 apex {
2004 name: "myapex",
2005 key: "myapex.key",
2006 }
2007 apex_key {
2008 name: "myapex.key",
2009 public_key: "testkey.avbpubkey",
2010 private_key: "testkey.pem",
2011 }`)
2012 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2013 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2014 if actual := rule.Args["certificates"]; actual != expected {
2015 t.Errorf("certificates should be %q, not %q", expected, actual)
2016 }
2017 })
2018 t.Run("override when unspecified", func(t *testing.T) {
2019 ctx, _ := testApex(t, `
2020 apex {
2021 name: "myapex_keytest",
2022 key: "myapex.key",
2023 file_contexts: ":myapex-file_contexts",
2024 }
2025 apex_key {
2026 name: "myapex.key",
2027 public_key: "testkey.avbpubkey",
2028 private_key: "testkey.pem",
2029 }
2030 android_app_certificate {
2031 name: "myapex.certificate.override",
2032 certificate: "testkey.override",
2033 }`)
2034 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2035 expected := "testkey.override.x509.pem testkey.override.pk8"
2036 if actual := rule.Args["certificates"]; actual != expected {
2037 t.Errorf("certificates should be %q, not %q", expected, actual)
2038 }
2039 })
2040 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
2041 ctx, _ := testApex(t, `
2042 apex {
2043 name: "myapex",
2044 key: "myapex.key",
2045 certificate: ":myapex.certificate",
2046 }
2047 apex_key {
2048 name: "myapex.key",
2049 public_key: "testkey.avbpubkey",
2050 private_key: "testkey.pem",
2051 }
2052 android_app_certificate {
2053 name: "myapex.certificate",
2054 certificate: "testkey",
2055 }`)
2056 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2057 expected := "testkey.x509.pem testkey.pk8"
2058 if actual := rule.Args["certificates"]; actual != expected {
2059 t.Errorf("certificates should be %q, not %q", expected, actual)
2060 }
2061 })
2062 t.Run("override when specifiec as <:module>", func(t *testing.T) {
2063 ctx, _ := testApex(t, `
2064 apex {
2065 name: "myapex_keytest",
2066 key: "myapex.key",
2067 file_contexts: ":myapex-file_contexts",
2068 certificate: ":myapex.certificate",
2069 }
2070 apex_key {
2071 name: "myapex.key",
2072 public_key: "testkey.avbpubkey",
2073 private_key: "testkey.pem",
2074 }
2075 android_app_certificate {
2076 name: "myapex.certificate.override",
2077 certificate: "testkey.override",
2078 }`)
2079 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2080 expected := "testkey.override.x509.pem testkey.override.pk8"
2081 if actual := rule.Args["certificates"]; actual != expected {
2082 t.Errorf("certificates should be %q, not %q", expected, actual)
2083 }
2084 })
2085 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
2086 ctx, _ := testApex(t, `
2087 apex {
2088 name: "myapex",
2089 key: "myapex.key",
2090 certificate: "testkey",
2091 }
2092 apex_key {
2093 name: "myapex.key",
2094 public_key: "testkey.avbpubkey",
2095 private_key: "testkey.pem",
2096 }`)
2097 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2098 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2099 if actual := rule.Args["certificates"]; actual != expected {
2100 t.Errorf("certificates should be %q, not %q", expected, actual)
2101 }
2102 })
2103 t.Run("override when specified as <name>", func(t *testing.T) {
2104 ctx, _ := testApex(t, `
2105 apex {
2106 name: "myapex_keytest",
2107 key: "myapex.key",
2108 file_contexts: ":myapex-file_contexts",
2109 certificate: "testkey",
2110 }
2111 apex_key {
2112 name: "myapex.key",
2113 public_key: "testkey.avbpubkey",
2114 private_key: "testkey.pem",
2115 }
2116 android_app_certificate {
2117 name: "myapex.certificate.override",
2118 certificate: "testkey.override",
2119 }`)
2120 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2121 expected := "testkey.override.x509.pem testkey.override.pk8"
2122 if actual := rule.Args["certificates"]; actual != expected {
2123 t.Errorf("certificates should be %q, not %q", expected, actual)
2124 }
2125 })
2126}
2127
Jiyong Park58e364a2019-01-19 19:24:06 +09002128func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002129 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002130 apex {
2131 name: "myapex",
2132 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002133 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09002134 }
2135
2136 apex {
2137 name: "otherapex",
2138 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002139 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002140 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002141 }
2142
2143 apex_key {
2144 name: "myapex.key",
2145 public_key: "testkey.avbpubkey",
2146 private_key: "testkey.pem",
2147 }
2148
2149 cc_library {
2150 name: "mylib",
2151 srcs: ["mylib.cpp"],
2152 system_shared_libs: [],
2153 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002154 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002155 "myapex",
2156 "otherapex",
2157 ],
Jooyung Han24282772020-03-21 23:20:55 +09002158 recovery_available: true,
Jiyong Park58e364a2019-01-19 19:24:06 +09002159 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002160 cc_library {
2161 name: "mylib2",
2162 srcs: ["mylib.cpp"],
2163 system_shared_libs: [],
2164 stl: "none",
2165 apex_available: [
2166 "myapex",
2167 "otherapex",
2168 ],
2169 use_apex_name_macro: true,
2170 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002171 `)
2172
Jooyung Hanc87a0592020-03-02 17:44:33 +09002173 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002174 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002175 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han75568392020-03-20 04:29:24 +09002176 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002177
Jooyung Hanccce2f22020-03-07 03:45:53 +09002178 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09002179 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
2180 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09002181 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09002182 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002183
Jooyung Hanccce2f22020-03-07 03:45:53 +09002184 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09002185 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
2186 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09002187 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09002188 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002189
Jooyung Hanc87a0592020-03-02 17:44:33 +09002190 // When cc_library sets use_apex_name_macro: true
2191 // apex variants define additional macro to distinguish which apex variant it is built for
2192
2193 // non-APEX variant does not have __ANDROID_APEX__ defined
2194 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2195 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2196
2197 // APEX variant has __ANDROID_APEX__ defined
2198 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002199 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002200 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2201 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002202
Jooyung Hanc87a0592020-03-02 17:44:33 +09002203 // APEX variant has __ANDROID_APEX__ defined
2204 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002205 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002206 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2207 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09002208
2209 // recovery variant does not set __ANDROID_SDK_VERSION__
2210 mylibCFlags = ctx.ModuleForTests("mylib", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2211 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2212 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002213}
Jiyong Park7e636d02019-01-28 16:16:54 +09002214
2215func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002216 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09002217 apex {
2218 name: "myapex",
2219 key: "myapex.key",
2220 native_shared_libs: ["mylib"],
2221 }
2222
2223 apex_key {
2224 name: "myapex.key",
2225 public_key: "testkey.avbpubkey",
2226 private_key: "testkey.pem",
2227 }
2228
2229 cc_library_headers {
2230 name: "mylib_headers",
2231 export_include_dirs: ["my_include"],
2232 system_shared_libs: [],
2233 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09002234 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002235 }
2236
2237 cc_library {
2238 name: "mylib",
2239 srcs: ["mylib.cpp"],
2240 system_shared_libs: [],
2241 stl: "none",
2242 header_libs: ["mylib_headers"],
2243 export_header_lib_headers: ["mylib_headers"],
2244 stubs: {
2245 versions: ["1", "2", "3"],
2246 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002247 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002248 }
2249
2250 cc_library {
2251 name: "otherlib",
2252 srcs: ["mylib.cpp"],
2253 system_shared_libs: [],
2254 stl: "none",
2255 shared_libs: ["mylib"],
2256 }
2257 `)
2258
Colin Cross7113d202019-11-20 16:39:12 -08002259 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09002260
2261 // Ensure that the include path of the header lib is exported to 'otherlib'
2262 ensureContains(t, cFlags, "-Imy_include")
2263}
Alex Light9670d332019-01-29 18:07:33 -08002264
Jiyong Park7cd10e32020-01-14 09:22:18 +09002265type fileInApex struct {
2266 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00002267 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09002268 isLink bool
2269}
2270
Jooyung Hana57af4a2020-01-23 05:36:59 +00002271func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09002272 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00002273 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09002274 copyCmds := apexRule.Args["copy_commands"]
2275 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09002276 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09002277 for _, cmd := range strings.Split(copyCmds, "&&") {
2278 cmd = strings.TrimSpace(cmd)
2279 if cmd == "" {
2280 continue
2281 }
2282 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00002283 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09002284 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09002285 switch terms[0] {
2286 case "mkdir":
2287 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09002288 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09002289 t.Fatal("copyCmds contains invalid cp command", cmd)
2290 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002291 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002292 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002293 isLink = false
2294 case "ln":
2295 if len(terms) != 3 && len(terms) != 4 {
2296 // ln LINK TARGET or ln -s LINK TARGET
2297 t.Fatal("copyCmds contains invalid ln command", cmd)
2298 }
2299 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002300 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002301 isLink = true
2302 default:
2303 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
2304 }
2305 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09002306 index := strings.Index(dst, imageApexDir)
2307 if index == -1 {
2308 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
2309 }
2310 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002311 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09002312 }
2313 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002314 return ret
2315}
2316
Jooyung Hana57af4a2020-01-23 05:36:59 +00002317func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
2318 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09002319 var failed bool
2320 var surplus []string
2321 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00002322 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09002323 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09002324 for _, expected := range files {
2325 if matched, _ := path.Match(expected, file.path); matched {
2326 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09002327 mactchFound = true
2328 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09002329 }
2330 }
Jooyung Hane6436d72020-02-27 13:31:56 +09002331 if !mactchFound {
2332 surplus = append(surplus, file.path)
2333 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002334 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002335
Jooyung Han31c470b2019-10-18 16:26:59 +09002336 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002337 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09002338 t.Log("surplus files", surplus)
2339 failed = true
2340 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002341
2342 if len(files) > len(filesMatched) {
2343 var missing []string
2344 for _, expected := range files {
2345 if !filesMatched[expected] {
2346 missing = append(missing, expected)
2347 }
2348 }
2349 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09002350 t.Log("missing files", missing)
2351 failed = true
2352 }
2353 if failed {
2354 t.Fail()
2355 }
2356}
2357
Jooyung Han344d5432019-08-23 11:17:39 +09002358func TestVndkApexCurrent(t *testing.T) {
2359 ctx, _ := testApex(t, `
2360 apex_vndk {
2361 name: "myapex",
2362 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002363 }
2364
2365 apex_key {
2366 name: "myapex.key",
2367 public_key: "testkey.avbpubkey",
2368 private_key: "testkey.pem",
2369 }
2370
2371 cc_library {
2372 name: "libvndk",
2373 srcs: ["mylib.cpp"],
2374 vendor_available: true,
2375 vndk: {
2376 enabled: true,
2377 },
2378 system_shared_libs: [],
2379 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002380 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002381 }
2382
2383 cc_library {
2384 name: "libvndksp",
2385 srcs: ["mylib.cpp"],
2386 vendor_available: true,
2387 vndk: {
2388 enabled: true,
2389 support_system_process: true,
2390 },
2391 system_shared_libs: [],
2392 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002393 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002394 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002395 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002396
Jooyung Hana57af4a2020-01-23 05:36:59 +00002397 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002398 "lib/libvndk.so",
2399 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002400 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002401 "lib64/libvndk.so",
2402 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002403 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002404 "etc/llndk.libraries.VER.txt",
2405 "etc/vndkcore.libraries.VER.txt",
2406 "etc/vndksp.libraries.VER.txt",
2407 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002408 })
Jooyung Han344d5432019-08-23 11:17:39 +09002409}
2410
2411func TestVndkApexWithPrebuilt(t *testing.T) {
2412 ctx, _ := testApex(t, `
2413 apex_vndk {
2414 name: "myapex",
2415 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002416 }
2417
2418 apex_key {
2419 name: "myapex.key",
2420 public_key: "testkey.avbpubkey",
2421 private_key: "testkey.pem",
2422 }
2423
2424 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002425 name: "libvndk",
2426 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002427 vendor_available: true,
2428 vndk: {
2429 enabled: true,
2430 },
2431 system_shared_libs: [],
2432 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002433 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002434 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002435
2436 cc_prebuilt_library_shared {
2437 name: "libvndk.arm",
2438 srcs: ["libvndk.arm.so"],
2439 vendor_available: true,
2440 vndk: {
2441 enabled: true,
2442 },
2443 enabled: false,
2444 arch: {
2445 arm: {
2446 enabled: true,
2447 },
2448 },
2449 system_shared_libs: [],
2450 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002451 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002452 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002453 `+vndkLibrariesTxtFiles("current"),
2454 withFiles(map[string][]byte{
2455 "libvndk.so": nil,
2456 "libvndk.arm.so": nil,
2457 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002458
Jooyung Hana57af4a2020-01-23 05:36:59 +00002459 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002460 "lib/libvndk.so",
2461 "lib/libvndk.arm.so",
2462 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002463 "lib/libc++.so",
2464 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002465 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002466 })
Jooyung Han344d5432019-08-23 11:17:39 +09002467}
2468
Jooyung Han39edb6c2019-11-06 16:53:07 +09002469func vndkLibrariesTxtFiles(vers ...string) (result string) {
2470 for _, v := range vers {
2471 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002472 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002473 result += `
2474 vndk_libraries_txt {
2475 name: "` + txt + `.libraries.txt",
2476 }
2477 `
2478 }
2479 } else {
2480 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2481 result += `
2482 prebuilt_etc {
2483 name: "` + txt + `.libraries.` + v + `.txt",
2484 src: "dummy.txt",
2485 }
2486 `
2487 }
2488 }
2489 }
2490 return
2491}
2492
Jooyung Han344d5432019-08-23 11:17:39 +09002493func TestVndkApexVersion(t *testing.T) {
2494 ctx, _ := testApex(t, `
2495 apex_vndk {
2496 name: "myapex_v27",
2497 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002498 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002499 vndk_version: "27",
2500 }
2501
2502 apex_key {
2503 name: "myapex.key",
2504 public_key: "testkey.avbpubkey",
2505 private_key: "testkey.pem",
2506 }
2507
Jooyung Han31c470b2019-10-18 16:26:59 +09002508 vndk_prebuilt_shared {
2509 name: "libvndk27",
2510 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002511 vendor_available: true,
2512 vndk: {
2513 enabled: true,
2514 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002515 target_arch: "arm64",
2516 arch: {
2517 arm: {
2518 srcs: ["libvndk27_arm.so"],
2519 },
2520 arm64: {
2521 srcs: ["libvndk27_arm64.so"],
2522 },
2523 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002524 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002525 }
2526
2527 vndk_prebuilt_shared {
2528 name: "libvndk27",
2529 version: "27",
2530 vendor_available: true,
2531 vndk: {
2532 enabled: true,
2533 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002534 target_arch: "x86_64",
2535 arch: {
2536 x86: {
2537 srcs: ["libvndk27_x86.so"],
2538 },
2539 x86_64: {
2540 srcs: ["libvndk27_x86_64.so"],
2541 },
2542 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002543 }
2544 `+vndkLibrariesTxtFiles("27"),
2545 withFiles(map[string][]byte{
2546 "libvndk27_arm.so": nil,
2547 "libvndk27_arm64.so": nil,
2548 "libvndk27_x86.so": nil,
2549 "libvndk27_x86_64.so": nil,
2550 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002551
Jooyung Hana57af4a2020-01-23 05:36:59 +00002552 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002553 "lib/libvndk27_arm.so",
2554 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002555 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002556 })
Jooyung Han344d5432019-08-23 11:17:39 +09002557}
2558
2559func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2560 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2561 apex_vndk {
2562 name: "myapex_v27",
2563 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002564 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002565 vndk_version: "27",
2566 }
2567 apex_vndk {
2568 name: "myapex_v27_other",
2569 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002570 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002571 vndk_version: "27",
2572 }
2573
2574 apex_key {
2575 name: "myapex.key",
2576 public_key: "testkey.avbpubkey",
2577 private_key: "testkey.pem",
2578 }
2579
2580 cc_library {
2581 name: "libvndk",
2582 srcs: ["mylib.cpp"],
2583 vendor_available: true,
2584 vndk: {
2585 enabled: true,
2586 },
2587 system_shared_libs: [],
2588 stl: "none",
2589 }
2590
2591 vndk_prebuilt_shared {
2592 name: "libvndk",
2593 version: "27",
2594 vendor_available: true,
2595 vndk: {
2596 enabled: true,
2597 },
2598 srcs: ["libvndk.so"],
2599 }
2600 `, withFiles(map[string][]byte{
2601 "libvndk.so": nil,
2602 }))
2603}
2604
Jooyung Han90eee022019-10-01 20:02:42 +09002605func TestVndkApexNameRule(t *testing.T) {
2606 ctx, _ := testApex(t, `
2607 apex_vndk {
2608 name: "myapex",
2609 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002610 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002611 }
2612 apex_vndk {
2613 name: "myapex_v28",
2614 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002615 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002616 vndk_version: "28",
2617 }
2618 apex_key {
2619 name: "myapex.key",
2620 public_key: "testkey.avbpubkey",
2621 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002622 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002623
2624 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002625 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002626 actual := proptools.String(bundle.properties.Apex_name)
2627 if !reflect.DeepEqual(actual, expected) {
2628 t.Errorf("Got '%v', expected '%v'", actual, expected)
2629 }
2630 }
2631
2632 assertApexName("com.android.vndk.vVER", "myapex")
2633 assertApexName("com.android.vndk.v28", "myapex_v28")
2634}
2635
Jooyung Han344d5432019-08-23 11:17:39 +09002636func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2637 ctx, _ := testApex(t, `
2638 apex_vndk {
2639 name: "myapex",
2640 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002641 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002642 }
2643
2644 apex_key {
2645 name: "myapex.key",
2646 public_key: "testkey.avbpubkey",
2647 private_key: "testkey.pem",
2648 }
2649
2650 cc_library {
2651 name: "libvndk",
2652 srcs: ["mylib.cpp"],
2653 vendor_available: true,
2654 native_bridge_supported: true,
2655 host_supported: true,
2656 vndk: {
2657 enabled: true,
2658 },
2659 system_shared_libs: [],
2660 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002661 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002662 }
Jooyung Han35155c42020-02-06 17:33:20 +09002663 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09002664
Jooyung Hana57af4a2020-01-23 05:36:59 +00002665 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002666 "lib/libvndk.so",
2667 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002668 "lib/libc++.so",
2669 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002670 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002671 })
Jooyung Han344d5432019-08-23 11:17:39 +09002672}
2673
2674func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2675 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2676 apex_vndk {
2677 name: "myapex",
2678 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002679 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002680 native_bridge_supported: true,
2681 }
2682
2683 apex_key {
2684 name: "myapex.key",
2685 public_key: "testkey.avbpubkey",
2686 private_key: "testkey.pem",
2687 }
2688
2689 cc_library {
2690 name: "libvndk",
2691 srcs: ["mylib.cpp"],
2692 vendor_available: true,
2693 native_bridge_supported: true,
2694 host_supported: true,
2695 vndk: {
2696 enabled: true,
2697 },
2698 system_shared_libs: [],
2699 stl: "none",
2700 }
2701 `)
2702}
2703
Jooyung Han31c470b2019-10-18 16:26:59 +09002704func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002705 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002706 apex_vndk {
2707 name: "myapex_v27",
2708 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002709 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002710 vndk_version: "27",
2711 }
2712
2713 apex_key {
2714 name: "myapex.key",
2715 public_key: "testkey.avbpubkey",
2716 private_key: "testkey.pem",
2717 }
2718
2719 vndk_prebuilt_shared {
2720 name: "libvndk27",
2721 version: "27",
2722 target_arch: "arm",
2723 vendor_available: true,
2724 vndk: {
2725 enabled: true,
2726 },
2727 arch: {
2728 arm: {
2729 srcs: ["libvndk27.so"],
2730 }
2731 },
2732 }
2733
2734 vndk_prebuilt_shared {
2735 name: "libvndk27",
2736 version: "27",
2737 target_arch: "arm",
2738 binder32bit: true,
2739 vendor_available: true,
2740 vndk: {
2741 enabled: true,
2742 },
2743 arch: {
2744 arm: {
2745 srcs: ["libvndk27binder32.so"],
2746 }
2747 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002748 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002749 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002750 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002751 withFiles(map[string][]byte{
2752 "libvndk27.so": nil,
2753 "libvndk27binder32.so": nil,
2754 }),
2755 withBinder32bit,
2756 withTargets(map[android.OsType][]android.Target{
2757 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09002758 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
2759 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09002760 },
2761 }),
2762 )
2763
Jooyung Hana57af4a2020-01-23 05:36:59 +00002764 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002765 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002766 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002767 })
2768}
2769
Jooyung Hane1633032019-08-01 17:41:43 +09002770func TestDependenciesInApexManifest(t *testing.T) {
2771 ctx, _ := testApex(t, `
2772 apex {
2773 name: "myapex_nodep",
2774 key: "myapex.key",
2775 native_shared_libs: ["lib_nodep"],
2776 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002777 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002778 }
2779
2780 apex {
2781 name: "myapex_dep",
2782 key: "myapex.key",
2783 native_shared_libs: ["lib_dep"],
2784 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002785 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002786 }
2787
2788 apex {
2789 name: "myapex_provider",
2790 key: "myapex.key",
2791 native_shared_libs: ["libfoo"],
2792 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002793 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002794 }
2795
2796 apex {
2797 name: "myapex_selfcontained",
2798 key: "myapex.key",
2799 native_shared_libs: ["lib_dep", "libfoo"],
2800 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002801 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002802 }
2803
2804 apex_key {
2805 name: "myapex.key",
2806 public_key: "testkey.avbpubkey",
2807 private_key: "testkey.pem",
2808 }
2809
2810 cc_library {
2811 name: "lib_nodep",
2812 srcs: ["mylib.cpp"],
2813 system_shared_libs: [],
2814 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002815 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002816 }
2817
2818 cc_library {
2819 name: "lib_dep",
2820 srcs: ["mylib.cpp"],
2821 shared_libs: ["libfoo"],
2822 system_shared_libs: [],
2823 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002824 apex_available: [
2825 "myapex_dep",
2826 "myapex_provider",
2827 "myapex_selfcontained",
2828 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002829 }
2830
2831 cc_library {
2832 name: "libfoo",
2833 srcs: ["mytest.cpp"],
2834 stubs: {
2835 versions: ["1"],
2836 },
2837 system_shared_libs: [],
2838 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002839 apex_available: [
2840 "myapex_provider",
2841 "myapex_selfcontained",
2842 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002843 }
2844 `)
2845
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002846 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002847 var provideNativeLibs, requireNativeLibs []string
2848
Sundong Ahnabb64432019-10-22 13:58:29 +09002849 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002850 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2851 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002852 ensureListEmpty(t, provideNativeLibs)
2853 ensureListEmpty(t, requireNativeLibs)
2854
Sundong Ahnabb64432019-10-22 13:58:29 +09002855 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002856 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2857 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002858 ensureListEmpty(t, provideNativeLibs)
2859 ensureListContains(t, requireNativeLibs, "libfoo.so")
2860
Sundong Ahnabb64432019-10-22 13:58:29 +09002861 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002862 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2863 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002864 ensureListContains(t, provideNativeLibs, "libfoo.so")
2865 ensureListEmpty(t, requireNativeLibs)
2866
Sundong Ahnabb64432019-10-22 13:58:29 +09002867 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002868 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2869 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002870 ensureListContains(t, provideNativeLibs, "libfoo.so")
2871 ensureListEmpty(t, requireNativeLibs)
2872}
2873
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002874func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002875 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002876 apex {
2877 name: "myapex",
2878 key: "myapex.key",
2879 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002880 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002881 }
2882
2883 apex_key {
2884 name: "myapex.key",
2885 public_key: "testkey.avbpubkey",
2886 private_key: "testkey.pem",
2887 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002888
2889 cc_library {
2890 name: "mylib",
2891 srcs: ["mylib.cpp"],
2892 system_shared_libs: [],
2893 stl: "none",
2894 apex_available: [
2895 "//apex_available:platform",
2896 "myapex",
2897 ],
2898 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002899 `)
2900
Sundong Ahnabb64432019-10-22 13:58:29 +09002901 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002902 apexManifestRule := module.Rule("apexManifestRule")
2903 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2904 apexRule := module.Rule("apexRule")
2905 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002906
2907 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2908 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2909 name := apexBundle.BaseModuleName()
2910 prefix := "TARGET_"
2911 var builder strings.Builder
2912 data.Custom(&builder, name, prefix, "", data)
2913 androidMk := builder.String()
2914 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2915 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002916}
2917
Alex Light0851b882019-02-07 13:20:53 -08002918func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002919 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002920 apex {
2921 name: "myapex",
2922 key: "myapex.key",
2923 native_shared_libs: ["mylib_common"],
2924 }
2925
2926 apex_key {
2927 name: "myapex.key",
2928 public_key: "testkey.avbpubkey",
2929 private_key: "testkey.pem",
2930 }
2931
2932 cc_library {
2933 name: "mylib_common",
2934 srcs: ["mylib.cpp"],
2935 system_shared_libs: [],
2936 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002937 apex_available: [
2938 "//apex_available:platform",
2939 "myapex",
2940 ],
Alex Light0851b882019-02-07 13:20:53 -08002941 }
2942 `)
2943
Sundong Ahnabb64432019-10-22 13:58:29 +09002944 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002945 apexRule := module.Rule("apexRule")
2946 copyCmds := apexRule.Args["copy_commands"]
2947
2948 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2949 t.Log("Apex was a test apex!")
2950 t.Fail()
2951 }
2952 // Ensure that main rule creates an output
2953 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2954
2955 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002956 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002957
2958 // Ensure that both direct and indirect deps are copied into apex
2959 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2960
Colin Cross7113d202019-11-20 16:39:12 -08002961 // Ensure that the platform variant ends with _shared
2962 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002963
2964 if !android.InAnyApex("mylib_common") {
2965 t.Log("Found mylib_common not in any apex!")
2966 t.Fail()
2967 }
2968}
2969
2970func TestTestApex(t *testing.T) {
2971 if android.InAnyApex("mylib_common_test") {
2972 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!")
2973 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002974 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002975 apex_test {
2976 name: "myapex",
2977 key: "myapex.key",
2978 native_shared_libs: ["mylib_common_test"],
2979 }
2980
2981 apex_key {
2982 name: "myapex.key",
2983 public_key: "testkey.avbpubkey",
2984 private_key: "testkey.pem",
2985 }
2986
2987 cc_library {
2988 name: "mylib_common_test",
2989 srcs: ["mylib.cpp"],
2990 system_shared_libs: [],
2991 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002992 // TODO: remove //apex_available:platform
2993 apex_available: [
2994 "//apex_available:platform",
2995 "myapex",
2996 ],
Alex Light0851b882019-02-07 13:20:53 -08002997 }
2998 `)
2999
Sundong Ahnabb64432019-10-22 13:58:29 +09003000 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003001 apexRule := module.Rule("apexRule")
3002 copyCmds := apexRule.Args["copy_commands"]
3003
3004 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3005 t.Log("Apex was not a test apex!")
3006 t.Fail()
3007 }
3008 // Ensure that main rule creates an output
3009 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3010
3011 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08003012 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08003013
3014 // Ensure that both direct and indirect deps are copied into apex
3015 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3016
Colin Cross7113d202019-11-20 16:39:12 -08003017 // Ensure that the platform variant ends with _shared
3018 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003019}
3020
Alex Light9670d332019-01-29 18:07:33 -08003021func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003022 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003023 apex {
3024 name: "myapex",
3025 key: "myapex.key",
3026 multilib: {
3027 first: {
3028 native_shared_libs: ["mylib_common"],
3029 }
3030 },
3031 target: {
3032 android: {
3033 multilib: {
3034 first: {
3035 native_shared_libs: ["mylib"],
3036 }
3037 }
3038 },
3039 host: {
3040 multilib: {
3041 first: {
3042 native_shared_libs: ["mylib2"],
3043 }
3044 }
3045 }
3046 }
3047 }
3048
3049 apex_key {
3050 name: "myapex.key",
3051 public_key: "testkey.avbpubkey",
3052 private_key: "testkey.pem",
3053 }
3054
3055 cc_library {
3056 name: "mylib",
3057 srcs: ["mylib.cpp"],
3058 system_shared_libs: [],
3059 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003060 // TODO: remove //apex_available:platform
3061 apex_available: [
3062 "//apex_available:platform",
3063 "myapex",
3064 ],
Alex Light9670d332019-01-29 18:07:33 -08003065 }
3066
3067 cc_library {
3068 name: "mylib_common",
3069 srcs: ["mylib.cpp"],
3070 system_shared_libs: [],
3071 stl: "none",
3072 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003073 // TODO: remove //apex_available:platform
3074 apex_available: [
3075 "//apex_available:platform",
3076 "myapex",
3077 ],
Alex Light9670d332019-01-29 18:07:33 -08003078 }
3079
3080 cc_library {
3081 name: "mylib2",
3082 srcs: ["mylib.cpp"],
3083 system_shared_libs: [],
3084 stl: "none",
3085 compile_multilib: "first",
3086 }
3087 `)
3088
Sundong Ahnabb64432019-10-22 13:58:29 +09003089 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003090 copyCmds := apexRule.Args["copy_commands"]
3091
3092 // Ensure that main rule creates an output
3093 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3094
3095 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08003096 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3097 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
3098 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08003099
3100 // Ensure that both direct and indirect deps are copied into apex
3101 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3102 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3103 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3104
Colin Cross7113d202019-11-20 16:39:12 -08003105 // Ensure that the platform variant ends with _shared
3106 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3107 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3108 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003109}
Jiyong Park04480cf2019-02-06 00:16:29 +09003110
3111func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003112 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09003113 apex {
3114 name: "myapex",
3115 key: "myapex.key",
3116 binaries: ["myscript"],
3117 }
3118
3119 apex_key {
3120 name: "myapex.key",
3121 public_key: "testkey.avbpubkey",
3122 private_key: "testkey.pem",
3123 }
3124
3125 sh_binary {
3126 name: "myscript",
3127 src: "mylib.cpp",
3128 filename: "myscript.sh",
3129 sub_dir: "script",
3130 }
3131 `)
3132
Sundong Ahnabb64432019-10-22 13:58:29 +09003133 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09003134 copyCmds := apexRule.Args["copy_commands"]
3135
3136 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
3137}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003138
Jooyung Han91df2082019-11-20 01:49:42 +09003139func TestApexInVariousPartition(t *testing.T) {
3140 testcases := []struct {
3141 propName, parition, flattenedPartition string
3142 }{
3143 {"", "system", "system_ext"},
3144 {"product_specific: true", "product", "product"},
3145 {"soc_specific: true", "vendor", "vendor"},
3146 {"proprietary: true", "vendor", "vendor"},
3147 {"vendor: true", "vendor", "vendor"},
3148 {"system_ext_specific: true", "system_ext", "system_ext"},
3149 }
3150 for _, tc := range testcases {
3151 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
3152 ctx, _ := testApex(t, `
3153 apex {
3154 name: "myapex",
3155 key: "myapex.key",
3156 `+tc.propName+`
3157 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003158
Jooyung Han91df2082019-11-20 01:49:42 +09003159 apex_key {
3160 name: "myapex.key",
3161 public_key: "testkey.avbpubkey",
3162 private_key: "testkey.pem",
3163 }
3164 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003165
Jooyung Han91df2082019-11-20 01:49:42 +09003166 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3167 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
3168 actual := apex.installDir.String()
3169 if actual != expected {
3170 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
3171 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003172
Jooyung Han91df2082019-11-20 01:49:42 +09003173 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
3174 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
3175 actual = flattened.installDir.String()
3176 if actual != expected {
3177 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
3178 }
3179 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003180 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003181}
Jiyong Park67882562019-03-21 01:11:21 +09003182
Jooyung Han54aca7b2019-11-20 02:26:02 +09003183func TestFileContexts(t *testing.T) {
3184 ctx, _ := testApex(t, `
3185 apex {
3186 name: "myapex",
3187 key: "myapex.key",
3188 }
3189
3190 apex_key {
3191 name: "myapex.key",
3192 public_key: "testkey.avbpubkey",
3193 private_key: "testkey.pem",
3194 }
3195 `)
3196 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3197 apexRule := module.Rule("apexRule")
3198 actual := apexRule.Args["file_contexts"]
3199 expected := "system/sepolicy/apex/myapex-file_contexts"
3200 if actual != expected {
3201 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
3202 }
3203
3204 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
3205 apex {
3206 name: "myapex",
3207 key: "myapex.key",
3208 file_contexts: "my_own_file_contexts",
3209 }
3210
3211 apex_key {
3212 name: "myapex.key",
3213 public_key: "testkey.avbpubkey",
3214 private_key: "testkey.pem",
3215 }
3216 `, withFiles(map[string][]byte{
3217 "my_own_file_contexts": nil,
3218 }))
3219
3220 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
3221 apex {
3222 name: "myapex",
3223 key: "myapex.key",
3224 product_specific: true,
3225 file_contexts: "product_specific_file_contexts",
3226 }
3227
3228 apex_key {
3229 name: "myapex.key",
3230 public_key: "testkey.avbpubkey",
3231 private_key: "testkey.pem",
3232 }
3233 `)
3234
3235 ctx, _ = testApex(t, `
3236 apex {
3237 name: "myapex",
3238 key: "myapex.key",
3239 product_specific: true,
3240 file_contexts: "product_specific_file_contexts",
3241 }
3242
3243 apex_key {
3244 name: "myapex.key",
3245 public_key: "testkey.avbpubkey",
3246 private_key: "testkey.pem",
3247 }
3248 `, withFiles(map[string][]byte{
3249 "product_specific_file_contexts": nil,
3250 }))
3251 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
3252 apexRule = module.Rule("apexRule")
3253 actual = apexRule.Args["file_contexts"]
3254 expected = "product_specific_file_contexts"
3255 if actual != expected {
3256 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
3257 }
3258
3259 ctx, _ = testApex(t, `
3260 apex {
3261 name: "myapex",
3262 key: "myapex.key",
3263 product_specific: true,
3264 file_contexts: ":my-file-contexts",
3265 }
3266
3267 apex_key {
3268 name: "myapex.key",
3269 public_key: "testkey.avbpubkey",
3270 private_key: "testkey.pem",
3271 }
3272
3273 filegroup {
3274 name: "my-file-contexts",
3275 srcs: ["product_specific_file_contexts"],
3276 }
3277 `, withFiles(map[string][]byte{
3278 "product_specific_file_contexts": nil,
3279 }))
3280 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
3281 apexRule = module.Rule("apexRule")
3282 actual = apexRule.Args["file_contexts"]
3283 expected = "product_specific_file_contexts"
3284 if actual != expected {
3285 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
3286 }
3287}
3288
Jiyong Park67882562019-03-21 01:11:21 +09003289func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003290 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09003291 apex_key {
3292 name: "myapex.key",
3293 public_key: ":my.avbpubkey",
3294 private_key: ":my.pem",
3295 product_specific: true,
3296 }
3297
3298 filegroup {
3299 name: "my.avbpubkey",
3300 srcs: ["testkey2.avbpubkey"],
3301 }
3302
3303 filegroup {
3304 name: "my.pem",
3305 srcs: ["testkey2.pem"],
3306 }
3307 `)
3308
3309 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
3310 expected_pubkey := "testkey2.avbpubkey"
3311 actual_pubkey := apex_key.public_key_file.String()
3312 if actual_pubkey != expected_pubkey {
3313 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
3314 }
3315 expected_privkey := "testkey2.pem"
3316 actual_privkey := apex_key.private_key_file.String()
3317 if actual_privkey != expected_privkey {
3318 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
3319 }
3320}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003321
3322func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003323 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003324 prebuilt_apex {
3325 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09003326 arch: {
3327 arm64: {
3328 src: "myapex-arm64.apex",
3329 },
3330 arm: {
3331 src: "myapex-arm.apex",
3332 },
3333 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003334 }
3335 `)
3336
3337 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3338
Jiyong Parkc95714e2019-03-29 14:23:10 +09003339 expectedInput := "myapex-arm64.apex"
3340 if prebuilt.inputApex.String() != expectedInput {
3341 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
3342 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003343}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003344
3345func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003346 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003347 prebuilt_apex {
3348 name: "myapex",
3349 src: "myapex-arm.apex",
3350 filename: "notmyapex.apex",
3351 }
3352 `)
3353
3354 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3355
3356 expected := "notmyapex.apex"
3357 if p.installFilename != expected {
3358 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
3359 }
3360}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003361
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003362func TestPrebuiltOverrides(t *testing.T) {
3363 ctx, config := testApex(t, `
3364 prebuilt_apex {
3365 name: "myapex.prebuilt",
3366 src: "myapex-arm.apex",
3367 overrides: [
3368 "myapex",
3369 ],
3370 }
3371 `)
3372
3373 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3374
3375 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003376 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003377 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003378 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003379 }
3380}
3381
Roland Levillain630846d2019-06-26 12:48:34 +01003382func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003383 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003384 apex_test {
3385 name: "myapex",
3386 key: "myapex.key",
3387 tests: [
3388 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003389 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003390 ],
3391 }
3392
3393 apex_key {
3394 name: "myapex.key",
3395 public_key: "testkey.avbpubkey",
3396 private_key: "testkey.pem",
3397 }
3398
Liz Kammer1c14a212020-05-12 15:26:55 -07003399 filegroup {
3400 name: "fg",
3401 srcs: [
3402 "baz",
3403 "bar/baz"
3404 ],
3405 }
3406
Roland Levillain630846d2019-06-26 12:48:34 +01003407 cc_test {
3408 name: "mytest",
3409 gtest: false,
3410 srcs: ["mytest.cpp"],
3411 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003412 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01003413 system_shared_libs: [],
3414 static_executable: true,
3415 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07003416 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01003417 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003418
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003419 cc_library {
3420 name: "mylib",
3421 srcs: ["mylib.cpp"],
3422 system_shared_libs: [],
3423 stl: "none",
3424 }
3425
Roland Levillain9b5fde92019-06-28 15:41:19 +01003426 cc_test {
3427 name: "mytests",
3428 gtest: false,
3429 srcs: [
3430 "mytest1.cpp",
3431 "mytest2.cpp",
3432 "mytest3.cpp",
3433 ],
3434 test_per_src: true,
3435 relative_install_path: "test",
3436 system_shared_libs: [],
3437 static_executable: true,
3438 stl: "none",
3439 }
Roland Levillain630846d2019-06-26 12:48:34 +01003440 `)
3441
Sundong Ahnabb64432019-10-22 13:58:29 +09003442 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003443 copyCmds := apexRule.Args["copy_commands"]
3444
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003445 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01003446 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003447 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003448
Liz Kammer1c14a212020-05-12 15:26:55 -07003449 //Ensure that test data are copied into apex.
3450 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
3451 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
3452
Roland Levillain9b5fde92019-06-28 15:41:19 +01003453 // Ensure that test deps built with `test_per_src` are copied into apex.
3454 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3455 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3456 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003457
3458 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003459 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003460 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3461 name := apexBundle.BaseModuleName()
3462 prefix := "TARGET_"
3463 var builder strings.Builder
3464 data.Custom(&builder, name, prefix, "", data)
3465 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003466 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3467 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3468 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3469 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003470 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003471 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003472 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003473}
3474
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003475func TestInstallExtraFlattenedApexes(t *testing.T) {
3476 ctx, config := testApex(t, `
3477 apex {
3478 name: "myapex",
3479 key: "myapex.key",
3480 }
3481 apex_key {
3482 name: "myapex.key",
3483 public_key: "testkey.avbpubkey",
3484 private_key: "testkey.pem",
3485 }
3486 `, func(fs map[string][]byte, config android.Config) {
3487 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3488 })
3489 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003490 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003491 mk := android.AndroidMkDataForTest(t, config, "", ab)
3492 var builder strings.Builder
3493 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3494 androidMk := builder.String()
3495 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3496}
3497
Jooyung Han5c998b92019-06-27 11:30:33 +09003498func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003499 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003500 apex {
3501 name: "myapex",
3502 key: "myapex.key",
3503 native_shared_libs: ["mylib"],
3504 uses: ["commonapex"],
3505 }
3506
3507 apex {
3508 name: "commonapex",
3509 key: "myapex.key",
3510 native_shared_libs: ["libcommon"],
3511 provide_cpp_shared_libs: true,
3512 }
3513
3514 apex_key {
3515 name: "myapex.key",
3516 public_key: "testkey.avbpubkey",
3517 private_key: "testkey.pem",
3518 }
3519
3520 cc_library {
3521 name: "mylib",
3522 srcs: ["mylib.cpp"],
3523 shared_libs: ["libcommon"],
3524 system_shared_libs: [],
3525 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003526 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003527 }
3528
3529 cc_library {
3530 name: "libcommon",
3531 srcs: ["mylib_common.cpp"],
3532 system_shared_libs: [],
3533 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003534 // TODO: remove //apex_available:platform
3535 apex_available: [
3536 "//apex_available:platform",
3537 "commonapex",
3538 "myapex",
3539 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003540 }
3541 `)
3542
Sundong Ahnabb64432019-10-22 13:58:29 +09003543 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003544 apexRule1 := module1.Rule("apexRule")
3545 copyCmds1 := apexRule1.Args["copy_commands"]
3546
Sundong Ahnabb64432019-10-22 13:58:29 +09003547 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003548 apexRule2 := module2.Rule("apexRule")
3549 copyCmds2 := apexRule2.Args["copy_commands"]
3550
Colin Cross7113d202019-11-20 16:39:12 -08003551 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3552 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003553 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3554 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3555 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3556}
3557
3558func TestApexUsesFailsIfNotProvided(t *testing.T) {
3559 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3560 apex {
3561 name: "myapex",
3562 key: "myapex.key",
3563 uses: ["commonapex"],
3564 }
3565
3566 apex {
3567 name: "commonapex",
3568 key: "myapex.key",
3569 }
3570
3571 apex_key {
3572 name: "myapex.key",
3573 public_key: "testkey.avbpubkey",
3574 private_key: "testkey.pem",
3575 }
3576 `)
3577 testApexError(t, `uses: "commonapex" is not a provider`, `
3578 apex {
3579 name: "myapex",
3580 key: "myapex.key",
3581 uses: ["commonapex"],
3582 }
3583
3584 cc_library {
3585 name: "commonapex",
3586 system_shared_libs: [],
3587 stl: "none",
3588 }
3589
3590 apex_key {
3591 name: "myapex.key",
3592 public_key: "testkey.avbpubkey",
3593 private_key: "testkey.pem",
3594 }
3595 `)
3596}
3597
3598func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3599 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3600 apex {
3601 name: "myapex",
3602 key: "myapex.key",
3603 use_vendor: true,
3604 uses: ["commonapex"],
3605 }
3606
3607 apex {
3608 name: "commonapex",
3609 key: "myapex.key",
3610 provide_cpp_shared_libs: true,
3611 }
3612
3613 apex_key {
3614 name: "myapex.key",
3615 public_key: "testkey.avbpubkey",
3616 private_key: "testkey.pem",
3617 }
Jooyung Handc782442019-11-01 03:14:38 +09003618 `, func(fs map[string][]byte, config android.Config) {
3619 setUseVendorWhitelistForTest(config, []string{"myapex"})
3620 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003621}
3622
Jooyung Hand48f3c32019-08-23 11:18:57 +09003623func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3624 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3625 apex {
3626 name: "myapex",
3627 key: "myapex.key",
3628 native_shared_libs: ["libfoo"],
3629 }
3630
3631 apex_key {
3632 name: "myapex.key",
3633 public_key: "testkey.avbpubkey",
3634 private_key: "testkey.pem",
3635 }
3636
3637 cc_library {
3638 name: "libfoo",
3639 stl: "none",
3640 system_shared_libs: [],
3641 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003642 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003643 }
3644 `)
3645 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3646 apex {
3647 name: "myapex",
3648 key: "myapex.key",
3649 java_libs: ["myjar"],
3650 }
3651
3652 apex_key {
3653 name: "myapex.key",
3654 public_key: "testkey.avbpubkey",
3655 private_key: "testkey.pem",
3656 }
3657
3658 java_library {
3659 name: "myjar",
3660 srcs: ["foo/bar/MyClass.java"],
3661 sdk_version: "none",
3662 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003663 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003664 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003665 }
3666 `)
3667}
3668
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003669func TestApexWithApps(t *testing.T) {
3670 ctx, _ := testApex(t, `
3671 apex {
3672 name: "myapex",
3673 key: "myapex.key",
3674 apps: [
3675 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003676 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003677 ],
3678 }
3679
3680 apex_key {
3681 name: "myapex.key",
3682 public_key: "testkey.avbpubkey",
3683 private_key: "testkey.pem",
3684 }
3685
3686 android_app {
3687 name: "AppFoo",
3688 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003689 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003690 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003691 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08003692 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003693 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003694 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003695
3696 android_app {
3697 name: "AppFooPriv",
3698 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003699 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09003700 system_modules: "none",
3701 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08003702 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003703 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003704 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003705
3706 cc_library_shared {
3707 name: "libjni",
3708 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003709 shared_libs: ["libfoo"],
3710 stl: "none",
3711 system_shared_libs: [],
3712 apex_available: [ "myapex" ],
3713 sdk_version: "current",
3714 }
3715
3716 cc_library_shared {
3717 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09003718 stl: "none",
3719 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003720 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08003721 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003722 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003723 `)
3724
Sundong Ahnabb64432019-10-22 13:58:29 +09003725 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003726 apexRule := module.Rule("apexRule")
3727 copyCmds := apexRule.Args["copy_commands"]
3728
3729 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003730 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003731
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003732 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3733 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003734 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003735 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003736 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003737 // JNI libraries including transitive deps are
3738 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossc511bc52020-04-07 16:50:32 +00003739 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_myapex").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003740 // ... embedded inside APK (jnilibs.zip)
3741 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3742 // ... and not directly inside the APEX
3743 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3744 }
Dario Frenicde2a032019-10-27 00:29:22 +01003745}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003746
Dario Frenicde2a032019-10-27 00:29:22 +01003747func TestApexWithAppImports(t *testing.T) {
3748 ctx, _ := testApex(t, `
3749 apex {
3750 name: "myapex",
3751 key: "myapex.key",
3752 apps: [
3753 "AppFooPrebuilt",
3754 "AppFooPrivPrebuilt",
3755 ],
3756 }
3757
3758 apex_key {
3759 name: "myapex.key",
3760 public_key: "testkey.avbpubkey",
3761 private_key: "testkey.pem",
3762 }
3763
3764 android_app_import {
3765 name: "AppFooPrebuilt",
3766 apk: "PrebuiltAppFoo.apk",
3767 presigned: true,
3768 dex_preopt: {
3769 enabled: false,
3770 },
Jiyong Park592a6a42020-04-21 22:34:28 +09003771 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01003772 }
3773
3774 android_app_import {
3775 name: "AppFooPrivPrebuilt",
3776 apk: "PrebuiltAppFooPriv.apk",
3777 privileged: true,
3778 presigned: true,
3779 dex_preopt: {
3780 enabled: false,
3781 },
Jooyung Han39ee1192020-03-23 20:21:11 +09003782 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09003783 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01003784 }
3785 `)
3786
Sundong Ahnabb64432019-10-22 13:58:29 +09003787 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003788 apexRule := module.Rule("apexRule")
3789 copyCmds := apexRule.Args["copy_commands"]
3790
3791 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09003792 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
3793}
3794
3795func TestApexWithAppImportsPrefer(t *testing.T) {
3796 ctx, _ := testApex(t, `
3797 apex {
3798 name: "myapex",
3799 key: "myapex.key",
3800 apps: [
3801 "AppFoo",
3802 ],
3803 }
3804
3805 apex_key {
3806 name: "myapex.key",
3807 public_key: "testkey.avbpubkey",
3808 private_key: "testkey.pem",
3809 }
3810
3811 android_app {
3812 name: "AppFoo",
3813 srcs: ["foo/bar/MyClass.java"],
3814 sdk_version: "none",
3815 system_modules: "none",
3816 apex_available: [ "myapex" ],
3817 }
3818
3819 android_app_import {
3820 name: "AppFoo",
3821 apk: "AppFooPrebuilt.apk",
3822 filename: "AppFooPrebuilt.apk",
3823 presigned: true,
3824 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09003825 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09003826 }
3827 `, withFiles(map[string][]byte{
3828 "AppFooPrebuilt.apk": nil,
3829 }))
3830
3831 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3832 "app/AppFoo/AppFooPrebuilt.apk",
3833 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003834}
3835
Dario Freni6f3937c2019-12-20 22:58:03 +00003836func TestApexWithTestHelperApp(t *testing.T) {
3837 ctx, _ := testApex(t, `
3838 apex {
3839 name: "myapex",
3840 key: "myapex.key",
3841 apps: [
3842 "TesterHelpAppFoo",
3843 ],
3844 }
3845
3846 apex_key {
3847 name: "myapex.key",
3848 public_key: "testkey.avbpubkey",
3849 private_key: "testkey.pem",
3850 }
3851
3852 android_test_helper_app {
3853 name: "TesterHelpAppFoo",
3854 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003855 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003856 }
3857
3858 `)
3859
3860 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3861 apexRule := module.Rule("apexRule")
3862 copyCmds := apexRule.Args["copy_commands"]
3863
3864 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3865}
3866
Jooyung Han18020ea2019-11-13 10:50:48 +09003867func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3868 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09003869 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003870 apex {
3871 name: "myapex",
3872 key: "myapex.key",
3873 native_shared_libs: ["libfoo"],
3874 }
3875
3876 apex_key {
3877 name: "myapex.key",
3878 public_key: "testkey.avbpubkey",
3879 private_key: "testkey.pem",
3880 }
3881
3882 apex {
3883 name: "otherapex",
3884 key: "myapex.key",
3885 native_shared_libs: ["libfoo"],
3886 }
3887
3888 cc_defaults {
3889 name: "libfoo-defaults",
3890 apex_available: ["otherapex"],
3891 }
3892
3893 cc_library {
3894 name: "libfoo",
3895 defaults: ["libfoo-defaults"],
3896 stl: "none",
3897 system_shared_libs: [],
3898 }`)
3899}
3900
Paul Duffine52e66f2020-03-30 17:54:29 +01003901func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003902 // libfoo is not available to myapex, but only to otherapex
3903 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3904 apex {
3905 name: "myapex",
3906 key: "myapex.key",
3907 native_shared_libs: ["libfoo"],
3908 }
3909
3910 apex_key {
3911 name: "myapex.key",
3912 public_key: "testkey.avbpubkey",
3913 private_key: "testkey.pem",
3914 }
3915
3916 apex {
3917 name: "otherapex",
3918 key: "otherapex.key",
3919 native_shared_libs: ["libfoo"],
3920 }
3921
3922 apex_key {
3923 name: "otherapex.key",
3924 public_key: "testkey.avbpubkey",
3925 private_key: "testkey.pem",
3926 }
3927
3928 cc_library {
3929 name: "libfoo",
3930 stl: "none",
3931 system_shared_libs: [],
3932 apex_available: ["otherapex"],
3933 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01003934}
Jiyong Park127b40b2019-09-30 16:04:35 +09003935
Paul Duffine52e66f2020-03-30 17:54:29 +01003936func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09003937 // libbbaz is an indirect dep
Paul Duffindf915ff2020-03-30 17:58:21 +01003938 testApexError(t, `requires "libbaz" that is not available for the APEX. Dependency path:
Paul Duffinc5192442020-03-31 11:31:36 +01003939.*via tag apex\.dependencyTag.*"sharedLib".*
Paul Duffindf915ff2020-03-30 17:58:21 +01003940.*-> libfoo.*link:shared.*
Paul Duffin65347702020-03-31 15:23:40 +01003941.*via tag cc\.DependencyTag.*"shared".*
Paul Duffindf915ff2020-03-30 17:58:21 +01003942.*-> libbar.*link:shared.*
Paul Duffin65347702020-03-31 15:23:40 +01003943.*via tag cc\.DependencyTag.*"shared".*
3944.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003945 apex {
3946 name: "myapex",
3947 key: "myapex.key",
3948 native_shared_libs: ["libfoo"],
3949 }
3950
3951 apex_key {
3952 name: "myapex.key",
3953 public_key: "testkey.avbpubkey",
3954 private_key: "testkey.pem",
3955 }
3956
Jiyong Park127b40b2019-09-30 16:04:35 +09003957 cc_library {
3958 name: "libfoo",
3959 stl: "none",
3960 shared_libs: ["libbar"],
3961 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003962 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003963 }
3964
3965 cc_library {
3966 name: "libbar",
3967 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09003968 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003969 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003970 apex_available: ["myapex"],
3971 }
3972
3973 cc_library {
3974 name: "libbaz",
3975 stl: "none",
3976 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003977 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01003978}
Jiyong Park127b40b2019-09-30 16:04:35 +09003979
Paul Duffine52e66f2020-03-30 17:54:29 +01003980func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003981 testApexError(t, "\"otherapex\" is not a valid module name", `
3982 apex {
3983 name: "myapex",
3984 key: "myapex.key",
3985 native_shared_libs: ["libfoo"],
3986 }
3987
3988 apex_key {
3989 name: "myapex.key",
3990 public_key: "testkey.avbpubkey",
3991 private_key: "testkey.pem",
3992 }
3993
3994 cc_library {
3995 name: "libfoo",
3996 stl: "none",
3997 system_shared_libs: [],
3998 apex_available: ["otherapex"],
3999 }`)
4000
Paul Duffine52e66f2020-03-30 17:54:29 +01004001 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004002 apex {
4003 name: "myapex",
4004 key: "myapex.key",
4005 native_shared_libs: ["libfoo", "libbar"],
4006 }
4007
4008 apex_key {
4009 name: "myapex.key",
4010 public_key: "testkey.avbpubkey",
4011 private_key: "testkey.pem",
4012 }
4013
4014 cc_library {
4015 name: "libfoo",
4016 stl: "none",
4017 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09004018 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004019 apex_available: ["myapex"],
4020 }
4021
4022 cc_library {
4023 name: "libbar",
4024 stl: "none",
4025 system_shared_libs: [],
4026 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09004027 }
4028
4029 cc_library {
4030 name: "libbaz",
4031 stl: "none",
4032 system_shared_libs: [],
4033 stubs: {
4034 versions: ["10", "20", "30"],
4035 },
Jiyong Park127b40b2019-09-30 16:04:35 +09004036 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004037}
Jiyong Park127b40b2019-09-30 16:04:35 +09004038
Jiyong Park89e850a2020-04-07 16:37:39 +09004039func TestApexAvailable_CheckForPlatform(t *testing.T) {
Paul Duffine52e66f2020-03-30 17:54:29 +01004040 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004041 apex {
4042 name: "myapex",
4043 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09004044 native_shared_libs: ["libbar", "libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004045 }
4046
4047 apex_key {
4048 name: "myapex.key",
4049 public_key: "testkey.avbpubkey",
4050 private_key: "testkey.pem",
4051 }
4052
4053 cc_library {
4054 name: "libfoo",
4055 stl: "none",
4056 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09004057 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004058 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09004059 }
4060
4061 cc_library {
4062 name: "libfoo2",
4063 stl: "none",
4064 system_shared_libs: [],
4065 shared_libs: ["libbaz"],
4066 apex_available: ["//apex_available:platform"],
4067 }
4068
4069 cc_library {
4070 name: "libbar",
4071 stl: "none",
4072 system_shared_libs: [],
4073 apex_available: ["myapex"],
4074 }
4075
4076 cc_library {
4077 name: "libbaz",
4078 stl: "none",
4079 system_shared_libs: [],
4080 apex_available: ["myapex"],
4081 stubs: {
4082 versions: ["1"],
4083 },
Jiyong Park127b40b2019-09-30 16:04:35 +09004084 }`)
4085
Jiyong Park89e850a2020-04-07 16:37:39 +09004086 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
4087 // because it depends on libbar which isn't available to platform
4088 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4089 if libfoo.NotAvailableForPlatform() != true {
4090 t.Errorf("%q shouldn't be available to platform", libfoo.String())
4091 }
4092
4093 // libfoo2 however can be available to platform because it depends on libbaz which provides
4094 // stubs
4095 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4096 if libfoo2.NotAvailableForPlatform() == true {
4097 t.Errorf("%q should be available to platform", libfoo2.String())
4098 }
Paul Duffine52e66f2020-03-30 17:54:29 +01004099}
Jiyong Parka90ca002019-10-07 15:47:24 +09004100
Paul Duffine52e66f2020-03-30 17:54:29 +01004101func TestApexAvailable_CreatedForApex(t *testing.T) {
Jiyong Park89e850a2020-04-07 16:37:39 +09004102 ctx, _ := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09004103 apex {
4104 name: "myapex",
4105 key: "myapex.key",
4106 native_shared_libs: ["libfoo"],
4107 }
4108
4109 apex_key {
4110 name: "myapex.key",
4111 public_key: "testkey.avbpubkey",
4112 private_key: "testkey.pem",
4113 }
4114
4115 cc_library {
4116 name: "libfoo",
4117 stl: "none",
4118 system_shared_libs: [],
4119 apex_available: ["myapex"],
4120 static: {
4121 apex_available: ["//apex_available:platform"],
4122 },
4123 }`)
4124
Jiyong Park89e850a2020-04-07 16:37:39 +09004125 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4126 if libfooShared.NotAvailableForPlatform() != true {
4127 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
4128 }
4129 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
4130 if libfooStatic.NotAvailableForPlatform() != false {
4131 t.Errorf("%q should be available to platform", libfooStatic.String())
4132 }
Jiyong Park127b40b2019-09-30 16:04:35 +09004133}
4134
Jiyong Park5d790c32019-11-15 18:40:32 +09004135func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004136 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09004137 apex {
4138 name: "myapex",
4139 key: "myapex.key",
4140 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004141 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09004142 }
4143
4144 override_apex {
4145 name: "override_myapex",
4146 base: "myapex",
4147 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004148 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08004149 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07004150 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09004151 }
4152
4153 apex_key {
4154 name: "myapex.key",
4155 public_key: "testkey.avbpubkey",
4156 private_key: "testkey.pem",
4157 }
4158
4159 android_app {
4160 name: "app",
4161 srcs: ["foo/bar/MyClass.java"],
4162 package_name: "foo",
4163 sdk_version: "none",
4164 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004165 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09004166 }
4167
4168 override_android_app {
4169 name: "override_app",
4170 base: "app",
4171 package_name: "bar",
4172 }
Jiyong Park20bacab2020-03-03 11:45:41 +09004173 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09004174
Jiyong Park317645e2019-12-05 13:20:58 +09004175 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
4176 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
4177 if originalVariant.GetOverriddenBy() != "" {
4178 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
4179 }
4180 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
4181 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
4182 }
4183
Jiyong Park5d790c32019-11-15 18:40:32 +09004184 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
4185 apexRule := module.Rule("apexRule")
4186 copyCmds := apexRule.Args["copy_commands"]
4187
4188 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09004189 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004190
4191 apexBundle := module.Module().(*apexBundle)
4192 name := apexBundle.Name()
4193 if name != "override_myapex" {
4194 t.Errorf("name should be \"override_myapex\", but was %q", name)
4195 }
4196
Baligh Uddin004d7172020-02-19 21:29:28 -08004197 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
4198 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
4199 }
4200
Jiyong Park20bacab2020-03-03 11:45:41 +09004201 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07004202 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09004203
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004204 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
4205 var builder strings.Builder
4206 data.Custom(&builder, name, "TARGET_", "", data)
4207 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09004208 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004209 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
4210 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004211 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004212 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09004213 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004214 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
4215 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09004216}
4217
Jooyung Han214bf372019-11-12 13:03:50 +09004218func TestLegacyAndroid10Support(t *testing.T) {
4219 ctx, _ := testApex(t, `
4220 apex {
4221 name: "myapex",
4222 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004223 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09004224 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09004225 }
4226
4227 apex_key {
4228 name: "myapex.key",
4229 public_key: "testkey.avbpubkey",
4230 private_key: "testkey.pem",
4231 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004232
4233 cc_library {
4234 name: "mylib",
4235 srcs: ["mylib.cpp"],
4236 stl: "libc++",
4237 system_shared_libs: [],
4238 apex_available: [ "myapex" ],
4239 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004240 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09004241
4242 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4243 args := module.Rule("apexRule").Args
4244 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00004245 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004246
4247 // The copies of the libraries in the apex should have one more dependency than
4248 // the ones outside the apex, namely the unwinder. Ideally we should check
4249 // the dependency names directly here but for some reason the names are blank in
4250 // this test.
4251 for _, lib := range []string{"libc++", "mylib"} {
4252 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
4253 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
4254 if len(apexImplicits) != len(nonApexImplicits)+1 {
4255 t.Errorf("%q missing unwinder dep", lib)
4256 }
4257 }
Jooyung Han214bf372019-11-12 13:03:50 +09004258}
4259
Paul Duffin9b879592020-05-26 13:21:35 +01004260var filesForSdkLibrary = map[string][]byte{
4261 "api/current.txt": nil,
4262 "api/removed.txt": nil,
4263 "api/system-current.txt": nil,
4264 "api/system-removed.txt": nil,
4265 "api/test-current.txt": nil,
4266 "api/test-removed.txt": nil,
4267}
4268
Jooyung Han58f26ab2019-12-18 15:34:32 +09004269func TestJavaSDKLibrary(t *testing.T) {
4270 ctx, _ := testApex(t, `
4271 apex {
4272 name: "myapex",
4273 key: "myapex.key",
4274 java_libs: ["foo"],
4275 }
4276
4277 apex_key {
4278 name: "myapex.key",
4279 public_key: "testkey.avbpubkey",
4280 private_key: "testkey.pem",
4281 }
4282
4283 java_sdk_library {
4284 name: "foo",
4285 srcs: ["a.java"],
4286 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004287 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09004288 }
Paul Duffin9b879592020-05-26 13:21:35 +01004289 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09004290
4291 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00004292 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09004293 "javalib/foo.jar",
4294 "etc/permissions/foo.xml",
4295 })
4296 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09004297 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
4298 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09004299}
4300
Paul Duffin9b879592020-05-26 13:21:35 +01004301func TestJavaSDKLibrary_WithinApex(t *testing.T) {
4302 ctx, _ := testApex(t, `
4303 apex {
4304 name: "myapex",
4305 key: "myapex.key",
4306 java_libs: ["foo", "bar"],
4307 }
4308
4309 apex_key {
4310 name: "myapex.key",
4311 public_key: "testkey.avbpubkey",
4312 private_key: "testkey.pem",
4313 }
4314
4315 java_sdk_library {
4316 name: "foo",
4317 srcs: ["a.java"],
4318 api_packages: ["foo"],
4319 apex_available: ["myapex"],
4320 sdk_version: "none",
4321 system_modules: "none",
4322 }
4323
4324 java_library {
4325 name: "bar",
4326 srcs: ["a.java"],
4327 libs: ["foo"],
4328 apex_available: ["myapex"],
4329 sdk_version: "none",
4330 system_modules: "none",
4331 }
4332 `, withFiles(filesForSdkLibrary))
4333
4334 // java_sdk_library installs both impl jar and permission XML
4335 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4336 "javalib/bar.jar",
4337 "javalib/foo.jar",
4338 "etc/permissions/foo.xml",
4339 })
4340
4341 // The bar library should depend on the implementation jar.
4342 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
4343 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
4344 t.Errorf("expected %q, found %#q", expected, actual)
4345 }
4346}
4347
4348func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
4349 ctx, _ := testApex(t, `
4350 apex {
4351 name: "myapex",
4352 key: "myapex.key",
4353 java_libs: ["foo"],
4354 }
4355
4356 apex_key {
4357 name: "myapex.key",
4358 public_key: "testkey.avbpubkey",
4359 private_key: "testkey.pem",
4360 }
4361
4362 java_sdk_library {
4363 name: "foo",
4364 srcs: ["a.java"],
4365 api_packages: ["foo"],
4366 apex_available: ["myapex"],
4367 sdk_version: "none",
4368 system_modules: "none",
4369 }
4370
4371 java_library {
4372 name: "bar",
4373 srcs: ["a.java"],
4374 libs: ["foo"],
4375 sdk_version: "none",
4376 system_modules: "none",
4377 }
4378 `, withFiles(filesForSdkLibrary))
4379
4380 // java_sdk_library installs both impl jar and permission XML
4381 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4382 "javalib/foo.jar",
4383 "etc/permissions/foo.xml",
4384 })
4385
4386 // The bar library should depend on the stubs jar.
4387 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
4388 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
4389 t.Errorf("expected %q, found %#q", expected, actual)
4390 }
4391}
4392
atrost6e126252020-01-27 17:01:16 +00004393func TestCompatConfig(t *testing.T) {
4394 ctx, _ := testApex(t, `
4395 apex {
4396 name: "myapex",
4397 key: "myapex.key",
4398 prebuilts: ["myjar-platform-compat-config"],
4399 java_libs: ["myjar"],
4400 }
4401
4402 apex_key {
4403 name: "myapex.key",
4404 public_key: "testkey.avbpubkey",
4405 private_key: "testkey.pem",
4406 }
4407
4408 platform_compat_config {
4409 name: "myjar-platform-compat-config",
4410 src: ":myjar",
4411 }
4412
4413 java_library {
4414 name: "myjar",
4415 srcs: ["foo/bar/MyClass.java"],
4416 sdk_version: "none",
4417 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00004418 apex_available: [ "myapex" ],
4419 }
4420 `)
4421 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4422 "etc/compatconfig/myjar-platform-compat-config.xml",
4423 "javalib/myjar.jar",
4424 })
4425}
4426
Jiyong Park479321d2019-12-16 11:47:12 +09004427func TestRejectNonInstallableJavaLibrary(t *testing.T) {
4428 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
4429 apex {
4430 name: "myapex",
4431 key: "myapex.key",
4432 java_libs: ["myjar"],
4433 }
4434
4435 apex_key {
4436 name: "myapex.key",
4437 public_key: "testkey.avbpubkey",
4438 private_key: "testkey.pem",
4439 }
4440
4441 java_library {
4442 name: "myjar",
4443 srcs: ["foo/bar/MyClass.java"],
4444 sdk_version: "none",
4445 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09004446 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004447 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09004448 }
4449 `)
4450}
4451
Jiyong Park7afd1072019-12-30 16:56:33 +09004452func TestCarryRequiredModuleNames(t *testing.T) {
4453 ctx, config := testApex(t, `
4454 apex {
4455 name: "myapex",
4456 key: "myapex.key",
4457 native_shared_libs: ["mylib"],
4458 }
4459
4460 apex_key {
4461 name: "myapex.key",
4462 public_key: "testkey.avbpubkey",
4463 private_key: "testkey.pem",
4464 }
4465
4466 cc_library {
4467 name: "mylib",
4468 srcs: ["mylib.cpp"],
4469 system_shared_libs: [],
4470 stl: "none",
4471 required: ["a", "b"],
4472 host_required: ["c", "d"],
4473 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004474 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09004475 }
4476 `)
4477
4478 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4479 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
4480 name := apexBundle.BaseModuleName()
4481 prefix := "TARGET_"
4482 var builder strings.Builder
4483 data.Custom(&builder, name, prefix, "", data)
4484 androidMk := builder.String()
4485 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
4486 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
4487 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
4488}
4489
Jiyong Park7cd10e32020-01-14 09:22:18 +09004490func TestSymlinksFromApexToSystem(t *testing.T) {
4491 bp := `
4492 apex {
4493 name: "myapex",
4494 key: "myapex.key",
4495 native_shared_libs: ["mylib"],
4496 java_libs: ["myjar"],
4497 }
4498
Jiyong Park9d677202020-02-19 16:29:35 +09004499 apex {
4500 name: "myapex.updatable",
4501 key: "myapex.key",
4502 native_shared_libs: ["mylib"],
4503 java_libs: ["myjar"],
4504 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09004505 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09004506 }
4507
Jiyong Park7cd10e32020-01-14 09:22:18 +09004508 apex_key {
4509 name: "myapex.key",
4510 public_key: "testkey.avbpubkey",
4511 private_key: "testkey.pem",
4512 }
4513
4514 cc_library {
4515 name: "mylib",
4516 srcs: ["mylib.cpp"],
4517 shared_libs: ["myotherlib"],
4518 system_shared_libs: [],
4519 stl: "none",
4520 apex_available: [
4521 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004522 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004523 "//apex_available:platform",
4524 ],
4525 }
4526
4527 cc_library {
4528 name: "myotherlib",
4529 srcs: ["mylib.cpp"],
4530 system_shared_libs: [],
4531 stl: "none",
4532 apex_available: [
4533 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004534 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004535 "//apex_available:platform",
4536 ],
4537 }
4538
4539 java_library {
4540 name: "myjar",
4541 srcs: ["foo/bar/MyClass.java"],
4542 sdk_version: "none",
4543 system_modules: "none",
4544 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09004545 apex_available: [
4546 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004547 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004548 "//apex_available:platform",
4549 ],
4550 }
4551
4552 java_library {
4553 name: "myotherjar",
4554 srcs: ["foo/bar/MyClass.java"],
4555 sdk_version: "none",
4556 system_modules: "none",
4557 apex_available: [
4558 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004559 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004560 "//apex_available:platform",
4561 ],
4562 }
4563 `
4564
4565 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
4566 for _, f := range files {
4567 if f.path == file {
4568 if f.isLink {
4569 t.Errorf("%q is not a real file", file)
4570 }
4571 return
4572 }
4573 }
4574 t.Errorf("%q is not found", file)
4575 }
4576
4577 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
4578 for _, f := range files {
4579 if f.path == file {
4580 if !f.isLink {
4581 t.Errorf("%q is not a symlink", file)
4582 }
4583 return
4584 }
4585 }
4586 t.Errorf("%q is not found", file)
4587 }
4588
Jiyong Park9d677202020-02-19 16:29:35 +09004589 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4590 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004591 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004592 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004593 ensureRealfileExists(t, files, "javalib/myjar.jar")
4594 ensureRealfileExists(t, files, "lib64/mylib.so")
4595 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4596
Jiyong Park9d677202020-02-19 16:29:35 +09004597 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4598 ensureRealfileExists(t, files, "javalib/myjar.jar")
4599 ensureRealfileExists(t, files, "lib64/mylib.so")
4600 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4601
4602 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004603 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004604 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004605 ensureRealfileExists(t, files, "javalib/myjar.jar")
4606 ensureRealfileExists(t, files, "lib64/mylib.so")
4607 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004608
4609 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4610 ensureRealfileExists(t, files, "javalib/myjar.jar")
4611 ensureRealfileExists(t, files, "lib64/mylib.so")
4612 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004613}
4614
Jooyung Han643adc42020-02-27 13:50:06 +09004615func TestApexWithJniLibs(t *testing.T) {
4616 ctx, _ := testApex(t, `
4617 apex {
4618 name: "myapex",
4619 key: "myapex.key",
4620 jni_libs: ["mylib"],
4621 }
4622
4623 apex_key {
4624 name: "myapex.key",
4625 public_key: "testkey.avbpubkey",
4626 private_key: "testkey.pem",
4627 }
4628
4629 cc_library {
4630 name: "mylib",
4631 srcs: ["mylib.cpp"],
4632 shared_libs: ["mylib2"],
4633 system_shared_libs: [],
4634 stl: "none",
4635 apex_available: [ "myapex" ],
4636 }
4637
4638 cc_library {
4639 name: "mylib2",
4640 srcs: ["mylib.cpp"],
4641 system_shared_libs: [],
4642 stl: "none",
4643 apex_available: [ "myapex" ],
4644 }
4645 `)
4646
4647 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
4648 // Notice mylib2.so (transitive dep) is not added as a jni_lib
4649 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
4650 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4651 "lib64/mylib.so",
4652 "lib64/mylib2.so",
4653 })
4654}
4655
Jooyung Han49f67012020-04-17 13:43:10 +09004656func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
4657 ctx, _ := testApex(t, `
4658 apex {
4659 name: "myapex",
4660 key: "myapex.key",
4661 }
4662 apex_key {
4663 name: "myapex.key",
4664 public_key: "testkey.avbpubkey",
4665 private_key: "testkey.pem",
4666 }
4667 `, func(fs map[string][]byte, config android.Config) {
4668 delete(config.Targets, android.Android)
4669 config.AndroidCommonTarget = android.Target{}
4670 })
4671
4672 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
4673 t.Errorf("Expected variants: %v, but got: %v", expected, got)
4674 }
4675}
4676
Jooyung Han643adc42020-02-27 13:50:06 +09004677func TestApexWithJniLibs_Errors(t *testing.T) {
4678 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
4679 apex {
4680 name: "myapex",
4681 key: "myapex.key",
4682 jni_libs: ["xxx"],
4683 }
4684
4685 apex_key {
4686 name: "myapex.key",
4687 public_key: "testkey.avbpubkey",
4688 private_key: "testkey.pem",
4689 }
4690
4691 prebuilt_etc {
4692 name: "xxx",
4693 src: "xxx",
4694 }
4695 `, withFiles(map[string][]byte{
4696 "xxx": nil,
4697 }))
4698}
4699
Jiyong Parkbd159612020-02-28 15:22:21 +09004700func TestAppBundle(t *testing.T) {
4701 ctx, _ := testApex(t, `
4702 apex {
4703 name: "myapex",
4704 key: "myapex.key",
4705 apps: ["AppFoo"],
4706 }
4707
4708 apex_key {
4709 name: "myapex.key",
4710 public_key: "testkey.avbpubkey",
4711 private_key: "testkey.pem",
4712 }
4713
4714 android_app {
4715 name: "AppFoo",
4716 srcs: ["foo/bar/MyClass.java"],
4717 sdk_version: "none",
4718 system_modules: "none",
4719 apex_available: [ "myapex" ],
4720 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004721 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09004722
4723 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4724 content := bundleConfigRule.Args["content"]
4725
4726 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004727 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 +09004728}
4729
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004730func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004731 t.Helper()
4732
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004733 bp := `
4734 java_library {
4735 name: "some-updatable-apex-lib",
4736 srcs: ["a.java"],
4737 sdk_version: "current",
4738 apex_available: [
4739 "some-updatable-apex",
4740 ],
4741 }
4742
4743 java_library {
4744 name: "some-non-updatable-apex-lib",
4745 srcs: ["a.java"],
4746 apex_available: [
4747 "some-non-updatable-apex",
4748 ],
4749 }
4750
4751 java_library {
4752 name: "some-platform-lib",
4753 srcs: ["a.java"],
4754 sdk_version: "current",
4755 installable: true,
4756 }
4757
4758 java_library {
4759 name: "some-art-lib",
4760 srcs: ["a.java"],
4761 sdk_version: "current",
4762 apex_available: [
4763 "com.android.art.something",
4764 ],
4765 hostdex: true,
4766 }
4767
4768 apex {
4769 name: "some-updatable-apex",
4770 key: "some-updatable-apex.key",
4771 java_libs: ["some-updatable-apex-lib"],
4772 updatable: true,
4773 min_sdk_version: "current",
4774 }
4775
4776 apex {
4777 name: "some-non-updatable-apex",
4778 key: "some-non-updatable-apex.key",
4779 java_libs: ["some-non-updatable-apex-lib"],
4780 }
4781
4782 apex_key {
4783 name: "some-updatable-apex.key",
4784 }
4785
4786 apex_key {
4787 name: "some-non-updatable-apex.key",
4788 }
4789
4790 apex {
4791 name: "com.android.art.something",
4792 key: "com.android.art.something.key",
4793 java_libs: ["some-art-lib"],
4794 updatable: true,
4795 min_sdk_version: "current",
4796 }
4797
4798 apex_key {
4799 name: "com.android.art.something.key",
4800 }
4801
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004802 filegroup {
4803 name: "some-updatable-apex-file_contexts",
4804 srcs: [
4805 "system/sepolicy/apex/some-updatable-apex-file_contexts",
4806 ],
4807 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004808
4809 filegroup {
4810 name: "some-non-updatable-apex-file_contexts",
4811 srcs: [
4812 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
4813 ],
4814 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004815 `
4816 bp += cc.GatherRequiredDepsForTest(android.Android)
4817 bp += java.GatherRequiredDepsForTest()
4818 bp += dexpreopt.BpToolModulesForTest()
4819
4820 fs := map[string][]byte{
4821 "a.java": nil,
4822 "a.jar": nil,
4823 "build/make/target/product/security": nil,
4824 "apex_manifest.json": nil,
4825 "AndroidManifest.xml": nil,
4826 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004827 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004828 "system/sepolicy/apex/com.android.art.something-file_contexts": nil,
4829 "framework/aidl/a.aidl": nil,
4830 }
4831 cc.GatherRequiredFilesForTest(fs)
4832
4833 ctx := android.NewTestArchContext()
4834 ctx.RegisterModuleType("apex", BundleFactory)
4835 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
4836 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01004837 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004838 cc.RegisterRequiredBuildComponentsForTest(ctx)
4839 java.RegisterJavaBuildComponents(ctx)
4840 java.RegisterSystemModulesBuildComponents(ctx)
4841 java.RegisterAppBuildComponents(ctx)
4842 java.RegisterDexpreoptBootJarsComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004843 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
4844 ctx.PreDepsMutators(RegisterPreDepsMutators)
4845 ctx.PostDepsMutators(RegisterPostDepsMutators)
4846
4847 config := android.TestArchConfig(buildDir, nil, bp, fs)
4848 ctx.Register(config)
4849
4850 _ = dexpreopt.GlobalSoongConfigForTests(config)
4851 dexpreopt.RegisterToolModulesForTest(ctx)
4852 pathCtx := android.PathContextForTesting(config)
4853 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
4854 transformDexpreoptConfig(dexpreoptConfig)
4855 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
4856
4857 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
4858 android.FailIfErrored(t, errs)
4859
4860 _, errs = ctx.PrepareBuildActions(config)
4861 if errmsg == "" {
4862 android.FailIfErrored(t, errs)
4863 } else if len(errs) > 0 {
4864 android.FailIfNoMatchingErrors(t, errmsg, errs)
4865 return
4866 } else {
4867 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
4868 }
4869}
4870
Jooyung Han548640b2020-04-27 12:10:30 +09004871func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
4872 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
4873 apex {
4874 name: "myapex",
4875 key: "myapex.key",
4876 updatable: true,
4877 }
4878
4879 apex_key {
4880 name: "myapex.key",
4881 public_key: "testkey.avbpubkey",
4882 private_key: "testkey.pem",
4883 }
4884 `)
4885}
4886
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004887func TestNoUpdatableJarsInBootImage(t *testing.T) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004888
4889 var error string
4890 var transform func(*dexpreopt.GlobalConfig)
4891
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004892 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
4893 transform = func(config *dexpreopt.GlobalConfig) {
4894 config.ArtApexJars = []string{"com.android.art.something:some-art-lib"}
4895 }
4896 testNoUpdatableJarsInBootImage(t, "", transform)
4897 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004898
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004899 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
4900 error = "module 'some-art-lib' from updatable apex 'com.android.art.something' is not allowed in the framework boot image"
4901 transform = func(config *dexpreopt.GlobalConfig) {
4902 config.BootJars = []string{"com.android.art.something:some-art-lib"}
4903 }
4904 testNoUpdatableJarsInBootImage(t, error, transform)
4905 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004906
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004907 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
4908 error = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the ART boot image"
4909 transform = func(config *dexpreopt.GlobalConfig) {
4910 config.ArtApexJars = []string{"some-updatable-apex:some-updatable-apex-lib"}
4911 }
4912 testNoUpdatableJarsInBootImage(t, error, transform)
4913 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004914
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004915 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
4916 error = "module 'some-non-updatable-apex-lib' is not allowed in the ART boot image"
4917 transform = func(config *dexpreopt.GlobalConfig) {
4918 config.ArtApexJars = []string{"some-non-updatable-apex:some-non-updatable-apex-lib"}
4919 }
4920 testNoUpdatableJarsInBootImage(t, error, transform)
4921 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004922
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004923 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
4924 error = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the framework boot image"
4925 transform = func(config *dexpreopt.GlobalConfig) {
4926 config.BootJars = []string{"some-updatable-apex:some-updatable-apex-lib"}
4927 }
4928 testNoUpdatableJarsInBootImage(t, error, transform)
4929 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004930
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004931 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
4932 transform = func(config *dexpreopt.GlobalConfig) {
4933 config.BootJars = []string{"some-non-updatable-apex:some-non-updatable-apex-lib"}
4934 }
4935 testNoUpdatableJarsInBootImage(t, "", transform)
4936 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004937
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004938 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
4939 error = "failed to find a dex jar path for module 'nonexistent'"
4940 transform = func(config *dexpreopt.GlobalConfig) {
4941 config.ArtApexJars = []string{"platform:nonexistent"}
4942 }
4943 testNoUpdatableJarsInBootImage(t, error, transform)
4944 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004945
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004946 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
4947 error = "failed to find a dex jar path for module 'nonexistent'"
4948 transform = func(config *dexpreopt.GlobalConfig) {
4949 config.BootJars = []string{"platform:nonexistent"}
4950 }
4951 testNoUpdatableJarsInBootImage(t, error, transform)
4952 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004953
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004954 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
4955 error = "module 'some-platform-lib' is not allowed in the ART boot image"
4956 transform = func(config *dexpreopt.GlobalConfig) {
4957 config.ArtApexJars = []string{"platform:some-platform-lib"}
4958 }
4959 testNoUpdatableJarsInBootImage(t, error, transform)
4960 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004961
Ulya Trafimovich7caef202020-05-19 12:00:52 +01004962 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
4963 transform = func(config *dexpreopt.GlobalConfig) {
4964 config.BootJars = []string{"platform:some-platform-lib"}
4965 }
4966 testNoUpdatableJarsInBootImage(t, "", transform)
4967 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004968}
4969
Jiyong Park62304bb2020-04-13 16:19:48 +09004970func TestTestFor(t *testing.T) {
4971 ctx, _ := testApex(t, `
4972 apex {
4973 name: "myapex",
4974 key: "myapex.key",
4975 native_shared_libs: ["mylib", "myprivlib"],
4976 }
4977
4978 apex_key {
4979 name: "myapex.key",
4980 public_key: "testkey.avbpubkey",
4981 private_key: "testkey.pem",
4982 }
4983
4984 cc_library {
4985 name: "mylib",
4986 srcs: ["mylib.cpp"],
4987 system_shared_libs: [],
4988 stl: "none",
4989 stubs: {
4990 versions: ["1"],
4991 },
4992 apex_available: ["myapex"],
4993 }
4994
4995 cc_library {
4996 name: "myprivlib",
4997 srcs: ["mylib.cpp"],
4998 system_shared_libs: [],
4999 stl: "none",
5000 apex_available: ["myapex"],
5001 }
5002
5003
5004 cc_test {
5005 name: "mytest",
5006 gtest: false,
5007 srcs: ["mylib.cpp"],
5008 system_shared_libs: [],
5009 stl: "none",
5010 shared_libs: ["mylib", "myprivlib"],
5011 test_for: ["myapex"]
5012 }
5013 `)
5014
5015 // the test 'mytest' is a test for the apex, therefore is linked to the
5016 // actual implementation of mylib instead of its stub.
5017 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
5018 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
5019 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
5020}
5021
Jaewoong Jungfa00c062020-05-14 14:15:24 -07005022// TODO(jungjw): Move this to proptools
5023func intPtr(i int) *int {
5024 return &i
5025}
5026
5027func TestApexSet(t *testing.T) {
5028 ctx, config := testApex(t, `
5029 apex_set {
5030 name: "myapex",
5031 set: "myapex.apks",
5032 filename: "foo_v2.apex",
5033 overrides: ["foo"],
5034 }
5035 `, func(fs map[string][]byte, config android.Config) {
5036 config.TestProductVariables.Platform_sdk_version = intPtr(30)
5037 config.TestProductVariables.DeviceArch = proptools.StringPtr("arm")
5038 config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm64")
5039 })
5040
5041 m := ctx.ModuleForTests("myapex", "android_common")
5042
5043 // Check extract_apks tool parameters.
5044 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
5045 actual := extractedApex.Args["abis"]
5046 expected := "ARMEABI_V7A,ARM64_V8A"
5047 if actual != expected {
5048 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
5049 }
5050 actual = extractedApex.Args["sdk-version"]
5051 expected = "30"
5052 if actual != expected {
5053 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
5054 }
5055
5056 a := m.Module().(*ApexSet)
5057 expectedOverrides := []string{"foo"}
5058 actualOverrides := android.AndroidMkEntriesForTest(t, config, "", a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
5059 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
5060 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
5061 }
5062}
5063
Jiyong Park7d95a512020-05-10 15:16:24 +09005064func TestNoStaticLinkingToStubsLib(t *testing.T) {
5065 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
5066 apex {
5067 name: "myapex",
5068 key: "myapex.key",
5069 native_shared_libs: ["mylib"],
5070 }
5071
5072 apex_key {
5073 name: "myapex.key",
5074 public_key: "testkey.avbpubkey",
5075 private_key: "testkey.pem",
5076 }
5077
5078 cc_library {
5079 name: "mylib",
5080 srcs: ["mylib.cpp"],
5081 static_libs: ["otherlib"],
5082 system_shared_libs: [],
5083 stl: "none",
5084 apex_available: [ "myapex" ],
5085 }
5086
5087 cc_library {
5088 name: "otherlib",
5089 srcs: ["mylib.cpp"],
5090 system_shared_libs: [],
5091 stl: "none",
5092 stubs: {
5093 versions: ["1", "2", "3"],
5094 },
5095 apex_available: [ "myapex" ],
5096 }
5097 `)
5098}
5099
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07005100func TestMain(m *testing.M) {
5101 run := func() int {
5102 setUp()
5103 defer tearDown()
5104
5105 return m.Run()
5106 }
5107
5108 os.Exit(run())
5109}