blob: 8807d00c458557f179e08a304d9e805be2f510ba [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,
Paul Duffindddd5462020-04-07 15:25:44 +0100162 "prebuilt.so": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800163 "vendor/foo/devkeys/test.x509.pem": nil,
164 "vendor/foo/devkeys/test.pk8": nil,
165 "testkey.x509.pem": nil,
166 "testkey.pk8": nil,
167 "testkey.override.x509.pem": nil,
168 "testkey.override.pk8": nil,
169 "vendor/foo/devkeys/testkey.avbpubkey": nil,
170 "vendor/foo/devkeys/testkey.pem": nil,
171 "NOTICE": nil,
172 "custom_notice": nil,
Jiyong Park9918e1a2020-03-17 19:16:40 +0900173 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800174 "testkey2.avbpubkey": nil,
175 "testkey2.pem": nil,
176 "myapex-arm64.apex": nil,
177 "myapex-arm.apex": nil,
178 "frameworks/base/api/current.txt": nil,
179 "framework/aidl/a.aidl": nil,
180 "build/make/core/proguard.flags": nil,
181 "build/make/core/proguard_basic_keeps.flags": nil,
182 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900183 }
184
Colin Crossf9aabd72020-02-15 11:29:50 -0800185 cc.GatherRequiredFilesForTest(fs)
186
Jooyung Han344d5432019-08-23 11:17:39 +0900187 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800188 // The fs now needs to be populated before creating the config, call handlers twice
189 // for now, once to get any fs changes, and later after the config was created to
190 // set product variables or targets.
191 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
192 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900193 }
194
Colin Cross98be1bb2019-12-13 20:41:13 -0800195 config := android.TestArchConfig(buildDir, nil, bp, fs)
196 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
197 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
198 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
199 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
200 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
201 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
202
203 for _, handler := range handlers {
204 // The fs now needs to be populated before creating the config, call handlers twice
205 // for now, earlier to get any fs changes, and now after the config was created to
206 // set product variables or targets.
207 tempFS := map[string][]byte{}
208 handler(tempFS, config)
209 }
210
211 ctx := android.NewTestArchContext()
212 ctx.RegisterModuleType("apex", BundleFactory)
213 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
214 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
215 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
216 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
217 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
218 ctx.RegisterModuleType("override_apex", overrideApexFactory)
219
Jooyung Hana57af4a2020-01-23 05:36:59 +0000220 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
221 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
222
Paul Duffin77980a82019-12-19 16:01:36 +0000223 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800224 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800225 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
226 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800227 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000228 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800229 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800230 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000231 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000232 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000233 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900234 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800235
Colin Cross98be1bb2019-12-13 20:41:13 -0800236 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800237 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800238
239 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900240
Jooyung Han5c998b92019-06-27 11:30:33 +0900241 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242}
243
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700244func setUp() {
245 var err error
246 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700248 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900249 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900250}
251
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700252func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900253 os.RemoveAll(buildDir)
254}
255
Jooyung Han643adc42020-02-27 13:50:06 +0900256// ensure that 'result' equals 'expected'
257func ensureEquals(t *testing.T, result string, expected string) {
258 t.Helper()
259 if result != expected {
260 t.Errorf("%q != %q", expected, result)
261 }
262}
263
Jiyong Park25fc6a92018-11-18 18:02:45 +0900264// ensure that 'result' contains 'expected'
265func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900266 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900267 if !strings.Contains(result, expected) {
268 t.Errorf("%q is not found in %q", expected, result)
269 }
270}
271
272// ensures that 'result' does not contain 'notExpected'
273func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900274 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900275 if strings.Contains(result, notExpected) {
276 t.Errorf("%q is found in %q", notExpected, result)
277 }
278}
279
280func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900281 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900282 if !android.InList(expected, result) {
283 t.Errorf("%q is not found in %v", expected, result)
284 }
285}
286
287func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900288 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900289 if android.InList(notExpected, result) {
290 t.Errorf("%q is found in %v", notExpected, result)
291 }
292}
293
Jooyung Hane1633032019-08-01 17:41:43 +0900294func ensureListEmpty(t *testing.T, result []string) {
295 t.Helper()
296 if len(result) > 0 {
297 t.Errorf("%q is expected to be empty", result)
298 }
299}
300
Jiyong Park25fc6a92018-11-18 18:02:45 +0900301// Minimal test
302func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700303 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900304 apex_defaults {
305 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900306 manifest: ":myapex.manifest",
307 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308 key: "myapex.key",
309 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800310 multilib: {
311 both: {
312 binaries: ["foo",],
313 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900314 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900315 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900316 }
317
Jiyong Park30ca9372019-02-07 16:27:23 +0900318 apex {
319 name: "myapex",
320 defaults: ["myapex-defaults"],
321 }
322
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 apex_key {
324 name: "myapex.key",
325 public_key: "testkey.avbpubkey",
326 private_key: "testkey.pem",
327 }
328
Jiyong Park809bb722019-02-13 21:33:49 +0900329 filegroup {
330 name: "myapex.manifest",
331 srcs: ["apex_manifest.json"],
332 }
333
334 filegroup {
335 name: "myapex.androidmanifest",
336 srcs: ["AndroidManifest.xml"],
337 }
338
Jiyong Park25fc6a92018-11-18 18:02:45 +0900339 cc_library {
340 name: "mylib",
341 srcs: ["mylib.cpp"],
342 shared_libs: ["mylib2"],
343 system_shared_libs: [],
344 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000345 // TODO: remove //apex_available:platform
346 apex_available: [
347 "//apex_available:platform",
348 "myapex",
349 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900350 }
351
Alex Light3d673592019-01-18 14:37:31 -0800352 cc_binary {
353 name: "foo",
354 srcs: ["mylib.cpp"],
355 compile_multilib: "both",
356 multilib: {
357 lib32: {
358 suffix: "32",
359 },
360 lib64: {
361 suffix: "64",
362 },
363 },
364 symlinks: ["foo_link_"],
365 symlink_preferred_arch: true,
366 system_shared_libs: [],
367 static_executable: true,
368 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000369 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800370 }
371
Paul Duffindddd5462020-04-07 15:25:44 +0100372 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900373 name: "mylib2",
374 srcs: ["mylib.cpp"],
375 system_shared_libs: [],
376 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900377 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900378 static_libs: ["libstatic"],
379 // TODO: remove //apex_available:platform
380 apex_available: [
381 "//apex_available:platform",
382 "myapex",
383 ],
384 }
385
Paul Duffindddd5462020-04-07 15:25:44 +0100386 cc_prebuilt_library_shared {
387 name: "mylib2",
388 srcs: ["prebuilt.so"],
389 // TODO: remove //apex_available:platform
390 apex_available: [
391 "//apex_available:platform",
392 "myapex",
393 ],
394 }
395
Jiyong Park9918e1a2020-03-17 19:16:40 +0900396 cc_library_static {
397 name: "libstatic",
398 srcs: ["mylib.cpp"],
399 system_shared_libs: [],
400 stl: "none",
401 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000402 // TODO: remove //apex_available:platform
403 apex_available: [
404 "//apex_available:platform",
405 "myapex",
406 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900407 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900408
409 java_library {
410 name: "myjar",
411 srcs: ["foo/bar/MyClass.java"],
412 sdk_version: "none",
413 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900414 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900415 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000416 // TODO: remove //apex_available:platform
417 apex_available: [
418 "//apex_available:platform",
419 "myapex",
420 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900421 }
422
423 java_library {
424 name: "myotherjar",
425 srcs: ["foo/bar/MyClass.java"],
426 sdk_version: "none",
427 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900428 // TODO: remove //apex_available:platform
429 apex_available: [
430 "//apex_available:platform",
431 "myapex",
432 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900433 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900434
435 java_library {
436 name: "mysharedjar",
437 srcs: ["foo/bar/MyClass.java"],
438 sdk_version: "none",
439 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900440 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900441 `)
442
Sundong Ahnabb64432019-10-22 13:58:29 +0900443 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900444
445 optFlags := apexRule.Args["opt_flags"]
446 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700447 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900448 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900449
Jiyong Park25fc6a92018-11-18 18:02:45 +0900450 copyCmds := apexRule.Args["copy_commands"]
451
452 // Ensure that main rule creates an output
453 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
454
455 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800456 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900457 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900458
459 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800460 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900461 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900462
463 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800464 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
465 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900466 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
467 // .. but not for java libs
468 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900469 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800470
Colin Cross7113d202019-11-20 16:39:12 -0800471 // Ensure that the platform variant ends with _shared or _common
472 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
473 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900474 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
475 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900476 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
477
478 // Ensure that dynamic dependency to java libs are not included
479 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800480
481 // Ensure that all symlinks are present.
482 found_foo_link_64 := false
483 found_foo := false
484 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900485 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800486 if strings.HasSuffix(cmd, "bin/foo") {
487 found_foo = true
488 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
489 found_foo_link_64 = true
490 }
491 }
492 }
493 good := found_foo && found_foo_link_64
494 if !good {
495 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
496 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900497
Sundong Ahnabb64432019-10-22 13:58:29 +0900498 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700499 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900500 if len(noticeInputs) != 3 {
501 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900502 }
503 ensureListContains(t, noticeInputs, "NOTICE")
504 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900505 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900506
507 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 +0900508 ensureListContains(t, depsInfo, "myjar <- myapex")
509 ensureListContains(t, depsInfo, "mylib <- myapex")
510 ensureListContains(t, depsInfo, "mylib2 <- mylib")
511 ensureListContains(t, depsInfo, "myotherjar <- myjar")
512 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800513}
514
Jooyung Hanf21c7972019-12-16 22:32:06 +0900515func TestDefaults(t *testing.T) {
516 ctx, _ := testApex(t, `
517 apex_defaults {
518 name: "myapex-defaults",
519 key: "myapex.key",
520 prebuilts: ["myetc"],
521 native_shared_libs: ["mylib"],
522 java_libs: ["myjar"],
523 apps: ["AppFoo"],
524 }
525
526 prebuilt_etc {
527 name: "myetc",
528 src: "myprebuilt",
529 }
530
531 apex {
532 name: "myapex",
533 defaults: ["myapex-defaults"],
534 }
535
536 apex_key {
537 name: "myapex.key",
538 public_key: "testkey.avbpubkey",
539 private_key: "testkey.pem",
540 }
541
542 cc_library {
543 name: "mylib",
544 system_shared_libs: [],
545 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000546 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900547 }
548
549 java_library {
550 name: "myjar",
551 srcs: ["foo/bar/MyClass.java"],
552 sdk_version: "none",
553 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000554 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900555 }
556
557 android_app {
558 name: "AppFoo",
559 srcs: ["foo/bar/MyClass.java"],
560 sdk_version: "none",
561 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000562 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900563 }
564 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000565 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900566 "etc/myetc",
567 "javalib/myjar.jar",
568 "lib64/mylib.so",
569 "app/AppFoo/AppFoo.apk",
570 })
571}
572
Jooyung Han01a3ee22019-11-02 02:52:25 +0900573func TestApexManifest(t *testing.T) {
574 ctx, _ := testApex(t, `
575 apex {
576 name: "myapex",
577 key: "myapex.key",
578 }
579
580 apex_key {
581 name: "myapex.key",
582 public_key: "testkey.avbpubkey",
583 private_key: "testkey.pem",
584 }
585 `)
586
587 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900588 args := module.Rule("apexRule").Args
589 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
590 t.Error("manifest should be apex_manifest.pb, but " + manifest)
591 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900592}
593
Alex Light5098a612018-11-29 17:12:15 -0800594func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700595 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800596 apex {
597 name: "myapex",
598 key: "myapex.key",
599 payload_type: "zip",
600 native_shared_libs: ["mylib"],
601 }
602
603 apex_key {
604 name: "myapex.key",
605 public_key: "testkey.avbpubkey",
606 private_key: "testkey.pem",
607 }
608
609 cc_library {
610 name: "mylib",
611 srcs: ["mylib.cpp"],
612 shared_libs: ["mylib2"],
613 system_shared_libs: [],
614 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000615 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800616 }
617
618 cc_library {
619 name: "mylib2",
620 srcs: ["mylib.cpp"],
621 system_shared_libs: [],
622 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000623 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800624 }
625 `)
626
Sundong Ahnabb64432019-10-22 13:58:29 +0900627 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800628 copyCmds := zipApexRule.Args["copy_commands"]
629
630 // Ensure that main rule creates an output
631 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
632
633 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800634 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800635
636 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800637 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800638
639 // Ensure that both direct and indirect deps are copied into apex
640 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
641 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900642}
643
644func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700645 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900646 apex {
647 name: "myapex",
648 key: "myapex.key",
649 native_shared_libs: ["mylib", "mylib3"],
650 }
651
652 apex_key {
653 name: "myapex.key",
654 public_key: "testkey.avbpubkey",
655 private_key: "testkey.pem",
656 }
657
658 cc_library {
659 name: "mylib",
660 srcs: ["mylib.cpp"],
661 shared_libs: ["mylib2", "mylib3"],
662 system_shared_libs: [],
663 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000664 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900665 }
666
667 cc_library {
668 name: "mylib2",
669 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900670 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900671 system_shared_libs: [],
672 stl: "none",
673 stubs: {
674 versions: ["1", "2", "3"],
675 },
676 }
677
678 cc_library {
679 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900680 srcs: ["mylib.cpp"],
681 shared_libs: ["mylib4"],
682 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900683 stl: "none",
684 stubs: {
685 versions: ["10", "11", "12"],
686 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000687 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900688 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900689
690 cc_library {
691 name: "mylib4",
692 srcs: ["mylib.cpp"],
693 system_shared_libs: [],
694 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000695 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900696 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900697 `)
698
Sundong Ahnabb64432019-10-22 13:58:29 +0900699 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900700 copyCmds := apexRule.Args["copy_commands"]
701
702 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800703 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900704
705 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800706 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900707
708 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800709 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900710
Colin Cross7113d202019-11-20 16:39:12 -0800711 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900712
713 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900714 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900715 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900716 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900717
718 // 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 -0800719 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900720 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800721 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900722
723 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900724 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900725 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900726
727 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900728 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900729
Jooyung Hana57af4a2020-01-23 05:36:59 +0000730 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900731 "lib64/mylib.so",
732 "lib64/mylib3.so",
733 "lib64/mylib4.so",
734 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900735}
736
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900737func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700738 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900739 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900740 name: "myapex2",
741 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900742 native_shared_libs: ["mylib"],
743 }
744
745 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900746 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900747 public_key: "testkey.avbpubkey",
748 private_key: "testkey.pem",
749 }
750
751 cc_library {
752 name: "mylib",
753 srcs: ["mylib.cpp"],
754 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900755 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900756 system_shared_libs: [],
757 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000758 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900759 }
760
761 cc_library {
762 name: "libfoo",
763 srcs: ["mylib.cpp"],
764 shared_libs: ["libbar"],
765 system_shared_libs: [],
766 stl: "none",
767 stubs: {
768 versions: ["10", "20", "30"],
769 },
770 }
771
772 cc_library {
773 name: "libbar",
774 srcs: ["mylib.cpp"],
775 system_shared_libs: [],
776 stl: "none",
777 }
778
Jiyong Park678c8812020-02-07 17:25:49 +0900779 cc_library_static {
780 name: "libbaz",
781 srcs: ["mylib.cpp"],
782 system_shared_libs: [],
783 stl: "none",
784 apex_available: [ "myapex2" ],
785 }
786
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900787 `)
788
Jiyong Park83dc74b2020-01-14 18:38:44 +0900789 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900790 copyCmds := apexRule.Args["copy_commands"]
791
792 // Ensure that direct non-stubs dep is always included
793 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
794
795 // Ensure that indirect stubs dep is not included
796 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
797
798 // Ensure that dependency of stubs is not included
799 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
800
Jiyong Park83dc74b2020-01-14 18:38:44 +0900801 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900802
803 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900804 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900805 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900806 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900807
Jiyong Park3ff16992019-12-27 14:11:47 +0900808 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900809
810 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
811 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900812
813 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 +0900814
815 ensureListContains(t, depsInfo, "mylib <- myapex2")
816 ensureListContains(t, depsInfo, "libbaz <- mylib")
817 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900818}
819
Jooyung Hand3639552019-08-09 12:57:43 +0900820func TestApexWithRuntimeLibsDependency(t *testing.T) {
821 /*
822 myapex
823 |
824 v (runtime_libs)
825 mylib ------+------> libfoo [provides stub]
826 |
827 `------> libbar
828 */
829 ctx, _ := testApex(t, `
830 apex {
831 name: "myapex",
832 key: "myapex.key",
833 native_shared_libs: ["mylib"],
834 }
835
836 apex_key {
837 name: "myapex.key",
838 public_key: "testkey.avbpubkey",
839 private_key: "testkey.pem",
840 }
841
842 cc_library {
843 name: "mylib",
844 srcs: ["mylib.cpp"],
845 runtime_libs: ["libfoo", "libbar"],
846 system_shared_libs: [],
847 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000848 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900849 }
850
851 cc_library {
852 name: "libfoo",
853 srcs: ["mylib.cpp"],
854 system_shared_libs: [],
855 stl: "none",
856 stubs: {
857 versions: ["10", "20", "30"],
858 },
859 }
860
861 cc_library {
862 name: "libbar",
863 srcs: ["mylib.cpp"],
864 system_shared_libs: [],
865 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000866 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900867 }
868
869 `)
870
Sundong Ahnabb64432019-10-22 13:58:29 +0900871 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900872 copyCmds := apexRule.Args["copy_commands"]
873
874 // Ensure that direct non-stubs dep is always included
875 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
876
877 // Ensure that indirect stubs dep is not included
878 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
879
880 // Ensure that runtime_libs dep in included
881 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
882
Sundong Ahnabb64432019-10-22 13:58:29 +0900883 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900884 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
885 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900886
887}
888
Jooyung Han61b66e92020-03-21 14:21:46 +0000889func TestApexDependsOnLLNDKTransitively(t *testing.T) {
890 testcases := []struct {
891 name string
892 minSdkVersion string
893 shouldLink string
894 shouldNotLink []string
895 }{
896 {
Jooyung Han75568392020-03-20 04:29:24 +0900897 name: "should link to the latest",
Jooyung Han61b66e92020-03-21 14:21:46 +0000898 minSdkVersion: "current",
899 shouldLink: "30",
900 shouldNotLink: []string{"29"},
901 },
902 {
903 name: "should link to llndk#29",
904 minSdkVersion: "29",
905 shouldLink: "29",
906 shouldNotLink: []string{"30"},
907 },
908 }
909 for _, tc := range testcases {
910 t.Run(tc.name, func(t *testing.T) {
911 ctx, _ := testApex(t, `
912 apex {
913 name: "myapex",
914 key: "myapex.key",
915 use_vendor: true,
916 native_shared_libs: ["mylib"],
917 min_sdk_version: "`+tc.minSdkVersion+`",
918 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900919
Jooyung Han61b66e92020-03-21 14:21:46 +0000920 apex_key {
921 name: "myapex.key",
922 public_key: "testkey.avbpubkey",
923 private_key: "testkey.pem",
924 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900925
Jooyung Han61b66e92020-03-21 14:21:46 +0000926 cc_library {
927 name: "mylib",
928 srcs: ["mylib.cpp"],
929 vendor_available: true,
930 shared_libs: ["libbar"],
931 system_shared_libs: [],
932 stl: "none",
933 apex_available: [ "myapex" ],
934 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900935
Jooyung Han61b66e92020-03-21 14:21:46 +0000936 cc_library {
937 name: "libbar",
938 srcs: ["mylib.cpp"],
939 system_shared_libs: [],
940 stl: "none",
941 stubs: { versions: ["29","30"] },
942 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900943
Jooyung Han61b66e92020-03-21 14:21:46 +0000944 llndk_library {
945 name: "libbar",
946 symbol_file: "",
947 }
948 `, func(fs map[string][]byte, config android.Config) {
949 setUseVendorWhitelistForTest(config, []string{"myapex"})
950 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +0900951
Jooyung Han61b66e92020-03-21 14:21:46 +0000952 // Ensure that LLNDK dep is not included
953 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
954 "lib64/mylib.so",
955 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900956
Jooyung Han61b66e92020-03-21 14:21:46 +0000957 // Ensure that LLNDK dep is required
958 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
959 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
960 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900961
Jooyung Han61b66e92020-03-21 14:21:46 +0000962 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
963 ensureContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
964 for _, ver := range tc.shouldNotLink {
965 ensureNotContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
966 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900967
Jooyung Han61b66e92020-03-21 14:21:46 +0000968 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
969 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
970 })
971 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900972}
973
Jiyong Park25fc6a92018-11-18 18:02:45 +0900974func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700975 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900976 apex {
977 name: "myapex",
978 key: "myapex.key",
979 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
980 }
981
982 apex_key {
983 name: "myapex.key",
984 public_key: "testkey.avbpubkey",
985 private_key: "testkey.pem",
986 }
987
988 cc_library {
989 name: "mylib",
990 srcs: ["mylib.cpp"],
991 shared_libs: ["libdl#27"],
992 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000993 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900994 }
995
996 cc_library_shared {
997 name: "mylib_shared",
998 srcs: ["mylib.cpp"],
999 shared_libs: ["libdl#27"],
1000 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001001 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001002 }
1003
1004 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001005 name: "libBootstrap",
1006 srcs: ["mylib.cpp"],
1007 stl: "none",
1008 bootstrap: true,
1009 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010 `)
1011
Sundong Ahnabb64432019-10-22 13:58:29 +09001012 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001013 copyCmds := apexRule.Args["copy_commands"]
1014
1015 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001016 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001017 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1018 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001019
1020 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001021 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001022
Colin Cross7113d202019-11-20 16:39:12 -08001023 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1024 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1025 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001026
1027 // For dependency to libc
1028 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001029 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001030 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001031 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001032 // ... Cflags from stub is correctly exported to mylib
1033 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1034 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1035
1036 // For dependency to libm
1037 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001038 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001039 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001040 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001041 // ... and is not compiling with the stub
1042 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1043 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1044
1045 // For dependency to libdl
1046 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001047 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001048 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001049 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1050 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001051 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001052 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001053 // ... Cflags from stub is correctly exported to mylib
1054 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1055 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001056
1057 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001058 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1059 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1060 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1061 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001062}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001063
Jooyung Han03b51852020-02-26 22:45:42 +09001064func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
1065 // there are three links between liba --> libz
1066 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
1067 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
1068 // 3) (platform) -> liba -> libz : this should be non-stub link
1069 ctx, _ := testApex(t, `
1070 apex {
1071 name: "myapex",
1072 key: "myapex.key",
1073 native_shared_libs: ["libx"],
1074 min_sdk_version: "2",
1075 }
1076
1077 apex {
1078 name: "otherapex",
1079 key: "myapex.key",
1080 native_shared_libs: ["liby"],
1081 min_sdk_version: "3",
1082 }
1083
1084 apex_key {
1085 name: "myapex.key",
1086 public_key: "testkey.avbpubkey",
1087 private_key: "testkey.pem",
1088 }
1089
1090 cc_library {
1091 name: "libx",
1092 shared_libs: ["liba"],
1093 system_shared_libs: [],
1094 stl: "none",
1095 apex_available: [ "myapex" ],
1096 }
1097
1098 cc_library {
1099 name: "liby",
1100 shared_libs: ["liba"],
1101 system_shared_libs: [],
1102 stl: "none",
1103 apex_available: [ "otherapex" ],
1104 }
1105
1106 cc_library {
1107 name: "liba",
1108 shared_libs: ["libz"],
1109 system_shared_libs: [],
1110 stl: "none",
1111 apex_available: [
1112 "//apex_available:anyapex",
1113 "//apex_available:platform",
1114 ],
1115 }
1116
1117 cc_library {
1118 name: "libz",
1119 system_shared_libs: [],
1120 stl: "none",
1121 stubs: {
1122 versions: ["1", "3"],
1123 },
1124 }
1125 `, withUnbundledBuild)
1126
1127 expectLink := func(from, from_variant, to, to_variant string) {
1128 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1129 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1130 }
1131 expectNoLink := func(from, from_variant, to, to_variant string) {
1132 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1133 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1134 }
1135 // platform liba is linked to non-stub version
1136 expectLink("liba", "shared", "libz", "shared")
1137 // liba in myapex is linked to #1
1138 expectLink("liba", "shared_myapex", "libz", "shared_1")
1139 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1140 expectNoLink("liba", "shared_myapex", "libz", "shared")
1141 // liba in otherapex is linked to #3
1142 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1143 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1144 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1145}
1146
Jooyung Hanaed150d2020-04-02 01:41:41 +09001147func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
1148 ctx, _ := testApex(t, `
1149 apex {
1150 name: "myapex",
1151 key: "myapex.key",
1152 native_shared_libs: ["libx"],
1153 min_sdk_version: "R",
1154 }
1155
1156 apex_key {
1157 name: "myapex.key",
1158 public_key: "testkey.avbpubkey",
1159 private_key: "testkey.pem",
1160 }
1161
1162 cc_library {
1163 name: "libx",
1164 shared_libs: ["libz"],
1165 system_shared_libs: [],
1166 stl: "none",
1167 apex_available: [ "myapex" ],
1168 }
1169
1170 cc_library {
1171 name: "libz",
1172 system_shared_libs: [],
1173 stl: "none",
1174 stubs: {
1175 versions: ["29", "R"],
1176 },
1177 }
1178 `, func(fs map[string][]byte, config android.Config) {
1179 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
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 // 9000 is quite a magic number.
1191 // Finalized SDK codenames are mapped as P(28), Q(29), ...
1192 // And, codenames which are not finalized yet(active_codenames + future_codenames) are numbered from 9000, 9001, ...
1193 // to distinguish them from finalized and future_api(10000)
1194 // In this test, "R" is assumed not finalized yet( listed in Platform_version_active_codenames) and translated into 9000
1195 // (refer android/api_levels.go)
1196 expectLink("libx", "shared_myapex", "libz", "shared_9000")
1197 expectNoLink("libx", "shared_myapex", "libz", "shared_29")
1198 expectNoLink("libx", "shared_myapex", "libz", "shared")
1199}
1200
Jooyung Han03b51852020-02-26 22:45:42 +09001201func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1202 ctx, _ := testApex(t, `
1203 apex {
1204 name: "myapex",
1205 key: "myapex.key",
1206 native_shared_libs: ["libx"],
1207 }
1208
1209 apex_key {
1210 name: "myapex.key",
1211 public_key: "testkey.avbpubkey",
1212 private_key: "testkey.pem",
1213 }
1214
1215 cc_library {
1216 name: "libx",
1217 shared_libs: ["libz"],
1218 system_shared_libs: [],
1219 stl: "none",
1220 apex_available: [ "myapex" ],
1221 }
1222
1223 cc_library {
1224 name: "libz",
1225 system_shared_libs: [],
1226 stl: "none",
1227 stubs: {
1228 versions: ["1", "2"],
1229 },
1230 }
1231 `)
1232
1233 expectLink := func(from, from_variant, to, to_variant string) {
1234 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1235 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1236 }
1237 expectNoLink := func(from, from_variant, to, to_variant string) {
1238 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1239 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1240 }
1241 expectLink("libx", "shared_myapex", "libz", "shared_2")
1242 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1243 expectNoLink("libx", "shared_myapex", "libz", "shared")
1244}
1245
1246func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1247 ctx, _ := testApex(t, `
1248 apex {
1249 name: "myapex",
1250 key: "myapex.key",
1251 native_shared_libs: ["libx"],
1252 }
1253
1254 apex_key {
1255 name: "myapex.key",
1256 public_key: "testkey.avbpubkey",
1257 private_key: "testkey.pem",
1258 }
1259
1260 cc_library {
1261 name: "libx",
1262 system_shared_libs: [],
1263 stl: "none",
1264 apex_available: [ "myapex" ],
1265 stubs: {
1266 versions: ["1", "2"],
1267 },
1268 }
1269
1270 cc_library {
1271 name: "libz",
1272 shared_libs: ["libx"],
1273 system_shared_libs: [],
1274 stl: "none",
1275 }
1276 `)
1277
1278 expectLink := func(from, from_variant, to, to_variant string) {
1279 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1280 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1281 }
1282 expectNoLink := func(from, from_variant, to, to_variant string) {
1283 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1284 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1285 }
1286 expectLink("libz", "shared", "libx", "shared_2")
1287 expectNoLink("libz", "shared", "libz", "shared_1")
1288 expectNoLink("libz", "shared", "libz", "shared")
1289}
1290
Jooyung Han75568392020-03-20 04:29:24 +09001291func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001292 ctx, _ := testApex(t, `
1293 apex {
1294 name: "myapex",
1295 key: "myapex.key",
1296 native_shared_libs: ["libx"],
1297 min_sdk_version: "29",
1298 }
1299
1300 apex_key {
1301 name: "myapex.key",
1302 public_key: "testkey.avbpubkey",
1303 private_key: "testkey.pem",
1304 }
1305
1306 cc_library {
1307 name: "libx",
1308 shared_libs: ["libbar"],
1309 apex_available: [ "myapex" ],
1310 }
1311
1312 cc_library {
1313 name: "libbar",
1314 stubs: {
1315 versions: ["29", "30"],
1316 },
1317 }
Jooyung Han75568392020-03-20 04:29:24 +09001318 `, func(fs map[string][]byte, config android.Config) {
1319 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1320 })
Jooyung Han03b51852020-02-26 22:45:42 +09001321 expectLink := func(from, from_variant, to, to_variant string) {
1322 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1323 libFlags := ld.Args["libFlags"]
1324 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1325 }
Jooyung Han75568392020-03-20 04:29:24 +09001326 expectLink("libx", "shared_hwasan_myapex", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001327}
1328
Jooyung Han75568392020-03-20 04:29:24 +09001329func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001330 ctx, _ := testApex(t, `
1331 apex {
1332 name: "myapex",
1333 key: "myapex.key",
1334 native_shared_libs: ["libx"],
1335 min_sdk_version: "29",
1336 }
1337
1338 apex_key {
1339 name: "myapex.key",
1340 public_key: "testkey.avbpubkey",
1341 private_key: "testkey.pem",
1342 }
1343
1344 cc_library {
1345 name: "libx",
1346 apex_available: [ "myapex" ],
1347 }
Jooyung Han75568392020-03-20 04:29:24 +09001348 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001349
1350 // ensure apex variant of c++ is linked with static unwinder
1351 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1352 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1353 // note that platform variant is not.
1354 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1355 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Han03b51852020-02-26 22:45:42 +09001356}
1357
1358func TestInvalidMinSdkVersion(t *testing.T) {
Jooyung Han75568392020-03-20 04:29:24 +09001359 testApexError(t, `"libz" .*: not found a version\(<=29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001360 apex {
1361 name: "myapex",
1362 key: "myapex.key",
1363 native_shared_libs: ["libx"],
1364 min_sdk_version: "29",
1365 }
1366
1367 apex_key {
1368 name: "myapex.key",
1369 public_key: "testkey.avbpubkey",
1370 private_key: "testkey.pem",
1371 }
1372
1373 cc_library {
1374 name: "libx",
1375 shared_libs: ["libz"],
1376 system_shared_libs: [],
1377 stl: "none",
1378 apex_available: [ "myapex" ],
1379 }
1380
1381 cc_library {
1382 name: "libz",
1383 system_shared_libs: [],
1384 stl: "none",
1385 stubs: {
1386 versions: ["30"],
1387 },
1388 }
Jooyung Han75568392020-03-20 04:29:24 +09001389 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001390
Jooyung Hanaed150d2020-04-02 01:41:41 +09001391 testApexError(t, `"myapex" .*: min_sdk_version: SDK version should be .*`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001392 apex {
1393 name: "myapex",
1394 key: "myapex.key",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001395 min_sdk_version: "abc",
Jooyung Han03b51852020-02-26 22:45:42 +09001396 }
1397
1398 apex_key {
1399 name: "myapex.key",
1400 public_key: "testkey.avbpubkey",
1401 private_key: "testkey.pem",
1402 }
1403 `)
1404}
1405
Jiyong Park7c2ee712018-12-07 00:42:25 +09001406func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001407 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001408 apex {
1409 name: "myapex",
1410 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001411 native_shared_libs: ["mylib"],
1412 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001413 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001414 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001415 }
1416
1417 apex_key {
1418 name: "myapex.key",
1419 public_key: "testkey.avbpubkey",
1420 private_key: "testkey.pem",
1421 }
1422
1423 prebuilt_etc {
1424 name: "myetc",
1425 src: "myprebuilt",
1426 sub_dir: "foo/bar",
1427 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001428
1429 cc_library {
1430 name: "mylib",
1431 srcs: ["mylib.cpp"],
1432 relative_install_path: "foo/bar",
1433 system_shared_libs: [],
1434 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001435 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001436 }
1437
1438 cc_binary {
1439 name: "mybin",
1440 srcs: ["mylib.cpp"],
1441 relative_install_path: "foo/bar",
1442 system_shared_libs: [],
1443 static_executable: true,
1444 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001445 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001446 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001447 `)
1448
Sundong Ahnabb64432019-10-22 13:58:29 +09001449 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001450 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1451
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001452 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001453 ensureListContains(t, dirs, "etc")
1454 ensureListContains(t, dirs, "etc/foo")
1455 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001456 ensureListContains(t, dirs, "lib64")
1457 ensureListContains(t, dirs, "lib64/foo")
1458 ensureListContains(t, dirs, "lib64/foo/bar")
1459 ensureListContains(t, dirs, "lib")
1460 ensureListContains(t, dirs, "lib/foo")
1461 ensureListContains(t, dirs, "lib/foo/bar")
1462
Jiyong Parkbd13e442019-03-15 18:10:35 +09001463 ensureListContains(t, dirs, "bin")
1464 ensureListContains(t, dirs, "bin/foo")
1465 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001466}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001467
Jooyung Han35155c42020-02-06 17:33:20 +09001468func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
1469 ctx, _ := testApex(t, `
1470 apex {
1471 name: "myapex",
1472 key: "myapex.key",
1473 multilib: {
1474 both: {
1475 native_shared_libs: ["mylib"],
1476 binaries: ["mybin"],
1477 },
1478 },
1479 compile_multilib: "both",
1480 native_bridge_supported: true,
1481 }
1482
1483 apex_key {
1484 name: "myapex.key",
1485 public_key: "testkey.avbpubkey",
1486 private_key: "testkey.pem",
1487 }
1488
1489 cc_library {
1490 name: "mylib",
1491 relative_install_path: "foo/bar",
1492 system_shared_libs: [],
1493 stl: "none",
1494 apex_available: [ "myapex" ],
1495 native_bridge_supported: true,
1496 }
1497
1498 cc_binary {
1499 name: "mybin",
1500 relative_install_path: "foo/bar",
1501 system_shared_libs: [],
1502 static_executable: true,
1503 stl: "none",
1504 apex_available: [ "myapex" ],
1505 native_bridge_supported: true,
1506 compile_multilib: "both", // default is "first" for binary
1507 multilib: {
1508 lib64: {
1509 suffix: "64",
1510 },
1511 },
1512 }
1513 `, withNativeBridgeEnabled)
1514 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1515 "bin/foo/bar/mybin",
1516 "bin/foo/bar/mybin64",
1517 "bin/arm/foo/bar/mybin",
1518 "bin/arm64/foo/bar/mybin64",
1519 "lib/foo/bar/mylib.so",
1520 "lib/arm/foo/bar/mylib.so",
1521 "lib64/foo/bar/mylib.so",
1522 "lib64/arm64/foo/bar/mylib.so",
1523 })
1524}
1525
Jiyong Parkda6eb592018-12-19 17:12:36 +09001526func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001527 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001528 apex {
1529 name: "myapex",
1530 key: "myapex.key",
1531 native_shared_libs: ["mylib"],
1532 use_vendor: true,
1533 }
1534
1535 apex_key {
1536 name: "myapex.key",
1537 public_key: "testkey.avbpubkey",
1538 private_key: "testkey.pem",
1539 }
1540
1541 cc_library {
1542 name: "mylib",
1543 srcs: ["mylib.cpp"],
1544 shared_libs: ["mylib2"],
1545 system_shared_libs: [],
1546 vendor_available: true,
1547 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001548 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001549 }
1550
1551 cc_library {
1552 name: "mylib2",
1553 srcs: ["mylib.cpp"],
1554 system_shared_libs: [],
1555 vendor_available: true,
1556 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001557 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001558 }
Jooyung Handc782442019-11-01 03:14:38 +09001559 `, func(fs map[string][]byte, config android.Config) {
1560 setUseVendorWhitelistForTest(config, []string{"myapex"})
1561 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001562
1563 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001564 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001565 for _, implicit := range i.Implicits {
1566 inputsList = append(inputsList, implicit.String())
1567 }
1568 }
1569 inputsString := strings.Join(inputsList, " ")
1570
1571 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001572 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1573 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001574
1575 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001576 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1577 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001578}
Jiyong Park16e91a02018-12-20 18:18:08 +09001579
Jooyung Handc782442019-11-01 03:14:38 +09001580func TestUseVendorRestriction(t *testing.T) {
1581 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1582 apex {
1583 name: "myapex",
1584 key: "myapex.key",
1585 use_vendor: true,
1586 }
1587 apex_key {
1588 name: "myapex.key",
1589 public_key: "testkey.avbpubkey",
1590 private_key: "testkey.pem",
1591 }
1592 `, func(fs map[string][]byte, config android.Config) {
1593 setUseVendorWhitelistForTest(config, []string{""})
1594 })
1595 // no error with whitelist
1596 testApex(t, `
1597 apex {
1598 name: "myapex",
1599 key: "myapex.key",
1600 use_vendor: true,
1601 }
1602 apex_key {
1603 name: "myapex.key",
1604 public_key: "testkey.avbpubkey",
1605 private_key: "testkey.pem",
1606 }
1607 `, func(fs map[string][]byte, config android.Config) {
1608 setUseVendorWhitelistForTest(config, []string{"myapex"})
1609 })
1610}
1611
Jooyung Han5c998b92019-06-27 11:30:33 +09001612func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1613 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1614 apex {
1615 name: "myapex",
1616 key: "myapex.key",
1617 native_shared_libs: ["mylib"],
1618 use_vendor: true,
1619 }
1620
1621 apex_key {
1622 name: "myapex.key",
1623 public_key: "testkey.avbpubkey",
1624 private_key: "testkey.pem",
1625 }
1626
1627 cc_library {
1628 name: "mylib",
1629 srcs: ["mylib.cpp"],
1630 system_shared_libs: [],
1631 stl: "none",
1632 }
1633 `)
1634}
1635
Jiyong Park16e91a02018-12-20 18:18:08 +09001636func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001637 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001638 apex {
1639 name: "myapex",
1640 key: "myapex.key",
1641 native_shared_libs: ["mylib"],
1642 }
1643
1644 apex_key {
1645 name: "myapex.key",
1646 public_key: "testkey.avbpubkey",
1647 private_key: "testkey.pem",
1648 }
1649
1650 cc_library {
1651 name: "mylib",
1652 srcs: ["mylib.cpp"],
1653 system_shared_libs: [],
1654 stl: "none",
1655 stubs: {
1656 versions: ["1", "2", "3"],
1657 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001658 apex_available: [
1659 "//apex_available:platform",
1660 "myapex",
1661 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001662 }
1663
1664 cc_binary {
1665 name: "not_in_apex",
1666 srcs: ["mylib.cpp"],
1667 static_libs: ["mylib"],
1668 static_executable: true,
1669 system_shared_libs: [],
1670 stl: "none",
1671 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001672 `)
1673
Colin Cross7113d202019-11-20 16:39:12 -08001674 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001675
1676 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001677 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001678}
Jiyong Park9335a262018-12-24 11:31:58 +09001679
1680func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001681 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001682 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001683 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001684 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001685 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001686 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001687 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001688 }
1689
1690 cc_library {
1691 name: "mylib",
1692 srcs: ["mylib.cpp"],
1693 system_shared_libs: [],
1694 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001695 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001696 }
1697
1698 apex_key {
1699 name: "myapex.key",
1700 public_key: "testkey.avbpubkey",
1701 private_key: "testkey.pem",
1702 }
1703
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001704 android_app_certificate {
1705 name: "myapex.certificate",
1706 certificate: "testkey",
1707 }
1708
1709 android_app_certificate {
1710 name: "myapex.certificate.override",
1711 certificate: "testkey.override",
1712 }
1713
Jiyong Park9335a262018-12-24 11:31:58 +09001714 `)
1715
1716 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001717 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001718
1719 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1720 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1721 "vendor/foo/devkeys/testkey.avbpubkey")
1722 }
1723 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1724 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1725 "vendor/foo/devkeys/testkey.pem")
1726 }
1727
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001728 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001729 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001730 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001731 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001732 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001733 }
1734}
Jiyong Park58e364a2019-01-19 19:24:06 +09001735
Jooyung Hanf121a652019-12-17 14:30:11 +09001736func TestCertificate(t *testing.T) {
1737 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1738 ctx, _ := testApex(t, `
1739 apex {
1740 name: "myapex",
1741 key: "myapex.key",
1742 }
1743 apex_key {
1744 name: "myapex.key",
1745 public_key: "testkey.avbpubkey",
1746 private_key: "testkey.pem",
1747 }`)
1748 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1749 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1750 if actual := rule.Args["certificates"]; actual != expected {
1751 t.Errorf("certificates should be %q, not %q", expected, actual)
1752 }
1753 })
1754 t.Run("override when unspecified", func(t *testing.T) {
1755 ctx, _ := testApex(t, `
1756 apex {
1757 name: "myapex_keytest",
1758 key: "myapex.key",
1759 file_contexts: ":myapex-file_contexts",
1760 }
1761 apex_key {
1762 name: "myapex.key",
1763 public_key: "testkey.avbpubkey",
1764 private_key: "testkey.pem",
1765 }
1766 android_app_certificate {
1767 name: "myapex.certificate.override",
1768 certificate: "testkey.override",
1769 }`)
1770 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1771 expected := "testkey.override.x509.pem testkey.override.pk8"
1772 if actual := rule.Args["certificates"]; actual != expected {
1773 t.Errorf("certificates should be %q, not %q", expected, actual)
1774 }
1775 })
1776 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1777 ctx, _ := testApex(t, `
1778 apex {
1779 name: "myapex",
1780 key: "myapex.key",
1781 certificate: ":myapex.certificate",
1782 }
1783 apex_key {
1784 name: "myapex.key",
1785 public_key: "testkey.avbpubkey",
1786 private_key: "testkey.pem",
1787 }
1788 android_app_certificate {
1789 name: "myapex.certificate",
1790 certificate: "testkey",
1791 }`)
1792 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1793 expected := "testkey.x509.pem testkey.pk8"
1794 if actual := rule.Args["certificates"]; actual != expected {
1795 t.Errorf("certificates should be %q, not %q", expected, actual)
1796 }
1797 })
1798 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1799 ctx, _ := testApex(t, `
1800 apex {
1801 name: "myapex_keytest",
1802 key: "myapex.key",
1803 file_contexts: ":myapex-file_contexts",
1804 certificate: ":myapex.certificate",
1805 }
1806 apex_key {
1807 name: "myapex.key",
1808 public_key: "testkey.avbpubkey",
1809 private_key: "testkey.pem",
1810 }
1811 android_app_certificate {
1812 name: "myapex.certificate.override",
1813 certificate: "testkey.override",
1814 }`)
1815 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1816 expected := "testkey.override.x509.pem testkey.override.pk8"
1817 if actual := rule.Args["certificates"]; actual != expected {
1818 t.Errorf("certificates should be %q, not %q", expected, actual)
1819 }
1820 })
1821 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1822 ctx, _ := testApex(t, `
1823 apex {
1824 name: "myapex",
1825 key: "myapex.key",
1826 certificate: "testkey",
1827 }
1828 apex_key {
1829 name: "myapex.key",
1830 public_key: "testkey.avbpubkey",
1831 private_key: "testkey.pem",
1832 }`)
1833 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1834 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1835 if actual := rule.Args["certificates"]; actual != expected {
1836 t.Errorf("certificates should be %q, not %q", expected, actual)
1837 }
1838 })
1839 t.Run("override when specified as <name>", func(t *testing.T) {
1840 ctx, _ := testApex(t, `
1841 apex {
1842 name: "myapex_keytest",
1843 key: "myapex.key",
1844 file_contexts: ":myapex-file_contexts",
1845 certificate: "testkey",
1846 }
1847 apex_key {
1848 name: "myapex.key",
1849 public_key: "testkey.avbpubkey",
1850 private_key: "testkey.pem",
1851 }
1852 android_app_certificate {
1853 name: "myapex.certificate.override",
1854 certificate: "testkey.override",
1855 }`)
1856 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1857 expected := "testkey.override.x509.pem testkey.override.pk8"
1858 if actual := rule.Args["certificates"]; actual != expected {
1859 t.Errorf("certificates should be %q, not %q", expected, actual)
1860 }
1861 })
1862}
1863
Jiyong Park58e364a2019-01-19 19:24:06 +09001864func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001865 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001866 apex {
1867 name: "myapex",
1868 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001869 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001870 }
1871
1872 apex {
1873 name: "otherapex",
1874 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09001875 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09001876 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09001877 }
1878
1879 apex_key {
1880 name: "myapex.key",
1881 public_key: "testkey.avbpubkey",
1882 private_key: "testkey.pem",
1883 }
1884
1885 cc_library {
1886 name: "mylib",
1887 srcs: ["mylib.cpp"],
1888 system_shared_libs: [],
1889 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001890 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001891 "myapex",
1892 "otherapex",
1893 ],
Jooyung Han24282772020-03-21 23:20:55 +09001894 recovery_available: true,
Jiyong Park58e364a2019-01-19 19:24:06 +09001895 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09001896 cc_library {
1897 name: "mylib2",
1898 srcs: ["mylib.cpp"],
1899 system_shared_libs: [],
1900 stl: "none",
1901 apex_available: [
1902 "myapex",
1903 "otherapex",
1904 ],
1905 use_apex_name_macro: true,
1906 }
Jiyong Park58e364a2019-01-19 19:24:06 +09001907 `)
1908
Jooyung Hanc87a0592020-03-02 17:44:33 +09001909 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001910 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001911 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han75568392020-03-20 04:29:24 +09001912 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001913
Jooyung Hanccce2f22020-03-07 03:45:53 +09001914 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001915 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1916 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001917 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09001918 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09001919
Jooyung Hanccce2f22020-03-07 03:45:53 +09001920 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09001921 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
1922 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09001923 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09001924 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001925
Jooyung Hanc87a0592020-03-02 17:44:33 +09001926 // When cc_library sets use_apex_name_macro: true
1927 // apex variants define additional macro to distinguish which apex variant it is built for
1928
1929 // non-APEX variant does not have __ANDROID_APEX__ defined
1930 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1931 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1932
1933 // APEX variant has __ANDROID_APEX__ defined
1934 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001935 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001936 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1937 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001938
Jooyung Hanc87a0592020-03-02 17:44:33 +09001939 // APEX variant has __ANDROID_APEX__ defined
1940 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001941 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001942 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1943 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09001944
1945 // recovery variant does not set __ANDROID_SDK_VERSION__
1946 mylibCFlags = ctx.ModuleForTests("mylib", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1947 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
1948 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001949}
Jiyong Park7e636d02019-01-28 16:16:54 +09001950
1951func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001952 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001953 apex {
1954 name: "myapex",
1955 key: "myapex.key",
1956 native_shared_libs: ["mylib"],
1957 }
1958
1959 apex_key {
1960 name: "myapex.key",
1961 public_key: "testkey.avbpubkey",
1962 private_key: "testkey.pem",
1963 }
1964
1965 cc_library_headers {
1966 name: "mylib_headers",
1967 export_include_dirs: ["my_include"],
1968 system_shared_libs: [],
1969 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001970 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001971 }
1972
1973 cc_library {
1974 name: "mylib",
1975 srcs: ["mylib.cpp"],
1976 system_shared_libs: [],
1977 stl: "none",
1978 header_libs: ["mylib_headers"],
1979 export_header_lib_headers: ["mylib_headers"],
1980 stubs: {
1981 versions: ["1", "2", "3"],
1982 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001983 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001984 }
1985
1986 cc_library {
1987 name: "otherlib",
1988 srcs: ["mylib.cpp"],
1989 system_shared_libs: [],
1990 stl: "none",
1991 shared_libs: ["mylib"],
1992 }
1993 `)
1994
Colin Cross7113d202019-11-20 16:39:12 -08001995 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001996
1997 // Ensure that the include path of the header lib is exported to 'otherlib'
1998 ensureContains(t, cFlags, "-Imy_include")
1999}
Alex Light9670d332019-01-29 18:07:33 -08002000
Jiyong Park7cd10e32020-01-14 09:22:18 +09002001type fileInApex struct {
2002 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00002003 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09002004 isLink bool
2005}
2006
Jooyung Hana57af4a2020-01-23 05:36:59 +00002007func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09002008 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00002009 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09002010 copyCmds := apexRule.Args["copy_commands"]
2011 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09002012 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09002013 for _, cmd := range strings.Split(copyCmds, "&&") {
2014 cmd = strings.TrimSpace(cmd)
2015 if cmd == "" {
2016 continue
2017 }
2018 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00002019 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09002020 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09002021 switch terms[0] {
2022 case "mkdir":
2023 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09002024 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09002025 t.Fatal("copyCmds contains invalid cp command", cmd)
2026 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002027 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002028 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002029 isLink = false
2030 case "ln":
2031 if len(terms) != 3 && len(terms) != 4 {
2032 // ln LINK TARGET or ln -s LINK TARGET
2033 t.Fatal("copyCmds contains invalid ln command", cmd)
2034 }
2035 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002036 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002037 isLink = true
2038 default:
2039 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
2040 }
2041 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09002042 index := strings.Index(dst, imageApexDir)
2043 if index == -1 {
2044 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
2045 }
2046 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002047 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09002048 }
2049 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002050 return ret
2051}
2052
Jooyung Hana57af4a2020-01-23 05:36:59 +00002053func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
2054 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09002055 var failed bool
2056 var surplus []string
2057 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00002058 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09002059 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09002060 for _, expected := range files {
2061 if matched, _ := path.Match(expected, file.path); matched {
2062 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09002063 mactchFound = true
2064 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09002065 }
2066 }
Jooyung Hane6436d72020-02-27 13:31:56 +09002067 if !mactchFound {
2068 surplus = append(surplus, file.path)
2069 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002070 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002071
Jooyung Han31c470b2019-10-18 16:26:59 +09002072 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002073 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09002074 t.Log("surplus files", surplus)
2075 failed = true
2076 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002077
2078 if len(files) > len(filesMatched) {
2079 var missing []string
2080 for _, expected := range files {
2081 if !filesMatched[expected] {
2082 missing = append(missing, expected)
2083 }
2084 }
2085 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09002086 t.Log("missing files", missing)
2087 failed = true
2088 }
2089 if failed {
2090 t.Fail()
2091 }
2092}
2093
Jooyung Han344d5432019-08-23 11:17:39 +09002094func TestVndkApexCurrent(t *testing.T) {
2095 ctx, _ := testApex(t, `
2096 apex_vndk {
2097 name: "myapex",
2098 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002099 }
2100
2101 apex_key {
2102 name: "myapex.key",
2103 public_key: "testkey.avbpubkey",
2104 private_key: "testkey.pem",
2105 }
2106
2107 cc_library {
2108 name: "libvndk",
2109 srcs: ["mylib.cpp"],
2110 vendor_available: true,
2111 vndk: {
2112 enabled: true,
2113 },
2114 system_shared_libs: [],
2115 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002116 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002117 }
2118
2119 cc_library {
2120 name: "libvndksp",
2121 srcs: ["mylib.cpp"],
2122 vendor_available: true,
2123 vndk: {
2124 enabled: true,
2125 support_system_process: true,
2126 },
2127 system_shared_libs: [],
2128 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002129 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002130 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002131 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002132
Jooyung Hana57af4a2020-01-23 05:36:59 +00002133 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002134 "lib/libvndk.so",
2135 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002136 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002137 "lib64/libvndk.so",
2138 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002139 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002140 "etc/llndk.libraries.VER.txt",
2141 "etc/vndkcore.libraries.VER.txt",
2142 "etc/vndksp.libraries.VER.txt",
2143 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002144 })
Jooyung Han344d5432019-08-23 11:17:39 +09002145}
2146
2147func TestVndkApexWithPrebuilt(t *testing.T) {
2148 ctx, _ := testApex(t, `
2149 apex_vndk {
2150 name: "myapex",
2151 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002152 }
2153
2154 apex_key {
2155 name: "myapex.key",
2156 public_key: "testkey.avbpubkey",
2157 private_key: "testkey.pem",
2158 }
2159
2160 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002161 name: "libvndk",
2162 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002163 vendor_available: true,
2164 vndk: {
2165 enabled: true,
2166 },
2167 system_shared_libs: [],
2168 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002169 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002170 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002171
2172 cc_prebuilt_library_shared {
2173 name: "libvndk.arm",
2174 srcs: ["libvndk.arm.so"],
2175 vendor_available: true,
2176 vndk: {
2177 enabled: true,
2178 },
2179 enabled: false,
2180 arch: {
2181 arm: {
2182 enabled: true,
2183 },
2184 },
2185 system_shared_libs: [],
2186 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002187 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002188 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002189 `+vndkLibrariesTxtFiles("current"),
2190 withFiles(map[string][]byte{
2191 "libvndk.so": nil,
2192 "libvndk.arm.so": nil,
2193 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002194
Jooyung Hana57af4a2020-01-23 05:36:59 +00002195 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002196 "lib/libvndk.so",
2197 "lib/libvndk.arm.so",
2198 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002199 "lib/libc++.so",
2200 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002201 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002202 })
Jooyung Han344d5432019-08-23 11:17:39 +09002203}
2204
Jooyung Han39edb6c2019-11-06 16:53:07 +09002205func vndkLibrariesTxtFiles(vers ...string) (result string) {
2206 for _, v := range vers {
2207 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002208 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002209 result += `
2210 vndk_libraries_txt {
2211 name: "` + txt + `.libraries.txt",
2212 }
2213 `
2214 }
2215 } else {
2216 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2217 result += `
2218 prebuilt_etc {
2219 name: "` + txt + `.libraries.` + v + `.txt",
2220 src: "dummy.txt",
2221 }
2222 `
2223 }
2224 }
2225 }
2226 return
2227}
2228
Jooyung Han344d5432019-08-23 11:17:39 +09002229func TestVndkApexVersion(t *testing.T) {
2230 ctx, _ := testApex(t, `
2231 apex_vndk {
2232 name: "myapex_v27",
2233 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002234 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002235 vndk_version: "27",
2236 }
2237
2238 apex_key {
2239 name: "myapex.key",
2240 public_key: "testkey.avbpubkey",
2241 private_key: "testkey.pem",
2242 }
2243
Jooyung Han31c470b2019-10-18 16:26:59 +09002244 vndk_prebuilt_shared {
2245 name: "libvndk27",
2246 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002247 vendor_available: true,
2248 vndk: {
2249 enabled: true,
2250 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002251 target_arch: "arm64",
2252 arch: {
2253 arm: {
2254 srcs: ["libvndk27_arm.so"],
2255 },
2256 arm64: {
2257 srcs: ["libvndk27_arm64.so"],
2258 },
2259 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002260 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002261 }
2262
2263 vndk_prebuilt_shared {
2264 name: "libvndk27",
2265 version: "27",
2266 vendor_available: true,
2267 vndk: {
2268 enabled: true,
2269 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002270 target_arch: "x86_64",
2271 arch: {
2272 x86: {
2273 srcs: ["libvndk27_x86.so"],
2274 },
2275 x86_64: {
2276 srcs: ["libvndk27_x86_64.so"],
2277 },
2278 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002279 }
2280 `+vndkLibrariesTxtFiles("27"),
2281 withFiles(map[string][]byte{
2282 "libvndk27_arm.so": nil,
2283 "libvndk27_arm64.so": nil,
2284 "libvndk27_x86.so": nil,
2285 "libvndk27_x86_64.so": nil,
2286 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002287
Jooyung Hana57af4a2020-01-23 05:36:59 +00002288 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002289 "lib/libvndk27_arm.so",
2290 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002291 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002292 })
Jooyung Han344d5432019-08-23 11:17:39 +09002293}
2294
2295func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2296 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2297 apex_vndk {
2298 name: "myapex_v27",
2299 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002300 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002301 vndk_version: "27",
2302 }
2303 apex_vndk {
2304 name: "myapex_v27_other",
2305 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002306 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002307 vndk_version: "27",
2308 }
2309
2310 apex_key {
2311 name: "myapex.key",
2312 public_key: "testkey.avbpubkey",
2313 private_key: "testkey.pem",
2314 }
2315
2316 cc_library {
2317 name: "libvndk",
2318 srcs: ["mylib.cpp"],
2319 vendor_available: true,
2320 vndk: {
2321 enabled: true,
2322 },
2323 system_shared_libs: [],
2324 stl: "none",
2325 }
2326
2327 vndk_prebuilt_shared {
2328 name: "libvndk",
2329 version: "27",
2330 vendor_available: true,
2331 vndk: {
2332 enabled: true,
2333 },
2334 srcs: ["libvndk.so"],
2335 }
2336 `, withFiles(map[string][]byte{
2337 "libvndk.so": nil,
2338 }))
2339}
2340
Jooyung Han90eee022019-10-01 20:02:42 +09002341func TestVndkApexNameRule(t *testing.T) {
2342 ctx, _ := testApex(t, `
2343 apex_vndk {
2344 name: "myapex",
2345 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002346 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002347 }
2348 apex_vndk {
2349 name: "myapex_v28",
2350 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002351 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002352 vndk_version: "28",
2353 }
2354 apex_key {
2355 name: "myapex.key",
2356 public_key: "testkey.avbpubkey",
2357 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002358 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002359
2360 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002361 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002362 actual := proptools.String(bundle.properties.Apex_name)
2363 if !reflect.DeepEqual(actual, expected) {
2364 t.Errorf("Got '%v', expected '%v'", actual, expected)
2365 }
2366 }
2367
2368 assertApexName("com.android.vndk.vVER", "myapex")
2369 assertApexName("com.android.vndk.v28", "myapex_v28")
2370}
2371
Jooyung Han344d5432019-08-23 11:17:39 +09002372func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2373 ctx, _ := testApex(t, `
2374 apex_vndk {
2375 name: "myapex",
2376 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002377 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002378 }
2379
2380 apex_key {
2381 name: "myapex.key",
2382 public_key: "testkey.avbpubkey",
2383 private_key: "testkey.pem",
2384 }
2385
2386 cc_library {
2387 name: "libvndk",
2388 srcs: ["mylib.cpp"],
2389 vendor_available: true,
2390 native_bridge_supported: true,
2391 host_supported: true,
2392 vndk: {
2393 enabled: true,
2394 },
2395 system_shared_libs: [],
2396 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002397 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002398 }
Jooyung Han35155c42020-02-06 17:33:20 +09002399 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09002400
Jooyung Hana57af4a2020-01-23 05:36:59 +00002401 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002402 "lib/libvndk.so",
2403 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002404 "lib/libc++.so",
2405 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002406 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002407 })
Jooyung Han344d5432019-08-23 11:17:39 +09002408}
2409
2410func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2411 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2412 apex_vndk {
2413 name: "myapex",
2414 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002415 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002416 native_bridge_supported: true,
2417 }
2418
2419 apex_key {
2420 name: "myapex.key",
2421 public_key: "testkey.avbpubkey",
2422 private_key: "testkey.pem",
2423 }
2424
2425 cc_library {
2426 name: "libvndk",
2427 srcs: ["mylib.cpp"],
2428 vendor_available: true,
2429 native_bridge_supported: true,
2430 host_supported: true,
2431 vndk: {
2432 enabled: true,
2433 },
2434 system_shared_libs: [],
2435 stl: "none",
2436 }
2437 `)
2438}
2439
Jooyung Han31c470b2019-10-18 16:26:59 +09002440func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002441 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002442 apex_vndk {
2443 name: "myapex_v27",
2444 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002445 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002446 vndk_version: "27",
2447 }
2448
2449 apex_key {
2450 name: "myapex.key",
2451 public_key: "testkey.avbpubkey",
2452 private_key: "testkey.pem",
2453 }
2454
2455 vndk_prebuilt_shared {
2456 name: "libvndk27",
2457 version: "27",
2458 target_arch: "arm",
2459 vendor_available: true,
2460 vndk: {
2461 enabled: true,
2462 },
2463 arch: {
2464 arm: {
2465 srcs: ["libvndk27.so"],
2466 }
2467 },
2468 }
2469
2470 vndk_prebuilt_shared {
2471 name: "libvndk27",
2472 version: "27",
2473 target_arch: "arm",
2474 binder32bit: true,
2475 vendor_available: true,
2476 vndk: {
2477 enabled: true,
2478 },
2479 arch: {
2480 arm: {
2481 srcs: ["libvndk27binder32.so"],
2482 }
2483 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002484 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002485 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002486 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002487 withFiles(map[string][]byte{
2488 "libvndk27.so": nil,
2489 "libvndk27binder32.so": nil,
2490 }),
2491 withBinder32bit,
2492 withTargets(map[android.OsType][]android.Target{
2493 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09002494 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
2495 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09002496 },
2497 }),
2498 )
2499
Jooyung Hana57af4a2020-01-23 05:36:59 +00002500 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002501 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002502 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002503 })
2504}
2505
Jooyung Hane1633032019-08-01 17:41:43 +09002506func TestDependenciesInApexManifest(t *testing.T) {
2507 ctx, _ := testApex(t, `
2508 apex {
2509 name: "myapex_nodep",
2510 key: "myapex.key",
2511 native_shared_libs: ["lib_nodep"],
2512 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002513 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002514 }
2515
2516 apex {
2517 name: "myapex_dep",
2518 key: "myapex.key",
2519 native_shared_libs: ["lib_dep"],
2520 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002521 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002522 }
2523
2524 apex {
2525 name: "myapex_provider",
2526 key: "myapex.key",
2527 native_shared_libs: ["libfoo"],
2528 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002529 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002530 }
2531
2532 apex {
2533 name: "myapex_selfcontained",
2534 key: "myapex.key",
2535 native_shared_libs: ["lib_dep", "libfoo"],
2536 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002537 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002538 }
2539
2540 apex_key {
2541 name: "myapex.key",
2542 public_key: "testkey.avbpubkey",
2543 private_key: "testkey.pem",
2544 }
2545
2546 cc_library {
2547 name: "lib_nodep",
2548 srcs: ["mylib.cpp"],
2549 system_shared_libs: [],
2550 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002551 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002552 }
2553
2554 cc_library {
2555 name: "lib_dep",
2556 srcs: ["mylib.cpp"],
2557 shared_libs: ["libfoo"],
2558 system_shared_libs: [],
2559 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002560 apex_available: [
2561 "myapex_dep",
2562 "myapex_provider",
2563 "myapex_selfcontained",
2564 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002565 }
2566
2567 cc_library {
2568 name: "libfoo",
2569 srcs: ["mytest.cpp"],
2570 stubs: {
2571 versions: ["1"],
2572 },
2573 system_shared_libs: [],
2574 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002575 apex_available: [
2576 "myapex_provider",
2577 "myapex_selfcontained",
2578 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002579 }
2580 `)
2581
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002582 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002583 var provideNativeLibs, requireNativeLibs []string
2584
Sundong Ahnabb64432019-10-22 13:58:29 +09002585 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002586 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2587 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002588 ensureListEmpty(t, provideNativeLibs)
2589 ensureListEmpty(t, requireNativeLibs)
2590
Sundong Ahnabb64432019-10-22 13:58:29 +09002591 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002592 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2593 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002594 ensureListEmpty(t, provideNativeLibs)
2595 ensureListContains(t, requireNativeLibs, "libfoo.so")
2596
Sundong Ahnabb64432019-10-22 13:58:29 +09002597 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002598 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2599 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002600 ensureListContains(t, provideNativeLibs, "libfoo.so")
2601 ensureListEmpty(t, requireNativeLibs)
2602
Sundong Ahnabb64432019-10-22 13:58:29 +09002603 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002604 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2605 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002606 ensureListContains(t, provideNativeLibs, "libfoo.so")
2607 ensureListEmpty(t, requireNativeLibs)
2608}
2609
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002610func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002611 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002612 apex {
2613 name: "myapex",
2614 key: "myapex.key",
2615 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002616 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002617 }
2618
2619 apex_key {
2620 name: "myapex.key",
2621 public_key: "testkey.avbpubkey",
2622 private_key: "testkey.pem",
2623 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002624
2625 cc_library {
2626 name: "mylib",
2627 srcs: ["mylib.cpp"],
2628 system_shared_libs: [],
2629 stl: "none",
2630 apex_available: [
2631 "//apex_available:platform",
2632 "myapex",
2633 ],
2634 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002635 `)
2636
Sundong Ahnabb64432019-10-22 13:58:29 +09002637 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002638 apexManifestRule := module.Rule("apexManifestRule")
2639 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2640 apexRule := module.Rule("apexRule")
2641 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002642
2643 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2644 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2645 name := apexBundle.BaseModuleName()
2646 prefix := "TARGET_"
2647 var builder strings.Builder
2648 data.Custom(&builder, name, prefix, "", data)
2649 androidMk := builder.String()
2650 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2651 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002652}
2653
Alex Light0851b882019-02-07 13:20:53 -08002654func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002655 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002656 apex {
2657 name: "myapex",
2658 key: "myapex.key",
2659 native_shared_libs: ["mylib_common"],
2660 }
2661
2662 apex_key {
2663 name: "myapex.key",
2664 public_key: "testkey.avbpubkey",
2665 private_key: "testkey.pem",
2666 }
2667
2668 cc_library {
2669 name: "mylib_common",
2670 srcs: ["mylib.cpp"],
2671 system_shared_libs: [],
2672 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002673 apex_available: [
2674 "//apex_available:platform",
2675 "myapex",
2676 ],
Alex Light0851b882019-02-07 13:20:53 -08002677 }
2678 `)
2679
Sundong Ahnabb64432019-10-22 13:58:29 +09002680 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002681 apexRule := module.Rule("apexRule")
2682 copyCmds := apexRule.Args["copy_commands"]
2683
2684 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2685 t.Log("Apex was a test apex!")
2686 t.Fail()
2687 }
2688 // Ensure that main rule creates an output
2689 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2690
2691 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002692 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002693
2694 // Ensure that both direct and indirect deps are copied into apex
2695 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2696
Colin Cross7113d202019-11-20 16:39:12 -08002697 // Ensure that the platform variant ends with _shared
2698 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002699
2700 if !android.InAnyApex("mylib_common") {
2701 t.Log("Found mylib_common not in any apex!")
2702 t.Fail()
2703 }
2704}
2705
2706func TestTestApex(t *testing.T) {
2707 if android.InAnyApex("mylib_common_test") {
2708 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!")
2709 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002710 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002711 apex_test {
2712 name: "myapex",
2713 key: "myapex.key",
2714 native_shared_libs: ["mylib_common_test"],
2715 }
2716
2717 apex_key {
2718 name: "myapex.key",
2719 public_key: "testkey.avbpubkey",
2720 private_key: "testkey.pem",
2721 }
2722
2723 cc_library {
2724 name: "mylib_common_test",
2725 srcs: ["mylib.cpp"],
2726 system_shared_libs: [],
2727 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002728 // TODO: remove //apex_available:platform
2729 apex_available: [
2730 "//apex_available:platform",
2731 "myapex",
2732 ],
Alex Light0851b882019-02-07 13:20:53 -08002733 }
2734 `)
2735
Sundong Ahnabb64432019-10-22 13:58:29 +09002736 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002737 apexRule := module.Rule("apexRule")
2738 copyCmds := apexRule.Args["copy_commands"]
2739
2740 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2741 t.Log("Apex was not a test apex!")
2742 t.Fail()
2743 }
2744 // Ensure that main rule creates an output
2745 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2746
2747 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002748 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002749
2750 // Ensure that both direct and indirect deps are copied into apex
2751 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2752
Colin Cross7113d202019-11-20 16:39:12 -08002753 // Ensure that the platform variant ends with _shared
2754 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002755}
2756
Alex Light9670d332019-01-29 18:07:33 -08002757func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002758 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002759 apex {
2760 name: "myapex",
2761 key: "myapex.key",
2762 multilib: {
2763 first: {
2764 native_shared_libs: ["mylib_common"],
2765 }
2766 },
2767 target: {
2768 android: {
2769 multilib: {
2770 first: {
2771 native_shared_libs: ["mylib"],
2772 }
2773 }
2774 },
2775 host: {
2776 multilib: {
2777 first: {
2778 native_shared_libs: ["mylib2"],
2779 }
2780 }
2781 }
2782 }
2783 }
2784
2785 apex_key {
2786 name: "myapex.key",
2787 public_key: "testkey.avbpubkey",
2788 private_key: "testkey.pem",
2789 }
2790
2791 cc_library {
2792 name: "mylib",
2793 srcs: ["mylib.cpp"],
2794 system_shared_libs: [],
2795 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002796 // TODO: remove //apex_available:platform
2797 apex_available: [
2798 "//apex_available:platform",
2799 "myapex",
2800 ],
Alex Light9670d332019-01-29 18:07:33 -08002801 }
2802
2803 cc_library {
2804 name: "mylib_common",
2805 srcs: ["mylib.cpp"],
2806 system_shared_libs: [],
2807 stl: "none",
2808 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002809 // TODO: remove //apex_available:platform
2810 apex_available: [
2811 "//apex_available:platform",
2812 "myapex",
2813 ],
Alex Light9670d332019-01-29 18:07:33 -08002814 }
2815
2816 cc_library {
2817 name: "mylib2",
2818 srcs: ["mylib.cpp"],
2819 system_shared_libs: [],
2820 stl: "none",
2821 compile_multilib: "first",
2822 }
2823 `)
2824
Sundong Ahnabb64432019-10-22 13:58:29 +09002825 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002826 copyCmds := apexRule.Args["copy_commands"]
2827
2828 // Ensure that main rule creates an output
2829 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2830
2831 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002832 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2833 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2834 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002835
2836 // Ensure that both direct and indirect deps are copied into apex
2837 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2838 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2839 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2840
Colin Cross7113d202019-11-20 16:39:12 -08002841 // Ensure that the platform variant ends with _shared
2842 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2843 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2844 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002845}
Jiyong Park04480cf2019-02-06 00:16:29 +09002846
2847func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002848 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002849 apex {
2850 name: "myapex",
2851 key: "myapex.key",
2852 binaries: ["myscript"],
2853 }
2854
2855 apex_key {
2856 name: "myapex.key",
2857 public_key: "testkey.avbpubkey",
2858 private_key: "testkey.pem",
2859 }
2860
2861 sh_binary {
2862 name: "myscript",
2863 src: "mylib.cpp",
2864 filename: "myscript.sh",
2865 sub_dir: "script",
2866 }
2867 `)
2868
Sundong Ahnabb64432019-10-22 13:58:29 +09002869 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002870 copyCmds := apexRule.Args["copy_commands"]
2871
2872 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2873}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002874
Jooyung Han91df2082019-11-20 01:49:42 +09002875func TestApexInVariousPartition(t *testing.T) {
2876 testcases := []struct {
2877 propName, parition, flattenedPartition string
2878 }{
2879 {"", "system", "system_ext"},
2880 {"product_specific: true", "product", "product"},
2881 {"soc_specific: true", "vendor", "vendor"},
2882 {"proprietary: true", "vendor", "vendor"},
2883 {"vendor: true", "vendor", "vendor"},
2884 {"system_ext_specific: true", "system_ext", "system_ext"},
2885 }
2886 for _, tc := range testcases {
2887 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2888 ctx, _ := testApex(t, `
2889 apex {
2890 name: "myapex",
2891 key: "myapex.key",
2892 `+tc.propName+`
2893 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002894
Jooyung Han91df2082019-11-20 01:49:42 +09002895 apex_key {
2896 name: "myapex.key",
2897 public_key: "testkey.avbpubkey",
2898 private_key: "testkey.pem",
2899 }
2900 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002901
Jooyung Han91df2082019-11-20 01:49:42 +09002902 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2903 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2904 actual := apex.installDir.String()
2905 if actual != expected {
2906 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2907 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002908
Jooyung Han91df2082019-11-20 01:49:42 +09002909 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2910 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2911 actual = flattened.installDir.String()
2912 if actual != expected {
2913 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2914 }
2915 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002916 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002917}
Jiyong Park67882562019-03-21 01:11:21 +09002918
Jooyung Han54aca7b2019-11-20 02:26:02 +09002919func TestFileContexts(t *testing.T) {
2920 ctx, _ := testApex(t, `
2921 apex {
2922 name: "myapex",
2923 key: "myapex.key",
2924 }
2925
2926 apex_key {
2927 name: "myapex.key",
2928 public_key: "testkey.avbpubkey",
2929 private_key: "testkey.pem",
2930 }
2931 `)
2932 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2933 apexRule := module.Rule("apexRule")
2934 actual := apexRule.Args["file_contexts"]
2935 expected := "system/sepolicy/apex/myapex-file_contexts"
2936 if actual != expected {
2937 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2938 }
2939
2940 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2941 apex {
2942 name: "myapex",
2943 key: "myapex.key",
2944 file_contexts: "my_own_file_contexts",
2945 }
2946
2947 apex_key {
2948 name: "myapex.key",
2949 public_key: "testkey.avbpubkey",
2950 private_key: "testkey.pem",
2951 }
2952 `, withFiles(map[string][]byte{
2953 "my_own_file_contexts": nil,
2954 }))
2955
2956 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2957 apex {
2958 name: "myapex",
2959 key: "myapex.key",
2960 product_specific: true,
2961 file_contexts: "product_specific_file_contexts",
2962 }
2963
2964 apex_key {
2965 name: "myapex.key",
2966 public_key: "testkey.avbpubkey",
2967 private_key: "testkey.pem",
2968 }
2969 `)
2970
2971 ctx, _ = testApex(t, `
2972 apex {
2973 name: "myapex",
2974 key: "myapex.key",
2975 product_specific: true,
2976 file_contexts: "product_specific_file_contexts",
2977 }
2978
2979 apex_key {
2980 name: "myapex.key",
2981 public_key: "testkey.avbpubkey",
2982 private_key: "testkey.pem",
2983 }
2984 `, withFiles(map[string][]byte{
2985 "product_specific_file_contexts": nil,
2986 }))
2987 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2988 apexRule = module.Rule("apexRule")
2989 actual = apexRule.Args["file_contexts"]
2990 expected = "product_specific_file_contexts"
2991 if actual != expected {
2992 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2993 }
2994
2995 ctx, _ = testApex(t, `
2996 apex {
2997 name: "myapex",
2998 key: "myapex.key",
2999 product_specific: true,
3000 file_contexts: ":my-file-contexts",
3001 }
3002
3003 apex_key {
3004 name: "myapex.key",
3005 public_key: "testkey.avbpubkey",
3006 private_key: "testkey.pem",
3007 }
3008
3009 filegroup {
3010 name: "my-file-contexts",
3011 srcs: ["product_specific_file_contexts"],
3012 }
3013 `, withFiles(map[string][]byte{
3014 "product_specific_file_contexts": nil,
3015 }))
3016 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
3017 apexRule = module.Rule("apexRule")
3018 actual = apexRule.Args["file_contexts"]
3019 expected = "product_specific_file_contexts"
3020 if actual != expected {
3021 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
3022 }
3023}
3024
Jiyong Park67882562019-03-21 01:11:21 +09003025func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003026 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09003027 apex_key {
3028 name: "myapex.key",
3029 public_key: ":my.avbpubkey",
3030 private_key: ":my.pem",
3031 product_specific: true,
3032 }
3033
3034 filegroup {
3035 name: "my.avbpubkey",
3036 srcs: ["testkey2.avbpubkey"],
3037 }
3038
3039 filegroup {
3040 name: "my.pem",
3041 srcs: ["testkey2.pem"],
3042 }
3043 `)
3044
3045 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
3046 expected_pubkey := "testkey2.avbpubkey"
3047 actual_pubkey := apex_key.public_key_file.String()
3048 if actual_pubkey != expected_pubkey {
3049 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
3050 }
3051 expected_privkey := "testkey2.pem"
3052 actual_privkey := apex_key.private_key_file.String()
3053 if actual_privkey != expected_privkey {
3054 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
3055 }
3056}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003057
3058func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003059 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003060 prebuilt_apex {
3061 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09003062 arch: {
3063 arm64: {
3064 src: "myapex-arm64.apex",
3065 },
3066 arm: {
3067 src: "myapex-arm.apex",
3068 },
3069 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003070 }
3071 `)
3072
3073 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3074
Jiyong Parkc95714e2019-03-29 14:23:10 +09003075 expectedInput := "myapex-arm64.apex"
3076 if prebuilt.inputApex.String() != expectedInput {
3077 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
3078 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003079}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003080
3081func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003082 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003083 prebuilt_apex {
3084 name: "myapex",
3085 src: "myapex-arm.apex",
3086 filename: "notmyapex.apex",
3087 }
3088 `)
3089
3090 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3091
3092 expected := "notmyapex.apex"
3093 if p.installFilename != expected {
3094 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
3095 }
3096}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003097
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003098func TestPrebuiltOverrides(t *testing.T) {
3099 ctx, config := testApex(t, `
3100 prebuilt_apex {
3101 name: "myapex.prebuilt",
3102 src: "myapex-arm.apex",
3103 overrides: [
3104 "myapex",
3105 ],
3106 }
3107 `)
3108
3109 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3110
3111 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003112 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003113 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003114 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003115 }
3116}
3117
Roland Levillain630846d2019-06-26 12:48:34 +01003118func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003119 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003120 apex_test {
3121 name: "myapex",
3122 key: "myapex.key",
3123 tests: [
3124 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003125 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003126 ],
3127 }
3128
3129 apex_key {
3130 name: "myapex.key",
3131 public_key: "testkey.avbpubkey",
3132 private_key: "testkey.pem",
3133 }
3134
3135 cc_test {
3136 name: "mytest",
3137 gtest: false,
3138 srcs: ["mytest.cpp"],
3139 relative_install_path: "test",
3140 system_shared_libs: [],
3141 static_executable: true,
3142 stl: "none",
3143 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003144
3145 cc_test {
3146 name: "mytests",
3147 gtest: false,
3148 srcs: [
3149 "mytest1.cpp",
3150 "mytest2.cpp",
3151 "mytest3.cpp",
3152 ],
3153 test_per_src: true,
3154 relative_install_path: "test",
3155 system_shared_libs: [],
3156 static_executable: true,
3157 stl: "none",
3158 }
Roland Levillain630846d2019-06-26 12:48:34 +01003159 `)
3160
Sundong Ahnabb64432019-10-22 13:58:29 +09003161 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003162 copyCmds := apexRule.Args["copy_commands"]
3163
3164 // Ensure that test dep is copied into apex.
3165 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003166
3167 // Ensure that test deps built with `test_per_src` are copied into apex.
3168 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3169 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3170 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003171
3172 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003173 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003174 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3175 name := apexBundle.BaseModuleName()
3176 prefix := "TARGET_"
3177 var builder strings.Builder
3178 data.Custom(&builder, name, prefix, "", data)
3179 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003180 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3181 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3182 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3183 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003184 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003185 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003186 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003187}
3188
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003189func TestInstallExtraFlattenedApexes(t *testing.T) {
3190 ctx, config := testApex(t, `
3191 apex {
3192 name: "myapex",
3193 key: "myapex.key",
3194 }
3195 apex_key {
3196 name: "myapex.key",
3197 public_key: "testkey.avbpubkey",
3198 private_key: "testkey.pem",
3199 }
3200 `, func(fs map[string][]byte, config android.Config) {
3201 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3202 })
3203 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003204 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003205 mk := android.AndroidMkDataForTest(t, config, "", ab)
3206 var builder strings.Builder
3207 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3208 androidMk := builder.String()
3209 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3210}
3211
Jooyung Han5c998b92019-06-27 11:30:33 +09003212func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003213 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003214 apex {
3215 name: "myapex",
3216 key: "myapex.key",
3217 native_shared_libs: ["mylib"],
3218 uses: ["commonapex"],
3219 }
3220
3221 apex {
3222 name: "commonapex",
3223 key: "myapex.key",
3224 native_shared_libs: ["libcommon"],
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 }
3233
3234 cc_library {
3235 name: "mylib",
3236 srcs: ["mylib.cpp"],
3237 shared_libs: ["libcommon"],
3238 system_shared_libs: [],
3239 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003240 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003241 }
3242
3243 cc_library {
3244 name: "libcommon",
3245 srcs: ["mylib_common.cpp"],
3246 system_shared_libs: [],
3247 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003248 // TODO: remove //apex_available:platform
3249 apex_available: [
3250 "//apex_available:platform",
3251 "commonapex",
3252 "myapex",
3253 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003254 }
3255 `)
3256
Sundong Ahnabb64432019-10-22 13:58:29 +09003257 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003258 apexRule1 := module1.Rule("apexRule")
3259 copyCmds1 := apexRule1.Args["copy_commands"]
3260
Sundong Ahnabb64432019-10-22 13:58:29 +09003261 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003262 apexRule2 := module2.Rule("apexRule")
3263 copyCmds2 := apexRule2.Args["copy_commands"]
3264
Colin Cross7113d202019-11-20 16:39:12 -08003265 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3266 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003267 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3268 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3269 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3270}
3271
3272func TestApexUsesFailsIfNotProvided(t *testing.T) {
3273 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3274 apex {
3275 name: "myapex",
3276 key: "myapex.key",
3277 uses: ["commonapex"],
3278 }
3279
3280 apex {
3281 name: "commonapex",
3282 key: "myapex.key",
3283 }
3284
3285 apex_key {
3286 name: "myapex.key",
3287 public_key: "testkey.avbpubkey",
3288 private_key: "testkey.pem",
3289 }
3290 `)
3291 testApexError(t, `uses: "commonapex" is not a provider`, `
3292 apex {
3293 name: "myapex",
3294 key: "myapex.key",
3295 uses: ["commonapex"],
3296 }
3297
3298 cc_library {
3299 name: "commonapex",
3300 system_shared_libs: [],
3301 stl: "none",
3302 }
3303
3304 apex_key {
3305 name: "myapex.key",
3306 public_key: "testkey.avbpubkey",
3307 private_key: "testkey.pem",
3308 }
3309 `)
3310}
3311
3312func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3313 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3314 apex {
3315 name: "myapex",
3316 key: "myapex.key",
3317 use_vendor: true,
3318 uses: ["commonapex"],
3319 }
3320
3321 apex {
3322 name: "commonapex",
3323 key: "myapex.key",
3324 provide_cpp_shared_libs: true,
3325 }
3326
3327 apex_key {
3328 name: "myapex.key",
3329 public_key: "testkey.avbpubkey",
3330 private_key: "testkey.pem",
3331 }
Jooyung Handc782442019-11-01 03:14:38 +09003332 `, func(fs map[string][]byte, config android.Config) {
3333 setUseVendorWhitelistForTest(config, []string{"myapex"})
3334 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003335}
3336
Jooyung Hand48f3c32019-08-23 11:18:57 +09003337func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3338 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3339 apex {
3340 name: "myapex",
3341 key: "myapex.key",
3342 native_shared_libs: ["libfoo"],
3343 }
3344
3345 apex_key {
3346 name: "myapex.key",
3347 public_key: "testkey.avbpubkey",
3348 private_key: "testkey.pem",
3349 }
3350
3351 cc_library {
3352 name: "libfoo",
3353 stl: "none",
3354 system_shared_libs: [],
3355 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003356 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003357 }
3358 `)
3359 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3360 apex {
3361 name: "myapex",
3362 key: "myapex.key",
3363 java_libs: ["myjar"],
3364 }
3365
3366 apex_key {
3367 name: "myapex.key",
3368 public_key: "testkey.avbpubkey",
3369 private_key: "testkey.pem",
3370 }
3371
3372 java_library {
3373 name: "myjar",
3374 srcs: ["foo/bar/MyClass.java"],
3375 sdk_version: "none",
3376 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003377 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09003378 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003379 }
3380 `)
3381}
3382
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003383func TestApexWithApps(t *testing.T) {
3384 ctx, _ := testApex(t, `
3385 apex {
3386 name: "myapex",
3387 key: "myapex.key",
3388 apps: [
3389 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003390 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003391 ],
3392 }
3393
3394 apex_key {
3395 name: "myapex.key",
3396 public_key: "testkey.avbpubkey",
3397 private_key: "testkey.pem",
3398 }
3399
3400 android_app {
3401 name: "AppFoo",
3402 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003403 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003404 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003405 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08003406 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003407 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003408 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003409
3410 android_app {
3411 name: "AppFooPriv",
3412 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08003413 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09003414 system_modules: "none",
3415 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08003416 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003417 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003418 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003419
3420 cc_library_shared {
3421 name: "libjni",
3422 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003423 shared_libs: ["libfoo"],
3424 stl: "none",
3425 system_shared_libs: [],
3426 apex_available: [ "myapex" ],
3427 sdk_version: "current",
3428 }
3429
3430 cc_library_shared {
3431 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09003432 stl: "none",
3433 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003434 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08003435 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003436 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003437 `)
3438
Sundong Ahnabb64432019-10-22 13:58:29 +09003439 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003440 apexRule := module.Rule("apexRule")
3441 copyCmds := apexRule.Args["copy_commands"]
3442
3443 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003444 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003445
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003446 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3447 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003448 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003449 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003450 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003451 // JNI libraries including transitive deps are
3452 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossc511bc52020-04-07 16:50:32 +00003453 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_myapex").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09003454 // ... embedded inside APK (jnilibs.zip)
3455 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3456 // ... and not directly inside the APEX
3457 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3458 }
Dario Frenicde2a032019-10-27 00:29:22 +01003459}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003460
Dario Frenicde2a032019-10-27 00:29:22 +01003461func TestApexWithAppImports(t *testing.T) {
3462 ctx, _ := testApex(t, `
3463 apex {
3464 name: "myapex",
3465 key: "myapex.key",
3466 apps: [
3467 "AppFooPrebuilt",
3468 "AppFooPrivPrebuilt",
3469 ],
3470 }
3471
3472 apex_key {
3473 name: "myapex.key",
3474 public_key: "testkey.avbpubkey",
3475 private_key: "testkey.pem",
3476 }
3477
3478 android_app_import {
3479 name: "AppFooPrebuilt",
3480 apk: "PrebuiltAppFoo.apk",
3481 presigned: true,
3482 dex_preopt: {
3483 enabled: false,
3484 },
Jiyong Park592a6a42020-04-21 22:34:28 +09003485 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01003486 }
3487
3488 android_app_import {
3489 name: "AppFooPrivPrebuilt",
3490 apk: "PrebuiltAppFooPriv.apk",
3491 privileged: true,
3492 presigned: true,
3493 dex_preopt: {
3494 enabled: false,
3495 },
Jooyung Han39ee1192020-03-23 20:21:11 +09003496 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09003497 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01003498 }
3499 `)
3500
Sundong Ahnabb64432019-10-22 13:58:29 +09003501 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003502 apexRule := module.Rule("apexRule")
3503 copyCmds := apexRule.Args["copy_commands"]
3504
3505 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09003506 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
3507}
3508
3509func TestApexWithAppImportsPrefer(t *testing.T) {
3510 ctx, _ := testApex(t, `
3511 apex {
3512 name: "myapex",
3513 key: "myapex.key",
3514 apps: [
3515 "AppFoo",
3516 ],
3517 }
3518
3519 apex_key {
3520 name: "myapex.key",
3521 public_key: "testkey.avbpubkey",
3522 private_key: "testkey.pem",
3523 }
3524
3525 android_app {
3526 name: "AppFoo",
3527 srcs: ["foo/bar/MyClass.java"],
3528 sdk_version: "none",
3529 system_modules: "none",
3530 apex_available: [ "myapex" ],
3531 }
3532
3533 android_app_import {
3534 name: "AppFoo",
3535 apk: "AppFooPrebuilt.apk",
3536 filename: "AppFooPrebuilt.apk",
3537 presigned: true,
3538 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09003539 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09003540 }
3541 `, withFiles(map[string][]byte{
3542 "AppFooPrebuilt.apk": nil,
3543 }))
3544
3545 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3546 "app/AppFoo/AppFooPrebuilt.apk",
3547 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003548}
3549
Dario Freni6f3937c2019-12-20 22:58:03 +00003550func TestApexWithTestHelperApp(t *testing.T) {
3551 ctx, _ := testApex(t, `
3552 apex {
3553 name: "myapex",
3554 key: "myapex.key",
3555 apps: [
3556 "TesterHelpAppFoo",
3557 ],
3558 }
3559
3560 apex_key {
3561 name: "myapex.key",
3562 public_key: "testkey.avbpubkey",
3563 private_key: "testkey.pem",
3564 }
3565
3566 android_test_helper_app {
3567 name: "TesterHelpAppFoo",
3568 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003569 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003570 }
3571
3572 `)
3573
3574 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3575 apexRule := module.Rule("apexRule")
3576 copyCmds := apexRule.Args["copy_commands"]
3577
3578 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3579}
3580
Jooyung Han18020ea2019-11-13 10:50:48 +09003581func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3582 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09003583 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003584 apex {
3585 name: "myapex",
3586 key: "myapex.key",
3587 native_shared_libs: ["libfoo"],
3588 }
3589
3590 apex_key {
3591 name: "myapex.key",
3592 public_key: "testkey.avbpubkey",
3593 private_key: "testkey.pem",
3594 }
3595
3596 apex {
3597 name: "otherapex",
3598 key: "myapex.key",
3599 native_shared_libs: ["libfoo"],
3600 }
3601
3602 cc_defaults {
3603 name: "libfoo-defaults",
3604 apex_available: ["otherapex"],
3605 }
3606
3607 cc_library {
3608 name: "libfoo",
3609 defaults: ["libfoo-defaults"],
3610 stl: "none",
3611 system_shared_libs: [],
3612 }`)
3613}
3614
Paul Duffine52e66f2020-03-30 17:54:29 +01003615func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003616 // libfoo is not available to myapex, but only to otherapex
3617 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3618 apex {
3619 name: "myapex",
3620 key: "myapex.key",
3621 native_shared_libs: ["libfoo"],
3622 }
3623
3624 apex_key {
3625 name: "myapex.key",
3626 public_key: "testkey.avbpubkey",
3627 private_key: "testkey.pem",
3628 }
3629
3630 apex {
3631 name: "otherapex",
3632 key: "otherapex.key",
3633 native_shared_libs: ["libfoo"],
3634 }
3635
3636 apex_key {
3637 name: "otherapex.key",
3638 public_key: "testkey.avbpubkey",
3639 private_key: "testkey.pem",
3640 }
3641
3642 cc_library {
3643 name: "libfoo",
3644 stl: "none",
3645 system_shared_libs: [],
3646 apex_available: ["otherapex"],
3647 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01003648}
Jiyong Park127b40b2019-09-30 16:04:35 +09003649
Paul Duffine52e66f2020-03-30 17:54:29 +01003650func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09003651 // libbbaz is an indirect dep
Paul Duffindf915ff2020-03-30 17:58:21 +01003652 testApexError(t, `requires "libbaz" that is not available for the APEX. Dependency path:
Paul Duffinc5192442020-03-31 11:31:36 +01003653.*via tag apex\.dependencyTag.*"sharedLib".*
Paul Duffindf915ff2020-03-30 17:58:21 +01003654.*-> libfoo.*link:shared.*
Paul Duffin65347702020-03-31 15:23:40 +01003655.*via tag cc\.DependencyTag.*"shared".*
Paul Duffindf915ff2020-03-30 17:58:21 +01003656.*-> libbar.*link:shared.*
Paul Duffin65347702020-03-31 15:23:40 +01003657.*via tag cc\.DependencyTag.*"shared".*
3658.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003659 apex {
3660 name: "myapex",
3661 key: "myapex.key",
3662 native_shared_libs: ["libfoo"],
3663 }
3664
3665 apex_key {
3666 name: "myapex.key",
3667 public_key: "testkey.avbpubkey",
3668 private_key: "testkey.pem",
3669 }
3670
Jiyong Park127b40b2019-09-30 16:04:35 +09003671 cc_library {
3672 name: "libfoo",
3673 stl: "none",
3674 shared_libs: ["libbar"],
3675 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003676 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003677 }
3678
3679 cc_library {
3680 name: "libbar",
3681 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09003682 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003683 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09003684 apex_available: ["myapex"],
3685 }
3686
3687 cc_library {
3688 name: "libbaz",
3689 stl: "none",
3690 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003691 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01003692}
Jiyong Park127b40b2019-09-30 16:04:35 +09003693
Paul Duffine52e66f2020-03-30 17:54:29 +01003694func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003695 testApexError(t, "\"otherapex\" is not a valid module name", `
3696 apex {
3697 name: "myapex",
3698 key: "myapex.key",
3699 native_shared_libs: ["libfoo"],
3700 }
3701
3702 apex_key {
3703 name: "myapex.key",
3704 public_key: "testkey.avbpubkey",
3705 private_key: "testkey.pem",
3706 }
3707
3708 cc_library {
3709 name: "libfoo",
3710 stl: "none",
3711 system_shared_libs: [],
3712 apex_available: ["otherapex"],
3713 }`)
3714
Paul Duffine52e66f2020-03-30 17:54:29 +01003715 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003716 apex {
3717 name: "myapex",
3718 key: "myapex.key",
3719 native_shared_libs: ["libfoo", "libbar"],
3720 }
3721
3722 apex_key {
3723 name: "myapex.key",
3724 public_key: "testkey.avbpubkey",
3725 private_key: "testkey.pem",
3726 }
3727
3728 cc_library {
3729 name: "libfoo",
3730 stl: "none",
3731 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09003732 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003733 apex_available: ["myapex"],
3734 }
3735
3736 cc_library {
3737 name: "libbar",
3738 stl: "none",
3739 system_shared_libs: [],
3740 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09003741 }
3742
3743 cc_library {
3744 name: "libbaz",
3745 stl: "none",
3746 system_shared_libs: [],
3747 stubs: {
3748 versions: ["10", "20", "30"],
3749 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003750 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01003751}
Jiyong Park127b40b2019-09-30 16:04:35 +09003752
Paul Duffine52e66f2020-03-30 17:54:29 +01003753func TestApexAvailable_CreatedForPlatform(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003754 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003755 // TODO(jiyong) the checks for the platform variant are removed because we now create
3756 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3757 // the platform variants are not used from other platform modules. When that is done,
3758 // these checks will be replaced by expecting a specific error message that will be
3759 // emitted when the platform variant is used.
3760 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3761 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3762 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3763 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003764
Paul Duffine52e66f2020-03-30 17:54:29 +01003765 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003766 apex {
3767 name: "myapex",
3768 key: "myapex.key",
3769 }
3770
3771 apex_key {
3772 name: "myapex.key",
3773 public_key: "testkey.avbpubkey",
3774 private_key: "testkey.pem",
3775 }
3776
3777 cc_library {
3778 name: "libfoo",
3779 stl: "none",
3780 system_shared_libs: [],
3781 apex_available: ["//apex_available:platform"],
3782 }`)
3783
3784 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003785 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3786 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Paul Duffine52e66f2020-03-30 17:54:29 +01003787}
Jiyong Parka90ca002019-10-07 15:47:24 +09003788
Paul Duffine52e66f2020-03-30 17:54:29 +01003789func TestApexAvailable_CreatedForApex(t *testing.T) {
3790 testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09003791 apex {
3792 name: "myapex",
3793 key: "myapex.key",
3794 native_shared_libs: ["libfoo"],
3795 }
3796
3797 apex_key {
3798 name: "myapex.key",
3799 public_key: "testkey.avbpubkey",
3800 private_key: "testkey.pem",
3801 }
3802
3803 cc_library {
3804 name: "libfoo",
3805 stl: "none",
3806 system_shared_libs: [],
3807 apex_available: ["myapex"],
3808 static: {
3809 apex_available: ["//apex_available:platform"],
3810 },
3811 }`)
3812
3813 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003814 // TODO(jiyong) the checks for the platform variant are removed because we now create
3815 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3816 // the platform variants are not used from other platform modules. When that is done,
3817 // these checks will be replaced by expecting a specific error message that will be
3818 // emitted when the platform variant is used.
3819 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3820 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3821 // // but the static variant is available to both myapex and the platform
3822 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3823 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003824}
3825
Jiyong Park5d790c32019-11-15 18:40:32 +09003826func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003827 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003828 apex {
3829 name: "myapex",
3830 key: "myapex.key",
3831 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003832 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003833 }
3834
3835 override_apex {
3836 name: "override_myapex",
3837 base: "myapex",
3838 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003839 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003840 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003841 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09003842 }
3843
3844 apex_key {
3845 name: "myapex.key",
3846 public_key: "testkey.avbpubkey",
3847 private_key: "testkey.pem",
3848 }
3849
3850 android_app {
3851 name: "app",
3852 srcs: ["foo/bar/MyClass.java"],
3853 package_name: "foo",
3854 sdk_version: "none",
3855 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003856 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003857 }
3858
3859 override_android_app {
3860 name: "override_app",
3861 base: "app",
3862 package_name: "bar",
3863 }
Jiyong Park20bacab2020-03-03 11:45:41 +09003864 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003865
Jiyong Park317645e2019-12-05 13:20:58 +09003866 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3867 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3868 if originalVariant.GetOverriddenBy() != "" {
3869 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3870 }
3871 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3872 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3873 }
3874
Jiyong Park5d790c32019-11-15 18:40:32 +09003875 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3876 apexRule := module.Rule("apexRule")
3877 copyCmds := apexRule.Args["copy_commands"]
3878
3879 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09003880 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003881
3882 apexBundle := module.Module().(*apexBundle)
3883 name := apexBundle.Name()
3884 if name != "override_myapex" {
3885 t.Errorf("name should be \"override_myapex\", but was %q", name)
3886 }
3887
Baligh Uddin004d7172020-02-19 21:29:28 -08003888 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3889 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3890 }
3891
Jiyong Park20bacab2020-03-03 11:45:41 +09003892 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07003893 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09003894
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003895 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3896 var builder strings.Builder
3897 data.Custom(&builder, name, "TARGET_", "", data)
3898 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003899 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003900 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3901 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003902 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003903 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003904 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003905 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3906 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003907}
3908
Jooyung Han214bf372019-11-12 13:03:50 +09003909func TestLegacyAndroid10Support(t *testing.T) {
3910 ctx, _ := testApex(t, `
3911 apex {
3912 name: "myapex",
3913 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003914 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09003915 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09003916 }
3917
3918 apex_key {
3919 name: "myapex.key",
3920 public_key: "testkey.avbpubkey",
3921 private_key: "testkey.pem",
3922 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003923
3924 cc_library {
3925 name: "mylib",
3926 srcs: ["mylib.cpp"],
3927 stl: "libc++",
3928 system_shared_libs: [],
3929 apex_available: [ "myapex" ],
3930 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003931 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003932
3933 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3934 args := module.Rule("apexRule").Args
3935 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003936 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003937
3938 // The copies of the libraries in the apex should have one more dependency than
3939 // the ones outside the apex, namely the unwinder. Ideally we should check
3940 // the dependency names directly here but for some reason the names are blank in
3941 // this test.
3942 for _, lib := range []string{"libc++", "mylib"} {
3943 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3944 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3945 if len(apexImplicits) != len(nonApexImplicits)+1 {
3946 t.Errorf("%q missing unwinder dep", lib)
3947 }
3948 }
Jooyung Han214bf372019-11-12 13:03:50 +09003949}
3950
Jooyung Han58f26ab2019-12-18 15:34:32 +09003951func TestJavaSDKLibrary(t *testing.T) {
3952 ctx, _ := testApex(t, `
3953 apex {
3954 name: "myapex",
3955 key: "myapex.key",
3956 java_libs: ["foo"],
3957 }
3958
3959 apex_key {
3960 name: "myapex.key",
3961 public_key: "testkey.avbpubkey",
3962 private_key: "testkey.pem",
3963 }
3964
3965 java_sdk_library {
3966 name: "foo",
3967 srcs: ["a.java"],
3968 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003969 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003970 }
3971 `, withFiles(map[string][]byte{
3972 "api/current.txt": nil,
3973 "api/removed.txt": nil,
3974 "api/system-current.txt": nil,
3975 "api/system-removed.txt": nil,
3976 "api/test-current.txt": nil,
3977 "api/test-removed.txt": nil,
3978 }))
3979
3980 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003981 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003982 "javalib/foo.jar",
3983 "etc/permissions/foo.xml",
3984 })
3985 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003986 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3987 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003988}
3989
atrost6e126252020-01-27 17:01:16 +00003990func TestCompatConfig(t *testing.T) {
3991 ctx, _ := testApex(t, `
3992 apex {
3993 name: "myapex",
3994 key: "myapex.key",
3995 prebuilts: ["myjar-platform-compat-config"],
3996 java_libs: ["myjar"],
3997 }
3998
3999 apex_key {
4000 name: "myapex.key",
4001 public_key: "testkey.avbpubkey",
4002 private_key: "testkey.pem",
4003 }
4004
4005 platform_compat_config {
4006 name: "myjar-platform-compat-config",
4007 src: ":myjar",
4008 }
4009
4010 java_library {
4011 name: "myjar",
4012 srcs: ["foo/bar/MyClass.java"],
4013 sdk_version: "none",
4014 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00004015 apex_available: [ "myapex" ],
4016 }
4017 `)
4018 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4019 "etc/compatconfig/myjar-platform-compat-config.xml",
4020 "javalib/myjar.jar",
4021 })
4022}
4023
Jiyong Park479321d2019-12-16 11:47:12 +09004024func TestRejectNonInstallableJavaLibrary(t *testing.T) {
4025 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
4026 apex {
4027 name: "myapex",
4028 key: "myapex.key",
4029 java_libs: ["myjar"],
4030 }
4031
4032 apex_key {
4033 name: "myapex.key",
4034 public_key: "testkey.avbpubkey",
4035 private_key: "testkey.pem",
4036 }
4037
4038 java_library {
4039 name: "myjar",
4040 srcs: ["foo/bar/MyClass.java"],
4041 sdk_version: "none",
4042 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09004043 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004044 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09004045 }
4046 `)
4047}
4048
Jiyong Park7afd1072019-12-30 16:56:33 +09004049func TestCarryRequiredModuleNames(t *testing.T) {
4050 ctx, config := testApex(t, `
4051 apex {
4052 name: "myapex",
4053 key: "myapex.key",
4054 native_shared_libs: ["mylib"],
4055 }
4056
4057 apex_key {
4058 name: "myapex.key",
4059 public_key: "testkey.avbpubkey",
4060 private_key: "testkey.pem",
4061 }
4062
4063 cc_library {
4064 name: "mylib",
4065 srcs: ["mylib.cpp"],
4066 system_shared_libs: [],
4067 stl: "none",
4068 required: ["a", "b"],
4069 host_required: ["c", "d"],
4070 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004071 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09004072 }
4073 `)
4074
4075 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4076 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
4077 name := apexBundle.BaseModuleName()
4078 prefix := "TARGET_"
4079 var builder strings.Builder
4080 data.Custom(&builder, name, prefix, "", data)
4081 androidMk := builder.String()
4082 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
4083 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
4084 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
4085}
4086
Jiyong Park7cd10e32020-01-14 09:22:18 +09004087func TestSymlinksFromApexToSystem(t *testing.T) {
4088 bp := `
4089 apex {
4090 name: "myapex",
4091 key: "myapex.key",
4092 native_shared_libs: ["mylib"],
4093 java_libs: ["myjar"],
4094 }
4095
Jiyong Park9d677202020-02-19 16:29:35 +09004096 apex {
4097 name: "myapex.updatable",
4098 key: "myapex.key",
4099 native_shared_libs: ["mylib"],
4100 java_libs: ["myjar"],
4101 updatable: true,
4102 }
4103
Jiyong Park7cd10e32020-01-14 09:22:18 +09004104 apex_key {
4105 name: "myapex.key",
4106 public_key: "testkey.avbpubkey",
4107 private_key: "testkey.pem",
4108 }
4109
4110 cc_library {
4111 name: "mylib",
4112 srcs: ["mylib.cpp"],
4113 shared_libs: ["myotherlib"],
4114 system_shared_libs: [],
4115 stl: "none",
4116 apex_available: [
4117 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004118 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004119 "//apex_available:platform",
4120 ],
4121 }
4122
4123 cc_library {
4124 name: "myotherlib",
4125 srcs: ["mylib.cpp"],
4126 system_shared_libs: [],
4127 stl: "none",
4128 apex_available: [
4129 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004130 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004131 "//apex_available:platform",
4132 ],
4133 }
4134
4135 java_library {
4136 name: "myjar",
4137 srcs: ["foo/bar/MyClass.java"],
4138 sdk_version: "none",
4139 system_modules: "none",
4140 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09004141 apex_available: [
4142 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004143 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004144 "//apex_available:platform",
4145 ],
4146 }
4147
4148 java_library {
4149 name: "myotherjar",
4150 srcs: ["foo/bar/MyClass.java"],
4151 sdk_version: "none",
4152 system_modules: "none",
4153 apex_available: [
4154 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004155 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004156 "//apex_available:platform",
4157 ],
4158 }
4159 `
4160
4161 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
4162 for _, f := range files {
4163 if f.path == file {
4164 if f.isLink {
4165 t.Errorf("%q is not a real file", file)
4166 }
4167 return
4168 }
4169 }
4170 t.Errorf("%q is not found", file)
4171 }
4172
4173 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
4174 for _, f := range files {
4175 if f.path == file {
4176 if !f.isLink {
4177 t.Errorf("%q is not a symlink", file)
4178 }
4179 return
4180 }
4181 }
4182 t.Errorf("%q is not found", file)
4183 }
4184
Jiyong Park9d677202020-02-19 16:29:35 +09004185 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4186 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004187 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004188 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004189 ensureRealfileExists(t, files, "javalib/myjar.jar")
4190 ensureRealfileExists(t, files, "lib64/mylib.so")
4191 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4192
Jiyong Park9d677202020-02-19 16:29:35 +09004193 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4194 ensureRealfileExists(t, files, "javalib/myjar.jar")
4195 ensureRealfileExists(t, files, "lib64/mylib.so")
4196 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4197
4198 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004199 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004200 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004201 ensureRealfileExists(t, files, "javalib/myjar.jar")
4202 ensureRealfileExists(t, files, "lib64/mylib.so")
4203 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004204
4205 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4206 ensureRealfileExists(t, files, "javalib/myjar.jar")
4207 ensureRealfileExists(t, files, "lib64/mylib.so")
4208 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004209}
4210
Jooyung Han643adc42020-02-27 13:50:06 +09004211func TestApexWithJniLibs(t *testing.T) {
4212 ctx, _ := testApex(t, `
4213 apex {
4214 name: "myapex",
4215 key: "myapex.key",
4216 jni_libs: ["mylib"],
4217 }
4218
4219 apex_key {
4220 name: "myapex.key",
4221 public_key: "testkey.avbpubkey",
4222 private_key: "testkey.pem",
4223 }
4224
4225 cc_library {
4226 name: "mylib",
4227 srcs: ["mylib.cpp"],
4228 shared_libs: ["mylib2"],
4229 system_shared_libs: [],
4230 stl: "none",
4231 apex_available: [ "myapex" ],
4232 }
4233
4234 cc_library {
4235 name: "mylib2",
4236 srcs: ["mylib.cpp"],
4237 system_shared_libs: [],
4238 stl: "none",
4239 apex_available: [ "myapex" ],
4240 }
4241 `)
4242
4243 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
4244 // Notice mylib2.so (transitive dep) is not added as a jni_lib
4245 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
4246 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4247 "lib64/mylib.so",
4248 "lib64/mylib2.so",
4249 })
4250}
4251
Jooyung Han49f67012020-04-17 13:43:10 +09004252func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
4253 ctx, _ := testApex(t, `
4254 apex {
4255 name: "myapex",
4256 key: "myapex.key",
4257 }
4258 apex_key {
4259 name: "myapex.key",
4260 public_key: "testkey.avbpubkey",
4261 private_key: "testkey.pem",
4262 }
4263 `, func(fs map[string][]byte, config android.Config) {
4264 delete(config.Targets, android.Android)
4265 config.AndroidCommonTarget = android.Target{}
4266 })
4267
4268 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
4269 t.Errorf("Expected variants: %v, but got: %v", expected, got)
4270 }
4271}
4272
Jooyung Han643adc42020-02-27 13:50:06 +09004273func TestApexWithJniLibs_Errors(t *testing.T) {
4274 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
4275 apex {
4276 name: "myapex",
4277 key: "myapex.key",
4278 jni_libs: ["xxx"],
4279 }
4280
4281 apex_key {
4282 name: "myapex.key",
4283 public_key: "testkey.avbpubkey",
4284 private_key: "testkey.pem",
4285 }
4286
4287 prebuilt_etc {
4288 name: "xxx",
4289 src: "xxx",
4290 }
4291 `, withFiles(map[string][]byte{
4292 "xxx": nil,
4293 }))
4294}
4295
Jiyong Parkbd159612020-02-28 15:22:21 +09004296func TestAppBundle(t *testing.T) {
4297 ctx, _ := testApex(t, `
4298 apex {
4299 name: "myapex",
4300 key: "myapex.key",
4301 apps: ["AppFoo"],
4302 }
4303
4304 apex_key {
4305 name: "myapex.key",
4306 public_key: "testkey.avbpubkey",
4307 private_key: "testkey.pem",
4308 }
4309
4310 android_app {
4311 name: "AppFoo",
4312 srcs: ["foo/bar/MyClass.java"],
4313 sdk_version: "none",
4314 system_modules: "none",
4315 apex_available: [ "myapex" ],
4316 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004317 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09004318
4319 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4320 content := bundleConfigRule.Args["content"]
4321
4322 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09004323 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 +09004324}
4325
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004326func testNoUpdatableJarsInBootImage(t *testing.T, errmsg, bp string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
4327 t.Helper()
4328
4329 bp = bp + `
4330 filegroup {
4331 name: "some-updatable-apex-file_contexts",
4332 srcs: [
4333 "system/sepolicy/apex/some-updatable-apex-file_contexts",
4334 ],
4335 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004336
4337 filegroup {
4338 name: "some-non-updatable-apex-file_contexts",
4339 srcs: [
4340 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
4341 ],
4342 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004343 `
4344 bp += cc.GatherRequiredDepsForTest(android.Android)
4345 bp += java.GatherRequiredDepsForTest()
4346 bp += dexpreopt.BpToolModulesForTest()
4347
4348 fs := map[string][]byte{
4349 "a.java": nil,
4350 "a.jar": nil,
4351 "build/make/target/product/security": nil,
4352 "apex_manifest.json": nil,
4353 "AndroidManifest.xml": nil,
4354 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004355 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004356 "system/sepolicy/apex/com.android.art.something-file_contexts": nil,
4357 "framework/aidl/a.aidl": nil,
4358 }
4359 cc.GatherRequiredFilesForTest(fs)
4360
4361 ctx := android.NewTestArchContext()
4362 ctx.RegisterModuleType("apex", BundleFactory)
4363 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
4364 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
4365 cc.RegisterRequiredBuildComponentsForTest(ctx)
4366 java.RegisterJavaBuildComponents(ctx)
4367 java.RegisterSystemModulesBuildComponents(ctx)
4368 java.RegisterAppBuildComponents(ctx)
4369 java.RegisterDexpreoptBootJarsComponents(ctx)
4370 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
4371 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
4372 ctx.PreDepsMutators(RegisterPreDepsMutators)
4373 ctx.PostDepsMutators(RegisterPostDepsMutators)
4374
4375 config := android.TestArchConfig(buildDir, nil, bp, fs)
4376 ctx.Register(config)
4377
4378 _ = dexpreopt.GlobalSoongConfigForTests(config)
4379 dexpreopt.RegisterToolModulesForTest(ctx)
4380 pathCtx := android.PathContextForTesting(config)
4381 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
4382 transformDexpreoptConfig(dexpreoptConfig)
4383 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
4384
4385 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
4386 android.FailIfErrored(t, errs)
4387
4388 _, errs = ctx.PrepareBuildActions(config)
4389 if errmsg == "" {
4390 android.FailIfErrored(t, errs)
4391 } else if len(errs) > 0 {
4392 android.FailIfNoMatchingErrors(t, errmsg, errs)
4393 return
4394 } else {
4395 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
4396 }
4397}
4398
4399func TestNoUpdatableJarsInBootImage(t *testing.T) {
4400 bp := `
4401 java_library {
4402 name: "some-updatable-apex-lib",
4403 srcs: ["a.java"],
4404 apex_available: [
4405 "some-updatable-apex",
4406 ],
4407 }
4408
4409 java_library {
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004410 name: "some-non-updatable-apex-lib",
4411 srcs: ["a.java"],
4412 apex_available: [
4413 "some-non-updatable-apex",
4414 ],
4415 }
4416
4417 java_library {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004418 name: "some-platform-lib",
4419 srcs: ["a.java"],
4420 installable: true,
4421 }
4422
4423 java_library {
4424 name: "some-art-lib",
4425 srcs: ["a.java"],
4426 apex_available: [
4427 "com.android.art.something",
4428 ],
4429 hostdex: true,
4430 }
4431
4432 apex {
4433 name: "some-updatable-apex",
4434 key: "some-updatable-apex.key",
4435 java_libs: ["some-updatable-apex-lib"],
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004436 updatable: true,
4437 }
4438
4439 apex {
4440 name: "some-non-updatable-apex",
4441 key: "some-non-updatable-apex.key",
4442 java_libs: ["some-non-updatable-apex-lib"],
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004443 }
4444
4445 apex_key {
4446 name: "some-updatable-apex.key",
4447 }
4448
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004449 apex_key {
4450 name: "some-non-updatable-apex.key",
4451 }
4452
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004453 apex {
4454 name: "com.android.art.something",
4455 key: "com.android.art.something.key",
4456 java_libs: ["some-art-lib"],
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004457 updatable: true,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004458 }
4459
4460 apex_key {
4461 name: "com.android.art.something.key",
4462 }
4463 `
4464
4465 var error string
4466 var transform func(*dexpreopt.GlobalConfig)
4467
4468 // updatable jar from ART apex in the ART boot image => ok
4469 transform = func(config *dexpreopt.GlobalConfig) {
4470 config.ArtApexJars = []string{"some-art-lib"}
4471 }
4472 testNoUpdatableJarsInBootImage(t, "", bp, transform)
4473
4474 // updatable jar from ART apex in the framework boot image => error
4475 error = "module 'some-art-lib' from updatable apex 'com.android.art.something' is not allowed in the framework boot image"
4476 transform = func(config *dexpreopt.GlobalConfig) {
4477 config.BootJars = []string{"some-art-lib"}
4478 }
4479 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4480
4481 // updatable jar from some other apex in the ART boot image => error
4482 error = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the ART boot image"
4483 transform = func(config *dexpreopt.GlobalConfig) {
4484 config.ArtApexJars = []string{"some-updatable-apex-lib"}
4485 }
4486 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4487
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004488 // non-updatable jar from some other apex in the ART boot image => error
4489 error = "module 'some-non-updatable-apex-lib' is not allowed in the ART boot image"
4490 transform = func(config *dexpreopt.GlobalConfig) {
4491 config.ArtApexJars = []string{"some-non-updatable-apex-lib"}
4492 }
4493 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4494
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004495 // updatable jar from some other apex in the framework boot image => error
4496 error = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the framework boot image"
4497 transform = func(config *dexpreopt.GlobalConfig) {
4498 config.BootJars = []string{"some-updatable-apex-lib"}
4499 }
4500 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4501
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004502 // non-updatable jar from some other apex in the framework boot image => ok
4503 transform = func(config *dexpreopt.GlobalConfig) {
4504 config.BootJars = []string{"some-non-updatable-apex-lib"}
4505 }
4506 testNoUpdatableJarsInBootImage(t, "", bp, transform)
4507
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004508 // nonexistent jar in the ART boot image => error
4509 error = "failed to find a dex jar path for module 'nonexistent'"
4510 transform = func(config *dexpreopt.GlobalConfig) {
4511 config.ArtApexJars = []string{"nonexistent"}
4512 }
4513 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4514
4515 // nonexistent jar in the framework boot image => error
4516 error = "failed to find a dex jar path for module 'nonexistent'"
4517 transform = func(config *dexpreopt.GlobalConfig) {
4518 config.BootJars = []string{"nonexistent"}
4519 }
4520 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4521
4522 // platform jar in the ART boot image => error
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01004523 error = "module 'some-platform-lib' is not allowed in the ART boot image"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00004524 transform = func(config *dexpreopt.GlobalConfig) {
4525 config.ArtApexJars = []string{"some-platform-lib"}
4526 }
4527 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4528
4529 // platform jar in the framework boot image => ok
4530 transform = func(config *dexpreopt.GlobalConfig) {
4531 config.BootJars = []string{"some-platform-lib"}
4532 }
4533 testNoUpdatableJarsInBootImage(t, "", bp, transform)
4534}
4535
Jiyong Park62304bb2020-04-13 16:19:48 +09004536func TestTestFor(t *testing.T) {
4537 ctx, _ := testApex(t, `
4538 apex {
4539 name: "myapex",
4540 key: "myapex.key",
4541 native_shared_libs: ["mylib", "myprivlib"],
4542 }
4543
4544 apex_key {
4545 name: "myapex.key",
4546 public_key: "testkey.avbpubkey",
4547 private_key: "testkey.pem",
4548 }
4549
4550 cc_library {
4551 name: "mylib",
4552 srcs: ["mylib.cpp"],
4553 system_shared_libs: [],
4554 stl: "none",
4555 stubs: {
4556 versions: ["1"],
4557 },
4558 apex_available: ["myapex"],
4559 }
4560
4561 cc_library {
4562 name: "myprivlib",
4563 srcs: ["mylib.cpp"],
4564 system_shared_libs: [],
4565 stl: "none",
4566 apex_available: ["myapex"],
4567 }
4568
4569
4570 cc_test {
4571 name: "mytest",
4572 gtest: false,
4573 srcs: ["mylib.cpp"],
4574 system_shared_libs: [],
4575 stl: "none",
4576 shared_libs: ["mylib", "myprivlib"],
4577 test_for: ["myapex"]
4578 }
4579 `)
4580
4581 // the test 'mytest' is a test for the apex, therefore is linked to the
4582 // actual implementation of mylib instead of its stub.
4583 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
4584 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
4585 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
4586}
4587
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004588func TestMain(m *testing.M) {
4589 run := func() int {
4590 setUp()
4591 defer tearDown()
4592
4593 return m.Run()
4594 }
4595
4596 os.Exit(run())
4597}