blob: 7f882e51360d127e9eb615374ffa2fd3f0f61042 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000030 "android/soong/dexpreopt"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090031 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090032)
33
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070034var buildDir string
35
Jooyung Hand3639552019-08-09 12:57:43 +090036// names returns name list from white space separated string
37func names(s string) (ns []string) {
38 for _, n := range strings.Split(s, " ") {
39 if len(n) > 0 {
40 ns = append(ns, n)
41 }
42 }
43 return
44}
45
Jooyung Han344d5432019-08-23 11:17:39 +090046func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
47 t.Helper()
48 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090049 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
50 if len(errs) > 0 {
51 android.FailIfNoMatchingErrors(t, pattern, errs)
52 return
53 }
54 _, errs = ctx.PrepareBuildActions(config)
55 if len(errs) > 0 {
56 android.FailIfNoMatchingErrors(t, pattern, errs)
57 return
58 }
59
60 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
61}
62
Jooyung Han344d5432019-08-23 11:17:39 +090063func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
64 t.Helper()
65 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090066 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
67 android.FailIfErrored(t, errs)
68 _, errs = ctx.PrepareBuildActions(config)
69 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070070 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090071}
72
Jooyung Han344d5432019-08-23 11:17:39 +090073type testCustomizer func(fs map[string][]byte, config android.Config)
74
75func withFiles(files map[string][]byte) testCustomizer {
76 return func(fs map[string][]byte, config android.Config) {
77 for k, v := range files {
78 fs[k] = v
79 }
80 }
81}
82
83func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
84 return func(fs map[string][]byte, config android.Config) {
85 for k, v := range targets {
86 config.Targets[k] = v
87 }
88 }
89}
90
Jooyung Han35155c42020-02-06 17:33:20 +090091// withNativeBridgeTargets sets configuration with targets including:
92// - X86_64 (primary)
93// - X86 (secondary)
94// - Arm64 on X86_64 (native bridge)
95// - Arm on X86 (native bridge)
96func withNativeBridgeEnabled(fs map[string][]byte, config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107}
108
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900109func withManifestPackageNameOverrides(specs []string) testCustomizer {
110 return func(fs map[string][]byte, config android.Config) {
111 config.TestProductVariables.ManifestPackageNameOverrides = specs
112 }
113}
114
Jooyung Han31c470b2019-10-18 16:26:59 +0900115func withBinder32bit(fs map[string][]byte, config android.Config) {
116 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
117}
118
Jiyong Park7cd10e32020-01-14 09:22:18 +0900119func withUnbundledBuild(fs map[string][]byte, config android.Config) {
120 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
121}
122
Jooyung Han344d5432019-08-23 11:17:39 +0900123func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +0900124 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900125
126 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900127 filegroup {
128 name: "myapex-file_contexts",
129 srcs: [
130 "system/sepolicy/apex/myapex-file_contexts",
131 ],
132 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900133 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800134
Colin Crossf9aabd72020-02-15 11:29:50 -0800135 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
136
Dario Frenicde2a032019-10-27 00:29:22 +0100137 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900138
Jooyung Han344d5432019-08-23 11:17:39 +0900139 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900140 "a.java": nil,
141 "PrebuiltAppFoo.apk": nil,
142 "PrebuiltAppFooPriv.apk": nil,
143 "build/make/target/product/security": nil,
144 "apex_manifest.json": nil,
145 "AndroidManifest.xml": nil,
146 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900147 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900148 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900149 "system/sepolicy/apex/otherapex-file_contexts": nil,
150 "system/sepolicy/apex/commonapex-file_contexts": nil,
151 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800152 "mylib.cpp": nil,
153 "mylib_common.cpp": nil,
154 "mytest.cpp": nil,
155 "mytest1.cpp": nil,
156 "mytest2.cpp": nil,
157 "mytest3.cpp": nil,
158 "myprebuilt": nil,
159 "my_include": nil,
160 "foo/bar/MyClass.java": nil,
161 "prebuilt.jar": nil,
162 "vendor/foo/devkeys/test.x509.pem": nil,
163 "vendor/foo/devkeys/test.pk8": nil,
164 "testkey.x509.pem": nil,
165 "testkey.pk8": nil,
166 "testkey.override.x509.pem": nil,
167 "testkey.override.pk8": nil,
168 "vendor/foo/devkeys/testkey.avbpubkey": nil,
169 "vendor/foo/devkeys/testkey.pem": nil,
170 "NOTICE": nil,
171 "custom_notice": nil,
Jiyong Park9918e1a2020-03-17 19:16:40 +0900172 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800173 "testkey2.avbpubkey": nil,
174 "testkey2.pem": nil,
175 "myapex-arm64.apex": nil,
176 "myapex-arm.apex": nil,
177 "frameworks/base/api/current.txt": nil,
178 "framework/aidl/a.aidl": nil,
179 "build/make/core/proguard.flags": nil,
180 "build/make/core/proguard_basic_keeps.flags": nil,
181 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900182 }
183
Colin Crossf9aabd72020-02-15 11:29:50 -0800184 cc.GatherRequiredFilesForTest(fs)
185
Jooyung Han344d5432019-08-23 11:17:39 +0900186 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800187 // The fs now needs to be populated before creating the config, call handlers twice
188 // for now, once to get any fs changes, and later after the config was created to
189 // set product variables or targets.
190 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
191 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900192 }
193
Colin Cross98be1bb2019-12-13 20:41:13 -0800194 config := android.TestArchConfig(buildDir, nil, bp, fs)
195 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
196 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
197 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
198 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
199 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
200 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
201
202 for _, handler := range handlers {
203 // The fs now needs to be populated before creating the config, call handlers twice
204 // for now, earlier to get any fs changes, and now after the config was created to
205 // set product variables or targets.
206 tempFS := map[string][]byte{}
207 handler(tempFS, config)
208 }
209
210 ctx := android.NewTestArchContext()
211 ctx.RegisterModuleType("apex", BundleFactory)
212 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
213 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
214 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
215 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
216 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
217 ctx.RegisterModuleType("override_apex", overrideApexFactory)
218
Jooyung Hana57af4a2020-01-23 05:36:59 +0000219 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
220 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
221
Paul Duffin77980a82019-12-19 16:01:36 +0000222 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800223 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800224 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
225 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800226 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000227 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800228 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800229 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000230 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000231 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000232 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900233 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800234
Colin Cross98be1bb2019-12-13 20:41:13 -0800235 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800236 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800237
238 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900239
Jooyung Han5c998b92019-06-27 11:30:33 +0900240 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900241}
242
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700243func setUp() {
244 var err error
245 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900246 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700247 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900248 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900249}
250
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700251func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900252 os.RemoveAll(buildDir)
253}
254
Jooyung Han643adc42020-02-27 13:50:06 +0900255// ensure that 'result' equals 'expected'
256func ensureEquals(t *testing.T, result string, expected string) {
257 t.Helper()
258 if result != expected {
259 t.Errorf("%q != %q", expected, result)
260 }
261}
262
Jiyong Park25fc6a92018-11-18 18:02:45 +0900263// ensure that 'result' contains 'expected'
264func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900265 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900266 if !strings.Contains(result, expected) {
267 t.Errorf("%q is not found in %q", expected, result)
268 }
269}
270
271// ensures that 'result' does not contain 'notExpected'
272func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900273 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274 if strings.Contains(result, notExpected) {
275 t.Errorf("%q is found in %q", notExpected, result)
276 }
277}
278
279func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900280 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281 if !android.InList(expected, result) {
282 t.Errorf("%q is not found in %v", expected, result)
283 }
284}
285
286func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900287 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900288 if android.InList(notExpected, result) {
289 t.Errorf("%q is found in %v", notExpected, result)
290 }
291}
292
Jooyung Hane1633032019-08-01 17:41:43 +0900293func ensureListEmpty(t *testing.T, result []string) {
294 t.Helper()
295 if len(result) > 0 {
296 t.Errorf("%q is expected to be empty", result)
297 }
298}
299
Jiyong Park25fc6a92018-11-18 18:02:45 +0900300// Minimal test
301func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700302 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900303 apex_defaults {
304 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900305 manifest: ":myapex.manifest",
306 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900307 key: "myapex.key",
308 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800309 multilib: {
310 both: {
311 binaries: ["foo",],
312 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900313 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900314 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 }
316
Jiyong Park30ca9372019-02-07 16:27:23 +0900317 apex {
318 name: "myapex",
319 defaults: ["myapex-defaults"],
320 }
321
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322 apex_key {
323 name: "myapex.key",
324 public_key: "testkey.avbpubkey",
325 private_key: "testkey.pem",
326 }
327
Jiyong Park809bb722019-02-13 21:33:49 +0900328 filegroup {
329 name: "myapex.manifest",
330 srcs: ["apex_manifest.json"],
331 }
332
333 filegroup {
334 name: "myapex.androidmanifest",
335 srcs: ["AndroidManifest.xml"],
336 }
337
Jiyong Park25fc6a92018-11-18 18:02:45 +0900338 cc_library {
339 name: "mylib",
340 srcs: ["mylib.cpp"],
341 shared_libs: ["mylib2"],
342 system_shared_libs: [],
343 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000344 // TODO: remove //apex_available:platform
345 apex_available: [
346 "//apex_available:platform",
347 "myapex",
348 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900349 }
350
Alex Light3d673592019-01-18 14:37:31 -0800351 cc_binary {
352 name: "foo",
353 srcs: ["mylib.cpp"],
354 compile_multilib: "both",
355 multilib: {
356 lib32: {
357 suffix: "32",
358 },
359 lib64: {
360 suffix: "64",
361 },
362 },
363 symlinks: ["foo_link_"],
364 symlink_preferred_arch: true,
365 system_shared_libs: [],
366 static_executable: true,
367 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000368 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800369 }
370
Jiyong Park25fc6a92018-11-18 18:02:45 +0900371 cc_library {
372 name: "mylib2",
373 srcs: ["mylib.cpp"],
374 system_shared_libs: [],
375 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900376 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900377 static_libs: ["libstatic"],
378 // TODO: remove //apex_available:platform
379 apex_available: [
380 "//apex_available:platform",
381 "myapex",
382 ],
383 }
384
385 cc_library_static {
386 name: "libstatic",
387 srcs: ["mylib.cpp"],
388 system_shared_libs: [],
389 stl: "none",
390 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000391 // TODO: remove //apex_available:platform
392 apex_available: [
393 "//apex_available:platform",
394 "myapex",
395 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900396 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900397
398 java_library {
399 name: "myjar",
400 srcs: ["foo/bar/MyClass.java"],
401 sdk_version: "none",
402 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900403 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900404 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000405 // TODO: remove //apex_available:platform
406 apex_available: [
407 "//apex_available:platform",
408 "myapex",
409 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900410 }
411
412 java_library {
413 name: "myotherjar",
414 srcs: ["foo/bar/MyClass.java"],
415 sdk_version: "none",
416 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900417 // TODO: remove //apex_available:platform
418 apex_available: [
419 "//apex_available:platform",
420 "myapex",
421 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900422 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900423
424 java_library {
425 name: "mysharedjar",
426 srcs: ["foo/bar/MyClass.java"],
427 sdk_version: "none",
428 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900429 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900430 `)
431
Sundong Ahnabb64432019-10-22 13:58:29 +0900432 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900433
434 optFlags := apexRule.Args["opt_flags"]
435 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700436 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900437 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900438
Jiyong Park25fc6a92018-11-18 18:02:45 +0900439 copyCmds := apexRule.Args["copy_commands"]
440
441 // Ensure that main rule creates an output
442 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
443
444 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800445 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900446 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900447
448 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800449 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900450 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900451
452 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800453 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
454 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900455 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
456 // .. but not for java libs
457 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900458 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800459
Colin Cross7113d202019-11-20 16:39:12 -0800460 // Ensure that the platform variant ends with _shared or _common
461 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
462 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900463 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
464 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900465 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
466
467 // Ensure that dynamic dependency to java libs are not included
468 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800469
470 // Ensure that all symlinks are present.
471 found_foo_link_64 := false
472 found_foo := false
473 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900474 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800475 if strings.HasSuffix(cmd, "bin/foo") {
476 found_foo = true
477 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
478 found_foo_link_64 = true
479 }
480 }
481 }
482 good := found_foo && found_foo_link_64
483 if !good {
484 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
485 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900486
Sundong Ahnabb64432019-10-22 13:58:29 +0900487 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700488 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900489 if len(noticeInputs) != 3 {
490 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900491 }
492 ensureListContains(t, noticeInputs, "NOTICE")
493 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900494 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900495
496 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900497 ensureListContains(t, depsInfo, "myjar <- myapex")
498 ensureListContains(t, depsInfo, "mylib <- myapex")
499 ensureListContains(t, depsInfo, "mylib2 <- mylib")
500 ensureListContains(t, depsInfo, "myotherjar <- myjar")
501 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800502}
503
Jooyung Hanf21c7972019-12-16 22:32:06 +0900504func TestDefaults(t *testing.T) {
505 ctx, _ := testApex(t, `
506 apex_defaults {
507 name: "myapex-defaults",
508 key: "myapex.key",
509 prebuilts: ["myetc"],
510 native_shared_libs: ["mylib"],
511 java_libs: ["myjar"],
512 apps: ["AppFoo"],
513 }
514
515 prebuilt_etc {
516 name: "myetc",
517 src: "myprebuilt",
518 }
519
520 apex {
521 name: "myapex",
522 defaults: ["myapex-defaults"],
523 }
524
525 apex_key {
526 name: "myapex.key",
527 public_key: "testkey.avbpubkey",
528 private_key: "testkey.pem",
529 }
530
531 cc_library {
532 name: "mylib",
533 system_shared_libs: [],
534 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000535 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900536 }
537
538 java_library {
539 name: "myjar",
540 srcs: ["foo/bar/MyClass.java"],
541 sdk_version: "none",
542 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000543 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900544 }
545
546 android_app {
547 name: "AppFoo",
548 srcs: ["foo/bar/MyClass.java"],
549 sdk_version: "none",
550 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000551 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900552 }
553 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000554 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900555 "etc/myetc",
556 "javalib/myjar.jar",
557 "lib64/mylib.so",
558 "app/AppFoo/AppFoo.apk",
559 })
560}
561
Jooyung Han01a3ee22019-11-02 02:52:25 +0900562func TestApexManifest(t *testing.T) {
563 ctx, _ := testApex(t, `
564 apex {
565 name: "myapex",
566 key: "myapex.key",
567 }
568
569 apex_key {
570 name: "myapex.key",
571 public_key: "testkey.avbpubkey",
572 private_key: "testkey.pem",
573 }
574 `)
575
576 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900577 args := module.Rule("apexRule").Args
578 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
579 t.Error("manifest should be apex_manifest.pb, but " + manifest)
580 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900581}
582
Alex Light5098a612018-11-29 17:12:15 -0800583func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700584 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800585 apex {
586 name: "myapex",
587 key: "myapex.key",
588 payload_type: "zip",
589 native_shared_libs: ["mylib"],
590 }
591
592 apex_key {
593 name: "myapex.key",
594 public_key: "testkey.avbpubkey",
595 private_key: "testkey.pem",
596 }
597
598 cc_library {
599 name: "mylib",
600 srcs: ["mylib.cpp"],
601 shared_libs: ["mylib2"],
602 system_shared_libs: [],
603 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000604 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800605 }
606
607 cc_library {
608 name: "mylib2",
609 srcs: ["mylib.cpp"],
610 system_shared_libs: [],
611 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000612 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800613 }
614 `)
615
Sundong Ahnabb64432019-10-22 13:58:29 +0900616 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800617 copyCmds := zipApexRule.Args["copy_commands"]
618
619 // Ensure that main rule creates an output
620 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
621
622 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800623 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800624
625 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800626 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800627
628 // Ensure that both direct and indirect deps are copied into apex
629 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
630 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900631}
632
633func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700634 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900635 apex {
636 name: "myapex",
637 key: "myapex.key",
638 native_shared_libs: ["mylib", "mylib3"],
639 }
640
641 apex_key {
642 name: "myapex.key",
643 public_key: "testkey.avbpubkey",
644 private_key: "testkey.pem",
645 }
646
647 cc_library {
648 name: "mylib",
649 srcs: ["mylib.cpp"],
650 shared_libs: ["mylib2", "mylib3"],
651 system_shared_libs: [],
652 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000653 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900654 }
655
656 cc_library {
657 name: "mylib2",
658 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900659 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900660 system_shared_libs: [],
661 stl: "none",
662 stubs: {
663 versions: ["1", "2", "3"],
664 },
665 }
666
667 cc_library {
668 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900669 srcs: ["mylib.cpp"],
670 shared_libs: ["mylib4"],
671 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900672 stl: "none",
673 stubs: {
674 versions: ["10", "11", "12"],
675 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000676 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900677 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900678
679 cc_library {
680 name: "mylib4",
681 srcs: ["mylib.cpp"],
682 system_shared_libs: [],
683 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000684 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900685 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900686 `)
687
Sundong Ahnabb64432019-10-22 13:58:29 +0900688 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900689 copyCmds := apexRule.Args["copy_commands"]
690
691 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800692 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900693
694 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800695 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900696
697 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800698 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900699
Colin Cross7113d202019-11-20 16:39:12 -0800700 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900701
702 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900703 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900704 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900705 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900706
707 // 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 -0800708 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900709 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800710 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900711
712 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900713 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900714 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900715
716 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900717 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900718
Jooyung Hana57af4a2020-01-23 05:36:59 +0000719 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900720 "lib64/mylib.so",
721 "lib64/mylib3.so",
722 "lib64/mylib4.so",
723 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900724}
725
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900726func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700727 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900728 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900729 name: "myapex2",
730 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900731 native_shared_libs: ["mylib"],
732 }
733
734 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900735 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900736 public_key: "testkey.avbpubkey",
737 private_key: "testkey.pem",
738 }
739
740 cc_library {
741 name: "mylib",
742 srcs: ["mylib.cpp"],
743 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900744 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900745 system_shared_libs: [],
746 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000747 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900748 }
749
750 cc_library {
751 name: "libfoo",
752 srcs: ["mylib.cpp"],
753 shared_libs: ["libbar"],
754 system_shared_libs: [],
755 stl: "none",
756 stubs: {
757 versions: ["10", "20", "30"],
758 },
759 }
760
761 cc_library {
762 name: "libbar",
763 srcs: ["mylib.cpp"],
764 system_shared_libs: [],
765 stl: "none",
766 }
767
Jiyong Park678c8812020-02-07 17:25:49 +0900768 cc_library_static {
769 name: "libbaz",
770 srcs: ["mylib.cpp"],
771 system_shared_libs: [],
772 stl: "none",
773 apex_available: [ "myapex2" ],
774 }
775
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900776 `)
777
Jiyong Park83dc74b2020-01-14 18:38:44 +0900778 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900779 copyCmds := apexRule.Args["copy_commands"]
780
781 // Ensure that direct non-stubs dep is always included
782 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
783
784 // Ensure that indirect stubs dep is not included
785 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
786
787 // Ensure that dependency of stubs is not included
788 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
789
Jiyong Park83dc74b2020-01-14 18:38:44 +0900790 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900791
792 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900793 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900794 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900795 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900796
Jiyong Park3ff16992019-12-27 14:11:47 +0900797 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900798
799 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
800 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900801
802 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900803
804 ensureListContains(t, depsInfo, "mylib <- myapex2")
805 ensureListContains(t, depsInfo, "libbaz <- mylib")
806 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900807}
808
Jooyung Hand3639552019-08-09 12:57:43 +0900809func TestApexWithRuntimeLibsDependency(t *testing.T) {
810 /*
811 myapex
812 |
813 v (runtime_libs)
814 mylib ------+------> libfoo [provides stub]
815 |
816 `------> libbar
817 */
818 ctx, _ := testApex(t, `
819 apex {
820 name: "myapex",
821 key: "myapex.key",
822 native_shared_libs: ["mylib"],
823 }
824
825 apex_key {
826 name: "myapex.key",
827 public_key: "testkey.avbpubkey",
828 private_key: "testkey.pem",
829 }
830
831 cc_library {
832 name: "mylib",
833 srcs: ["mylib.cpp"],
834 runtime_libs: ["libfoo", "libbar"],
835 system_shared_libs: [],
836 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000837 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900838 }
839
840 cc_library {
841 name: "libfoo",
842 srcs: ["mylib.cpp"],
843 system_shared_libs: [],
844 stl: "none",
845 stubs: {
846 versions: ["10", "20", "30"],
847 },
848 }
849
850 cc_library {
851 name: "libbar",
852 srcs: ["mylib.cpp"],
853 system_shared_libs: [],
854 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000855 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900856 }
857
858 `)
859
Sundong Ahnabb64432019-10-22 13:58:29 +0900860 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900861 copyCmds := apexRule.Args["copy_commands"]
862
863 // Ensure that direct non-stubs dep is always included
864 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
865
866 // Ensure that indirect stubs dep is not included
867 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
868
869 // Ensure that runtime_libs dep in included
870 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
871
Sundong Ahnabb64432019-10-22 13:58:29 +0900872 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900873 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
874 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900875
876}
877
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000878func TestApexDependencyToLLNDK(t *testing.T) {
879 ctx, _ := testApex(t, `
880 apex {
881 name: "myapex",
882 key: "myapex.key",
883 use_vendor: true,
884 native_shared_libs: ["mylib"],
885 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900886
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000887 apex_key {
888 name: "myapex.key",
889 public_key: "testkey.avbpubkey",
890 private_key: "testkey.pem",
891 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900892
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000893 cc_library {
894 name: "mylib",
895 srcs: ["mylib.cpp"],
896 vendor_available: true,
897 shared_libs: ["libbar"],
898 system_shared_libs: [],
899 stl: "none",
900 apex_available: [ "myapex" ],
901 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900902
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000903 cc_library {
904 name: "libbar",
905 srcs: ["mylib.cpp"],
906 system_shared_libs: [],
907 stl: "none",
908 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900909
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000910 llndk_library {
911 name: "libbar",
912 symbol_file: "",
913 }
914 `, func(fs map[string][]byte, config android.Config) {
915 setUseVendorWhitelistForTest(config, []string{"myapex"})
916 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900917
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000918 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
919 copyCmds := apexRule.Args["copy_commands"]
Jooyung Han9c80bae2019-08-20 17:30:57 +0900920
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000921 // Ensure that LLNDK dep is not included
922 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900923
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000924 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
925 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900926
Jooyung Hanbacf34d2020-03-21 13:58:19 +0000927 // Ensure that LLNDK dep is required
928 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
929
Jooyung Han9c80bae2019-08-20 17:30:57 +0900930}
931
Jiyong Park25fc6a92018-11-18 18:02:45 +0900932func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700933 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900934 apex {
935 name: "myapex",
936 key: "myapex.key",
937 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
938 }
939
940 apex_key {
941 name: "myapex.key",
942 public_key: "testkey.avbpubkey",
943 private_key: "testkey.pem",
944 }
945
946 cc_library {
947 name: "mylib",
948 srcs: ["mylib.cpp"],
949 shared_libs: ["libdl#27"],
950 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000951 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900952 }
953
954 cc_library_shared {
955 name: "mylib_shared",
956 srcs: ["mylib.cpp"],
957 shared_libs: ["libdl#27"],
958 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000959 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900960 }
961
962 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900963 name: "libBootstrap",
964 srcs: ["mylib.cpp"],
965 stl: "none",
966 bootstrap: true,
967 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900968 `)
969
Sundong Ahnabb64432019-10-22 13:58:29 +0900970 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900971 copyCmds := apexRule.Args["copy_commands"]
972
973 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800974 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900975 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
976 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900977
978 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900979 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980
Colin Cross7113d202019-11-20 16:39:12 -0800981 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
982 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
983 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900984
985 // For dependency to libc
986 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900987 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900988 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900989 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900990 // ... Cflags from stub is correctly exported to mylib
991 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
992 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
993
994 // For dependency to libm
995 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800996 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900997 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900998 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900999 // ... and is not compiling with the stub
1000 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1001 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1002
1003 // For dependency to libdl
1004 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001005 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001006 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001007 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1008 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001009 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001010 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011 // ... Cflags from stub is correctly exported to mylib
1012 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1013 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001014
1015 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001016 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1017 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1018 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1019 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001020}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001021
Jooyung Han03b51852020-02-26 22:45:42 +09001022func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
1023 // there are three links between liba --> libz
1024 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
1025 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
1026 // 3) (platform) -> liba -> libz : this should be non-stub link
1027 ctx, _ := testApex(t, `
1028 apex {
1029 name: "myapex",
1030 key: "myapex.key",
1031 native_shared_libs: ["libx"],
1032 min_sdk_version: "2",
1033 }
1034
1035 apex {
1036 name: "otherapex",
1037 key: "myapex.key",
1038 native_shared_libs: ["liby"],
1039 min_sdk_version: "3",
1040 }
1041
1042 apex_key {
1043 name: "myapex.key",
1044 public_key: "testkey.avbpubkey",
1045 private_key: "testkey.pem",
1046 }
1047
1048 cc_library {
1049 name: "libx",
1050 shared_libs: ["liba"],
1051 system_shared_libs: [],
1052 stl: "none",
1053 apex_available: [ "myapex" ],
1054 }
1055
1056 cc_library {
1057 name: "liby",
1058 shared_libs: ["liba"],
1059 system_shared_libs: [],
1060 stl: "none",
1061 apex_available: [ "otherapex" ],
1062 }
1063
1064 cc_library {
1065 name: "liba",
1066 shared_libs: ["libz"],
1067 system_shared_libs: [],
1068 stl: "none",
1069 apex_available: [
1070 "//apex_available:anyapex",
1071 "//apex_available:platform",
1072 ],
1073 }
1074
1075 cc_library {
1076 name: "libz",
1077 system_shared_libs: [],
1078 stl: "none",
1079 stubs: {
1080 versions: ["1", "3"],
1081 },
1082 }
1083 `, withUnbundledBuild)
1084
1085 expectLink := func(from, from_variant, to, to_variant string) {
1086 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1087 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1088 }
1089 expectNoLink := func(from, from_variant, to, to_variant string) {
1090 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1091 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1092 }
1093 // platform liba is linked to non-stub version
1094 expectLink("liba", "shared", "libz", "shared")
1095 // liba in myapex is linked to #1
1096 expectLink("liba", "shared_myapex", "libz", "shared_1")
1097 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1098 expectNoLink("liba", "shared_myapex", "libz", "shared")
1099 // liba in otherapex is linked to #3
1100 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1101 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1102 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1103}
1104
1105func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1106 ctx, _ := testApex(t, `
1107 apex {
1108 name: "myapex",
1109 key: "myapex.key",
1110 native_shared_libs: ["libx"],
1111 }
1112
1113 apex_key {
1114 name: "myapex.key",
1115 public_key: "testkey.avbpubkey",
1116 private_key: "testkey.pem",
1117 }
1118
1119 cc_library {
1120 name: "libx",
1121 shared_libs: ["libz"],
1122 system_shared_libs: [],
1123 stl: "none",
1124 apex_available: [ "myapex" ],
1125 }
1126
1127 cc_library {
1128 name: "libz",
1129 system_shared_libs: [],
1130 stl: "none",
1131 stubs: {
1132 versions: ["1", "2"],
1133 },
1134 }
1135 `)
1136
1137 expectLink := func(from, from_variant, to, to_variant string) {
1138 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1139 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1140 }
1141 expectNoLink := func(from, from_variant, to, to_variant string) {
1142 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1143 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1144 }
1145 expectLink("libx", "shared_myapex", "libz", "shared_2")
1146 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1147 expectNoLink("libx", "shared_myapex", "libz", "shared")
1148}
1149
1150func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1151 ctx, _ := testApex(t, `
1152 apex {
1153 name: "myapex",
1154 key: "myapex.key",
1155 native_shared_libs: ["libx"],
1156 }
1157
1158 apex_key {
1159 name: "myapex.key",
1160 public_key: "testkey.avbpubkey",
1161 private_key: "testkey.pem",
1162 }
1163
1164 cc_library {
1165 name: "libx",
1166 system_shared_libs: [],
1167 stl: "none",
1168 apex_available: [ "myapex" ],
1169 stubs: {
1170 versions: ["1", "2"],
1171 },
1172 }
1173
1174 cc_library {
1175 name: "libz",
1176 shared_libs: ["libx"],
1177 system_shared_libs: [],
1178 stl: "none",
1179 }
1180 `)
1181
1182 expectLink := func(from, from_variant, to, to_variant string) {
1183 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1184 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1185 }
1186 expectNoLink := func(from, from_variant, to, to_variant string) {
1187 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1188 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1189 }
1190 expectLink("libz", "shared", "libx", "shared_2")
1191 expectNoLink("libz", "shared", "libz", "shared_1")
1192 expectNoLink("libz", "shared", "libz", "shared")
1193}
1194
1195func TestQApexesUseLatestStubsInBundledBuilds(t *testing.T) {
1196 ctx, _ := testApex(t, `
1197 apex {
1198 name: "myapex",
1199 key: "myapex.key",
1200 native_shared_libs: ["libx"],
1201 min_sdk_version: "29",
1202 }
1203
1204 apex_key {
1205 name: "myapex.key",
1206 public_key: "testkey.avbpubkey",
1207 private_key: "testkey.pem",
1208 }
1209
1210 cc_library {
1211 name: "libx",
1212 shared_libs: ["libbar"],
1213 apex_available: [ "myapex" ],
1214 }
1215
1216 cc_library {
1217 name: "libbar",
1218 stubs: {
1219 versions: ["29", "30"],
1220 },
1221 }
1222 `)
1223 expectLink := func(from, from_variant, to, to_variant string) {
1224 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1225 libFlags := ld.Args["libFlags"]
1226 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1227 }
1228 expectLink("libx", "shared_myapex", "libbar", "shared_30")
1229}
1230
1231func TestQTargetApexUseStaticUnwinder(t *testing.T) {
1232 ctx, _ := testApex(t, `
1233 apex {
1234 name: "myapex",
1235 key: "myapex.key",
1236 native_shared_libs: ["libx"],
1237 min_sdk_version: "29",
1238 }
1239
1240 apex_key {
1241 name: "myapex.key",
1242 public_key: "testkey.avbpubkey",
1243 private_key: "testkey.pem",
1244 }
1245
1246 cc_library {
1247 name: "libx",
1248 apex_available: [ "myapex" ],
1249 }
1250
1251 `, withUnbundledBuild)
1252
1253 // ensure apex variant of c++ is linked with static unwinder
1254 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1255 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1256 // note that platform variant is not.
1257 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1258 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Hanbacf34d2020-03-21 13:58:19 +00001259
1260 libFlags := ctx.ModuleForTests("libx", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1261 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_myapex/libc++.so")
1262 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_29/libc.so") // min_sdk_version applied
Jooyung Han03b51852020-02-26 22:45:42 +09001263}
1264
1265func TestInvalidMinSdkVersion(t *testing.T) {
1266 testApexError(t, `"libz" .*: min_sdk_version is set 29.*`, `
1267 apex {
1268 name: "myapex",
1269 key: "myapex.key",
1270 native_shared_libs: ["libx"],
1271 min_sdk_version: "29",
1272 }
1273
1274 apex_key {
1275 name: "myapex.key",
1276 public_key: "testkey.avbpubkey",
1277 private_key: "testkey.pem",
1278 }
1279
1280 cc_library {
1281 name: "libx",
1282 shared_libs: ["libz"],
1283 system_shared_libs: [],
1284 stl: "none",
1285 apex_available: [ "myapex" ],
1286 }
1287
1288 cc_library {
1289 name: "libz",
1290 system_shared_libs: [],
1291 stl: "none",
1292 stubs: {
1293 versions: ["30"],
1294 },
1295 }
1296 `, withUnbundledBuild)
1297
1298 testApexError(t, `"myapex" .*: min_sdk_version: should be .*`, `
1299 apex {
1300 name: "myapex",
1301 key: "myapex.key",
1302 min_sdk_version: "R",
1303 }
1304
1305 apex_key {
1306 name: "myapex.key",
1307 public_key: "testkey.avbpubkey",
1308 private_key: "testkey.pem",
1309 }
1310 `)
1311}
1312
Jiyong Park7c2ee712018-12-07 00:42:25 +09001313func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001314 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001315 apex {
1316 name: "myapex",
1317 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001318 native_shared_libs: ["mylib"],
1319 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001320 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001321 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001322 }
1323
1324 apex_key {
1325 name: "myapex.key",
1326 public_key: "testkey.avbpubkey",
1327 private_key: "testkey.pem",
1328 }
1329
1330 prebuilt_etc {
1331 name: "myetc",
1332 src: "myprebuilt",
1333 sub_dir: "foo/bar",
1334 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001335
1336 cc_library {
1337 name: "mylib",
1338 srcs: ["mylib.cpp"],
1339 relative_install_path: "foo/bar",
1340 system_shared_libs: [],
1341 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001342 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001343 }
1344
1345 cc_binary {
1346 name: "mybin",
1347 srcs: ["mylib.cpp"],
1348 relative_install_path: "foo/bar",
1349 system_shared_libs: [],
1350 static_executable: true,
1351 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001352 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001353 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001354 `)
1355
Sundong Ahnabb64432019-10-22 13:58:29 +09001356 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001357 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1358
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001359 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001360 ensureListContains(t, dirs, "etc")
1361 ensureListContains(t, dirs, "etc/foo")
1362 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001363 ensureListContains(t, dirs, "lib64")
1364 ensureListContains(t, dirs, "lib64/foo")
1365 ensureListContains(t, dirs, "lib64/foo/bar")
1366 ensureListContains(t, dirs, "lib")
1367 ensureListContains(t, dirs, "lib/foo")
1368 ensureListContains(t, dirs, "lib/foo/bar")
1369
Jiyong Parkbd13e442019-03-15 18:10:35 +09001370 ensureListContains(t, dirs, "bin")
1371 ensureListContains(t, dirs, "bin/foo")
1372 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001373}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001374
Jooyung Han35155c42020-02-06 17:33:20 +09001375func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
1376 ctx, _ := testApex(t, `
1377 apex {
1378 name: "myapex",
1379 key: "myapex.key",
1380 multilib: {
1381 both: {
1382 native_shared_libs: ["mylib"],
1383 binaries: ["mybin"],
1384 },
1385 },
1386 compile_multilib: "both",
1387 native_bridge_supported: true,
1388 }
1389
1390 apex_key {
1391 name: "myapex.key",
1392 public_key: "testkey.avbpubkey",
1393 private_key: "testkey.pem",
1394 }
1395
1396 cc_library {
1397 name: "mylib",
1398 relative_install_path: "foo/bar",
1399 system_shared_libs: [],
1400 stl: "none",
1401 apex_available: [ "myapex" ],
1402 native_bridge_supported: true,
1403 }
1404
1405 cc_binary {
1406 name: "mybin",
1407 relative_install_path: "foo/bar",
1408 system_shared_libs: [],
1409 static_executable: true,
1410 stl: "none",
1411 apex_available: [ "myapex" ],
1412 native_bridge_supported: true,
1413 compile_multilib: "both", // default is "first" for binary
1414 multilib: {
1415 lib64: {
1416 suffix: "64",
1417 },
1418 },
1419 }
1420 `, withNativeBridgeEnabled)
1421 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1422 "bin/foo/bar/mybin",
1423 "bin/foo/bar/mybin64",
1424 "bin/arm/foo/bar/mybin",
1425 "bin/arm64/foo/bar/mybin64",
1426 "lib/foo/bar/mylib.so",
1427 "lib/arm/foo/bar/mylib.so",
1428 "lib64/foo/bar/mylib.so",
1429 "lib64/arm64/foo/bar/mylib.so",
1430 })
1431}
1432
Jiyong Parkda6eb592018-12-19 17:12:36 +09001433func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001434 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001435 apex {
1436 name: "myapex",
1437 key: "myapex.key",
1438 native_shared_libs: ["mylib"],
1439 use_vendor: true,
1440 }
1441
1442 apex_key {
1443 name: "myapex.key",
1444 public_key: "testkey.avbpubkey",
1445 private_key: "testkey.pem",
1446 }
1447
1448 cc_library {
1449 name: "mylib",
1450 srcs: ["mylib.cpp"],
1451 shared_libs: ["mylib2"],
1452 system_shared_libs: [],
1453 vendor_available: true,
1454 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001455 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001456 }
1457
1458 cc_library {
1459 name: "mylib2",
1460 srcs: ["mylib.cpp"],
1461 system_shared_libs: [],
1462 vendor_available: true,
1463 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001464 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001465 }
Jooyung Handc782442019-11-01 03:14:38 +09001466 `, func(fs map[string][]byte, config android.Config) {
1467 setUseVendorWhitelistForTest(config, []string{"myapex"})
1468 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001469
1470 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001471 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001472 for _, implicit := range i.Implicits {
1473 inputsList = append(inputsList, implicit.String())
1474 }
1475 }
1476 inputsString := strings.Join(inputsList, " ")
1477
1478 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001479 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1480 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001481
1482 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001483 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1484 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001485}
Jiyong Park16e91a02018-12-20 18:18:08 +09001486
Jooyung Handc782442019-11-01 03:14:38 +09001487func TestUseVendorRestriction(t *testing.T) {
1488 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1489 apex {
1490 name: "myapex",
1491 key: "myapex.key",
1492 use_vendor: true,
1493 }
1494 apex_key {
1495 name: "myapex.key",
1496 public_key: "testkey.avbpubkey",
1497 private_key: "testkey.pem",
1498 }
1499 `, func(fs map[string][]byte, config android.Config) {
1500 setUseVendorWhitelistForTest(config, []string{""})
1501 })
1502 // no error with whitelist
1503 testApex(t, `
1504 apex {
1505 name: "myapex",
1506 key: "myapex.key",
1507 use_vendor: true,
1508 }
1509 apex_key {
1510 name: "myapex.key",
1511 public_key: "testkey.avbpubkey",
1512 private_key: "testkey.pem",
1513 }
1514 `, func(fs map[string][]byte, config android.Config) {
1515 setUseVendorWhitelistForTest(config, []string{"myapex"})
1516 })
1517}
1518
Jooyung Han5c998b92019-06-27 11:30:33 +09001519func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1520 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1521 apex {
1522 name: "myapex",
1523 key: "myapex.key",
1524 native_shared_libs: ["mylib"],
1525 use_vendor: true,
1526 }
1527
1528 apex_key {
1529 name: "myapex.key",
1530 public_key: "testkey.avbpubkey",
1531 private_key: "testkey.pem",
1532 }
1533
1534 cc_library {
1535 name: "mylib",
1536 srcs: ["mylib.cpp"],
1537 system_shared_libs: [],
1538 stl: "none",
1539 }
1540 `)
1541}
1542
Jiyong Park16e91a02018-12-20 18:18:08 +09001543func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001544 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001545 apex {
1546 name: "myapex",
1547 key: "myapex.key",
1548 native_shared_libs: ["mylib"],
1549 }
1550
1551 apex_key {
1552 name: "myapex.key",
1553 public_key: "testkey.avbpubkey",
1554 private_key: "testkey.pem",
1555 }
1556
1557 cc_library {
1558 name: "mylib",
1559 srcs: ["mylib.cpp"],
1560 system_shared_libs: [],
1561 stl: "none",
1562 stubs: {
1563 versions: ["1", "2", "3"],
1564 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001565 apex_available: [
1566 "//apex_available:platform",
1567 "myapex",
1568 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001569 }
1570
1571 cc_binary {
1572 name: "not_in_apex",
1573 srcs: ["mylib.cpp"],
1574 static_libs: ["mylib"],
1575 static_executable: true,
1576 system_shared_libs: [],
1577 stl: "none",
1578 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001579 `)
1580
Colin Cross7113d202019-11-20 16:39:12 -08001581 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001582
1583 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001584 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001585}
Jiyong Park9335a262018-12-24 11:31:58 +09001586
1587func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001588 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001589 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001590 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001591 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001592 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001593 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001594 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001595 }
1596
1597 cc_library {
1598 name: "mylib",
1599 srcs: ["mylib.cpp"],
1600 system_shared_libs: [],
1601 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001602 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001603 }
1604
1605 apex_key {
1606 name: "myapex.key",
1607 public_key: "testkey.avbpubkey",
1608 private_key: "testkey.pem",
1609 }
1610
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001611 android_app_certificate {
1612 name: "myapex.certificate",
1613 certificate: "testkey",
1614 }
1615
1616 android_app_certificate {
1617 name: "myapex.certificate.override",
1618 certificate: "testkey.override",
1619 }
1620
Jiyong Park9335a262018-12-24 11:31:58 +09001621 `)
1622
1623 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001624 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001625
1626 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1627 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1628 "vendor/foo/devkeys/testkey.avbpubkey")
1629 }
1630 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1631 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1632 "vendor/foo/devkeys/testkey.pem")
1633 }
1634
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001635 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001636 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001637 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001638 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001639 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001640 }
1641}
Jiyong Park58e364a2019-01-19 19:24:06 +09001642
Jooyung Hanf121a652019-12-17 14:30:11 +09001643func TestCertificate(t *testing.T) {
1644 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1645 ctx, _ := testApex(t, `
1646 apex {
1647 name: "myapex",
1648 key: "myapex.key",
1649 }
1650 apex_key {
1651 name: "myapex.key",
1652 public_key: "testkey.avbpubkey",
1653 private_key: "testkey.pem",
1654 }`)
1655 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1656 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1657 if actual := rule.Args["certificates"]; actual != expected {
1658 t.Errorf("certificates should be %q, not %q", expected, actual)
1659 }
1660 })
1661 t.Run("override when unspecified", func(t *testing.T) {
1662 ctx, _ := testApex(t, `
1663 apex {
1664 name: "myapex_keytest",
1665 key: "myapex.key",
1666 file_contexts: ":myapex-file_contexts",
1667 }
1668 apex_key {
1669 name: "myapex.key",
1670 public_key: "testkey.avbpubkey",
1671 private_key: "testkey.pem",
1672 }
1673 android_app_certificate {
1674 name: "myapex.certificate.override",
1675 certificate: "testkey.override",
1676 }`)
1677 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1678 expected := "testkey.override.x509.pem testkey.override.pk8"
1679 if actual := rule.Args["certificates"]; actual != expected {
1680 t.Errorf("certificates should be %q, not %q", expected, actual)
1681 }
1682 })
1683 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1684 ctx, _ := testApex(t, `
1685 apex {
1686 name: "myapex",
1687 key: "myapex.key",
1688 certificate: ":myapex.certificate",
1689 }
1690 apex_key {
1691 name: "myapex.key",
1692 public_key: "testkey.avbpubkey",
1693 private_key: "testkey.pem",
1694 }
1695 android_app_certificate {
1696 name: "myapex.certificate",
1697 certificate: "testkey",
1698 }`)
1699 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1700 expected := "testkey.x509.pem testkey.pk8"
1701 if actual := rule.Args["certificates"]; actual != expected {
1702 t.Errorf("certificates should be %q, not %q", expected, actual)
1703 }
1704 })
1705 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1706 ctx, _ := testApex(t, `
1707 apex {
1708 name: "myapex_keytest",
1709 key: "myapex.key",
1710 file_contexts: ":myapex-file_contexts",
1711 certificate: ":myapex.certificate",
1712 }
1713 apex_key {
1714 name: "myapex.key",
1715 public_key: "testkey.avbpubkey",
1716 private_key: "testkey.pem",
1717 }
1718 android_app_certificate {
1719 name: "myapex.certificate.override",
1720 certificate: "testkey.override",
1721 }`)
1722 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1723 expected := "testkey.override.x509.pem testkey.override.pk8"
1724 if actual := rule.Args["certificates"]; actual != expected {
1725 t.Errorf("certificates should be %q, not %q", expected, actual)
1726 }
1727 })
1728 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1729 ctx, _ := testApex(t, `
1730 apex {
1731 name: "myapex",
1732 key: "myapex.key",
1733 certificate: "testkey",
1734 }
1735 apex_key {
1736 name: "myapex.key",
1737 public_key: "testkey.avbpubkey",
1738 private_key: "testkey.pem",
1739 }`)
1740 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1741 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1742 if actual := rule.Args["certificates"]; actual != expected {
1743 t.Errorf("certificates should be %q, not %q", expected, actual)
1744 }
1745 })
1746 t.Run("override when specified as <name>", func(t *testing.T) {
1747 ctx, _ := testApex(t, `
1748 apex {
1749 name: "myapex_keytest",
1750 key: "myapex.key",
1751 file_contexts: ":myapex-file_contexts",
1752 certificate: "testkey",
1753 }
1754 apex_key {
1755 name: "myapex.key",
1756 public_key: "testkey.avbpubkey",
1757 private_key: "testkey.pem",
1758 }
1759 android_app_certificate {
1760 name: "myapex.certificate.override",
1761 certificate: "testkey.override",
1762 }`)
1763 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1764 expected := "testkey.override.x509.pem testkey.override.pk8"
1765 if actual := rule.Args["certificates"]; actual != expected {
1766 t.Errorf("certificates should be %q, not %q", expected, actual)
1767 }
1768 })
1769}
1770
Jiyong Park58e364a2019-01-19 19:24:06 +09001771func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001772 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001773 apex {
1774 name: "myapex",
1775 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001776 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001777 }
1778
1779 apex {
1780 name: "otherapex",
1781 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001782 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09001783 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09001784 }
1785
1786 apex_key {
1787 name: "myapex.key",
1788 public_key: "testkey.avbpubkey",
1789 private_key: "testkey.pem",
1790 }
1791
1792 cc_library {
1793 name: "mylib",
1794 srcs: ["mylib.cpp"],
1795 system_shared_libs: [],
1796 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001797 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001798 "myapex",
1799 "otherapex",
1800 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001801 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09001802 cc_library {
1803 name: "mylib2",
1804 srcs: ["mylib.cpp"],
1805 system_shared_libs: [],
1806 stl: "none",
1807 apex_available: [
1808 "myapex",
1809 "otherapex",
1810 ],
1811 use_apex_name_macro: true,
1812 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001813 `)
1814
Jooyung Hanc87a0592020-03-02 17:44:33 +09001815 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001816 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001817 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001818 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001819
Jooyung Hanccce2f22020-03-07 03:45:53 +09001820 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001821 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1822 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001823 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09001824 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001825
Jooyung Hanccce2f22020-03-07 03:45:53 +09001826 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001827 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1828 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001829 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09001830 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001831
Jooyung Hanc87a0592020-03-02 17:44:33 +09001832 // When cc_library sets use_apex_name_macro: true
1833 // apex variants define additional macro to distinguish which apex variant it is built for
1834
1835 // non-APEX variant does not have __ANDROID_APEX__ defined
1836 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1837 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1838
1839 // APEX variant has __ANDROID_APEX__ defined
1840 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001841 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001842 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1843 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001844
Jooyung Hanc87a0592020-03-02 17:44:33 +09001845 // APEX variant has __ANDROID_APEX__ defined
1846 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001847 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001848 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1849 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001850}
Jiyong Park7e636d02019-01-28 16:16:54 +09001851
1852func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001853 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001854 apex {
1855 name: "myapex",
1856 key: "myapex.key",
1857 native_shared_libs: ["mylib"],
1858 }
1859
1860 apex_key {
1861 name: "myapex.key",
1862 public_key: "testkey.avbpubkey",
1863 private_key: "testkey.pem",
1864 }
1865
1866 cc_library_headers {
1867 name: "mylib_headers",
1868 export_include_dirs: ["my_include"],
1869 system_shared_libs: [],
1870 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001871 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001872 }
1873
1874 cc_library {
1875 name: "mylib",
1876 srcs: ["mylib.cpp"],
1877 system_shared_libs: [],
1878 stl: "none",
1879 header_libs: ["mylib_headers"],
1880 export_header_lib_headers: ["mylib_headers"],
1881 stubs: {
1882 versions: ["1", "2", "3"],
1883 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001884 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001885 }
1886
1887 cc_library {
1888 name: "otherlib",
1889 srcs: ["mylib.cpp"],
1890 system_shared_libs: [],
1891 stl: "none",
1892 shared_libs: ["mylib"],
1893 }
1894 `)
1895
Colin Cross7113d202019-11-20 16:39:12 -08001896 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001897
1898 // Ensure that the include path of the header lib is exported to 'otherlib'
1899 ensureContains(t, cFlags, "-Imy_include")
1900}
Alex Light9670d332019-01-29 18:07:33 -08001901
Jiyong Park7cd10e32020-01-14 09:22:18 +09001902type fileInApex struct {
1903 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001904 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001905 isLink bool
1906}
1907
Jooyung Hana57af4a2020-01-23 05:36:59 +00001908func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001909 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001910 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001911 copyCmds := apexRule.Args["copy_commands"]
1912 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001913 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001914 for _, cmd := range strings.Split(copyCmds, "&&") {
1915 cmd = strings.TrimSpace(cmd)
1916 if cmd == "" {
1917 continue
1918 }
1919 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001920 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001921 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001922 switch terms[0] {
1923 case "mkdir":
1924 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001925 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001926 t.Fatal("copyCmds contains invalid cp command", cmd)
1927 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001928 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001929 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001930 isLink = false
1931 case "ln":
1932 if len(terms) != 3 && len(terms) != 4 {
1933 // ln LINK TARGET or ln -s LINK TARGET
1934 t.Fatal("copyCmds contains invalid ln command", cmd)
1935 }
1936 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001937 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001938 isLink = true
1939 default:
1940 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1941 }
1942 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001943 index := strings.Index(dst, imageApexDir)
1944 if index == -1 {
1945 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1946 }
1947 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001948 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001949 }
1950 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001951 return ret
1952}
1953
Jooyung Hana57af4a2020-01-23 05:36:59 +00001954func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1955 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001956 var failed bool
1957 var surplus []string
1958 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001959 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001960 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001961 for _, expected := range files {
1962 if matched, _ := path.Match(expected, file.path); matched {
1963 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001964 mactchFound = true
1965 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001966 }
1967 }
Jooyung Hane6436d72020-02-27 13:31:56 +09001968 if !mactchFound {
1969 surplus = append(surplus, file.path)
1970 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001971 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001972
Jooyung Han31c470b2019-10-18 16:26:59 +09001973 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001974 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001975 t.Log("surplus files", surplus)
1976 failed = true
1977 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001978
1979 if len(files) > len(filesMatched) {
1980 var missing []string
1981 for _, expected := range files {
1982 if !filesMatched[expected] {
1983 missing = append(missing, expected)
1984 }
1985 }
1986 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001987 t.Log("missing files", missing)
1988 failed = true
1989 }
1990 if failed {
1991 t.Fail()
1992 }
1993}
1994
Jooyung Han344d5432019-08-23 11:17:39 +09001995func TestVndkApexCurrent(t *testing.T) {
1996 ctx, _ := testApex(t, `
1997 apex_vndk {
1998 name: "myapex",
1999 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002000 }
2001
2002 apex_key {
2003 name: "myapex.key",
2004 public_key: "testkey.avbpubkey",
2005 private_key: "testkey.pem",
2006 }
2007
2008 cc_library {
2009 name: "libvndk",
2010 srcs: ["mylib.cpp"],
2011 vendor_available: true,
2012 vndk: {
2013 enabled: true,
2014 },
2015 system_shared_libs: [],
2016 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002017 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002018 }
2019
2020 cc_library {
2021 name: "libvndksp",
2022 srcs: ["mylib.cpp"],
2023 vendor_available: true,
2024 vndk: {
2025 enabled: true,
2026 support_system_process: true,
2027 },
2028 system_shared_libs: [],
2029 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002030 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002031 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002032 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002033
Jooyung Hana57af4a2020-01-23 05:36:59 +00002034 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002035 "lib/libvndk.so",
2036 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002037 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002038 "lib64/libvndk.so",
2039 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002040 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002041 "etc/llndk.libraries.VER.txt",
2042 "etc/vndkcore.libraries.VER.txt",
2043 "etc/vndksp.libraries.VER.txt",
2044 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002045 })
Jooyung Han344d5432019-08-23 11:17:39 +09002046}
2047
2048func TestVndkApexWithPrebuilt(t *testing.T) {
2049 ctx, _ := testApex(t, `
2050 apex_vndk {
2051 name: "myapex",
2052 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002053 }
2054
2055 apex_key {
2056 name: "myapex.key",
2057 public_key: "testkey.avbpubkey",
2058 private_key: "testkey.pem",
2059 }
2060
2061 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002062 name: "libvndk",
2063 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002064 vendor_available: true,
2065 vndk: {
2066 enabled: true,
2067 },
2068 system_shared_libs: [],
2069 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002070 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002071 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002072
2073 cc_prebuilt_library_shared {
2074 name: "libvndk.arm",
2075 srcs: ["libvndk.arm.so"],
2076 vendor_available: true,
2077 vndk: {
2078 enabled: true,
2079 },
2080 enabled: false,
2081 arch: {
2082 arm: {
2083 enabled: true,
2084 },
2085 },
2086 system_shared_libs: [],
2087 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002088 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002089 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002090 `+vndkLibrariesTxtFiles("current"),
2091 withFiles(map[string][]byte{
2092 "libvndk.so": nil,
2093 "libvndk.arm.so": nil,
2094 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002095
Jooyung Hana57af4a2020-01-23 05:36:59 +00002096 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002097 "lib/libvndk.so",
2098 "lib/libvndk.arm.so",
2099 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002100 "lib/libc++.so",
2101 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002102 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002103 })
Jooyung Han344d5432019-08-23 11:17:39 +09002104}
2105
Jooyung Han39edb6c2019-11-06 16:53:07 +09002106func vndkLibrariesTxtFiles(vers ...string) (result string) {
2107 for _, v := range vers {
2108 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002109 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002110 result += `
2111 vndk_libraries_txt {
2112 name: "` + txt + `.libraries.txt",
2113 }
2114 `
2115 }
2116 } else {
2117 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2118 result += `
2119 prebuilt_etc {
2120 name: "` + txt + `.libraries.` + v + `.txt",
2121 src: "dummy.txt",
2122 }
2123 `
2124 }
2125 }
2126 }
2127 return
2128}
2129
Jooyung Han344d5432019-08-23 11:17:39 +09002130func TestVndkApexVersion(t *testing.T) {
2131 ctx, _ := testApex(t, `
2132 apex_vndk {
2133 name: "myapex_v27",
2134 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002135 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002136 vndk_version: "27",
2137 }
2138
2139 apex_key {
2140 name: "myapex.key",
2141 public_key: "testkey.avbpubkey",
2142 private_key: "testkey.pem",
2143 }
2144
Jooyung Han31c470b2019-10-18 16:26:59 +09002145 vndk_prebuilt_shared {
2146 name: "libvndk27",
2147 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002148 vendor_available: true,
2149 vndk: {
2150 enabled: true,
2151 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002152 target_arch: "arm64",
2153 arch: {
2154 arm: {
2155 srcs: ["libvndk27_arm.so"],
2156 },
2157 arm64: {
2158 srcs: ["libvndk27_arm64.so"],
2159 },
2160 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002161 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002162 }
2163
2164 vndk_prebuilt_shared {
2165 name: "libvndk27",
2166 version: "27",
2167 vendor_available: true,
2168 vndk: {
2169 enabled: true,
2170 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002171 target_arch: "x86_64",
2172 arch: {
2173 x86: {
2174 srcs: ["libvndk27_x86.so"],
2175 },
2176 x86_64: {
2177 srcs: ["libvndk27_x86_64.so"],
2178 },
2179 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002180 }
2181 `+vndkLibrariesTxtFiles("27"),
2182 withFiles(map[string][]byte{
2183 "libvndk27_arm.so": nil,
2184 "libvndk27_arm64.so": nil,
2185 "libvndk27_x86.so": nil,
2186 "libvndk27_x86_64.so": nil,
2187 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002188
Jooyung Hana57af4a2020-01-23 05:36:59 +00002189 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002190 "lib/libvndk27_arm.so",
2191 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002192 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002193 })
Jooyung Han344d5432019-08-23 11:17:39 +09002194}
2195
2196func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2197 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2198 apex_vndk {
2199 name: "myapex_v27",
2200 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002201 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002202 vndk_version: "27",
2203 }
2204 apex_vndk {
2205 name: "myapex_v27_other",
2206 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002207 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002208 vndk_version: "27",
2209 }
2210
2211 apex_key {
2212 name: "myapex.key",
2213 public_key: "testkey.avbpubkey",
2214 private_key: "testkey.pem",
2215 }
2216
2217 cc_library {
2218 name: "libvndk",
2219 srcs: ["mylib.cpp"],
2220 vendor_available: true,
2221 vndk: {
2222 enabled: true,
2223 },
2224 system_shared_libs: [],
2225 stl: "none",
2226 }
2227
2228 vndk_prebuilt_shared {
2229 name: "libvndk",
2230 version: "27",
2231 vendor_available: true,
2232 vndk: {
2233 enabled: true,
2234 },
2235 srcs: ["libvndk.so"],
2236 }
2237 `, withFiles(map[string][]byte{
2238 "libvndk.so": nil,
2239 }))
2240}
2241
Jooyung Han90eee022019-10-01 20:02:42 +09002242func TestVndkApexNameRule(t *testing.T) {
2243 ctx, _ := testApex(t, `
2244 apex_vndk {
2245 name: "myapex",
2246 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002247 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002248 }
2249 apex_vndk {
2250 name: "myapex_v28",
2251 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002252 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002253 vndk_version: "28",
2254 }
2255 apex_key {
2256 name: "myapex.key",
2257 public_key: "testkey.avbpubkey",
2258 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002259 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002260
2261 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002262 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002263 actual := proptools.String(bundle.properties.Apex_name)
2264 if !reflect.DeepEqual(actual, expected) {
2265 t.Errorf("Got '%v', expected '%v'", actual, expected)
2266 }
2267 }
2268
2269 assertApexName("com.android.vndk.vVER", "myapex")
2270 assertApexName("com.android.vndk.v28", "myapex_v28")
2271}
2272
Jooyung Han344d5432019-08-23 11:17:39 +09002273func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2274 ctx, _ := testApex(t, `
2275 apex_vndk {
2276 name: "myapex",
2277 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002278 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002279 }
2280
2281 apex_key {
2282 name: "myapex.key",
2283 public_key: "testkey.avbpubkey",
2284 private_key: "testkey.pem",
2285 }
2286
2287 cc_library {
2288 name: "libvndk",
2289 srcs: ["mylib.cpp"],
2290 vendor_available: true,
2291 native_bridge_supported: true,
2292 host_supported: true,
2293 vndk: {
2294 enabled: true,
2295 },
2296 system_shared_libs: [],
2297 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002298 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002299 }
Jooyung Han35155c42020-02-06 17:33:20 +09002300 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09002301
Jooyung Hana57af4a2020-01-23 05:36:59 +00002302 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002303 "lib/libvndk.so",
2304 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002305 "lib/libc++.so",
2306 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002307 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002308 })
Jooyung Han344d5432019-08-23 11:17:39 +09002309}
2310
2311func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2312 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2313 apex_vndk {
2314 name: "myapex",
2315 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002316 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002317 native_bridge_supported: true,
2318 }
2319
2320 apex_key {
2321 name: "myapex.key",
2322 public_key: "testkey.avbpubkey",
2323 private_key: "testkey.pem",
2324 }
2325
2326 cc_library {
2327 name: "libvndk",
2328 srcs: ["mylib.cpp"],
2329 vendor_available: true,
2330 native_bridge_supported: true,
2331 host_supported: true,
2332 vndk: {
2333 enabled: true,
2334 },
2335 system_shared_libs: [],
2336 stl: "none",
2337 }
2338 `)
2339}
2340
Jooyung Han31c470b2019-10-18 16:26:59 +09002341func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002342 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002343 apex_vndk {
2344 name: "myapex_v27",
2345 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002346 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002347 vndk_version: "27",
2348 }
2349
2350 apex_key {
2351 name: "myapex.key",
2352 public_key: "testkey.avbpubkey",
2353 private_key: "testkey.pem",
2354 }
2355
2356 vndk_prebuilt_shared {
2357 name: "libvndk27",
2358 version: "27",
2359 target_arch: "arm",
2360 vendor_available: true,
2361 vndk: {
2362 enabled: true,
2363 },
2364 arch: {
2365 arm: {
2366 srcs: ["libvndk27.so"],
2367 }
2368 },
2369 }
2370
2371 vndk_prebuilt_shared {
2372 name: "libvndk27",
2373 version: "27",
2374 target_arch: "arm",
2375 binder32bit: true,
2376 vendor_available: true,
2377 vndk: {
2378 enabled: true,
2379 },
2380 arch: {
2381 arm: {
2382 srcs: ["libvndk27binder32.so"],
2383 }
2384 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002385 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002386 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002387 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002388 withFiles(map[string][]byte{
2389 "libvndk27.so": nil,
2390 "libvndk27binder32.so": nil,
2391 }),
2392 withBinder32bit,
2393 withTargets(map[android.OsType][]android.Target{
2394 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09002395 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
2396 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09002397 },
2398 }),
2399 )
2400
Jooyung Hana57af4a2020-01-23 05:36:59 +00002401 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002402 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002403 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002404 })
2405}
2406
Jooyung Hane1633032019-08-01 17:41:43 +09002407func TestDependenciesInApexManifest(t *testing.T) {
2408 ctx, _ := testApex(t, `
2409 apex {
2410 name: "myapex_nodep",
2411 key: "myapex.key",
2412 native_shared_libs: ["lib_nodep"],
2413 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002414 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002415 }
2416
2417 apex {
2418 name: "myapex_dep",
2419 key: "myapex.key",
2420 native_shared_libs: ["lib_dep"],
2421 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002422 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002423 }
2424
2425 apex {
2426 name: "myapex_provider",
2427 key: "myapex.key",
2428 native_shared_libs: ["libfoo"],
2429 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002430 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002431 }
2432
2433 apex {
2434 name: "myapex_selfcontained",
2435 key: "myapex.key",
2436 native_shared_libs: ["lib_dep", "libfoo"],
2437 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002438 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002439 }
2440
2441 apex_key {
2442 name: "myapex.key",
2443 public_key: "testkey.avbpubkey",
2444 private_key: "testkey.pem",
2445 }
2446
2447 cc_library {
2448 name: "lib_nodep",
2449 srcs: ["mylib.cpp"],
2450 system_shared_libs: [],
2451 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002452 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002453 }
2454
2455 cc_library {
2456 name: "lib_dep",
2457 srcs: ["mylib.cpp"],
2458 shared_libs: ["libfoo"],
2459 system_shared_libs: [],
2460 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002461 apex_available: [
2462 "myapex_dep",
2463 "myapex_provider",
2464 "myapex_selfcontained",
2465 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002466 }
2467
2468 cc_library {
2469 name: "libfoo",
2470 srcs: ["mytest.cpp"],
2471 stubs: {
2472 versions: ["1"],
2473 },
2474 system_shared_libs: [],
2475 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002476 apex_available: [
2477 "myapex_provider",
2478 "myapex_selfcontained",
2479 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002480 }
2481 `)
2482
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002483 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002484 var provideNativeLibs, requireNativeLibs []string
2485
Sundong Ahnabb64432019-10-22 13:58:29 +09002486 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002487 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2488 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002489 ensureListEmpty(t, provideNativeLibs)
2490 ensureListEmpty(t, requireNativeLibs)
2491
Sundong Ahnabb64432019-10-22 13:58:29 +09002492 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002493 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2494 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002495 ensureListEmpty(t, provideNativeLibs)
2496 ensureListContains(t, requireNativeLibs, "libfoo.so")
2497
Sundong Ahnabb64432019-10-22 13:58:29 +09002498 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002499 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2500 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002501 ensureListContains(t, provideNativeLibs, "libfoo.so")
2502 ensureListEmpty(t, requireNativeLibs)
2503
Sundong Ahnabb64432019-10-22 13:58:29 +09002504 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002505 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2506 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002507 ensureListContains(t, provideNativeLibs, "libfoo.so")
2508 ensureListEmpty(t, requireNativeLibs)
2509}
2510
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002511func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002512 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002513 apex {
2514 name: "myapex",
2515 key: "myapex.key",
2516 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002517 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002518 }
2519
2520 apex_key {
2521 name: "myapex.key",
2522 public_key: "testkey.avbpubkey",
2523 private_key: "testkey.pem",
2524 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002525
2526 cc_library {
2527 name: "mylib",
2528 srcs: ["mylib.cpp"],
2529 system_shared_libs: [],
2530 stl: "none",
2531 apex_available: [
2532 "//apex_available:platform",
2533 "myapex",
2534 ],
2535 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002536 `)
2537
Sundong Ahnabb64432019-10-22 13:58:29 +09002538 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002539 apexManifestRule := module.Rule("apexManifestRule")
2540 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2541 apexRule := module.Rule("apexRule")
2542 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002543
2544 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2545 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2546 name := apexBundle.BaseModuleName()
2547 prefix := "TARGET_"
2548 var builder strings.Builder
2549 data.Custom(&builder, name, prefix, "", data)
2550 androidMk := builder.String()
2551 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2552 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002553}
2554
Alex Light0851b882019-02-07 13:20:53 -08002555func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002556 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002557 apex {
2558 name: "myapex",
2559 key: "myapex.key",
2560 native_shared_libs: ["mylib_common"],
2561 }
2562
2563 apex_key {
2564 name: "myapex.key",
2565 public_key: "testkey.avbpubkey",
2566 private_key: "testkey.pem",
2567 }
2568
2569 cc_library {
2570 name: "mylib_common",
2571 srcs: ["mylib.cpp"],
2572 system_shared_libs: [],
2573 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002574 apex_available: [
2575 "//apex_available:platform",
2576 "myapex",
2577 ],
Alex Light0851b882019-02-07 13:20:53 -08002578 }
2579 `)
2580
Sundong Ahnabb64432019-10-22 13:58:29 +09002581 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002582 apexRule := module.Rule("apexRule")
2583 copyCmds := apexRule.Args["copy_commands"]
2584
2585 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2586 t.Log("Apex was a test apex!")
2587 t.Fail()
2588 }
2589 // Ensure that main rule creates an output
2590 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2591
2592 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002593 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002594
2595 // Ensure that both direct and indirect deps are copied into apex
2596 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2597
Colin Cross7113d202019-11-20 16:39:12 -08002598 // Ensure that the platform variant ends with _shared
2599 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002600
2601 if !android.InAnyApex("mylib_common") {
2602 t.Log("Found mylib_common not in any apex!")
2603 t.Fail()
2604 }
2605}
2606
2607func TestTestApex(t *testing.T) {
2608 if android.InAnyApex("mylib_common_test") {
2609 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!")
2610 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002611 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002612 apex_test {
2613 name: "myapex",
2614 key: "myapex.key",
2615 native_shared_libs: ["mylib_common_test"],
2616 }
2617
2618 apex_key {
2619 name: "myapex.key",
2620 public_key: "testkey.avbpubkey",
2621 private_key: "testkey.pem",
2622 }
2623
2624 cc_library {
2625 name: "mylib_common_test",
2626 srcs: ["mylib.cpp"],
2627 system_shared_libs: [],
2628 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002629 // TODO: remove //apex_available:platform
2630 apex_available: [
2631 "//apex_available:platform",
2632 "myapex",
2633 ],
Alex Light0851b882019-02-07 13:20:53 -08002634 }
2635 `)
2636
Sundong Ahnabb64432019-10-22 13:58:29 +09002637 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002638 apexRule := module.Rule("apexRule")
2639 copyCmds := apexRule.Args["copy_commands"]
2640
2641 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2642 t.Log("Apex was not a test apex!")
2643 t.Fail()
2644 }
2645 // Ensure that main rule creates an output
2646 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2647
2648 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002649 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002650
2651 // Ensure that both direct and indirect deps are copied into apex
2652 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2653
Colin Cross7113d202019-11-20 16:39:12 -08002654 // Ensure that the platform variant ends with _shared
2655 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002656}
2657
Alex Light9670d332019-01-29 18:07:33 -08002658func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002659 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002660 apex {
2661 name: "myapex",
2662 key: "myapex.key",
2663 multilib: {
2664 first: {
2665 native_shared_libs: ["mylib_common"],
2666 }
2667 },
2668 target: {
2669 android: {
2670 multilib: {
2671 first: {
2672 native_shared_libs: ["mylib"],
2673 }
2674 }
2675 },
2676 host: {
2677 multilib: {
2678 first: {
2679 native_shared_libs: ["mylib2"],
2680 }
2681 }
2682 }
2683 }
2684 }
2685
2686 apex_key {
2687 name: "myapex.key",
2688 public_key: "testkey.avbpubkey",
2689 private_key: "testkey.pem",
2690 }
2691
2692 cc_library {
2693 name: "mylib",
2694 srcs: ["mylib.cpp"],
2695 system_shared_libs: [],
2696 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002697 // TODO: remove //apex_available:platform
2698 apex_available: [
2699 "//apex_available:platform",
2700 "myapex",
2701 ],
Alex Light9670d332019-01-29 18:07:33 -08002702 }
2703
2704 cc_library {
2705 name: "mylib_common",
2706 srcs: ["mylib.cpp"],
2707 system_shared_libs: [],
2708 stl: "none",
2709 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002710 // TODO: remove //apex_available:platform
2711 apex_available: [
2712 "//apex_available:platform",
2713 "myapex",
2714 ],
Alex Light9670d332019-01-29 18:07:33 -08002715 }
2716
2717 cc_library {
2718 name: "mylib2",
2719 srcs: ["mylib.cpp"],
2720 system_shared_libs: [],
2721 stl: "none",
2722 compile_multilib: "first",
2723 }
2724 `)
2725
Sundong Ahnabb64432019-10-22 13:58:29 +09002726 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002727 copyCmds := apexRule.Args["copy_commands"]
2728
2729 // Ensure that main rule creates an output
2730 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2731
2732 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002733 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2734 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2735 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002736
2737 // Ensure that both direct and indirect deps are copied into apex
2738 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2739 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2740 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2741
Colin Cross7113d202019-11-20 16:39:12 -08002742 // Ensure that the platform variant ends with _shared
2743 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2744 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2745 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002746}
Jiyong Park04480cf2019-02-06 00:16:29 +09002747
2748func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002749 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002750 apex {
2751 name: "myapex",
2752 key: "myapex.key",
2753 binaries: ["myscript"],
2754 }
2755
2756 apex_key {
2757 name: "myapex.key",
2758 public_key: "testkey.avbpubkey",
2759 private_key: "testkey.pem",
2760 }
2761
2762 sh_binary {
2763 name: "myscript",
2764 src: "mylib.cpp",
2765 filename: "myscript.sh",
2766 sub_dir: "script",
2767 }
2768 `)
2769
Sundong Ahnabb64432019-10-22 13:58:29 +09002770 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002771 copyCmds := apexRule.Args["copy_commands"]
2772
2773 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2774}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002775
Jooyung Han91df2082019-11-20 01:49:42 +09002776func TestApexInVariousPartition(t *testing.T) {
2777 testcases := []struct {
2778 propName, parition, flattenedPartition string
2779 }{
2780 {"", "system", "system_ext"},
2781 {"product_specific: true", "product", "product"},
2782 {"soc_specific: true", "vendor", "vendor"},
2783 {"proprietary: true", "vendor", "vendor"},
2784 {"vendor: true", "vendor", "vendor"},
2785 {"system_ext_specific: true", "system_ext", "system_ext"},
2786 }
2787 for _, tc := range testcases {
2788 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2789 ctx, _ := testApex(t, `
2790 apex {
2791 name: "myapex",
2792 key: "myapex.key",
2793 `+tc.propName+`
2794 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002795
Jooyung Han91df2082019-11-20 01:49:42 +09002796 apex_key {
2797 name: "myapex.key",
2798 public_key: "testkey.avbpubkey",
2799 private_key: "testkey.pem",
2800 }
2801 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002802
Jooyung Han91df2082019-11-20 01:49:42 +09002803 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2804 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2805 actual := apex.installDir.String()
2806 if actual != expected {
2807 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2808 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002809
Jooyung Han91df2082019-11-20 01:49:42 +09002810 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2811 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2812 actual = flattened.installDir.String()
2813 if actual != expected {
2814 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2815 }
2816 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002817 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002818}
Jiyong Park67882562019-03-21 01:11:21 +09002819
Jooyung Han54aca7b2019-11-20 02:26:02 +09002820func TestFileContexts(t *testing.T) {
2821 ctx, _ := testApex(t, `
2822 apex {
2823 name: "myapex",
2824 key: "myapex.key",
2825 }
2826
2827 apex_key {
2828 name: "myapex.key",
2829 public_key: "testkey.avbpubkey",
2830 private_key: "testkey.pem",
2831 }
2832 `)
2833 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2834 apexRule := module.Rule("apexRule")
2835 actual := apexRule.Args["file_contexts"]
2836 expected := "system/sepolicy/apex/myapex-file_contexts"
2837 if actual != expected {
2838 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2839 }
2840
2841 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2842 apex {
2843 name: "myapex",
2844 key: "myapex.key",
2845 file_contexts: "my_own_file_contexts",
2846 }
2847
2848 apex_key {
2849 name: "myapex.key",
2850 public_key: "testkey.avbpubkey",
2851 private_key: "testkey.pem",
2852 }
2853 `, withFiles(map[string][]byte{
2854 "my_own_file_contexts": nil,
2855 }))
2856
2857 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2858 apex {
2859 name: "myapex",
2860 key: "myapex.key",
2861 product_specific: true,
2862 file_contexts: "product_specific_file_contexts",
2863 }
2864
2865 apex_key {
2866 name: "myapex.key",
2867 public_key: "testkey.avbpubkey",
2868 private_key: "testkey.pem",
2869 }
2870 `)
2871
2872 ctx, _ = testApex(t, `
2873 apex {
2874 name: "myapex",
2875 key: "myapex.key",
2876 product_specific: true,
2877 file_contexts: "product_specific_file_contexts",
2878 }
2879
2880 apex_key {
2881 name: "myapex.key",
2882 public_key: "testkey.avbpubkey",
2883 private_key: "testkey.pem",
2884 }
2885 `, withFiles(map[string][]byte{
2886 "product_specific_file_contexts": nil,
2887 }))
2888 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2889 apexRule = module.Rule("apexRule")
2890 actual = apexRule.Args["file_contexts"]
2891 expected = "product_specific_file_contexts"
2892 if actual != expected {
2893 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2894 }
2895
2896 ctx, _ = testApex(t, `
2897 apex {
2898 name: "myapex",
2899 key: "myapex.key",
2900 product_specific: true,
2901 file_contexts: ":my-file-contexts",
2902 }
2903
2904 apex_key {
2905 name: "myapex.key",
2906 public_key: "testkey.avbpubkey",
2907 private_key: "testkey.pem",
2908 }
2909
2910 filegroup {
2911 name: "my-file-contexts",
2912 srcs: ["product_specific_file_contexts"],
2913 }
2914 `, withFiles(map[string][]byte{
2915 "product_specific_file_contexts": nil,
2916 }))
2917 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2918 apexRule = module.Rule("apexRule")
2919 actual = apexRule.Args["file_contexts"]
2920 expected = "product_specific_file_contexts"
2921 if actual != expected {
2922 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2923 }
2924}
2925
Jiyong Park67882562019-03-21 01:11:21 +09002926func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002927 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002928 apex_key {
2929 name: "myapex.key",
2930 public_key: ":my.avbpubkey",
2931 private_key: ":my.pem",
2932 product_specific: true,
2933 }
2934
2935 filegroup {
2936 name: "my.avbpubkey",
2937 srcs: ["testkey2.avbpubkey"],
2938 }
2939
2940 filegroup {
2941 name: "my.pem",
2942 srcs: ["testkey2.pem"],
2943 }
2944 `)
2945
2946 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2947 expected_pubkey := "testkey2.avbpubkey"
2948 actual_pubkey := apex_key.public_key_file.String()
2949 if actual_pubkey != expected_pubkey {
2950 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2951 }
2952 expected_privkey := "testkey2.pem"
2953 actual_privkey := apex_key.private_key_file.String()
2954 if actual_privkey != expected_privkey {
2955 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2956 }
2957}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002958
2959func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002960 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002961 prebuilt_apex {
2962 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002963 arch: {
2964 arm64: {
2965 src: "myapex-arm64.apex",
2966 },
2967 arm: {
2968 src: "myapex-arm.apex",
2969 },
2970 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002971 }
2972 `)
2973
2974 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2975
Jiyong Parkc95714e2019-03-29 14:23:10 +09002976 expectedInput := "myapex-arm64.apex"
2977 if prebuilt.inputApex.String() != expectedInput {
2978 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2979 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002980}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002981
2982func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002983 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002984 prebuilt_apex {
2985 name: "myapex",
2986 src: "myapex-arm.apex",
2987 filename: "notmyapex.apex",
2988 }
2989 `)
2990
2991 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2992
2993 expected := "notmyapex.apex"
2994 if p.installFilename != expected {
2995 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2996 }
2997}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002998
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002999func TestPrebuiltOverrides(t *testing.T) {
3000 ctx, config := testApex(t, `
3001 prebuilt_apex {
3002 name: "myapex.prebuilt",
3003 src: "myapex-arm.apex",
3004 overrides: [
3005 "myapex",
3006 ],
3007 }
3008 `)
3009
3010 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3011
3012 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003013 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003014 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003015 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003016 }
3017}
3018
Roland Levillain630846d2019-06-26 12:48:34 +01003019func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003020 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003021 apex_test {
3022 name: "myapex",
3023 key: "myapex.key",
3024 tests: [
3025 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003026 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003027 ],
3028 }
3029
3030 apex_key {
3031 name: "myapex.key",
3032 public_key: "testkey.avbpubkey",
3033 private_key: "testkey.pem",
3034 }
3035
3036 cc_test {
3037 name: "mytest",
3038 gtest: false,
3039 srcs: ["mytest.cpp"],
3040 relative_install_path: "test",
3041 system_shared_libs: [],
3042 static_executable: true,
3043 stl: "none",
3044 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003045
3046 cc_test {
3047 name: "mytests",
3048 gtest: false,
3049 srcs: [
3050 "mytest1.cpp",
3051 "mytest2.cpp",
3052 "mytest3.cpp",
3053 ],
3054 test_per_src: true,
3055 relative_install_path: "test",
3056 system_shared_libs: [],
3057 static_executable: true,
3058 stl: "none",
3059 }
Roland Levillain630846d2019-06-26 12:48:34 +01003060 `)
3061
Sundong Ahnabb64432019-10-22 13:58:29 +09003062 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003063 copyCmds := apexRule.Args["copy_commands"]
3064
3065 // Ensure that test dep is copied into apex.
3066 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003067
3068 // Ensure that test deps built with `test_per_src` are copied into apex.
3069 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3070 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3071 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003072
3073 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003074 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003075 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3076 name := apexBundle.BaseModuleName()
3077 prefix := "TARGET_"
3078 var builder strings.Builder
3079 data.Custom(&builder, name, prefix, "", data)
3080 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003081 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3082 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3083 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3084 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003085 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003086 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003087 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003088}
3089
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003090func TestInstallExtraFlattenedApexes(t *testing.T) {
3091 ctx, config := testApex(t, `
3092 apex {
3093 name: "myapex",
3094 key: "myapex.key",
3095 }
3096 apex_key {
3097 name: "myapex.key",
3098 public_key: "testkey.avbpubkey",
3099 private_key: "testkey.pem",
3100 }
3101 `, func(fs map[string][]byte, config android.Config) {
3102 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3103 })
3104 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003105 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003106 mk := android.AndroidMkDataForTest(t, config, "", ab)
3107 var builder strings.Builder
3108 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3109 androidMk := builder.String()
3110 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3111}
3112
Jooyung Han5c998b92019-06-27 11:30:33 +09003113func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003114 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003115 apex {
3116 name: "myapex",
3117 key: "myapex.key",
3118 native_shared_libs: ["mylib"],
3119 uses: ["commonapex"],
3120 }
3121
3122 apex {
3123 name: "commonapex",
3124 key: "myapex.key",
3125 native_shared_libs: ["libcommon"],
3126 provide_cpp_shared_libs: true,
3127 }
3128
3129 apex_key {
3130 name: "myapex.key",
3131 public_key: "testkey.avbpubkey",
3132 private_key: "testkey.pem",
3133 }
3134
3135 cc_library {
3136 name: "mylib",
3137 srcs: ["mylib.cpp"],
3138 shared_libs: ["libcommon"],
3139 system_shared_libs: [],
3140 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003141 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003142 }
3143
3144 cc_library {
3145 name: "libcommon",
3146 srcs: ["mylib_common.cpp"],
3147 system_shared_libs: [],
3148 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003149 // TODO: remove //apex_available:platform
3150 apex_available: [
3151 "//apex_available:platform",
3152 "commonapex",
3153 "myapex",
3154 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003155 }
3156 `)
3157
Sundong Ahnabb64432019-10-22 13:58:29 +09003158 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003159 apexRule1 := module1.Rule("apexRule")
3160 copyCmds1 := apexRule1.Args["copy_commands"]
3161
Sundong Ahnabb64432019-10-22 13:58:29 +09003162 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003163 apexRule2 := module2.Rule("apexRule")
3164 copyCmds2 := apexRule2.Args["copy_commands"]
3165
Colin Cross7113d202019-11-20 16:39:12 -08003166 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3167 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003168 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3169 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3170 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3171}
3172
3173func TestApexUsesFailsIfNotProvided(t *testing.T) {
3174 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3175 apex {
3176 name: "myapex",
3177 key: "myapex.key",
3178 uses: ["commonapex"],
3179 }
3180
3181 apex {
3182 name: "commonapex",
3183 key: "myapex.key",
3184 }
3185
3186 apex_key {
3187 name: "myapex.key",
3188 public_key: "testkey.avbpubkey",
3189 private_key: "testkey.pem",
3190 }
3191 `)
3192 testApexError(t, `uses: "commonapex" is not a provider`, `
3193 apex {
3194 name: "myapex",
3195 key: "myapex.key",
3196 uses: ["commonapex"],
3197 }
3198
3199 cc_library {
3200 name: "commonapex",
3201 system_shared_libs: [],
3202 stl: "none",
3203 }
3204
3205 apex_key {
3206 name: "myapex.key",
3207 public_key: "testkey.avbpubkey",
3208 private_key: "testkey.pem",
3209 }
3210 `)
3211}
3212
3213func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3214 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3215 apex {
3216 name: "myapex",
3217 key: "myapex.key",
3218 use_vendor: true,
3219 uses: ["commonapex"],
3220 }
3221
3222 apex {
3223 name: "commonapex",
3224 key: "myapex.key",
3225 provide_cpp_shared_libs: true,
3226 }
3227
3228 apex_key {
3229 name: "myapex.key",
3230 public_key: "testkey.avbpubkey",
3231 private_key: "testkey.pem",
3232 }
Jooyung Handc782442019-11-01 03:14:38 +09003233 `, func(fs map[string][]byte, config android.Config) {
3234 setUseVendorWhitelistForTest(config, []string{"myapex"})
3235 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003236}
3237
Jooyung Hand48f3c32019-08-23 11:18:57 +09003238func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3239 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3240 apex {
3241 name: "myapex",
3242 key: "myapex.key",
3243 native_shared_libs: ["libfoo"],
3244 }
3245
3246 apex_key {
3247 name: "myapex.key",
3248 public_key: "testkey.avbpubkey",
3249 private_key: "testkey.pem",
3250 }
3251
3252 cc_library {
3253 name: "libfoo",
3254 stl: "none",
3255 system_shared_libs: [],
3256 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003257 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003258 }
3259 `)
3260 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3261 apex {
3262 name: "myapex",
3263 key: "myapex.key",
3264 java_libs: ["myjar"],
3265 }
3266
3267 apex_key {
3268 name: "myapex.key",
3269 public_key: "testkey.avbpubkey",
3270 private_key: "testkey.pem",
3271 }
3272
3273 java_library {
3274 name: "myjar",
3275 srcs: ["foo/bar/MyClass.java"],
3276 sdk_version: "none",
3277 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003278 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003279 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003280 }
3281 `)
3282}
3283
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003284func TestApexWithApps(t *testing.T) {
3285 ctx, _ := testApex(t, `
3286 apex {
3287 name: "myapex",
3288 key: "myapex.key",
3289 apps: [
3290 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003291 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003292 ],
3293 }
3294
3295 apex_key {
3296 name: "myapex.key",
3297 public_key: "testkey.avbpubkey",
3298 private_key: "testkey.pem",
3299 }
3300
3301 android_app {
3302 name: "AppFoo",
3303 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003304 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003305 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003306 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08003307 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003308 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003309 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003310
3311 android_app {
3312 name: "AppFooPriv",
3313 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003314 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09003315 system_modules: "none",
3316 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08003317 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003318 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003319 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003320
3321 cc_library_shared {
3322 name: "libjni",
3323 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003324 shared_libs: ["libfoo"],
3325 stl: "none",
3326 system_shared_libs: [],
3327 apex_available: [ "myapex" ],
3328 sdk_version: "current",
3329 }
3330
3331 cc_library_shared {
3332 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09003333 stl: "none",
3334 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003335 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08003336 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003337 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003338 `)
3339
Sundong Ahnabb64432019-10-22 13:58:29 +09003340 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003341 apexRule := module.Rule("apexRule")
3342 copyCmds := apexRule.Args["copy_commands"]
3343
3344 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003345 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003346
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003347 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3348 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003349 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003350 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003351 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003352 // JNI libraries including transitive deps are
3353 for _, jni := range []string{"libjni", "libfoo"} {
3354 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
3355 // ... embedded inside APK (jnilibs.zip)
3356 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3357 // ... and not directly inside the APEX
3358 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3359 }
Dario Frenicde2a032019-10-27 00:29:22 +01003360}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003361
Dario Frenicde2a032019-10-27 00:29:22 +01003362func TestApexWithAppImports(t *testing.T) {
3363 ctx, _ := testApex(t, `
3364 apex {
3365 name: "myapex",
3366 key: "myapex.key",
3367 apps: [
3368 "AppFooPrebuilt",
3369 "AppFooPrivPrebuilt",
3370 ],
3371 }
3372
3373 apex_key {
3374 name: "myapex.key",
3375 public_key: "testkey.avbpubkey",
3376 private_key: "testkey.pem",
3377 }
3378
3379 android_app_import {
3380 name: "AppFooPrebuilt",
3381 apk: "PrebuiltAppFoo.apk",
3382 presigned: true,
3383 dex_preopt: {
3384 enabled: false,
3385 },
3386 }
3387
3388 android_app_import {
3389 name: "AppFooPrivPrebuilt",
3390 apk: "PrebuiltAppFooPriv.apk",
3391 privileged: true,
3392 presigned: true,
3393 dex_preopt: {
3394 enabled: false,
3395 },
3396 }
3397 `)
3398
Sundong Ahnabb64432019-10-22 13:58:29 +09003399 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003400 apexRule := module.Rule("apexRule")
3401 copyCmds := apexRule.Args["copy_commands"]
3402
3403 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3404 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003405}
3406
Dario Freni6f3937c2019-12-20 22:58:03 +00003407func TestApexWithTestHelperApp(t *testing.T) {
3408 ctx, _ := testApex(t, `
3409 apex {
3410 name: "myapex",
3411 key: "myapex.key",
3412 apps: [
3413 "TesterHelpAppFoo",
3414 ],
3415 }
3416
3417 apex_key {
3418 name: "myapex.key",
3419 public_key: "testkey.avbpubkey",
3420 private_key: "testkey.pem",
3421 }
3422
3423 android_test_helper_app {
3424 name: "TesterHelpAppFoo",
3425 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003426 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003427 }
3428
3429 `)
3430
3431 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3432 apexRule := module.Rule("apexRule")
3433 copyCmds := apexRule.Args["copy_commands"]
3434
3435 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3436}
3437
Jooyung Han18020ea2019-11-13 10:50:48 +09003438func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3439 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09003440 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003441 apex {
3442 name: "myapex",
3443 key: "myapex.key",
3444 native_shared_libs: ["libfoo"],
3445 }
3446
3447 apex_key {
3448 name: "myapex.key",
3449 public_key: "testkey.avbpubkey",
3450 private_key: "testkey.pem",
3451 }
3452
3453 apex {
3454 name: "otherapex",
3455 key: "myapex.key",
3456 native_shared_libs: ["libfoo"],
3457 }
3458
3459 cc_defaults {
3460 name: "libfoo-defaults",
3461 apex_available: ["otherapex"],
3462 }
3463
3464 cc_library {
3465 name: "libfoo",
3466 defaults: ["libfoo-defaults"],
3467 stl: "none",
3468 system_shared_libs: [],
3469 }`)
3470}
3471
Jiyong Park127b40b2019-09-30 16:04:35 +09003472func TestApexAvailable(t *testing.T) {
3473 // libfoo is not available to myapex, but only to otherapex
3474 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3475 apex {
3476 name: "myapex",
3477 key: "myapex.key",
3478 native_shared_libs: ["libfoo"],
3479 }
3480
3481 apex_key {
3482 name: "myapex.key",
3483 public_key: "testkey.avbpubkey",
3484 private_key: "testkey.pem",
3485 }
3486
3487 apex {
3488 name: "otherapex",
3489 key: "otherapex.key",
3490 native_shared_libs: ["libfoo"],
3491 }
3492
3493 apex_key {
3494 name: "otherapex.key",
3495 public_key: "testkey.avbpubkey",
3496 private_key: "testkey.pem",
3497 }
3498
3499 cc_library {
3500 name: "libfoo",
3501 stl: "none",
3502 system_shared_libs: [],
3503 apex_available: ["otherapex"],
3504 }`)
3505
Jooyung Han5e9013b2020-03-10 06:23:13 +09003506 // libbbaz is an indirect dep
3507 testApexError(t, "requires \"libbaz\" that is not available for the APEX", `
Jiyong Park127b40b2019-09-30 16:04:35 +09003508 apex {
3509 name: "myapex",
3510 key: "myapex.key",
3511 native_shared_libs: ["libfoo"],
3512 }
3513
3514 apex_key {
3515 name: "myapex.key",
3516 public_key: "testkey.avbpubkey",
3517 private_key: "testkey.pem",
3518 }
3519
Jiyong Park127b40b2019-09-30 16:04:35 +09003520 cc_library {
3521 name: "libfoo",
3522 stl: "none",
3523 shared_libs: ["libbar"],
3524 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003525 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003526 }
3527
3528 cc_library {
3529 name: "libbar",
3530 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09003531 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003532 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003533 apex_available: ["myapex"],
3534 }
3535
3536 cc_library {
3537 name: "libbaz",
3538 stl: "none",
3539 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003540 }`)
3541
3542 testApexError(t, "\"otherapex\" is not a valid module name", `
3543 apex {
3544 name: "myapex",
3545 key: "myapex.key",
3546 native_shared_libs: ["libfoo"],
3547 }
3548
3549 apex_key {
3550 name: "myapex.key",
3551 public_key: "testkey.avbpubkey",
3552 private_key: "testkey.pem",
3553 }
3554
3555 cc_library {
3556 name: "libfoo",
3557 stl: "none",
3558 system_shared_libs: [],
3559 apex_available: ["otherapex"],
3560 }`)
3561
3562 ctx, _ := testApex(t, `
3563 apex {
3564 name: "myapex",
3565 key: "myapex.key",
3566 native_shared_libs: ["libfoo", "libbar"],
3567 }
3568
3569 apex_key {
3570 name: "myapex.key",
3571 public_key: "testkey.avbpubkey",
3572 private_key: "testkey.pem",
3573 }
3574
3575 cc_library {
3576 name: "libfoo",
3577 stl: "none",
3578 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09003579 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003580 apex_available: ["myapex"],
3581 }
3582
3583 cc_library {
3584 name: "libbar",
3585 stl: "none",
3586 system_shared_libs: [],
3587 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09003588 }
3589
3590 cc_library {
3591 name: "libbaz",
3592 stl: "none",
3593 system_shared_libs: [],
3594 stubs: {
3595 versions: ["10", "20", "30"],
3596 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003597 }`)
3598
3599 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003600 // TODO(jiyong) the checks for the platform variant are removed because we now create
3601 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3602 // the platform variants are not used from other platform modules. When that is done,
3603 // these checks will be replaced by expecting a specific error message that will be
3604 // emitted when the platform variant is used.
3605 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3606 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3607 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3608 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003609
3610 ctx, _ = testApex(t, `
3611 apex {
3612 name: "myapex",
3613 key: "myapex.key",
3614 }
3615
3616 apex_key {
3617 name: "myapex.key",
3618 public_key: "testkey.avbpubkey",
3619 private_key: "testkey.pem",
3620 }
3621
3622 cc_library {
3623 name: "libfoo",
3624 stl: "none",
3625 system_shared_libs: [],
3626 apex_available: ["//apex_available:platform"],
3627 }`)
3628
3629 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003630 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3631 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003632
3633 ctx, _ = testApex(t, `
3634 apex {
3635 name: "myapex",
3636 key: "myapex.key",
3637 native_shared_libs: ["libfoo"],
3638 }
3639
3640 apex_key {
3641 name: "myapex.key",
3642 public_key: "testkey.avbpubkey",
3643 private_key: "testkey.pem",
3644 }
3645
3646 cc_library {
3647 name: "libfoo",
3648 stl: "none",
3649 system_shared_libs: [],
3650 apex_available: ["myapex"],
3651 static: {
3652 apex_available: ["//apex_available:platform"],
3653 },
3654 }`)
3655
3656 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003657 // TODO(jiyong) the checks for the platform variant are removed because we now create
3658 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3659 // the platform variants are not used from other platform modules. When that is done,
3660 // these checks will be replaced by expecting a specific error message that will be
3661 // emitted when the platform variant is used.
3662 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3663 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3664 // // but the static variant is available to both myapex and the platform
3665 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3666 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003667}
3668
Jiyong Park5d790c32019-11-15 18:40:32 +09003669func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003670 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003671 apex {
3672 name: "myapex",
3673 key: "myapex.key",
3674 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003675 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003676 }
3677
3678 override_apex {
3679 name: "override_myapex",
3680 base: "myapex",
3681 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003682 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003683 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003684 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09003685 }
3686
3687 apex_key {
3688 name: "myapex.key",
3689 public_key: "testkey.avbpubkey",
3690 private_key: "testkey.pem",
3691 }
3692
3693 android_app {
3694 name: "app",
3695 srcs: ["foo/bar/MyClass.java"],
3696 package_name: "foo",
3697 sdk_version: "none",
3698 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003699 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003700 }
3701
3702 override_android_app {
3703 name: "override_app",
3704 base: "app",
3705 package_name: "bar",
3706 }
Jiyong Park20bacab2020-03-03 11:45:41 +09003707 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003708
Jiyong Park317645e2019-12-05 13:20:58 +09003709 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3710 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3711 if originalVariant.GetOverriddenBy() != "" {
3712 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3713 }
3714 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3715 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3716 }
3717
Jiyong Park5d790c32019-11-15 18:40:32 +09003718 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3719 apexRule := module.Rule("apexRule")
3720 copyCmds := apexRule.Args["copy_commands"]
3721
3722 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3723 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003724
3725 apexBundle := module.Module().(*apexBundle)
3726 name := apexBundle.Name()
3727 if name != "override_myapex" {
3728 t.Errorf("name should be \"override_myapex\", but was %q", name)
3729 }
3730
Baligh Uddin004d7172020-02-19 21:29:28 -08003731 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3732 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3733 }
3734
Jiyong Park20bacab2020-03-03 11:45:41 +09003735 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003736 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09003737
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003738 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3739 var builder strings.Builder
3740 data.Custom(&builder, name, "TARGET_", "", data)
3741 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003742 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003743 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3744 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003745 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003746 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003747 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003748 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3749 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003750}
3751
Jooyung Han214bf372019-11-12 13:03:50 +09003752func TestLegacyAndroid10Support(t *testing.T) {
3753 ctx, _ := testApex(t, `
3754 apex {
3755 name: "myapex",
3756 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003757 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09003758 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09003759 }
3760
3761 apex_key {
3762 name: "myapex.key",
3763 public_key: "testkey.avbpubkey",
3764 private_key: "testkey.pem",
3765 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003766
3767 cc_library {
3768 name: "mylib",
3769 srcs: ["mylib.cpp"],
3770 stl: "libc++",
3771 system_shared_libs: [],
3772 apex_available: [ "myapex" ],
3773 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003774 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003775
3776 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3777 args := module.Rule("apexRule").Args
3778 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003779 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003780
3781 // The copies of the libraries in the apex should have one more dependency than
3782 // the ones outside the apex, namely the unwinder. Ideally we should check
3783 // the dependency names directly here but for some reason the names are blank in
3784 // this test.
3785 for _, lib := range []string{"libc++", "mylib"} {
3786 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3787 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3788 if len(apexImplicits) != len(nonApexImplicits)+1 {
3789 t.Errorf("%q missing unwinder dep", lib)
3790 }
3791 }
Jooyung Han214bf372019-11-12 13:03:50 +09003792}
3793
Jooyung Han58f26ab2019-12-18 15:34:32 +09003794func TestJavaSDKLibrary(t *testing.T) {
3795 ctx, _ := testApex(t, `
3796 apex {
3797 name: "myapex",
3798 key: "myapex.key",
3799 java_libs: ["foo"],
3800 }
3801
3802 apex_key {
3803 name: "myapex.key",
3804 public_key: "testkey.avbpubkey",
3805 private_key: "testkey.pem",
3806 }
3807
3808 java_sdk_library {
3809 name: "foo",
3810 srcs: ["a.java"],
3811 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003812 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003813 }
3814 `, withFiles(map[string][]byte{
3815 "api/current.txt": nil,
3816 "api/removed.txt": nil,
3817 "api/system-current.txt": nil,
3818 "api/system-removed.txt": nil,
3819 "api/test-current.txt": nil,
3820 "api/test-removed.txt": nil,
3821 }))
3822
3823 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003824 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003825 "javalib/foo.jar",
3826 "etc/permissions/foo.xml",
3827 })
3828 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003829 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3830 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003831}
3832
atrost6e126252020-01-27 17:01:16 +00003833func TestCompatConfig(t *testing.T) {
3834 ctx, _ := testApex(t, `
3835 apex {
3836 name: "myapex",
3837 key: "myapex.key",
3838 prebuilts: ["myjar-platform-compat-config"],
3839 java_libs: ["myjar"],
3840 }
3841
3842 apex_key {
3843 name: "myapex.key",
3844 public_key: "testkey.avbpubkey",
3845 private_key: "testkey.pem",
3846 }
3847
3848 platform_compat_config {
3849 name: "myjar-platform-compat-config",
3850 src: ":myjar",
3851 }
3852
3853 java_library {
3854 name: "myjar",
3855 srcs: ["foo/bar/MyClass.java"],
3856 sdk_version: "none",
3857 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003858 apex_available: [ "myapex" ],
3859 }
3860 `)
3861 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3862 "etc/compatconfig/myjar-platform-compat-config.xml",
3863 "javalib/myjar.jar",
3864 })
3865}
3866
Jiyong Park479321d2019-12-16 11:47:12 +09003867func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3868 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3869 apex {
3870 name: "myapex",
3871 key: "myapex.key",
3872 java_libs: ["myjar"],
3873 }
3874
3875 apex_key {
3876 name: "myapex.key",
3877 public_key: "testkey.avbpubkey",
3878 private_key: "testkey.pem",
3879 }
3880
3881 java_library {
3882 name: "myjar",
3883 srcs: ["foo/bar/MyClass.java"],
3884 sdk_version: "none",
3885 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003886 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003887 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09003888 }
3889 `)
3890}
3891
Jiyong Park7afd1072019-12-30 16:56:33 +09003892func TestCarryRequiredModuleNames(t *testing.T) {
3893 ctx, config := testApex(t, `
3894 apex {
3895 name: "myapex",
3896 key: "myapex.key",
3897 native_shared_libs: ["mylib"],
3898 }
3899
3900 apex_key {
3901 name: "myapex.key",
3902 public_key: "testkey.avbpubkey",
3903 private_key: "testkey.pem",
3904 }
3905
3906 cc_library {
3907 name: "mylib",
3908 srcs: ["mylib.cpp"],
3909 system_shared_libs: [],
3910 stl: "none",
3911 required: ["a", "b"],
3912 host_required: ["c", "d"],
3913 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003914 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003915 }
3916 `)
3917
3918 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3919 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3920 name := apexBundle.BaseModuleName()
3921 prefix := "TARGET_"
3922 var builder strings.Builder
3923 data.Custom(&builder, name, prefix, "", data)
3924 androidMk := builder.String()
3925 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3926 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3927 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3928}
3929
Jiyong Park7cd10e32020-01-14 09:22:18 +09003930func TestSymlinksFromApexToSystem(t *testing.T) {
3931 bp := `
3932 apex {
3933 name: "myapex",
3934 key: "myapex.key",
3935 native_shared_libs: ["mylib"],
3936 java_libs: ["myjar"],
3937 }
3938
Jiyong Park9d677202020-02-19 16:29:35 +09003939 apex {
3940 name: "myapex.updatable",
3941 key: "myapex.key",
3942 native_shared_libs: ["mylib"],
3943 java_libs: ["myjar"],
3944 updatable: true,
3945 }
3946
Jiyong Park7cd10e32020-01-14 09:22:18 +09003947 apex_key {
3948 name: "myapex.key",
3949 public_key: "testkey.avbpubkey",
3950 private_key: "testkey.pem",
3951 }
3952
3953 cc_library {
3954 name: "mylib",
3955 srcs: ["mylib.cpp"],
3956 shared_libs: ["myotherlib"],
3957 system_shared_libs: [],
3958 stl: "none",
3959 apex_available: [
3960 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003961 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003962 "//apex_available:platform",
3963 ],
3964 }
3965
3966 cc_library {
3967 name: "myotherlib",
3968 srcs: ["mylib.cpp"],
3969 system_shared_libs: [],
3970 stl: "none",
3971 apex_available: [
3972 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003973 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003974 "//apex_available:platform",
3975 ],
3976 }
3977
3978 java_library {
3979 name: "myjar",
3980 srcs: ["foo/bar/MyClass.java"],
3981 sdk_version: "none",
3982 system_modules: "none",
3983 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003984 apex_available: [
3985 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003986 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003987 "//apex_available:platform",
3988 ],
3989 }
3990
3991 java_library {
3992 name: "myotherjar",
3993 srcs: ["foo/bar/MyClass.java"],
3994 sdk_version: "none",
3995 system_modules: "none",
3996 apex_available: [
3997 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003998 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003999 "//apex_available:platform",
4000 ],
4001 }
4002 `
4003
4004 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
4005 for _, f := range files {
4006 if f.path == file {
4007 if f.isLink {
4008 t.Errorf("%q is not a real file", file)
4009 }
4010 return
4011 }
4012 }
4013 t.Errorf("%q is not found", file)
4014 }
4015
4016 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
4017 for _, f := range files {
4018 if f.path == file {
4019 if !f.isLink {
4020 t.Errorf("%q is not a symlink", file)
4021 }
4022 return
4023 }
4024 }
4025 t.Errorf("%q is not found", file)
4026 }
4027
Jiyong Park9d677202020-02-19 16:29:35 +09004028 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4029 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004030 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004031 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004032 ensureRealfileExists(t, files, "javalib/myjar.jar")
4033 ensureRealfileExists(t, files, "lib64/mylib.so")
4034 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4035
Jiyong Park9d677202020-02-19 16:29:35 +09004036 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4037 ensureRealfileExists(t, files, "javalib/myjar.jar")
4038 ensureRealfileExists(t, files, "lib64/mylib.so")
4039 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4040
4041 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004042 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004043 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004044 ensureRealfileExists(t, files, "javalib/myjar.jar")
4045 ensureRealfileExists(t, files, "lib64/mylib.so")
4046 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004047
4048 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4049 ensureRealfileExists(t, files, "javalib/myjar.jar")
4050 ensureRealfileExists(t, files, "lib64/mylib.so")
4051 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004052}
4053
Jooyung Han643adc42020-02-27 13:50:06 +09004054func TestApexWithJniLibs(t *testing.T) {
4055 ctx, _ := testApex(t, `
4056 apex {
4057 name: "myapex",
4058 key: "myapex.key",
4059 jni_libs: ["mylib"],
4060 }
4061
4062 apex_key {
4063 name: "myapex.key",
4064 public_key: "testkey.avbpubkey",
4065 private_key: "testkey.pem",
4066 }
4067
4068 cc_library {
4069 name: "mylib",
4070 srcs: ["mylib.cpp"],
4071 shared_libs: ["mylib2"],
4072 system_shared_libs: [],
4073 stl: "none",
4074 apex_available: [ "myapex" ],
4075 }
4076
4077 cc_library {
4078 name: "mylib2",
4079 srcs: ["mylib.cpp"],
4080 system_shared_libs: [],
4081 stl: "none",
4082 apex_available: [ "myapex" ],
4083 }
4084 `)
4085
4086 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
4087 // Notice mylib2.so (transitive dep) is not added as a jni_lib
4088 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
4089 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4090 "lib64/mylib.so",
4091 "lib64/mylib2.so",
4092 })
4093}
4094
4095func TestApexWithJniLibs_Errors(t *testing.T) {
4096 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
4097 apex {
4098 name: "myapex",
4099 key: "myapex.key",
4100 jni_libs: ["xxx"],
4101 }
4102
4103 apex_key {
4104 name: "myapex.key",
4105 public_key: "testkey.avbpubkey",
4106 private_key: "testkey.pem",
4107 }
4108
4109 prebuilt_etc {
4110 name: "xxx",
4111 src: "xxx",
4112 }
4113 `, withFiles(map[string][]byte{
4114 "xxx": nil,
4115 }))
4116}
4117
Jiyong Parkbd159612020-02-28 15:22:21 +09004118func TestAppBundle(t *testing.T) {
4119 ctx, _ := testApex(t, `
4120 apex {
4121 name: "myapex",
4122 key: "myapex.key",
4123 apps: ["AppFoo"],
4124 }
4125
4126 apex_key {
4127 name: "myapex.key",
4128 public_key: "testkey.avbpubkey",
4129 private_key: "testkey.pem",
4130 }
4131
4132 android_app {
4133 name: "AppFoo",
4134 srcs: ["foo/bar/MyClass.java"],
4135 sdk_version: "none",
4136 system_modules: "none",
4137 apex_available: [ "myapex" ],
4138 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004139 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09004140
4141 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4142 content := bundleConfigRule.Args["content"]
4143
4144 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004145 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 +09004146}
4147
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004148func testNoUpdatableJarsInBootImage(t *testing.T, errmsg, bp string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
4149 t.Helper()
4150
4151 bp = bp + `
4152 filegroup {
4153 name: "some-updatable-apex-file_contexts",
4154 srcs: [
4155 "system/sepolicy/apex/some-updatable-apex-file_contexts",
4156 ],
4157 }
4158 `
4159 bp += cc.GatherRequiredDepsForTest(android.Android)
4160 bp += java.GatherRequiredDepsForTest()
4161 bp += dexpreopt.BpToolModulesForTest()
4162
4163 fs := map[string][]byte{
4164 "a.java": nil,
4165 "a.jar": nil,
4166 "build/make/target/product/security": nil,
4167 "apex_manifest.json": nil,
4168 "AndroidManifest.xml": nil,
4169 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
4170 "system/sepolicy/apex/com.android.art.something-file_contexts": nil,
4171 "framework/aidl/a.aidl": nil,
4172 }
4173 cc.GatherRequiredFilesForTest(fs)
4174
4175 ctx := android.NewTestArchContext()
4176 ctx.RegisterModuleType("apex", BundleFactory)
4177 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
4178 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
4179 cc.RegisterRequiredBuildComponentsForTest(ctx)
4180 java.RegisterJavaBuildComponents(ctx)
4181 java.RegisterSystemModulesBuildComponents(ctx)
4182 java.RegisterAppBuildComponents(ctx)
4183 java.RegisterDexpreoptBootJarsComponents(ctx)
4184 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
4185 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
4186 ctx.PreDepsMutators(RegisterPreDepsMutators)
4187 ctx.PostDepsMutators(RegisterPostDepsMutators)
4188
4189 config := android.TestArchConfig(buildDir, nil, bp, fs)
4190 ctx.Register(config)
4191
4192 _ = dexpreopt.GlobalSoongConfigForTests(config)
4193 dexpreopt.RegisterToolModulesForTest(ctx)
4194 pathCtx := android.PathContextForTesting(config)
4195 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
4196 transformDexpreoptConfig(dexpreoptConfig)
4197 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
4198
4199 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
4200 android.FailIfErrored(t, errs)
4201
4202 _, errs = ctx.PrepareBuildActions(config)
4203 if errmsg == "" {
4204 android.FailIfErrored(t, errs)
4205 } else if len(errs) > 0 {
4206 android.FailIfNoMatchingErrors(t, errmsg, errs)
4207 return
4208 } else {
4209 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
4210 }
4211}
4212
4213func TestNoUpdatableJarsInBootImage(t *testing.T) {
4214 bp := `
4215 java_library {
4216 name: "some-updatable-apex-lib",
4217 srcs: ["a.java"],
4218 apex_available: [
4219 "some-updatable-apex",
4220 ],
4221 }
4222
4223 java_library {
4224 name: "some-platform-lib",
4225 srcs: ["a.java"],
4226 installable: true,
4227 }
4228
4229 java_library {
4230 name: "some-art-lib",
4231 srcs: ["a.java"],
4232 apex_available: [
4233 "com.android.art.something",
4234 ],
4235 hostdex: true,
4236 }
4237
4238 apex {
4239 name: "some-updatable-apex",
4240 key: "some-updatable-apex.key",
4241 java_libs: ["some-updatable-apex-lib"],
4242 }
4243
4244 apex_key {
4245 name: "some-updatable-apex.key",
4246 }
4247
4248 apex {
4249 name: "com.android.art.something",
4250 key: "com.android.art.something.key",
4251 java_libs: ["some-art-lib"],
4252 }
4253
4254 apex_key {
4255 name: "com.android.art.something.key",
4256 }
4257 `
4258
4259 var error string
4260 var transform func(*dexpreopt.GlobalConfig)
4261
4262 // updatable jar from ART apex in the ART boot image => ok
4263 transform = func(config *dexpreopt.GlobalConfig) {
4264 config.ArtApexJars = []string{"some-art-lib"}
4265 }
4266 testNoUpdatableJarsInBootImage(t, "", bp, transform)
4267
4268 // updatable jar from ART apex in the framework boot image => error
4269 error = "module 'some-art-lib' from updatable apex 'com.android.art.something' is not allowed in the framework boot image"
4270 transform = func(config *dexpreopt.GlobalConfig) {
4271 config.BootJars = []string{"some-art-lib"}
4272 }
4273 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4274
4275 // updatable jar from some other apex in the ART boot image => error
4276 error = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the ART boot image"
4277 transform = func(config *dexpreopt.GlobalConfig) {
4278 config.ArtApexJars = []string{"some-updatable-apex-lib"}
4279 }
4280 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4281
4282 // updatable jar from some other apex in the framework boot image => error
4283 error = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the framework boot image"
4284 transform = func(config *dexpreopt.GlobalConfig) {
4285 config.BootJars = []string{"some-updatable-apex-lib"}
4286 }
4287 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4288
4289 // nonexistent jar in the ART boot image => error
4290 error = "failed to find a dex jar path for module 'nonexistent'"
4291 transform = func(config *dexpreopt.GlobalConfig) {
4292 config.ArtApexJars = []string{"nonexistent"}
4293 }
4294 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4295
4296 // nonexistent jar in the framework boot image => error
4297 error = "failed to find a dex jar path for module 'nonexistent'"
4298 transform = func(config *dexpreopt.GlobalConfig) {
4299 config.BootJars = []string{"nonexistent"}
4300 }
4301 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4302
4303 // platform jar in the ART boot image => error
4304 error = "module 'some-platform-lib' is part of the platform and not allowed in the ART boot image"
4305 transform = func(config *dexpreopt.GlobalConfig) {
4306 config.ArtApexJars = []string{"some-platform-lib"}
4307 }
4308 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4309
4310 // platform jar in the framework boot image => ok
4311 transform = func(config *dexpreopt.GlobalConfig) {
4312 config.BootJars = []string{"some-platform-lib"}
4313 }
4314 testNoUpdatableJarsInBootImage(t, "", bp, transform)
4315}
4316
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004317func TestMain(m *testing.M) {
4318 run := func() int {
4319 setUp()
4320 defer tearDown()
4321
4322 return m.Run()
4323 }
4324
4325 os.Exit(run())
4326}