blob: 5666e6311fba399f3aa4ff2caf35839e9c115d09 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffinbf19a972020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090024 "strings"
25 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090026
27 "github.com/google/blueprint/proptools"
28
29 "android/soong/android"
30 "android/soong/cc"
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +000031 "android/soong/dexpreopt"
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070032 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090033 "android/soong/java"
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070034 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090035)
36
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070037var buildDir string
38
Jooyung Hand3639552019-08-09 12:57:43 +090039// names returns name list from white space separated string
40func names(s string) (ns []string) {
41 for _, n := range strings.Split(s, " ") {
42 if len(n) > 0 {
43 ns = append(ns, n)
44 }
45 }
46 return
47}
48
Jooyung Han344d5432019-08-23 11:17:39 +090049func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
50 t.Helper()
51 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090052 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
53 if len(errs) > 0 {
54 android.FailIfNoMatchingErrors(t, pattern, errs)
55 return
56 }
57 _, errs = ctx.PrepareBuildActions(config)
58 if len(errs) > 0 {
59 android.FailIfNoMatchingErrors(t, pattern, errs)
60 return
61 }
62
63 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
64}
65
Jooyung Han344d5432019-08-23 11:17:39 +090066func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
67 t.Helper()
68 ctx, config := testApexContext(t, bp, handlers...)
Paul Duffinf642a312020-06-12 17:46:39 +010069 _, errs := ctx.ParseBlueprintsFiles(".")
Jooyung Han5c998b92019-06-27 11:30:33 +090070 android.FailIfErrored(t, errs)
71 _, errs = ctx.PrepareBuildActions(config)
72 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070073 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090074}
75
Jooyung Han344d5432019-08-23 11:17:39 +090076type testCustomizer func(fs map[string][]byte, config android.Config)
77
78func withFiles(files map[string][]byte) testCustomizer {
79 return func(fs map[string][]byte, config android.Config) {
80 for k, v := range files {
81 fs[k] = v
82 }
83 }
84}
85
86func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
87 return func(fs map[string][]byte, config android.Config) {
88 for k, v := range targets {
89 config.Targets[k] = v
90 }
91 }
92}
93
Jiyong Parkaf8998c2020-02-28 16:51:07 +090094func withManifestPackageNameOverrides(specs []string) testCustomizer {
95 return func(fs map[string][]byte, config android.Config) {
96 config.TestProductVariables.ManifestPackageNameOverrides = specs
97 }
98}
99
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700100func withBinder32bit(_ map[string][]byte, config android.Config) {
Jooyung Han31c470b2019-10-18 16:26:59 +0900101 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
102}
103
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700104func withUnbundledBuild(_ map[string][]byte, config android.Config) {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900105 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
106}
107
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700108func testApexContext(_ *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +0900109 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900110
111 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900112 filegroup {
113 name: "myapex-file_contexts",
114 srcs: [
115 "system/sepolicy/apex/myapex-file_contexts",
116 ],
117 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900118 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800119
Colin Crossf9aabd72020-02-15 11:29:50 -0800120 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
121
Dario Frenicde2a032019-10-27 00:29:22 +0100122 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900123
Jooyung Han344d5432019-08-23 11:17:39 +0900124 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900125 "a.java": nil,
126 "PrebuiltAppFoo.apk": nil,
127 "PrebuiltAppFooPriv.apk": nil,
128 "build/make/target/product/security": nil,
129 "apex_manifest.json": nil,
130 "AndroidManifest.xml": nil,
131 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900132 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900133 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900134 "system/sepolicy/apex/otherapex-file_contexts": nil,
135 "system/sepolicy/apex/commonapex-file_contexts": nil,
136 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800137 "mylib.cpp": nil,
138 "mylib_common.cpp": nil,
139 "mytest.cpp": nil,
140 "mytest1.cpp": nil,
141 "mytest2.cpp": nil,
142 "mytest3.cpp": nil,
143 "myprebuilt": nil,
144 "my_include": nil,
145 "foo/bar/MyClass.java": nil,
146 "prebuilt.jar": nil,
Paul Duffin3766cb72020-04-07 15:25:44 +0100147 "prebuilt.so": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800148 "vendor/foo/devkeys/test.x509.pem": nil,
149 "vendor/foo/devkeys/test.pk8": nil,
150 "testkey.x509.pem": nil,
151 "testkey.pk8": nil,
152 "testkey.override.x509.pem": nil,
153 "testkey.override.pk8": nil,
154 "vendor/foo/devkeys/testkey.avbpubkey": nil,
155 "vendor/foo/devkeys/testkey.pem": nil,
156 "NOTICE": nil,
157 "custom_notice": nil,
Jiyong Park162e8442020-03-17 19:16:40 +0900158 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800159 "testkey2.avbpubkey": nil,
160 "testkey2.pem": nil,
161 "myapex-arm64.apex": nil,
162 "myapex-arm.apex": nil,
Jaewoong Jung8cf307e2020-05-14 14:15:24 -0700163 "myapex.apks": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800164 "frameworks/base/api/current.txt": nil,
165 "framework/aidl/a.aidl": nil,
166 "build/make/core/proguard.flags": nil,
167 "build/make/core/proguard_basic_keeps.flags": nil,
168 "dummy.txt": nil,
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700169 "AppSet.apks": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900170 }
171
Colin Crossf9aabd72020-02-15 11:29:50 -0800172 cc.GatherRequiredFilesForTest(fs)
173
Jooyung Han344d5432019-08-23 11:17:39 +0900174 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800175 // The fs now needs to be populated before creating the config, call handlers twice
176 // for now, once to get any fs changes, and later after the config was created to
177 // set product variables or targets.
178 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
179 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900180 }
181
Colin Cross98be1bb2019-12-13 20:41:13 -0800182 config := android.TestArchConfig(buildDir, nil, bp, fs)
183 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
184 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
185 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
186 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
187 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
188 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
189
190 for _, handler := range handlers {
191 // The fs now needs to be populated before creating the config, call handlers twice
192 // for now, earlier to get any fs changes, and now after the config was created to
193 // set product variables or targets.
194 tempFS := map[string][]byte{}
195 handler(tempFS, config)
196 }
197
198 ctx := android.NewTestArchContext()
Paul Duffinf642a312020-06-12 17:46:39 +0100199
200 // from android package
201 android.RegisterPackageBuildComponents(ctx)
202 ctx.PreArchMutators(android.RegisterVisibilityRuleChecker)
203
Colin Cross98be1bb2019-12-13 20:41:13 -0800204 ctx.RegisterModuleType("apex", BundleFactory)
205 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
206 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
207 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
208 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
209 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
210 ctx.RegisterModuleType("override_apex", overrideApexFactory)
Jaewoong Jung8cf307e2020-05-14 14:15:24 -0700211 ctx.RegisterModuleType("apex_set", apexSetFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800212
Jooyung Hana57af4a2020-01-23 05:36:59 +0000213 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
214 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
215
Paul Duffin77980a82019-12-19 16:01:36 +0000216 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf642a312020-06-12 17:46:39 +0100217
218 // Register this after the prebuilt mutators have been registered (in
219 // cc.RegisterRequiredBuildComponentsForTest) to match what happens at runtime.
220 ctx.PreArchMutators(android.RegisterVisibilityRuleGatherer)
221 ctx.PostDepsMutators(android.RegisterVisibilityRuleEnforcer)
222
Colin Cross98be1bb2019-12-13 20:41:13 -0800223 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800224 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
225 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700226 ctx.RegisterModuleType("prebuilt_etc", prebuilt_etc.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000227 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700228 ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800229 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000230 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000231 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000232 java.RegisterAppBuildComponents(ctx)
Paul Duffinf642a312020-06-12 17:46:39 +0100233 java.RegisterSdkLibraryBuildComponents(ctx)
Jiyong Park8d6286b2020-06-12 17:26:31 +0900234 ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
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() {
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700253 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900254}
255
256// ensure that 'result' contains 'expected'
257func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900258 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259 if !strings.Contains(result, expected) {
260 t.Errorf("%q is not found in %q", expected, result)
261 }
262}
263
264// ensures that 'result' does not contain 'notExpected'
265func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900266 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900267 if strings.Contains(result, notExpected) {
268 t.Errorf("%q is found in %q", notExpected, result)
269 }
270}
271
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700272func ensureMatches(t *testing.T, result string, expectedRex string) {
273 ok, err := regexp.MatchString(expectedRex, result)
274 if err != nil {
275 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
276 return
277 }
278 if !ok {
279 t.Errorf("%s does not match regular expession %s", result, expectedRex)
280 }
281}
282
Jiyong Park25fc6a92018-11-18 18:02:45 +0900283func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900284 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900285 if !android.InList(expected, result) {
286 t.Errorf("%q is not found in %v", expected, result)
287 }
288}
289
290func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900291 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900292 if android.InList(notExpected, result) {
293 t.Errorf("%q is found in %v", notExpected, result)
294 }
295}
296
Jooyung Hane1633032019-08-01 17:41:43 +0900297func ensureListEmpty(t *testing.T, result []string) {
298 t.Helper()
299 if len(result) > 0 {
300 t.Errorf("%q is expected to be empty", result)
301 }
302}
303
Jiyong Park25fc6a92018-11-18 18:02:45 +0900304// Minimal test
305func TestBasicApex(t *testing.T) {
Jiyong Parke26a63e2020-06-11 00:35:03 +0900306 ctx, config := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900307 apex_defaults {
308 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900309 manifest: ":myapex.manifest",
310 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900311 key: "myapex.key",
312 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800313 multilib: {
314 both: {
315 binaries: ["foo",],
316 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900317 },
Jiyong Park2cd081c2020-06-01 21:39:15 +0900318 java_libs: [
319 "myjar",
320 "myjar_dex",
321 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322 }
323
Jiyong Park30ca9372019-02-07 16:27:23 +0900324 apex {
325 name: "myapex",
326 defaults: ["myapex-defaults"],
327 }
328
Jiyong Park25fc6a92018-11-18 18:02:45 +0900329 apex_key {
330 name: "myapex.key",
331 public_key: "testkey.avbpubkey",
332 private_key: "testkey.pem",
333 }
334
Jiyong Park809bb722019-02-13 21:33:49 +0900335 filegroup {
336 name: "myapex.manifest",
337 srcs: ["apex_manifest.json"],
338 }
339
340 filegroup {
341 name: "myapex.androidmanifest",
342 srcs: ["AndroidManifest.xml"],
343 }
344
Jiyong Park25fc6a92018-11-18 18:02:45 +0900345 cc_library {
346 name: "mylib",
347 srcs: ["mylib.cpp"],
348 shared_libs: ["mylib2"],
349 system_shared_libs: [],
350 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000351 // TODO: remove //apex_available:platform
352 apex_available: [
353 "//apex_available:platform",
354 "myapex",
355 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900356 }
357
Alex Light3d673592019-01-18 14:37:31 -0800358 cc_binary {
359 name: "foo",
360 srcs: ["mylib.cpp"],
361 compile_multilib: "both",
362 multilib: {
363 lib32: {
364 suffix: "32",
365 },
366 lib64: {
367 suffix: "64",
368 },
369 },
370 symlinks: ["foo_link_"],
371 symlink_preferred_arch: true,
372 system_shared_libs: [],
373 static_executable: true,
374 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000375 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800376 }
377
Paul Duffin3766cb72020-04-07 15:25:44 +0100378 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900379 name: "mylib2",
380 srcs: ["mylib.cpp"],
381 system_shared_libs: [],
382 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900383 notice: "custom_notice",
Jiyong Park162e8442020-03-17 19:16:40 +0900384 static_libs: ["libstatic"],
385 // TODO: remove //apex_available:platform
386 apex_available: [
387 "//apex_available:platform",
388 "myapex",
389 ],
390 }
391
Paul Duffin3766cb72020-04-07 15:25:44 +0100392 cc_prebuilt_library_shared {
393 name: "mylib2",
394 srcs: ["prebuilt.so"],
395 // TODO: remove //apex_available:platform
396 apex_available: [
397 "//apex_available:platform",
398 "myapex",
399 ],
400 }
401
Jiyong Park162e8442020-03-17 19:16:40 +0900402 cc_library_static {
403 name: "libstatic",
404 srcs: ["mylib.cpp"],
405 system_shared_libs: [],
406 stl: "none",
407 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000408 // TODO: remove //apex_available:platform
409 apex_available: [
410 "//apex_available:platform",
411 "myapex",
412 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900413 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900414
415 java_library {
416 name: "myjar",
417 srcs: ["foo/bar/MyClass.java"],
Jiyong Parked50ca82020-05-28 23:46:55 +0900418 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900419 sdk_version: "none",
420 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900421 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900422 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000423 // TODO: remove //apex_available:platform
424 apex_available: [
425 "//apex_available:platform",
426 "myapex",
427 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900428 }
429
Jiyong Park2cd081c2020-06-01 21:39:15 +0900430 dex_import {
431 name: "myjar_dex",
432 jars: ["prebuilt.jar"],
433 apex_available: [
434 "//apex_available:platform",
435 "myapex",
436 ],
437 }
438
Jiyong Park7f7766d2019-07-25 22:02:35 +0900439 java_library {
440 name: "myotherjar",
441 srcs: ["foo/bar/MyClass.java"],
442 sdk_version: "none",
443 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900444 // TODO: remove //apex_available:platform
445 apex_available: [
446 "//apex_available:platform",
447 "myapex",
448 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900449 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900450
451 java_library {
452 name: "mysharedjar",
453 srcs: ["foo/bar/MyClass.java"],
454 sdk_version: "none",
455 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900456 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900457 `)
458
Sundong Ahnabb64432019-10-22 13:58:29 +0900459 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900460
Jiyong Parke26a63e2020-06-11 00:35:03 +0900461 // Make sure that Android.mk is created
462 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
463 data := android.AndroidMkDataForTest(t, config, "", ab)
464 var builder strings.Builder
465 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
466
467 androidMk := builder.String()
468 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
469 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
470
Jiyong Park42cca6c2019-04-01 11:15:50 +0900471 optFlags := apexRule.Args["opt_flags"]
472 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700473 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900474 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900475
Jiyong Park25fc6a92018-11-18 18:02:45 +0900476 copyCmds := apexRule.Args["copy_commands"]
477
478 // Ensure that main rule creates an output
479 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
480
481 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800482 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900483 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park2cd081c2020-06-01 21:39:15 +0900484 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900485
486 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800487 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900488 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900489
490 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800491 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
492 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parked50ca82020-05-28 23:46:55 +0900493 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park2cd081c2020-06-01 21:39:15 +0900494 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900495 // .. but not for java libs
496 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900497 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800498
Colin Cross7113d202019-11-20 16:39:12 -0800499 // Ensure that the platform variant ends with _shared or _common
500 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
501 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900502 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
503 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900504 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
505
506 // Ensure that dynamic dependency to java libs are not included
507 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800508
509 // Ensure that all symlinks are present.
510 found_foo_link_64 := false
511 found_foo := false
512 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900513 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800514 if strings.HasSuffix(cmd, "bin/foo") {
515 found_foo = true
516 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
517 found_foo_link_64 = true
518 }
519 }
520 }
521 good := found_foo && found_foo_link_64
522 if !good {
523 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
524 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900525
Sundong Ahnabb64432019-10-22 13:58:29 +0900526 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700527 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park162e8442020-03-17 19:16:40 +0900528 if len(noticeInputs) != 3 {
529 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900530 }
531 ensureListContains(t, noticeInputs, "NOTICE")
532 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park162e8442020-03-17 19:16:40 +0900533 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900534
Artur Satayev5e7c32d2020-04-27 18:07:06 +0100535 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev388d39b2020-04-27 18:53:18 +0100536 ensureListContains(t, fullDepsInfo, "myjar(minSdkVersion:(no version)) <- myapex")
537 ensureListContains(t, fullDepsInfo, "mylib(minSdkVersion:(no version)) <- myapex")
538 ensureListContains(t, fullDepsInfo, "mylib2(minSdkVersion:(no version)) <- mylib")
539 ensureListContains(t, fullDepsInfo, "myotherjar(minSdkVersion:(no version)) <- myjar")
540 ensureListContains(t, fullDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayev5e7c32d2020-04-27 18:07:06 +0100541
542 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev388d39b2020-04-27 18:53:18 +0100543 ensureListContains(t, flatDepsInfo, " myjar(minSdkVersion:(no version))")
544 ensureListContains(t, flatDepsInfo, " mylib(minSdkVersion:(no version))")
545 ensureListContains(t, flatDepsInfo, " mylib2(minSdkVersion:(no version))")
546 ensureListContains(t, flatDepsInfo, " myotherjar(minSdkVersion:(no version))")
547 ensureListContains(t, flatDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800548}
549
Jooyung Hanf21c7972019-12-16 22:32:06 +0900550func TestDefaults(t *testing.T) {
551 ctx, _ := testApex(t, `
552 apex_defaults {
553 name: "myapex-defaults",
554 key: "myapex.key",
555 prebuilts: ["myetc"],
556 native_shared_libs: ["mylib"],
557 java_libs: ["myjar"],
558 apps: ["AppFoo"],
559 }
560
561 prebuilt_etc {
562 name: "myetc",
563 src: "myprebuilt",
564 }
565
566 apex {
567 name: "myapex",
568 defaults: ["myapex-defaults"],
569 }
570
571 apex_key {
572 name: "myapex.key",
573 public_key: "testkey.avbpubkey",
574 private_key: "testkey.pem",
575 }
576
577 cc_library {
578 name: "mylib",
579 system_shared_libs: [],
580 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000581 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900582 }
583
584 java_library {
585 name: "myjar",
586 srcs: ["foo/bar/MyClass.java"],
587 sdk_version: "none",
588 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000589 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900590 }
591
592 android_app {
593 name: "AppFoo",
594 srcs: ["foo/bar/MyClass.java"],
595 sdk_version: "none",
596 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000597 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900598 }
599 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000600 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900601 "etc/myetc",
602 "javalib/myjar.jar",
603 "lib64/mylib.so",
604 "app/AppFoo/AppFoo.apk",
605 })
606}
607
Jooyung Han01a3ee22019-11-02 02:52:25 +0900608func TestApexManifest(t *testing.T) {
609 ctx, _ := testApex(t, `
610 apex {
611 name: "myapex",
612 key: "myapex.key",
613 }
614
615 apex_key {
616 name: "myapex.key",
617 public_key: "testkey.avbpubkey",
618 private_key: "testkey.pem",
619 }
620 `)
621
622 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900623 args := module.Rule("apexRule").Args
624 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
625 t.Error("manifest should be apex_manifest.pb, but " + manifest)
626 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900627}
628
Alex Light5098a612018-11-29 17:12:15 -0800629func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700630 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800631 apex {
632 name: "myapex",
633 key: "myapex.key",
634 payload_type: "zip",
635 native_shared_libs: ["mylib"],
636 }
637
638 apex_key {
639 name: "myapex.key",
640 public_key: "testkey.avbpubkey",
641 private_key: "testkey.pem",
642 }
643
644 cc_library {
645 name: "mylib",
646 srcs: ["mylib.cpp"],
647 shared_libs: ["mylib2"],
648 system_shared_libs: [],
649 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000650 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800651 }
652
653 cc_library {
654 name: "mylib2",
655 srcs: ["mylib.cpp"],
656 system_shared_libs: [],
657 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000658 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800659 }
660 `)
661
Sundong Ahnabb64432019-10-22 13:58:29 +0900662 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800663 copyCmds := zipApexRule.Args["copy_commands"]
664
665 // Ensure that main rule creates an output
666 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
667
668 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800669 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800670
671 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800672 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800673
674 // Ensure that both direct and indirect deps are copied into apex
675 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
676 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900677}
678
679func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700680 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900681 apex {
682 name: "myapex",
683 key: "myapex.key",
684 native_shared_libs: ["mylib", "mylib3"],
685 }
686
687 apex_key {
688 name: "myapex.key",
689 public_key: "testkey.avbpubkey",
690 private_key: "testkey.pem",
691 }
692
693 cc_library {
694 name: "mylib",
695 srcs: ["mylib.cpp"],
696 shared_libs: ["mylib2", "mylib3"],
697 system_shared_libs: [],
698 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000699 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900700 }
701
702 cc_library {
703 name: "mylib2",
704 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900705 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900706 system_shared_libs: [],
707 stl: "none",
708 stubs: {
709 versions: ["1", "2", "3"],
710 },
711 }
712
713 cc_library {
714 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900715 srcs: ["mylib.cpp"],
716 shared_libs: ["mylib4"],
717 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900718 stl: "none",
719 stubs: {
720 versions: ["10", "11", "12"],
721 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000722 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900723 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900724
725 cc_library {
726 name: "mylib4",
727 srcs: ["mylib.cpp"],
728 system_shared_libs: [],
729 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000730 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900731 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900732 `)
733
Sundong Ahnabb64432019-10-22 13:58:29 +0900734 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900735 copyCmds := apexRule.Args["copy_commands"]
736
737 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800738 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900739
740 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800741 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900742
743 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800744 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900745
Colin Cross7113d202019-11-20 16:39:12 -0800746 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900747
748 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900749 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900750 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900751 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900752
753 // 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 -0800754 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900755 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800756 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900757
758 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900759 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900760 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900761
762 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900763 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900764
Jooyung Hana57af4a2020-01-23 05:36:59 +0000765 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900766 "lib64/mylib.so",
767 "lib64/mylib3.so",
768 "lib64/mylib4.so",
769 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900770}
771
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900772func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700773 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900774 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900775 name: "myapex2",
776 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900777 native_shared_libs: ["mylib"],
778 }
779
780 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900781 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900782 public_key: "testkey.avbpubkey",
783 private_key: "testkey.pem",
784 }
785
786 cc_library {
787 name: "mylib",
788 srcs: ["mylib.cpp"],
789 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900790 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900791 system_shared_libs: [],
792 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000793 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900794 }
795
796 cc_library {
797 name: "libfoo",
798 srcs: ["mylib.cpp"],
799 shared_libs: ["libbar"],
800 system_shared_libs: [],
801 stl: "none",
802 stubs: {
803 versions: ["10", "20", "30"],
804 },
805 }
806
807 cc_library {
808 name: "libbar",
809 srcs: ["mylib.cpp"],
810 system_shared_libs: [],
811 stl: "none",
812 }
813
Jiyong Park678c8812020-02-07 17:25:49 +0900814 cc_library_static {
815 name: "libbaz",
816 srcs: ["mylib.cpp"],
817 system_shared_libs: [],
818 stl: "none",
819 apex_available: [ "myapex2" ],
820 }
821
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900822 `)
823
Jiyong Park83dc74b2020-01-14 18:38:44 +0900824 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900825 copyCmds := apexRule.Args["copy_commands"]
826
827 // Ensure that direct non-stubs dep is always included
828 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
829
830 // Ensure that indirect stubs dep is not included
831 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
832
833 // Ensure that dependency of stubs is not included
834 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
835
Jiyong Park83dc74b2020-01-14 18:38:44 +0900836 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900837
838 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900839 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900840 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900841 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900842
Jiyong Park3ff16992019-12-27 14:11:47 +0900843 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900844
845 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
846 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900847
Artur Satayev5e7c32d2020-04-27 18:07:06 +0100848 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev388d39b2020-04-27 18:53:18 +0100849 ensureListContains(t, fullDepsInfo, "mylib(minSdkVersion:(no version)) <- myapex2")
850 ensureListContains(t, fullDepsInfo, "libbaz(minSdkVersion:(no version)) <- mylib")
851 ensureListContains(t, fullDepsInfo, "libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +0900852
Artur Satayev5e7c32d2020-04-27 18:07:06 +0100853 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev388d39b2020-04-27 18:53:18 +0100854 ensureListContains(t, flatDepsInfo, " mylib(minSdkVersion:(no version))")
855 ensureListContains(t, flatDepsInfo, " libbaz(minSdkVersion:(no version))")
856 ensureListContains(t, flatDepsInfo, " libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900857}
858
Jooyung Hand3639552019-08-09 12:57:43 +0900859func TestApexWithRuntimeLibsDependency(t *testing.T) {
860 /*
861 myapex
862 |
863 v (runtime_libs)
864 mylib ------+------> libfoo [provides stub]
865 |
866 `------> libbar
867 */
868 ctx, _ := testApex(t, `
869 apex {
870 name: "myapex",
871 key: "myapex.key",
872 native_shared_libs: ["mylib"],
873 }
874
875 apex_key {
876 name: "myapex.key",
877 public_key: "testkey.avbpubkey",
878 private_key: "testkey.pem",
879 }
880
881 cc_library {
882 name: "mylib",
883 srcs: ["mylib.cpp"],
884 runtime_libs: ["libfoo", "libbar"],
885 system_shared_libs: [],
886 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000887 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900888 }
889
890 cc_library {
891 name: "libfoo",
892 srcs: ["mylib.cpp"],
893 system_shared_libs: [],
894 stl: "none",
895 stubs: {
896 versions: ["10", "20", "30"],
897 },
898 }
899
900 cc_library {
901 name: "libbar",
902 srcs: ["mylib.cpp"],
903 system_shared_libs: [],
904 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000905 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900906 }
907
908 `)
909
Sundong Ahnabb64432019-10-22 13:58:29 +0900910 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900911 copyCmds := apexRule.Args["copy_commands"]
912
913 // Ensure that direct non-stubs dep is always included
914 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
915
916 // Ensure that indirect stubs dep is not included
917 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
918
919 // Ensure that runtime_libs dep in included
920 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
921
Sundong Ahnabb64432019-10-22 13:58:29 +0900922 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900923 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
924 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900925
926}
927
Jooyung Han67a96cd2020-03-13 15:23:36 +0900928func TestApexDependsOnLLNDKTransitively(t *testing.T) {
929 testcases := []struct {
930 name string
931 minSdkVersion string
932 shouldLink string
933 shouldNotLink []string
934 }{
935 {
Jooyung Han74066602020-03-20 04:29:24 +0900936 name: "should link to the latest",
Jooyung Han67a96cd2020-03-13 15:23:36 +0900937 minSdkVersion: "current",
938 shouldLink: "30",
939 shouldNotLink: []string{"29"},
940 },
941 {
942 name: "should link to llndk#29",
943 minSdkVersion: "29",
944 shouldLink: "29",
945 shouldNotLink: []string{"30"},
946 },
947 }
948 for _, tc := range testcases {
949 t.Run(tc.name, func(t *testing.T) {
950 ctx, _ := testApex(t, `
951 apex {
952 name: "myapex",
953 key: "myapex.key",
954 use_vendor: true,
955 native_shared_libs: ["mylib"],
956 min_sdk_version: "`+tc.minSdkVersion+`",
957 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900958
Jooyung Han67a96cd2020-03-13 15:23:36 +0900959 apex_key {
960 name: "myapex.key",
961 public_key: "testkey.avbpubkey",
962 private_key: "testkey.pem",
963 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900964
Jooyung Han67a96cd2020-03-13 15:23:36 +0900965 cc_library {
966 name: "mylib",
967 srcs: ["mylib.cpp"],
968 vendor_available: true,
969 shared_libs: ["libbar"],
970 system_shared_libs: [],
971 stl: "none",
972 apex_available: [ "myapex" ],
973 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900974
Jooyung Han67a96cd2020-03-13 15:23:36 +0900975 cc_library {
976 name: "libbar",
977 srcs: ["mylib.cpp"],
978 system_shared_libs: [],
979 stl: "none",
980 stubs: { versions: ["29","30"] },
981 }
Jooyung Han9c80bae2019-08-20 17:30:57 +0900982
Jooyung Han67a96cd2020-03-13 15:23:36 +0900983 llndk_library {
984 name: "libbar",
985 symbol_file: "",
986 }
987 `, func(fs map[string][]byte, config android.Config) {
Colin Cross95f7b342020-06-11 11:32:11 -0700988 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Han67a96cd2020-03-13 15:23:36 +0900989 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +0900990
Jooyung Han67a96cd2020-03-13 15:23:36 +0900991 // Ensure that LLNDK dep is not included
992 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
993 "lib64/mylib.so",
994 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900995
Jooyung Han67a96cd2020-03-13 15:23:36 +0900996 // Ensure that LLNDK dep is required
997 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
998 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
999 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001000
Jooyung Han67a96cd2020-03-13 15:23:36 +09001001 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1002 ensureContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
1003 for _, ver := range tc.shouldNotLink {
1004 ensureNotContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
1005 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001006
Jooyung Han67a96cd2020-03-13 15:23:36 +09001007 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1008 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1009 })
1010 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001011}
1012
Jiyong Park25fc6a92018-11-18 18:02:45 +09001013func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001014 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001015 apex {
1016 name: "myapex",
1017 key: "myapex.key",
1018 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
1019 }
1020
1021 apex_key {
1022 name: "myapex.key",
1023 public_key: "testkey.avbpubkey",
1024 private_key: "testkey.pem",
1025 }
1026
1027 cc_library {
1028 name: "mylib",
1029 srcs: ["mylib.cpp"],
1030 shared_libs: ["libdl#27"],
1031 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001032 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001033 }
1034
1035 cc_library_shared {
1036 name: "mylib_shared",
1037 srcs: ["mylib.cpp"],
1038 shared_libs: ["libdl#27"],
1039 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001040 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001041 }
1042
1043 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001044 name: "libBootstrap",
1045 srcs: ["mylib.cpp"],
1046 stl: "none",
1047 bootstrap: true,
1048 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001049 `)
1050
Sundong Ahnabb64432019-10-22 13:58:29 +09001051 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001052 copyCmds := apexRule.Args["copy_commands"]
1053
1054 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001055 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001056 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1057 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001058
1059 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001060 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001061
Colin Cross7113d202019-11-20 16:39:12 -08001062 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1063 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1064 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001065
1066 // For dependency to libc
1067 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001068 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001069 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001070 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001071 // ... Cflags from stub is correctly exported to mylib
1072 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1073 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1074
1075 // For dependency to libm
1076 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001077 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001078 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001079 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001080 // ... and is not compiling with the stub
1081 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1082 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1083
1084 // For dependency to libdl
1085 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001086 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001087 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001088 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1089 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001090 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001091 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001092 // ... Cflags from stub is correctly exported to mylib
1093 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1094 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001095
1096 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001097 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1098 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1099 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1100 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001101}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001102
Jooyung Han0c4e0162020-02-26 22:45:42 +09001103func TestApexUseStubsAccordingToMinSdkVersionInUnbundledBuild(t *testing.T) {
1104 // there are three links between liba --> libz
1105 // 1) myapex -> libx -> liba -> libz : this should be #2 link, but fallback to #1
1106 // 2) otherapex -> liby -> liba -> libz : this should be #3 link
1107 // 3) (platform) -> liba -> libz : this should be non-stub link
1108 ctx, _ := testApex(t, `
1109 apex {
1110 name: "myapex",
1111 key: "myapex.key",
1112 native_shared_libs: ["libx"],
1113 min_sdk_version: "2",
1114 }
1115
1116 apex {
1117 name: "otherapex",
1118 key: "myapex.key",
1119 native_shared_libs: ["liby"],
1120 min_sdk_version: "3",
1121 }
1122
1123 apex_key {
1124 name: "myapex.key",
1125 public_key: "testkey.avbpubkey",
1126 private_key: "testkey.pem",
1127 }
1128
1129 cc_library {
1130 name: "libx",
1131 shared_libs: ["liba"],
1132 system_shared_libs: [],
1133 stl: "none",
1134 apex_available: [ "myapex" ],
1135 }
1136
1137 cc_library {
1138 name: "liby",
1139 shared_libs: ["liba"],
1140 system_shared_libs: [],
1141 stl: "none",
1142 apex_available: [ "otherapex" ],
1143 }
1144
1145 cc_library {
1146 name: "liba",
1147 shared_libs: ["libz"],
1148 system_shared_libs: [],
1149 stl: "none",
1150 apex_available: [
1151 "//apex_available:anyapex",
1152 "//apex_available:platform",
1153 ],
1154 }
1155
1156 cc_library {
1157 name: "libz",
1158 system_shared_libs: [],
1159 stl: "none",
1160 stubs: {
1161 versions: ["1", "3"],
1162 },
1163 }
1164 `, withUnbundledBuild)
1165
1166 expectLink := func(from, from_variant, to, to_variant string) {
1167 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1168 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1169 }
1170 expectNoLink := func(from, from_variant, to, to_variant string) {
1171 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1172 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1173 }
1174 // platform liba is linked to non-stub version
1175 expectLink("liba", "shared", "libz", "shared")
1176 // liba in myapex is linked to #1
1177 expectLink("liba", "shared_myapex", "libz", "shared_1")
1178 expectNoLink("liba", "shared_myapex", "libz", "shared_3")
1179 expectNoLink("liba", "shared_myapex", "libz", "shared")
1180 // liba in otherapex is linked to #3
1181 expectLink("liba", "shared_otherapex", "libz", "shared_3")
1182 expectNoLink("liba", "shared_otherapex", "libz", "shared_1")
1183 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1184}
1185
Jooyung Han29e91d22020-04-02 01:41:41 +09001186func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
1187 ctx, _ := testApex(t, `
1188 apex {
1189 name: "myapex",
1190 key: "myapex.key",
1191 native_shared_libs: ["libx"],
1192 min_sdk_version: "R",
1193 }
1194
1195 apex_key {
1196 name: "myapex.key",
1197 public_key: "testkey.avbpubkey",
1198 private_key: "testkey.pem",
1199 }
1200
1201 cc_library {
1202 name: "libx",
1203 shared_libs: ["libz"],
1204 system_shared_libs: [],
1205 stl: "none",
1206 apex_available: [ "myapex" ],
1207 }
1208
1209 cc_library {
1210 name: "libz",
1211 system_shared_libs: [],
1212 stl: "none",
1213 stubs: {
1214 versions: ["29", "R"],
1215 },
1216 }
1217 `, func(fs map[string][]byte, config android.Config) {
1218 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
1219 })
1220
1221 expectLink := func(from, from_variant, to, to_variant string) {
1222 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1223 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1224 }
1225 expectNoLink := func(from, from_variant, to, to_variant string) {
1226 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1227 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1228 }
1229 // 9000 is quite a magic number.
1230 // Finalized SDK codenames are mapped as P(28), Q(29), ...
1231 // And, codenames which are not finalized yet(active_codenames + future_codenames) are numbered from 9000, 9001, ...
1232 // to distinguish them from finalized and future_api(10000)
1233 // In this test, "R" is assumed not finalized yet( listed in Platform_version_active_codenames) and translated into 9000
1234 // (refer android/api_levels.go)
1235 expectLink("libx", "shared_myapex", "libz", "shared_9000")
1236 expectNoLink("libx", "shared_myapex", "libz", "shared_29")
1237 expectNoLink("libx", "shared_myapex", "libz", "shared")
1238}
1239
Jooyung Han0c4e0162020-02-26 22:45:42 +09001240func TestApexMinSdkVersionDefaultsToLatest(t *testing.T) {
1241 ctx, _ := testApex(t, `
1242 apex {
1243 name: "myapex",
1244 key: "myapex.key",
1245 native_shared_libs: ["libx"],
1246 }
1247
1248 apex_key {
1249 name: "myapex.key",
1250 public_key: "testkey.avbpubkey",
1251 private_key: "testkey.pem",
1252 }
1253
1254 cc_library {
1255 name: "libx",
1256 shared_libs: ["libz"],
1257 system_shared_libs: [],
1258 stl: "none",
1259 apex_available: [ "myapex" ],
1260 }
1261
1262 cc_library {
1263 name: "libz",
1264 system_shared_libs: [],
1265 stl: "none",
1266 stubs: {
1267 versions: ["1", "2"],
1268 },
1269 }
1270 `)
1271
1272 expectLink := func(from, from_variant, to, to_variant string) {
1273 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1274 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1275 }
1276 expectNoLink := func(from, from_variant, to, to_variant string) {
1277 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1278 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1279 }
1280 expectLink("libx", "shared_myapex", "libz", "shared_2")
1281 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1282 expectNoLink("libx", "shared_myapex", "libz", "shared")
1283}
1284
1285func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1286 ctx, _ := testApex(t, `
1287 apex {
1288 name: "myapex",
1289 key: "myapex.key",
1290 native_shared_libs: ["libx"],
1291 }
1292
1293 apex_key {
1294 name: "myapex.key",
1295 public_key: "testkey.avbpubkey",
1296 private_key: "testkey.pem",
1297 }
1298
1299 cc_library {
1300 name: "libx",
1301 system_shared_libs: [],
1302 stl: "none",
1303 apex_available: [ "myapex" ],
1304 stubs: {
1305 versions: ["1", "2"],
1306 },
1307 }
1308
1309 cc_library {
1310 name: "libz",
1311 shared_libs: ["libx"],
1312 system_shared_libs: [],
1313 stl: "none",
1314 }
1315 `)
1316
1317 expectLink := func(from, from_variant, to, to_variant string) {
1318 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1319 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1320 }
1321 expectNoLink := func(from, from_variant, to, to_variant string) {
1322 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1323 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1324 }
1325 expectLink("libz", "shared", "libx", "shared_2")
1326 expectNoLink("libz", "shared", "libz", "shared_1")
1327 expectNoLink("libz", "shared", "libz", "shared")
1328}
1329
Jooyung Han74066602020-03-20 04:29:24 +09001330func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Jooyung Han0c4e0162020-02-26 22:45:42 +09001331 ctx, _ := testApex(t, `
1332 apex {
1333 name: "myapex",
1334 key: "myapex.key",
1335 native_shared_libs: ["libx"],
1336 min_sdk_version: "29",
1337 }
1338
1339 apex_key {
1340 name: "myapex.key",
1341 public_key: "testkey.avbpubkey",
1342 private_key: "testkey.pem",
1343 }
1344
1345 cc_library {
1346 name: "libx",
1347 shared_libs: ["libbar"],
1348 apex_available: [ "myapex" ],
1349 }
1350
1351 cc_library {
1352 name: "libbar",
1353 stubs: {
1354 versions: ["29", "30"],
1355 },
1356 }
Jooyung Han74066602020-03-20 04:29:24 +09001357 `, func(fs map[string][]byte, config android.Config) {
1358 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1359 })
Jooyung Han0c4e0162020-02-26 22:45:42 +09001360 expectLink := func(from, from_variant, to, to_variant string) {
1361 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1362 libFlags := ld.Args["libFlags"]
1363 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1364 }
Jooyung Han74066602020-03-20 04:29:24 +09001365 expectLink("libx", "shared_hwasan_myapex", "libbar", "shared_30")
Jooyung Han0c4e0162020-02-26 22:45:42 +09001366}
1367
Jooyung Han74066602020-03-20 04:29:24 +09001368func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Jooyung Han0c4e0162020-02-26 22:45:42 +09001369 ctx, _ := testApex(t, `
1370 apex {
1371 name: "myapex",
1372 key: "myapex.key",
1373 native_shared_libs: ["libx"],
1374 min_sdk_version: "29",
1375 }
1376
1377 apex_key {
1378 name: "myapex.key",
1379 public_key: "testkey.avbpubkey",
1380 private_key: "testkey.pem",
1381 }
1382
1383 cc_library {
1384 name: "libx",
1385 apex_available: [ "myapex" ],
1386 }
Jooyung Han74066602020-03-20 04:29:24 +09001387 `)
Jooyung Han0c4e0162020-02-26 22:45:42 +09001388
1389 // ensure apex variant of c++ is linked with static unwinder
1390 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1391 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1392 // note that platform variant is not.
1393 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1394 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Han0c4e0162020-02-26 22:45:42 +09001395}
1396
1397func TestInvalidMinSdkVersion(t *testing.T) {
Jooyung Han74066602020-03-20 04:29:24 +09001398 testApexError(t, `"libz" .*: not found a version\(<=29\)`, `
Jooyung Han0c4e0162020-02-26 22:45:42 +09001399 apex {
1400 name: "myapex",
1401 key: "myapex.key",
1402 native_shared_libs: ["libx"],
1403 min_sdk_version: "29",
1404 }
1405
1406 apex_key {
1407 name: "myapex.key",
1408 public_key: "testkey.avbpubkey",
1409 private_key: "testkey.pem",
1410 }
1411
1412 cc_library {
1413 name: "libx",
1414 shared_libs: ["libz"],
1415 system_shared_libs: [],
1416 stl: "none",
1417 apex_available: [ "myapex" ],
1418 }
1419
1420 cc_library {
1421 name: "libz",
1422 system_shared_libs: [],
1423 stl: "none",
1424 stubs: {
1425 versions: ["30"],
1426 },
1427 }
Jooyung Han74066602020-03-20 04:29:24 +09001428 `)
Jooyung Han0c4e0162020-02-26 22:45:42 +09001429
Jooyung Han29e91d22020-04-02 01:41:41 +09001430 testApexError(t, `"myapex" .*: min_sdk_version: SDK version should be .*`, `
Jooyung Han0c4e0162020-02-26 22:45:42 +09001431 apex {
1432 name: "myapex",
1433 key: "myapex.key",
Jooyung Han29e91d22020-04-02 01:41:41 +09001434 min_sdk_version: "abc",
Jooyung Han0c4e0162020-02-26 22:45:42 +09001435 }
1436
1437 apex_key {
1438 name: "myapex.key",
1439 public_key: "testkey.avbpubkey",
1440 private_key: "testkey.pem",
1441 }
1442 `)
1443}
1444
Artur Satayev2eedf622020-04-15 17:29:42 +01001445func TestJavaStableSdkVersion(t *testing.T) {
1446 testCases := []struct {
1447 name string
1448 expectedError string
1449 bp string
1450 }{
1451 {
1452 name: "Non-updatable apex with non-stable dep",
1453 bp: `
1454 apex {
1455 name: "myapex",
1456 java_libs: ["myjar"],
1457 key: "myapex.key",
1458 }
1459 apex_key {
1460 name: "myapex.key",
1461 public_key: "testkey.avbpubkey",
1462 private_key: "testkey.pem",
1463 }
1464 java_library {
1465 name: "myjar",
1466 srcs: ["foo/bar/MyClass.java"],
1467 sdk_version: "core_platform",
1468 apex_available: ["myapex"],
1469 }
1470 `,
1471 },
1472 {
1473 name: "Updatable apex with stable dep",
1474 bp: `
1475 apex {
1476 name: "myapex",
1477 java_libs: ["myjar"],
1478 key: "myapex.key",
1479 updatable: true,
1480 min_sdk_version: "29",
1481 }
1482 apex_key {
1483 name: "myapex.key",
1484 public_key: "testkey.avbpubkey",
1485 private_key: "testkey.pem",
1486 }
1487 java_library {
1488 name: "myjar",
1489 srcs: ["foo/bar/MyClass.java"],
1490 sdk_version: "current",
1491 apex_available: ["myapex"],
1492 }
1493 `,
1494 },
1495 {
1496 name: "Updatable apex with non-stable dep",
1497 expectedError: "cannot depend on \"myjar\"",
1498 bp: `
1499 apex {
1500 name: "myapex",
1501 java_libs: ["myjar"],
1502 key: "myapex.key",
1503 updatable: true,
1504 }
1505 apex_key {
1506 name: "myapex.key",
1507 public_key: "testkey.avbpubkey",
1508 private_key: "testkey.pem",
1509 }
1510 java_library {
1511 name: "myjar",
1512 srcs: ["foo/bar/MyClass.java"],
1513 sdk_version: "core_platform",
1514 apex_available: ["myapex"],
1515 }
1516 `,
1517 },
1518 {
1519 name: "Updatable apex with non-stable transitive dep",
1520 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against non-public Android API.",
1521 bp: `
1522 apex {
1523 name: "myapex",
1524 java_libs: ["myjar"],
1525 key: "myapex.key",
1526 updatable: true,
1527 }
1528 apex_key {
1529 name: "myapex.key",
1530 public_key: "testkey.avbpubkey",
1531 private_key: "testkey.pem",
1532 }
1533 java_library {
1534 name: "myjar",
1535 srcs: ["foo/bar/MyClass.java"],
1536 sdk_version: "current",
1537 apex_available: ["myapex"],
1538 static_libs: ["transitive-jar"],
1539 }
1540 java_library {
1541 name: "transitive-jar",
1542 srcs: ["foo/bar/MyClass.java"],
1543 sdk_version: "core_platform",
1544 apex_available: ["myapex"],
1545 }
1546 `,
1547 },
1548 }
1549
1550 for _, test := range testCases {
1551 t.Run(test.name, func(t *testing.T) {
1552 if test.expectedError == "" {
1553 testApex(t, test.bp)
1554 } else {
1555 testApexError(t, test.expectedError, test.bp)
1556 }
1557 })
1558 }
1559}
1560
Jiyong Park7c2ee712018-12-07 00:42:25 +09001561func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001562 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001563 apex {
1564 name: "myapex",
1565 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001566 native_shared_libs: ["mylib"],
1567 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001568 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001569 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001570 }
1571
1572 apex_key {
1573 name: "myapex.key",
1574 public_key: "testkey.avbpubkey",
1575 private_key: "testkey.pem",
1576 }
1577
1578 prebuilt_etc {
1579 name: "myetc",
1580 src: "myprebuilt",
1581 sub_dir: "foo/bar",
1582 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001583
1584 cc_library {
1585 name: "mylib",
1586 srcs: ["mylib.cpp"],
1587 relative_install_path: "foo/bar",
1588 system_shared_libs: [],
1589 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001590 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001591 }
1592
1593 cc_binary {
1594 name: "mybin",
1595 srcs: ["mylib.cpp"],
1596 relative_install_path: "foo/bar",
1597 system_shared_libs: [],
1598 static_executable: true,
1599 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001600 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001601 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001602 `)
1603
Sundong Ahnabb64432019-10-22 13:58:29 +09001604 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001605 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1606
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001607 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001608 ensureListContains(t, dirs, "etc")
1609 ensureListContains(t, dirs, "etc/foo")
1610 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001611 ensureListContains(t, dirs, "lib64")
1612 ensureListContains(t, dirs, "lib64/foo")
1613 ensureListContains(t, dirs, "lib64/foo/bar")
1614 ensureListContains(t, dirs, "lib")
1615 ensureListContains(t, dirs, "lib/foo")
1616 ensureListContains(t, dirs, "lib/foo/bar")
1617
Jiyong Parkbd13e442019-03-15 18:10:35 +09001618 ensureListContains(t, dirs, "bin")
1619 ensureListContains(t, dirs, "bin/foo")
1620 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001621}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001622
1623func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001624 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001625 apex {
1626 name: "myapex",
1627 key: "myapex.key",
1628 native_shared_libs: ["mylib"],
1629 use_vendor: true,
1630 }
1631
1632 apex_key {
1633 name: "myapex.key",
1634 public_key: "testkey.avbpubkey",
1635 private_key: "testkey.pem",
1636 }
1637
1638 cc_library {
1639 name: "mylib",
1640 srcs: ["mylib.cpp"],
1641 shared_libs: ["mylib2"],
1642 system_shared_libs: [],
1643 vendor_available: true,
1644 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001645 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001646 }
1647
1648 cc_library {
1649 name: "mylib2",
1650 srcs: ["mylib.cpp"],
1651 system_shared_libs: [],
1652 vendor_available: true,
1653 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001654 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001655 }
Jooyung Handc782442019-11-01 03:14:38 +09001656 `, func(fs map[string][]byte, config android.Config) {
Colin Cross95f7b342020-06-11 11:32:11 -07001657 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09001658 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001659
1660 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001661 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001662 for _, implicit := range i.Implicits {
1663 inputsList = append(inputsList, implicit.String())
1664 }
1665 }
1666 inputsString := strings.Join(inputsList, " ")
1667
1668 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001669 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1670 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001671
1672 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001673 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1674 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001675}
Jiyong Park16e91a02018-12-20 18:18:08 +09001676
Jooyung Handc782442019-11-01 03:14:38 +09001677func TestUseVendorRestriction(t *testing.T) {
1678 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1679 apex {
1680 name: "myapex",
1681 key: "myapex.key",
1682 use_vendor: true,
1683 }
1684 apex_key {
1685 name: "myapex.key",
1686 public_key: "testkey.avbpubkey",
1687 private_key: "testkey.pem",
1688 }
1689 `, func(fs map[string][]byte, config android.Config) {
Colin Cross95f7b342020-06-11 11:32:11 -07001690 setUseVendorAllowListForTest(config, []string{""})
Jooyung Handc782442019-11-01 03:14:38 +09001691 })
Colin Cross95f7b342020-06-11 11:32:11 -07001692 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09001693 testApex(t, `
1694 apex {
1695 name: "myapex",
1696 key: "myapex.key",
1697 use_vendor: true,
1698 }
1699 apex_key {
1700 name: "myapex.key",
1701 public_key: "testkey.avbpubkey",
1702 private_key: "testkey.pem",
1703 }
1704 `, func(fs map[string][]byte, config android.Config) {
Colin Cross95f7b342020-06-11 11:32:11 -07001705 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09001706 })
1707}
1708
Jooyung Han5c998b92019-06-27 11:30:33 +09001709func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1710 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1711 apex {
1712 name: "myapex",
1713 key: "myapex.key",
1714 native_shared_libs: ["mylib"],
1715 use_vendor: true,
1716 }
1717
1718 apex_key {
1719 name: "myapex.key",
1720 public_key: "testkey.avbpubkey",
1721 private_key: "testkey.pem",
1722 }
1723
1724 cc_library {
1725 name: "mylib",
1726 srcs: ["mylib.cpp"],
1727 system_shared_libs: [],
1728 stl: "none",
1729 }
1730 `)
1731}
1732
Jiyong Park16e91a02018-12-20 18:18:08 +09001733func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001734 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001735 apex {
1736 name: "myapex",
1737 key: "myapex.key",
1738 native_shared_libs: ["mylib"],
1739 }
1740
1741 apex_key {
1742 name: "myapex.key",
1743 public_key: "testkey.avbpubkey",
1744 private_key: "testkey.pem",
1745 }
1746
1747 cc_library {
1748 name: "mylib",
1749 srcs: ["mylib.cpp"],
1750 system_shared_libs: [],
1751 stl: "none",
1752 stubs: {
1753 versions: ["1", "2", "3"],
1754 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001755 apex_available: [
1756 "//apex_available:platform",
1757 "myapex",
1758 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001759 }
1760
1761 cc_binary {
1762 name: "not_in_apex",
1763 srcs: ["mylib.cpp"],
1764 static_libs: ["mylib"],
1765 static_executable: true,
1766 system_shared_libs: [],
1767 stl: "none",
1768 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001769 `)
1770
Colin Cross7113d202019-11-20 16:39:12 -08001771 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001772
1773 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001774 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001775}
Jiyong Park9335a262018-12-24 11:31:58 +09001776
1777func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001778 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001779 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001780 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001781 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001782 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001783 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001784 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001785 }
1786
1787 cc_library {
1788 name: "mylib",
1789 srcs: ["mylib.cpp"],
1790 system_shared_libs: [],
1791 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001792 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001793 }
1794
1795 apex_key {
1796 name: "myapex.key",
1797 public_key: "testkey.avbpubkey",
1798 private_key: "testkey.pem",
1799 }
1800
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001801 android_app_certificate {
1802 name: "myapex.certificate",
1803 certificate: "testkey",
1804 }
1805
1806 android_app_certificate {
1807 name: "myapex.certificate.override",
1808 certificate: "testkey.override",
1809 }
1810
Jiyong Park9335a262018-12-24 11:31:58 +09001811 `)
1812
1813 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001814 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001815
1816 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1817 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1818 "vendor/foo/devkeys/testkey.avbpubkey")
1819 }
1820 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1821 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1822 "vendor/foo/devkeys/testkey.pem")
1823 }
1824
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001825 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001826 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001827 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001828 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001829 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001830 }
1831}
Jiyong Park58e364a2019-01-19 19:24:06 +09001832
Jooyung Hanf121a652019-12-17 14:30:11 +09001833func TestCertificate(t *testing.T) {
1834 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1835 ctx, _ := testApex(t, `
1836 apex {
1837 name: "myapex",
1838 key: "myapex.key",
1839 }
1840 apex_key {
1841 name: "myapex.key",
1842 public_key: "testkey.avbpubkey",
1843 private_key: "testkey.pem",
1844 }`)
1845 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1846 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1847 if actual := rule.Args["certificates"]; actual != expected {
1848 t.Errorf("certificates should be %q, not %q", expected, actual)
1849 }
1850 })
1851 t.Run("override when unspecified", func(t *testing.T) {
1852 ctx, _ := testApex(t, `
1853 apex {
1854 name: "myapex_keytest",
1855 key: "myapex.key",
1856 file_contexts: ":myapex-file_contexts",
1857 }
1858 apex_key {
1859 name: "myapex.key",
1860 public_key: "testkey.avbpubkey",
1861 private_key: "testkey.pem",
1862 }
1863 android_app_certificate {
1864 name: "myapex.certificate.override",
1865 certificate: "testkey.override",
1866 }`)
1867 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1868 expected := "testkey.override.x509.pem testkey.override.pk8"
1869 if actual := rule.Args["certificates"]; actual != expected {
1870 t.Errorf("certificates should be %q, not %q", expected, actual)
1871 }
1872 })
1873 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1874 ctx, _ := testApex(t, `
1875 apex {
1876 name: "myapex",
1877 key: "myapex.key",
1878 certificate: ":myapex.certificate",
1879 }
1880 apex_key {
1881 name: "myapex.key",
1882 public_key: "testkey.avbpubkey",
1883 private_key: "testkey.pem",
1884 }
1885 android_app_certificate {
1886 name: "myapex.certificate",
1887 certificate: "testkey",
1888 }`)
1889 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1890 expected := "testkey.x509.pem testkey.pk8"
1891 if actual := rule.Args["certificates"]; actual != expected {
1892 t.Errorf("certificates should be %q, not %q", expected, actual)
1893 }
1894 })
1895 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1896 ctx, _ := testApex(t, `
1897 apex {
1898 name: "myapex_keytest",
1899 key: "myapex.key",
1900 file_contexts: ":myapex-file_contexts",
1901 certificate: ":myapex.certificate",
1902 }
1903 apex_key {
1904 name: "myapex.key",
1905 public_key: "testkey.avbpubkey",
1906 private_key: "testkey.pem",
1907 }
1908 android_app_certificate {
1909 name: "myapex.certificate.override",
1910 certificate: "testkey.override",
1911 }`)
1912 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1913 expected := "testkey.override.x509.pem testkey.override.pk8"
1914 if actual := rule.Args["certificates"]; actual != expected {
1915 t.Errorf("certificates should be %q, not %q", expected, actual)
1916 }
1917 })
1918 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1919 ctx, _ := testApex(t, `
1920 apex {
1921 name: "myapex",
1922 key: "myapex.key",
1923 certificate: "testkey",
1924 }
1925 apex_key {
1926 name: "myapex.key",
1927 public_key: "testkey.avbpubkey",
1928 private_key: "testkey.pem",
1929 }`)
1930 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1931 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1932 if actual := rule.Args["certificates"]; actual != expected {
1933 t.Errorf("certificates should be %q, not %q", expected, actual)
1934 }
1935 })
1936 t.Run("override when specified as <name>", func(t *testing.T) {
1937 ctx, _ := testApex(t, `
1938 apex {
1939 name: "myapex_keytest",
1940 key: "myapex.key",
1941 file_contexts: ":myapex-file_contexts",
1942 certificate: "testkey",
1943 }
1944 apex_key {
1945 name: "myapex.key",
1946 public_key: "testkey.avbpubkey",
1947 private_key: "testkey.pem",
1948 }
1949 android_app_certificate {
1950 name: "myapex.certificate.override",
1951 certificate: "testkey.override",
1952 }`)
1953 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1954 expected := "testkey.override.x509.pem testkey.override.pk8"
1955 if actual := rule.Args["certificates"]; actual != expected {
1956 t.Errorf("certificates should be %q, not %q", expected, actual)
1957 }
1958 })
1959}
1960
Jiyong Park58e364a2019-01-19 19:24:06 +09001961func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001962 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001963 apex {
1964 name: "myapex",
1965 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001966 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09001967 }
1968
1969 apex {
1970 name: "otherapex",
1971 key: "myapex.key",
Jooyung Han68e511e2020-03-02 17:44:33 +09001972 native_shared_libs: ["mylib", "mylib2"],
Jooyung Han61c41542020-03-07 03:45:53 +09001973 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09001974 }
1975
1976 apex_key {
1977 name: "myapex.key",
1978 public_key: "testkey.avbpubkey",
1979 private_key: "testkey.pem",
1980 }
1981
1982 cc_library {
1983 name: "mylib",
1984 srcs: ["mylib.cpp"],
1985 system_shared_libs: [],
1986 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001987 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001988 "myapex",
1989 "otherapex",
1990 ],
Jooyung Hanc3e92632020-03-21 23:20:55 +09001991 recovery_available: true,
Jiyong Park58e364a2019-01-19 19:24:06 +09001992 }
Jooyung Han68e511e2020-03-02 17:44:33 +09001993 cc_library {
1994 name: "mylib2",
1995 srcs: ["mylib.cpp"],
1996 system_shared_libs: [],
1997 stl: "none",
1998 apex_available: [
1999 "myapex",
2000 "otherapex",
2001 ],
2002 use_apex_name_macro: true,
2003 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002004 `)
2005
Jooyung Han68e511e2020-03-02 17:44:33 +09002006 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002007 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002008 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han74066602020-03-20 04:29:24 +09002009 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jooyung Han68e511e2020-03-02 17:44:33 +09002010
Jooyung Han61c41542020-03-07 03:45:53 +09002011 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Han68e511e2020-03-02 17:44:33 +09002012 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
2013 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han61c41542020-03-07 03:45:53 +09002014 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09002015 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han68e511e2020-03-02 17:44:33 +09002016
Jooyung Han61c41542020-03-07 03:45:53 +09002017 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Han68e511e2020-03-02 17:44:33 +09002018 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
2019 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han61c41542020-03-07 03:45:53 +09002020 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09002021 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002022
Jooyung Han68e511e2020-03-02 17:44:33 +09002023 // When cc_library sets use_apex_name_macro: true
2024 // apex variants define additional macro to distinguish which apex variant it is built for
2025
2026 // non-APEX variant does not have __ANDROID_APEX__ defined
2027 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2028 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2029
2030 // APEX variant has __ANDROID_APEX__ defined
2031 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002032 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002033 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2034 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002035
Jooyung Han68e511e2020-03-02 17:44:33 +09002036 // APEX variant has __ANDROID_APEX__ defined
2037 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002038 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002039 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2040 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Hanc3e92632020-03-21 23:20:55 +09002041
2042 // recovery variant does not set __ANDROID_SDK_VERSION__
2043 mylibCFlags = ctx.ModuleForTests("mylib", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2044 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2045 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002046}
Jiyong Park7e636d02019-01-28 16:16:54 +09002047
2048func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002049 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09002050 apex {
2051 name: "myapex",
2052 key: "myapex.key",
2053 native_shared_libs: ["mylib"],
2054 }
2055
2056 apex_key {
2057 name: "myapex.key",
2058 public_key: "testkey.avbpubkey",
2059 private_key: "testkey.pem",
2060 }
2061
2062 cc_library_headers {
2063 name: "mylib_headers",
2064 export_include_dirs: ["my_include"],
2065 system_shared_libs: [],
2066 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09002067 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002068 }
2069
2070 cc_library {
2071 name: "mylib",
2072 srcs: ["mylib.cpp"],
2073 system_shared_libs: [],
2074 stl: "none",
2075 header_libs: ["mylib_headers"],
2076 export_header_lib_headers: ["mylib_headers"],
2077 stubs: {
2078 versions: ["1", "2", "3"],
2079 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002080 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002081 }
2082
2083 cc_library {
2084 name: "otherlib",
2085 srcs: ["mylib.cpp"],
2086 system_shared_libs: [],
2087 stl: "none",
2088 shared_libs: ["mylib"],
2089 }
2090 `)
2091
Colin Cross7113d202019-11-20 16:39:12 -08002092 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09002093
2094 // Ensure that the include path of the header lib is exported to 'otherlib'
2095 ensureContains(t, cFlags, "-Imy_include")
2096}
Alex Light9670d332019-01-29 18:07:33 -08002097
Jiyong Park7cd10e32020-01-14 09:22:18 +09002098type fileInApex struct {
2099 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00002100 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09002101 isLink bool
2102}
2103
Jooyung Hana57af4a2020-01-23 05:36:59 +00002104func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09002105 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00002106 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09002107 copyCmds := apexRule.Args["copy_commands"]
2108 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09002109 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09002110 for _, cmd := range strings.Split(copyCmds, "&&") {
2111 cmd = strings.TrimSpace(cmd)
2112 if cmd == "" {
2113 continue
2114 }
2115 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00002116 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09002117 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09002118 switch terms[0] {
2119 case "mkdir":
2120 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09002121 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09002122 t.Fatal("copyCmds contains invalid cp command", cmd)
2123 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002124 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002125 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002126 isLink = false
2127 case "ln":
2128 if len(terms) != 3 && len(terms) != 4 {
2129 // ln LINK TARGET or ln -s LINK TARGET
2130 t.Fatal("copyCmds contains invalid ln command", cmd)
2131 }
2132 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002133 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002134 isLink = true
2135 default:
2136 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
2137 }
2138 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09002139 index := strings.Index(dst, imageApexDir)
2140 if index == -1 {
2141 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
2142 }
2143 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002144 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09002145 }
2146 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002147 return ret
2148}
2149
Jooyung Hana57af4a2020-01-23 05:36:59 +00002150func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
2151 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09002152 var failed bool
2153 var surplus []string
2154 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00002155 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Han8d8906c2020-02-27 13:31:56 +09002156 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09002157 for _, expected := range files {
2158 if matched, _ := path.Match(expected, file.path); matched {
2159 filesMatched[expected] = true
Jooyung Han8d8906c2020-02-27 13:31:56 +09002160 mactchFound = true
2161 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09002162 }
2163 }
Jooyung Han8d8906c2020-02-27 13:31:56 +09002164 if !mactchFound {
2165 surplus = append(surplus, file.path)
2166 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002167 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002168
Jooyung Han31c470b2019-10-18 16:26:59 +09002169 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002170 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09002171 t.Log("surplus files", surplus)
2172 failed = true
2173 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002174
2175 if len(files) > len(filesMatched) {
2176 var missing []string
2177 for _, expected := range files {
2178 if !filesMatched[expected] {
2179 missing = append(missing, expected)
2180 }
2181 }
2182 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09002183 t.Log("missing files", missing)
2184 failed = true
2185 }
2186 if failed {
2187 t.Fail()
2188 }
2189}
2190
Jooyung Han344d5432019-08-23 11:17:39 +09002191func TestVndkApexCurrent(t *testing.T) {
2192 ctx, _ := testApex(t, `
2193 apex_vndk {
2194 name: "myapex",
2195 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002196 }
2197
2198 apex_key {
2199 name: "myapex.key",
2200 public_key: "testkey.avbpubkey",
2201 private_key: "testkey.pem",
2202 }
2203
2204 cc_library {
2205 name: "libvndk",
2206 srcs: ["mylib.cpp"],
2207 vendor_available: true,
2208 vndk: {
2209 enabled: true,
2210 },
2211 system_shared_libs: [],
2212 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002213 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002214 }
2215
2216 cc_library {
2217 name: "libvndksp",
2218 srcs: ["mylib.cpp"],
2219 vendor_available: true,
2220 vndk: {
2221 enabled: true,
2222 support_system_process: true,
2223 },
2224 system_shared_libs: [],
2225 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002226 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002227 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002228 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002229
Jooyung Hana57af4a2020-01-23 05:36:59 +00002230 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002231 "lib/libvndk.so",
2232 "lib/libvndksp.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002233 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002234 "lib64/libvndk.so",
2235 "lib64/libvndksp.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002236 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002237 "etc/llndk.libraries.VER.txt",
2238 "etc/vndkcore.libraries.VER.txt",
2239 "etc/vndksp.libraries.VER.txt",
2240 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002241 })
Jooyung Han344d5432019-08-23 11:17:39 +09002242}
2243
2244func TestVndkApexWithPrebuilt(t *testing.T) {
2245 ctx, _ := testApex(t, `
2246 apex_vndk {
2247 name: "myapex",
2248 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002249 }
2250
2251 apex_key {
2252 name: "myapex.key",
2253 public_key: "testkey.avbpubkey",
2254 private_key: "testkey.pem",
2255 }
2256
2257 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002258 name: "libvndk",
2259 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002260 vendor_available: true,
2261 vndk: {
2262 enabled: true,
2263 },
2264 system_shared_libs: [],
2265 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002266 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002267 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002268
2269 cc_prebuilt_library_shared {
2270 name: "libvndk.arm",
2271 srcs: ["libvndk.arm.so"],
2272 vendor_available: true,
2273 vndk: {
2274 enabled: true,
2275 },
2276 enabled: false,
2277 arch: {
2278 arm: {
2279 enabled: true,
2280 },
2281 },
2282 system_shared_libs: [],
2283 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002284 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002285 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002286 `+vndkLibrariesTxtFiles("current"),
2287 withFiles(map[string][]byte{
2288 "libvndk.so": nil,
2289 "libvndk.arm.so": nil,
2290 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002291
Jooyung Hana57af4a2020-01-23 05:36:59 +00002292 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002293 "lib/libvndk.so",
2294 "lib/libvndk.arm.so",
2295 "lib64/libvndk.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002296 "lib/libc++.so",
2297 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002298 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002299 })
Jooyung Han344d5432019-08-23 11:17:39 +09002300}
2301
Jooyung Han39edb6c2019-11-06 16:53:07 +09002302func vndkLibrariesTxtFiles(vers ...string) (result string) {
2303 for _, v := range vers {
2304 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002305 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002306 result += `
2307 vndk_libraries_txt {
2308 name: "` + txt + `.libraries.txt",
2309 }
2310 `
2311 }
2312 } else {
2313 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2314 result += `
2315 prebuilt_etc {
2316 name: "` + txt + `.libraries.` + v + `.txt",
2317 src: "dummy.txt",
2318 }
2319 `
2320 }
2321 }
2322 }
2323 return
2324}
2325
Jooyung Han344d5432019-08-23 11:17:39 +09002326func TestVndkApexVersion(t *testing.T) {
2327 ctx, _ := testApex(t, `
2328 apex_vndk {
2329 name: "myapex_v27",
2330 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002331 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002332 vndk_version: "27",
2333 }
2334
2335 apex_key {
2336 name: "myapex.key",
2337 public_key: "testkey.avbpubkey",
2338 private_key: "testkey.pem",
2339 }
2340
Jooyung Han31c470b2019-10-18 16:26:59 +09002341 vndk_prebuilt_shared {
2342 name: "libvndk27",
2343 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002344 vendor_available: true,
2345 vndk: {
2346 enabled: true,
2347 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002348 target_arch: "arm64",
2349 arch: {
2350 arm: {
2351 srcs: ["libvndk27_arm.so"],
2352 },
2353 arm64: {
2354 srcs: ["libvndk27_arm64.so"],
2355 },
2356 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002357 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002358 }
2359
2360 vndk_prebuilt_shared {
2361 name: "libvndk27",
2362 version: "27",
2363 vendor_available: true,
2364 vndk: {
2365 enabled: true,
2366 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002367 target_arch: "x86_64",
2368 arch: {
2369 x86: {
2370 srcs: ["libvndk27_x86.so"],
2371 },
2372 x86_64: {
2373 srcs: ["libvndk27_x86_64.so"],
2374 },
2375 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002376 }
2377 `+vndkLibrariesTxtFiles("27"),
2378 withFiles(map[string][]byte{
2379 "libvndk27_arm.so": nil,
2380 "libvndk27_arm64.so": nil,
2381 "libvndk27_x86.so": nil,
2382 "libvndk27_x86_64.so": nil,
2383 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002384
Jooyung Hana57af4a2020-01-23 05:36:59 +00002385 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002386 "lib/libvndk27_arm.so",
2387 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002388 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002389 })
Jooyung Han344d5432019-08-23 11:17:39 +09002390}
2391
2392func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2393 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2394 apex_vndk {
2395 name: "myapex_v27",
2396 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002397 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002398 vndk_version: "27",
2399 }
2400 apex_vndk {
2401 name: "myapex_v27_other",
2402 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002403 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002404 vndk_version: "27",
2405 }
2406
2407 apex_key {
2408 name: "myapex.key",
2409 public_key: "testkey.avbpubkey",
2410 private_key: "testkey.pem",
2411 }
2412
2413 cc_library {
2414 name: "libvndk",
2415 srcs: ["mylib.cpp"],
2416 vendor_available: true,
2417 vndk: {
2418 enabled: true,
2419 },
2420 system_shared_libs: [],
2421 stl: "none",
2422 }
2423
2424 vndk_prebuilt_shared {
2425 name: "libvndk",
2426 version: "27",
2427 vendor_available: true,
2428 vndk: {
2429 enabled: true,
2430 },
2431 srcs: ["libvndk.so"],
2432 }
2433 `, withFiles(map[string][]byte{
2434 "libvndk.so": nil,
2435 }))
2436}
2437
Jooyung Han90eee022019-10-01 20:02:42 +09002438func TestVndkApexNameRule(t *testing.T) {
2439 ctx, _ := testApex(t, `
2440 apex_vndk {
2441 name: "myapex",
2442 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002443 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002444 }
2445 apex_vndk {
2446 name: "myapex_v28",
2447 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002448 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09002449 vndk_version: "28",
2450 }
2451 apex_key {
2452 name: "myapex.key",
2453 public_key: "testkey.avbpubkey",
2454 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002455 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09002456
2457 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00002458 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09002459 actual := proptools.String(bundle.properties.Apex_name)
2460 if !reflect.DeepEqual(actual, expected) {
2461 t.Errorf("Got '%v', expected '%v'", actual, expected)
2462 }
2463 }
2464
2465 assertApexName("com.android.vndk.vVER", "myapex")
2466 assertApexName("com.android.vndk.v28", "myapex_v28")
2467}
2468
Jooyung Han344d5432019-08-23 11:17:39 +09002469func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
2470 ctx, _ := testApex(t, `
2471 apex_vndk {
2472 name: "myapex",
2473 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002474 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002475 }
2476
2477 apex_key {
2478 name: "myapex.key",
2479 public_key: "testkey.avbpubkey",
2480 private_key: "testkey.pem",
2481 }
2482
2483 cc_library {
2484 name: "libvndk",
2485 srcs: ["mylib.cpp"],
2486 vendor_available: true,
2487 native_bridge_supported: true,
2488 host_supported: true,
2489 vndk: {
2490 enabled: true,
2491 },
2492 system_shared_libs: [],
2493 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002494 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002495 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002496 `+vndkLibrariesTxtFiles("current"),
2497 withTargets(map[android.OsType][]android.Target{
2498 android.Android: []android.Target{
2499 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2500 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2501 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2502 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2503 },
2504 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002505
Jooyung Hana57af4a2020-01-23 05:36:59 +00002506 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002507 "lib/libvndk.so",
2508 "lib64/libvndk.so",
Jooyung Han8d8906c2020-02-27 13:31:56 +09002509 "lib/libc++.so",
2510 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002511 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002512 })
Jooyung Han344d5432019-08-23 11:17:39 +09002513}
2514
2515func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2516 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2517 apex_vndk {
2518 name: "myapex",
2519 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002520 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002521 native_bridge_supported: true,
2522 }
2523
2524 apex_key {
2525 name: "myapex.key",
2526 public_key: "testkey.avbpubkey",
2527 private_key: "testkey.pem",
2528 }
2529
2530 cc_library {
2531 name: "libvndk",
2532 srcs: ["mylib.cpp"],
2533 vendor_available: true,
2534 native_bridge_supported: true,
2535 host_supported: true,
2536 vndk: {
2537 enabled: true,
2538 },
2539 system_shared_libs: [],
2540 stl: "none",
2541 }
2542 `)
2543}
2544
Jooyung Han31c470b2019-10-18 16:26:59 +09002545func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002546 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002547 apex_vndk {
2548 name: "myapex_v27",
2549 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002550 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002551 vndk_version: "27",
2552 }
2553
2554 apex_key {
2555 name: "myapex.key",
2556 public_key: "testkey.avbpubkey",
2557 private_key: "testkey.pem",
2558 }
2559
2560 vndk_prebuilt_shared {
2561 name: "libvndk27",
2562 version: "27",
2563 target_arch: "arm",
2564 vendor_available: true,
2565 vndk: {
2566 enabled: true,
2567 },
2568 arch: {
2569 arm: {
2570 srcs: ["libvndk27.so"],
2571 }
2572 },
2573 }
2574
2575 vndk_prebuilt_shared {
2576 name: "libvndk27",
2577 version: "27",
2578 target_arch: "arm",
2579 binder32bit: true,
2580 vendor_available: true,
2581 vndk: {
2582 enabled: true,
2583 },
2584 arch: {
2585 arm: {
2586 srcs: ["libvndk27binder32.so"],
2587 }
2588 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002589 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002590 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002591 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002592 withFiles(map[string][]byte{
2593 "libvndk27.so": nil,
2594 "libvndk27binder32.so": nil,
2595 }),
2596 withBinder32bit,
2597 withTargets(map[android.OsType][]android.Target{
2598 android.Android: []android.Target{
2599 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2600 },
2601 }),
2602 )
2603
Jooyung Hana57af4a2020-01-23 05:36:59 +00002604 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002605 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002606 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002607 })
2608}
2609
Jooyung Hane1633032019-08-01 17:41:43 +09002610func TestDependenciesInApexManifest(t *testing.T) {
2611 ctx, _ := testApex(t, `
2612 apex {
2613 name: "myapex_nodep",
2614 key: "myapex.key",
2615 native_shared_libs: ["lib_nodep"],
2616 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002617 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002618 }
2619
2620 apex {
2621 name: "myapex_dep",
2622 key: "myapex.key",
2623 native_shared_libs: ["lib_dep"],
2624 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002625 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002626 }
2627
2628 apex {
2629 name: "myapex_provider",
2630 key: "myapex.key",
2631 native_shared_libs: ["libfoo"],
2632 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002633 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002634 }
2635
2636 apex {
2637 name: "myapex_selfcontained",
2638 key: "myapex.key",
2639 native_shared_libs: ["lib_dep", "libfoo"],
2640 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002641 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002642 }
2643
2644 apex_key {
2645 name: "myapex.key",
2646 public_key: "testkey.avbpubkey",
2647 private_key: "testkey.pem",
2648 }
2649
2650 cc_library {
2651 name: "lib_nodep",
2652 srcs: ["mylib.cpp"],
2653 system_shared_libs: [],
2654 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002655 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002656 }
2657
2658 cc_library {
2659 name: "lib_dep",
2660 srcs: ["mylib.cpp"],
2661 shared_libs: ["libfoo"],
2662 system_shared_libs: [],
2663 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002664 apex_available: [
2665 "myapex_dep",
2666 "myapex_provider",
2667 "myapex_selfcontained",
2668 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002669 }
2670
2671 cc_library {
2672 name: "libfoo",
2673 srcs: ["mytest.cpp"],
2674 stubs: {
2675 versions: ["1"],
2676 },
2677 system_shared_libs: [],
2678 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002679 apex_available: [
2680 "myapex_provider",
2681 "myapex_selfcontained",
2682 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002683 }
2684 `)
2685
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002686 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002687 var provideNativeLibs, requireNativeLibs []string
2688
Sundong Ahnabb64432019-10-22 13:58:29 +09002689 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002690 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2691 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002692 ensureListEmpty(t, provideNativeLibs)
2693 ensureListEmpty(t, requireNativeLibs)
2694
Sundong Ahnabb64432019-10-22 13:58:29 +09002695 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002696 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2697 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002698 ensureListEmpty(t, provideNativeLibs)
2699 ensureListContains(t, requireNativeLibs, "libfoo.so")
2700
Sundong Ahnabb64432019-10-22 13:58:29 +09002701 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002702 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2703 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002704 ensureListContains(t, provideNativeLibs, "libfoo.so")
2705 ensureListEmpty(t, requireNativeLibs)
2706
Sundong Ahnabb64432019-10-22 13:58:29 +09002707 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002708 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2709 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002710 ensureListContains(t, provideNativeLibs, "libfoo.so")
2711 ensureListEmpty(t, requireNativeLibs)
2712}
2713
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002714func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002715 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002716 apex {
2717 name: "myapex",
2718 key: "myapex.key",
2719 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002720 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002721 }
2722
2723 apex_key {
2724 name: "myapex.key",
2725 public_key: "testkey.avbpubkey",
2726 private_key: "testkey.pem",
2727 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002728
2729 cc_library {
2730 name: "mylib",
2731 srcs: ["mylib.cpp"],
2732 system_shared_libs: [],
2733 stl: "none",
2734 apex_available: [
2735 "//apex_available:platform",
2736 "myapex",
2737 ],
2738 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002739 `)
2740
Sundong Ahnabb64432019-10-22 13:58:29 +09002741 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002742 apexManifestRule := module.Rule("apexManifestRule")
2743 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2744 apexRule := module.Rule("apexRule")
2745 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002746
2747 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2748 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2749 name := apexBundle.BaseModuleName()
2750 prefix := "TARGET_"
2751 var builder strings.Builder
2752 data.Custom(&builder, name, prefix, "", data)
2753 androidMk := builder.String()
2754 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2755 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002756}
2757
Alex Light0851b882019-02-07 13:20:53 -08002758func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002759 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002760 apex {
2761 name: "myapex",
2762 key: "myapex.key",
2763 native_shared_libs: ["mylib_common"],
2764 }
2765
2766 apex_key {
2767 name: "myapex.key",
2768 public_key: "testkey.avbpubkey",
2769 private_key: "testkey.pem",
2770 }
2771
2772 cc_library {
2773 name: "mylib_common",
2774 srcs: ["mylib.cpp"],
2775 system_shared_libs: [],
2776 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002777 apex_available: [
2778 "//apex_available:platform",
2779 "myapex",
2780 ],
Alex Light0851b882019-02-07 13:20:53 -08002781 }
2782 `)
2783
Sundong Ahnabb64432019-10-22 13:58:29 +09002784 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002785 apexRule := module.Rule("apexRule")
2786 copyCmds := apexRule.Args["copy_commands"]
2787
2788 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2789 t.Log("Apex was a test apex!")
2790 t.Fail()
2791 }
2792 // Ensure that main rule creates an output
2793 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2794
2795 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002796 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002797
2798 // Ensure that both direct and indirect deps are copied into apex
2799 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2800
Colin Cross7113d202019-11-20 16:39:12 -08002801 // Ensure that the platform variant ends with _shared
2802 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002803
2804 if !android.InAnyApex("mylib_common") {
2805 t.Log("Found mylib_common not in any apex!")
2806 t.Fail()
2807 }
2808}
2809
2810func TestTestApex(t *testing.T) {
2811 if android.InAnyApex("mylib_common_test") {
2812 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!")
2813 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002814 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002815 apex_test {
2816 name: "myapex",
2817 key: "myapex.key",
2818 native_shared_libs: ["mylib_common_test"],
2819 }
2820
2821 apex_key {
2822 name: "myapex.key",
2823 public_key: "testkey.avbpubkey",
2824 private_key: "testkey.pem",
2825 }
2826
2827 cc_library {
2828 name: "mylib_common_test",
2829 srcs: ["mylib.cpp"],
2830 system_shared_libs: [],
2831 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002832 // TODO: remove //apex_available:platform
2833 apex_available: [
2834 "//apex_available:platform",
2835 "myapex",
2836 ],
Alex Light0851b882019-02-07 13:20:53 -08002837 }
2838 `)
2839
Sundong Ahnabb64432019-10-22 13:58:29 +09002840 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002841 apexRule := module.Rule("apexRule")
2842 copyCmds := apexRule.Args["copy_commands"]
2843
2844 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2845 t.Log("Apex was not a test apex!")
2846 t.Fail()
2847 }
2848 // Ensure that main rule creates an output
2849 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2850
2851 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002852 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002853
2854 // Ensure that both direct and indirect deps are copied into apex
2855 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2856
Colin Cross7113d202019-11-20 16:39:12 -08002857 // Ensure that the platform variant ends with _shared
2858 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002859}
2860
Alex Light9670d332019-01-29 18:07:33 -08002861func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002862 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002863 apex {
2864 name: "myapex",
2865 key: "myapex.key",
2866 multilib: {
2867 first: {
2868 native_shared_libs: ["mylib_common"],
2869 }
2870 },
2871 target: {
2872 android: {
2873 multilib: {
2874 first: {
2875 native_shared_libs: ["mylib"],
2876 }
2877 }
2878 },
2879 host: {
2880 multilib: {
2881 first: {
2882 native_shared_libs: ["mylib2"],
2883 }
2884 }
2885 }
2886 }
2887 }
2888
2889 apex_key {
2890 name: "myapex.key",
2891 public_key: "testkey.avbpubkey",
2892 private_key: "testkey.pem",
2893 }
2894
2895 cc_library {
2896 name: "mylib",
2897 srcs: ["mylib.cpp"],
2898 system_shared_libs: [],
2899 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002900 // TODO: remove //apex_available:platform
2901 apex_available: [
2902 "//apex_available:platform",
2903 "myapex",
2904 ],
Alex Light9670d332019-01-29 18:07:33 -08002905 }
2906
2907 cc_library {
2908 name: "mylib_common",
2909 srcs: ["mylib.cpp"],
2910 system_shared_libs: [],
2911 stl: "none",
2912 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002913 // TODO: remove //apex_available:platform
2914 apex_available: [
2915 "//apex_available:platform",
2916 "myapex",
2917 ],
Alex Light9670d332019-01-29 18:07:33 -08002918 }
2919
2920 cc_library {
2921 name: "mylib2",
2922 srcs: ["mylib.cpp"],
2923 system_shared_libs: [],
2924 stl: "none",
2925 compile_multilib: "first",
2926 }
2927 `)
2928
Sundong Ahnabb64432019-10-22 13:58:29 +09002929 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002930 copyCmds := apexRule.Args["copy_commands"]
2931
2932 // Ensure that main rule creates an output
2933 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2934
2935 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002936 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2937 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2938 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002939
2940 // Ensure that both direct and indirect deps are copied into apex
2941 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2942 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2943 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2944
Colin Cross7113d202019-11-20 16:39:12 -08002945 // Ensure that the platform variant ends with _shared
2946 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2947 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2948 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002949}
Jiyong Park04480cf2019-02-06 00:16:29 +09002950
2951func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002952 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002953 apex {
2954 name: "myapex",
2955 key: "myapex.key",
2956 binaries: ["myscript"],
2957 }
2958
2959 apex_key {
2960 name: "myapex.key",
2961 public_key: "testkey.avbpubkey",
2962 private_key: "testkey.pem",
2963 }
2964
2965 sh_binary {
2966 name: "myscript",
2967 src: "mylib.cpp",
2968 filename: "myscript.sh",
2969 sub_dir: "script",
2970 }
2971 `)
2972
Sundong Ahnabb64432019-10-22 13:58:29 +09002973 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002974 copyCmds := apexRule.Args["copy_commands"]
2975
2976 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2977}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002978
Jooyung Han91df2082019-11-20 01:49:42 +09002979func TestApexInVariousPartition(t *testing.T) {
2980 testcases := []struct {
2981 propName, parition, flattenedPartition string
2982 }{
2983 {"", "system", "system_ext"},
2984 {"product_specific: true", "product", "product"},
2985 {"soc_specific: true", "vendor", "vendor"},
2986 {"proprietary: true", "vendor", "vendor"},
2987 {"vendor: true", "vendor", "vendor"},
2988 {"system_ext_specific: true", "system_ext", "system_ext"},
2989 }
2990 for _, tc := range testcases {
2991 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2992 ctx, _ := testApex(t, `
2993 apex {
2994 name: "myapex",
2995 key: "myapex.key",
2996 `+tc.propName+`
2997 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002998
Jooyung Han91df2082019-11-20 01:49:42 +09002999 apex_key {
3000 name: "myapex.key",
3001 public_key: "testkey.avbpubkey",
3002 private_key: "testkey.pem",
3003 }
3004 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003005
Jooyung Han91df2082019-11-20 01:49:42 +09003006 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3007 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
3008 actual := apex.installDir.String()
3009 if actual != expected {
3010 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
3011 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003012
Jooyung Han91df2082019-11-20 01:49:42 +09003013 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
3014 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
3015 actual = flattened.installDir.String()
3016 if actual != expected {
3017 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
3018 }
3019 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003020 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003021}
Jiyong Park67882562019-03-21 01:11:21 +09003022
Jooyung Han54aca7b2019-11-20 02:26:02 +09003023func TestFileContexts(t *testing.T) {
3024 ctx, _ := testApex(t, `
3025 apex {
3026 name: "myapex",
3027 key: "myapex.key",
3028 }
3029
3030 apex_key {
3031 name: "myapex.key",
3032 public_key: "testkey.avbpubkey",
3033 private_key: "testkey.pem",
3034 }
3035 `)
3036 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3037 apexRule := module.Rule("apexRule")
3038 actual := apexRule.Args["file_contexts"]
3039 expected := "system/sepolicy/apex/myapex-file_contexts"
3040 if actual != expected {
3041 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
3042 }
3043
3044 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
3045 apex {
3046 name: "myapex",
3047 key: "myapex.key",
3048 file_contexts: "my_own_file_contexts",
3049 }
3050
3051 apex_key {
3052 name: "myapex.key",
3053 public_key: "testkey.avbpubkey",
3054 private_key: "testkey.pem",
3055 }
3056 `, withFiles(map[string][]byte{
3057 "my_own_file_contexts": nil,
3058 }))
3059
3060 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
3061 apex {
3062 name: "myapex",
3063 key: "myapex.key",
3064 product_specific: true,
3065 file_contexts: "product_specific_file_contexts",
3066 }
3067
3068 apex_key {
3069 name: "myapex.key",
3070 public_key: "testkey.avbpubkey",
3071 private_key: "testkey.pem",
3072 }
3073 `)
3074
3075 ctx, _ = testApex(t, `
3076 apex {
3077 name: "myapex",
3078 key: "myapex.key",
3079 product_specific: true,
3080 file_contexts: "product_specific_file_contexts",
3081 }
3082
3083 apex_key {
3084 name: "myapex.key",
3085 public_key: "testkey.avbpubkey",
3086 private_key: "testkey.pem",
3087 }
3088 `, withFiles(map[string][]byte{
3089 "product_specific_file_contexts": nil,
3090 }))
3091 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
3092 apexRule = module.Rule("apexRule")
3093 actual = apexRule.Args["file_contexts"]
3094 expected = "product_specific_file_contexts"
3095 if actual != expected {
3096 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
3097 }
3098
3099 ctx, _ = testApex(t, `
3100 apex {
3101 name: "myapex",
3102 key: "myapex.key",
3103 product_specific: true,
3104 file_contexts: ":my-file-contexts",
3105 }
3106
3107 apex_key {
3108 name: "myapex.key",
3109 public_key: "testkey.avbpubkey",
3110 private_key: "testkey.pem",
3111 }
3112
3113 filegroup {
3114 name: "my-file-contexts",
3115 srcs: ["product_specific_file_contexts"],
3116 }
3117 `, withFiles(map[string][]byte{
3118 "product_specific_file_contexts": nil,
3119 }))
3120 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
3121 apexRule = module.Rule("apexRule")
3122 actual = apexRule.Args["file_contexts"]
3123 expected = "product_specific_file_contexts"
3124 if actual != expected {
3125 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
3126 }
3127}
3128
Jiyong Park67882562019-03-21 01:11:21 +09003129func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003130 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09003131 apex_key {
3132 name: "myapex.key",
3133 public_key: ":my.avbpubkey",
3134 private_key: ":my.pem",
3135 product_specific: true,
3136 }
3137
3138 filegroup {
3139 name: "my.avbpubkey",
3140 srcs: ["testkey2.avbpubkey"],
3141 }
3142
3143 filegroup {
3144 name: "my.pem",
3145 srcs: ["testkey2.pem"],
3146 }
3147 `)
3148
3149 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
3150 expected_pubkey := "testkey2.avbpubkey"
3151 actual_pubkey := apex_key.public_key_file.String()
3152 if actual_pubkey != expected_pubkey {
3153 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
3154 }
3155 expected_privkey := "testkey2.pem"
3156 actual_privkey := apex_key.private_key_file.String()
3157 if actual_privkey != expected_privkey {
3158 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
3159 }
3160}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003161
3162func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003163 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003164 prebuilt_apex {
3165 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09003166 arch: {
3167 arm64: {
3168 src: "myapex-arm64.apex",
3169 },
3170 arm: {
3171 src: "myapex-arm.apex",
3172 },
3173 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003174 }
3175 `)
3176
3177 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3178
Jiyong Parkc95714e2019-03-29 14:23:10 +09003179 expectedInput := "myapex-arm64.apex"
3180 if prebuilt.inputApex.String() != expectedInput {
3181 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
3182 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003183}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003184
3185func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003186 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003187 prebuilt_apex {
3188 name: "myapex",
3189 src: "myapex-arm.apex",
3190 filename: "notmyapex.apex",
3191 }
3192 `)
3193
3194 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3195
3196 expected := "notmyapex.apex"
3197 if p.installFilename != expected {
3198 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
3199 }
3200}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003201
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003202func TestPrebuiltOverrides(t *testing.T) {
3203 ctx, config := testApex(t, `
3204 prebuilt_apex {
3205 name: "myapex.prebuilt",
3206 src: "myapex-arm.apex",
3207 overrides: [
3208 "myapex",
3209 ],
3210 }
3211 `)
3212
3213 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3214
3215 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003216 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003217 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003218 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003219 }
3220}
3221
Roland Levillain630846d2019-06-26 12:48:34 +01003222func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003223 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003224 apex_test {
3225 name: "myapex",
3226 key: "myapex.key",
3227 tests: [
3228 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003229 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003230 ],
3231 }
3232
3233 apex_key {
3234 name: "myapex.key",
3235 public_key: "testkey.avbpubkey",
3236 private_key: "testkey.pem",
3237 }
3238
3239 cc_test {
3240 name: "mytest",
3241 gtest: false,
3242 srcs: ["mytest.cpp"],
3243 relative_install_path: "test",
3244 system_shared_libs: [],
3245 static_executable: true,
3246 stl: "none",
3247 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003248
3249 cc_test {
3250 name: "mytests",
3251 gtest: false,
3252 srcs: [
3253 "mytest1.cpp",
3254 "mytest2.cpp",
3255 "mytest3.cpp",
3256 ],
3257 test_per_src: true,
3258 relative_install_path: "test",
3259 system_shared_libs: [],
3260 static_executable: true,
3261 stl: "none",
3262 }
Roland Levillain630846d2019-06-26 12:48:34 +01003263 `)
3264
Sundong Ahnabb64432019-10-22 13:58:29 +09003265 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003266 copyCmds := apexRule.Args["copy_commands"]
3267
3268 // Ensure that test dep is copied into apex.
3269 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003270
3271 // Ensure that test deps built with `test_per_src` are copied into apex.
3272 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3273 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3274 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003275
3276 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09003277 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01003278 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3279 name := apexBundle.BaseModuleName()
3280 prefix := "TARGET_"
3281 var builder strings.Builder
3282 data.Custom(&builder, name, prefix, "", data)
3283 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003284 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3285 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3286 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3287 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003288 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003289 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003290 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003291}
3292
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003293func TestInstallExtraFlattenedApexes(t *testing.T) {
3294 ctx, config := testApex(t, `
3295 apex {
3296 name: "myapex",
3297 key: "myapex.key",
3298 }
3299 apex_key {
3300 name: "myapex.key",
3301 public_key: "testkey.avbpubkey",
3302 private_key: "testkey.pem",
3303 }
3304 `, func(fs map[string][]byte, config android.Config) {
3305 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3306 })
3307 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003308 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003309 mk := android.AndroidMkDataForTest(t, config, "", ab)
3310 var builder strings.Builder
3311 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3312 androidMk := builder.String()
3313 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3314}
3315
Jooyung Han5c998b92019-06-27 11:30:33 +09003316func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003317 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003318 apex {
3319 name: "myapex",
3320 key: "myapex.key",
3321 native_shared_libs: ["mylib"],
3322 uses: ["commonapex"],
3323 }
3324
3325 apex {
3326 name: "commonapex",
3327 key: "myapex.key",
3328 native_shared_libs: ["libcommon"],
3329 provide_cpp_shared_libs: true,
3330 }
3331
3332 apex_key {
3333 name: "myapex.key",
3334 public_key: "testkey.avbpubkey",
3335 private_key: "testkey.pem",
3336 }
3337
3338 cc_library {
3339 name: "mylib",
3340 srcs: ["mylib.cpp"],
3341 shared_libs: ["libcommon"],
3342 system_shared_libs: [],
3343 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003344 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003345 }
3346
3347 cc_library {
3348 name: "libcommon",
3349 srcs: ["mylib_common.cpp"],
3350 system_shared_libs: [],
3351 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003352 // TODO: remove //apex_available:platform
3353 apex_available: [
3354 "//apex_available:platform",
3355 "commonapex",
3356 "myapex",
3357 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003358 }
3359 `)
3360
Sundong Ahnabb64432019-10-22 13:58:29 +09003361 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003362 apexRule1 := module1.Rule("apexRule")
3363 copyCmds1 := apexRule1.Args["copy_commands"]
3364
Sundong Ahnabb64432019-10-22 13:58:29 +09003365 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003366 apexRule2 := module2.Rule("apexRule")
3367 copyCmds2 := apexRule2.Args["copy_commands"]
3368
Colin Cross7113d202019-11-20 16:39:12 -08003369 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3370 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09003371 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
3372 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
3373 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
3374}
3375
3376func TestApexUsesFailsIfNotProvided(t *testing.T) {
3377 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
3378 apex {
3379 name: "myapex",
3380 key: "myapex.key",
3381 uses: ["commonapex"],
3382 }
3383
3384 apex {
3385 name: "commonapex",
3386 key: "myapex.key",
3387 }
3388
3389 apex_key {
3390 name: "myapex.key",
3391 public_key: "testkey.avbpubkey",
3392 private_key: "testkey.pem",
3393 }
3394 `)
3395 testApexError(t, `uses: "commonapex" is not a provider`, `
3396 apex {
3397 name: "myapex",
3398 key: "myapex.key",
3399 uses: ["commonapex"],
3400 }
3401
3402 cc_library {
3403 name: "commonapex",
3404 system_shared_libs: [],
3405 stl: "none",
3406 }
3407
3408 apex_key {
3409 name: "myapex.key",
3410 public_key: "testkey.avbpubkey",
3411 private_key: "testkey.pem",
3412 }
3413 `)
3414}
3415
3416func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
3417 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
3418 apex {
3419 name: "myapex",
3420 key: "myapex.key",
3421 use_vendor: true,
3422 uses: ["commonapex"],
3423 }
3424
3425 apex {
3426 name: "commonapex",
3427 key: "myapex.key",
3428 provide_cpp_shared_libs: true,
3429 }
3430
3431 apex_key {
3432 name: "myapex.key",
3433 public_key: "testkey.avbpubkey",
3434 private_key: "testkey.pem",
3435 }
Jooyung Handc782442019-11-01 03:14:38 +09003436 `, func(fs map[string][]byte, config android.Config) {
Colin Cross95f7b342020-06-11 11:32:11 -07003437 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09003438 })
Jooyung Han5c998b92019-06-27 11:30:33 +09003439}
3440
Jooyung Hand48f3c32019-08-23 11:18:57 +09003441func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
3442 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
3443 apex {
3444 name: "myapex",
3445 key: "myapex.key",
3446 native_shared_libs: ["libfoo"],
3447 }
3448
3449 apex_key {
3450 name: "myapex.key",
3451 public_key: "testkey.avbpubkey",
3452 private_key: "testkey.pem",
3453 }
3454
3455 cc_library {
3456 name: "libfoo",
3457 stl: "none",
3458 system_shared_libs: [],
3459 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003460 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003461 }
3462 `)
3463 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
3464 apex {
3465 name: "myapex",
3466 key: "myapex.key",
3467 java_libs: ["myjar"],
3468 }
3469
3470 apex_key {
3471 name: "myapex.key",
3472 public_key: "testkey.avbpubkey",
3473 private_key: "testkey.pem",
3474 }
3475
3476 java_library {
3477 name: "myjar",
3478 srcs: ["foo/bar/MyClass.java"],
3479 sdk_version: "none",
3480 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09003481 enabled: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003482 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09003483 }
3484 `)
3485}
3486
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003487func TestApexWithApps(t *testing.T) {
3488 ctx, _ := testApex(t, `
3489 apex {
3490 name: "myapex",
3491 key: "myapex.key",
3492 apps: [
3493 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003494 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003495 ],
3496 }
3497
3498 apex_key {
3499 name: "myapex.key",
3500 public_key: "testkey.avbpubkey",
3501 private_key: "testkey.pem",
3502 }
3503
3504 android_app {
3505 name: "AppFoo",
3506 srcs: ["foo/bar/MyClass.java"],
Colin Cross1c93c292020-02-15 10:38:00 -08003507 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003508 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003509 jni_libs: ["libjni"],
Colin Cross1c93c292020-02-15 10:38:00 -08003510 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003511 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003512 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003513
3514 android_app {
3515 name: "AppFooPriv",
3516 srcs: ["foo/bar/MyClass.java"],
Colin Cross1c93c292020-02-15 10:38:00 -08003517 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09003518 system_modules: "none",
3519 privileged: true,
Colin Cross1c93c292020-02-15 10:38:00 -08003520 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003521 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003522 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003523
3524 cc_library_shared {
3525 name: "libjni",
3526 srcs: ["mylib.cpp"],
Jooyung Han65041792020-02-25 16:59:29 +09003527 shared_libs: ["libfoo"],
Jiyong Park8be103b2019-11-08 15:53:48 +09003528 stl: "none",
3529 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003530 apex_available: [ "myapex" ],
Jooyung Han65041792020-02-25 16:59:29 +09003531 sdk_version: "current",
3532 }
3533
3534 cc_library_shared {
3535 name: "libfoo",
3536 stl: "none",
3537 system_shared_libs: [],
3538 apex_available: [ "myapex" ],
3539 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09003540 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003541 `)
3542
Sundong Ahnabb64432019-10-22 13:58:29 +09003543 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003544 apexRule := module.Rule("apexRule")
3545 copyCmds := apexRule.Args["copy_commands"]
3546
3547 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003548 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003549
Jooyung Han65041792020-02-25 16:59:29 +09003550 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
3551 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09003552 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Han65041792020-02-25 16:59:29 +09003553 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003554 }
Jooyung Han65041792020-02-25 16:59:29 +09003555 // JNI libraries including transitive deps are
3556 for _, jni := range []string{"libjni", "libfoo"} {
Colin Cross01fd7cc2020-02-19 16:54:04 -08003557 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_myapex").Module().(*cc.Module).OutputFile()
Jooyung Han65041792020-02-25 16:59:29 +09003558 // ... embedded inside APK (jnilibs.zip)
3559 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
3560 // ... and not directly inside the APEX
3561 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
3562 }
Dario Frenicde2a032019-10-27 00:29:22 +01003563}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003564
Dario Frenicde2a032019-10-27 00:29:22 +01003565func TestApexWithAppImports(t *testing.T) {
3566 ctx, _ := testApex(t, `
3567 apex {
3568 name: "myapex",
3569 key: "myapex.key",
3570 apps: [
3571 "AppFooPrebuilt",
3572 "AppFooPrivPrebuilt",
3573 ],
3574 }
3575
3576 apex_key {
3577 name: "myapex.key",
3578 public_key: "testkey.avbpubkey",
3579 private_key: "testkey.pem",
3580 }
3581
3582 android_app_import {
3583 name: "AppFooPrebuilt",
3584 apk: "PrebuiltAppFoo.apk",
3585 presigned: true,
3586 dex_preopt: {
3587 enabled: false,
3588 },
3589 }
3590
3591 android_app_import {
3592 name: "AppFooPrivPrebuilt",
3593 apk: "PrebuiltAppFooPriv.apk",
3594 privileged: true,
3595 presigned: true,
3596 dex_preopt: {
3597 enabled: false,
3598 },
Jooyung Han65cd0f02020-03-23 20:21:11 +09003599 filename: "AwesomePrebuiltAppFooPriv.apk",
Dario Frenicde2a032019-10-27 00:29:22 +01003600 }
3601 `)
3602
Sundong Ahnabb64432019-10-22 13:58:29 +09003603 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003604 apexRule := module.Rule("apexRule")
3605 copyCmds := apexRule.Args["copy_commands"]
3606
3607 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han65cd0f02020-03-23 20:21:11 +09003608 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
3609}
3610
3611func TestApexWithAppImportsPrefer(t *testing.T) {
3612 ctx, _ := testApex(t, `
3613 apex {
3614 name: "myapex",
3615 key: "myapex.key",
3616 apps: [
3617 "AppFoo",
3618 ],
3619 }
3620
3621 apex_key {
3622 name: "myapex.key",
3623 public_key: "testkey.avbpubkey",
3624 private_key: "testkey.pem",
3625 }
3626
3627 android_app {
3628 name: "AppFoo",
3629 srcs: ["foo/bar/MyClass.java"],
3630 sdk_version: "none",
3631 system_modules: "none",
3632 apex_available: [ "myapex" ],
3633 }
3634
3635 android_app_import {
3636 name: "AppFoo",
3637 apk: "AppFooPrebuilt.apk",
3638 filename: "AppFooPrebuilt.apk",
3639 presigned: true,
3640 prefer: true,
3641 }
3642 `, withFiles(map[string][]byte{
3643 "AppFooPrebuilt.apk": nil,
3644 }))
3645
3646 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3647 "app/AppFoo/AppFooPrebuilt.apk",
3648 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003649}
3650
Dario Freni6f3937c2019-12-20 22:58:03 +00003651func TestApexWithTestHelperApp(t *testing.T) {
3652 ctx, _ := testApex(t, `
3653 apex {
3654 name: "myapex",
3655 key: "myapex.key",
3656 apps: [
3657 "TesterHelpAppFoo",
3658 ],
3659 }
3660
3661 apex_key {
3662 name: "myapex.key",
3663 public_key: "testkey.avbpubkey",
3664 private_key: "testkey.pem",
3665 }
3666
3667 android_test_helper_app {
3668 name: "TesterHelpAppFoo",
3669 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003670 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003671 }
3672
3673 `)
3674
3675 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3676 apexRule := module.Rule("apexRule")
3677 copyCmds := apexRule.Args["copy_commands"]
3678
3679 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3680}
3681
Jooyung Han18020ea2019-11-13 10:50:48 +09003682func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3683 // libfoo's apex_available comes from cc_defaults
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003684 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09003685 apex {
3686 name: "myapex",
3687 key: "myapex.key",
3688 native_shared_libs: ["libfoo"],
3689 }
3690
3691 apex_key {
3692 name: "myapex.key",
3693 public_key: "testkey.avbpubkey",
3694 private_key: "testkey.pem",
3695 }
3696
3697 apex {
3698 name: "otherapex",
3699 key: "myapex.key",
3700 native_shared_libs: ["libfoo"],
3701 }
3702
3703 cc_defaults {
3704 name: "libfoo-defaults",
3705 apex_available: ["otherapex"],
3706 }
3707
3708 cc_library {
3709 name: "libfoo",
3710 defaults: ["libfoo-defaults"],
3711 stl: "none",
3712 system_shared_libs: [],
3713 }`)
3714}
3715
Paul Duffinde7464c2020-03-30 17:54:29 +01003716func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003717 // libfoo is not available to myapex, but only to otherapex
3718 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3719 apex {
3720 name: "myapex",
3721 key: "myapex.key",
3722 native_shared_libs: ["libfoo"],
3723 }
3724
3725 apex_key {
3726 name: "myapex.key",
3727 public_key: "testkey.avbpubkey",
3728 private_key: "testkey.pem",
3729 }
3730
3731 apex {
3732 name: "otherapex",
3733 key: "otherapex.key",
3734 native_shared_libs: ["libfoo"],
3735 }
3736
3737 apex_key {
3738 name: "otherapex.key",
3739 public_key: "testkey.avbpubkey",
3740 private_key: "testkey.pem",
3741 }
3742
3743 cc_library {
3744 name: "libfoo",
3745 stl: "none",
3746 system_shared_libs: [],
3747 apex_available: ["otherapex"],
3748 }`)
Paul Duffinde7464c2020-03-30 17:54:29 +01003749}
Jiyong Park127b40b2019-09-30 16:04:35 +09003750
Paul Duffinde7464c2020-03-30 17:54:29 +01003751func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003752 // libbbaz is an indirect dep
Paul Duffin868ecfd2020-03-30 17:58:21 +01003753 testApexError(t, `requires "libbaz" that is not available for the APEX. Dependency path:
Paul Duffinf0207962020-03-31 11:31:36 +01003754.*via tag apex\.dependencyTag.*"sharedLib".*
Paul Duffin868ecfd2020-03-30 17:58:21 +01003755.*-> libfoo.*link:shared.*
Paul Duffinb20ad0a2020-03-31 15:23:40 +01003756.*via tag cc\.DependencyTag.*"shared".*
Paul Duffin868ecfd2020-03-30 17:58:21 +01003757.*-> libbar.*link:shared.*
Paul Duffinb20ad0a2020-03-31 15:23:40 +01003758.*via tag cc\.DependencyTag.*"shared".*
3759.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003760 apex {
3761 name: "myapex",
3762 key: "myapex.key",
3763 native_shared_libs: ["libfoo"],
3764 }
3765
3766 apex_key {
3767 name: "myapex.key",
3768 public_key: "testkey.avbpubkey",
3769 private_key: "testkey.pem",
3770 }
3771
Jiyong Park127b40b2019-09-30 16:04:35 +09003772 cc_library {
3773 name: "libfoo",
3774 stl: "none",
3775 shared_libs: ["libbar"],
3776 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003777 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003778 }
3779
3780 cc_library {
3781 name: "libbar",
3782 stl: "none",
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003783 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003784 system_shared_libs: [],
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09003785 apex_available: ["myapex"],
3786 }
3787
3788 cc_library {
3789 name: "libbaz",
3790 stl: "none",
3791 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09003792 }`)
Paul Duffinde7464c2020-03-30 17:54:29 +01003793}
Jiyong Park127b40b2019-09-30 16:04:35 +09003794
Paul Duffinde7464c2020-03-30 17:54:29 +01003795func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09003796 testApexError(t, "\"otherapex\" is not a valid module name", `
3797 apex {
3798 name: "myapex",
3799 key: "myapex.key",
3800 native_shared_libs: ["libfoo"],
3801 }
3802
3803 apex_key {
3804 name: "myapex.key",
3805 public_key: "testkey.avbpubkey",
3806 private_key: "testkey.pem",
3807 }
3808
3809 cc_library {
3810 name: "libfoo",
3811 stl: "none",
3812 system_shared_libs: [],
3813 apex_available: ["otherapex"],
3814 }`)
3815
Paul Duffinde7464c2020-03-30 17:54:29 +01003816 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003817 apex {
3818 name: "myapex",
3819 key: "myapex.key",
3820 native_shared_libs: ["libfoo", "libbar"],
3821 }
3822
3823 apex_key {
3824 name: "myapex.key",
3825 public_key: "testkey.avbpubkey",
3826 private_key: "testkey.pem",
3827 }
3828
3829 cc_library {
3830 name: "libfoo",
3831 stl: "none",
3832 system_shared_libs: [],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003833 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003834 apex_available: ["myapex"],
3835 }
3836
3837 cc_library {
3838 name: "libbar",
3839 stl: "none",
3840 system_shared_libs: [],
3841 apex_available: ["//apex_available:anyapex"],
Jiyong Park9f14b9b2020-03-01 17:29:06 +09003842 }
3843
3844 cc_library {
3845 name: "libbaz",
3846 stl: "none",
3847 system_shared_libs: [],
3848 stubs: {
3849 versions: ["10", "20", "30"],
3850 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003851 }`)
Paul Duffinde7464c2020-03-30 17:54:29 +01003852}
Jiyong Park127b40b2019-09-30 16:04:35 +09003853
Jiyong Park6a9ddc32020-04-07 16:37:39 +09003854func TestApexAvailable_CheckForPlatform(t *testing.T) {
Paul Duffinde7464c2020-03-30 17:54:29 +01003855 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09003856 apex {
3857 name: "myapex",
3858 key: "myapex.key",
Jiyong Park6a9ddc32020-04-07 16:37:39 +09003859 native_shared_libs: ["libbar", "libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003860 }
3861
3862 apex_key {
3863 name: "myapex.key",
3864 public_key: "testkey.avbpubkey",
3865 private_key: "testkey.pem",
3866 }
3867
3868 cc_library {
3869 name: "libfoo",
3870 stl: "none",
3871 system_shared_libs: [],
Jiyong Park6a9ddc32020-04-07 16:37:39 +09003872 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09003873 apex_available: ["//apex_available:platform"],
Jiyong Park6a9ddc32020-04-07 16:37:39 +09003874 }
3875
3876 cc_library {
3877 name: "libfoo2",
3878 stl: "none",
3879 system_shared_libs: [],
3880 shared_libs: ["libbaz"],
3881 apex_available: ["//apex_available:platform"],
3882 }
3883
3884 cc_library {
3885 name: "libbar",
3886 stl: "none",
3887 system_shared_libs: [],
3888 apex_available: ["myapex"],
3889 }
3890
3891 cc_library {
3892 name: "libbaz",
3893 stl: "none",
3894 system_shared_libs: [],
3895 apex_available: ["myapex"],
3896 stubs: {
3897 versions: ["1"],
3898 },
Jiyong Park127b40b2019-09-30 16:04:35 +09003899 }`)
3900
Jiyong Park6a9ddc32020-04-07 16:37:39 +09003901 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
3902 // because it depends on libbar which isn't available to platform
3903 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
3904 if libfoo.NotAvailableForPlatform() != true {
3905 t.Errorf("%q shouldn't be available to platform", libfoo.String())
3906 }
3907
3908 // libfoo2 however can be available to platform because it depends on libbaz which provides
3909 // stubs
3910 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
3911 if libfoo2.NotAvailableForPlatform() == true {
3912 t.Errorf("%q should be available to platform", libfoo2.String())
3913 }
Paul Duffinde7464c2020-03-30 17:54:29 +01003914}
Jiyong Parka90ca002019-10-07 15:47:24 +09003915
Paul Duffinde7464c2020-03-30 17:54:29 +01003916func TestApexAvailable_CreatedForApex(t *testing.T) {
Jiyong Park6a9ddc32020-04-07 16:37:39 +09003917 ctx, _ := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09003918 apex {
3919 name: "myapex",
3920 key: "myapex.key",
3921 native_shared_libs: ["libfoo"],
3922 }
3923
3924 apex_key {
3925 name: "myapex.key",
3926 public_key: "testkey.avbpubkey",
3927 private_key: "testkey.pem",
3928 }
3929
3930 cc_library {
3931 name: "libfoo",
3932 stl: "none",
3933 system_shared_libs: [],
3934 apex_available: ["myapex"],
3935 static: {
3936 apex_available: ["//apex_available:platform"],
3937 },
3938 }`)
3939
Jiyong Park6a9ddc32020-04-07 16:37:39 +09003940 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
3941 if libfooShared.NotAvailableForPlatform() != true {
3942 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
3943 }
3944 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
3945 if libfooStatic.NotAvailableForPlatform() != false {
3946 t.Errorf("%q should be available to platform", libfooStatic.String())
3947 }
Jiyong Park127b40b2019-09-30 16:04:35 +09003948}
3949
Jiyong Park5d790c32019-11-15 18:40:32 +09003950func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003951 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003952 apex {
3953 name: "myapex",
3954 key: "myapex.key",
3955 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003956 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003957 }
3958
3959 override_apex {
3960 name: "override_myapex",
3961 base: "myapex",
3962 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003963 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003964 logging_parent: "com.foo.bar",
Baligh Uddincb6aa122020-03-15 13:01:05 -07003965 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09003966 }
3967
3968 apex_key {
3969 name: "myapex.key",
3970 public_key: "testkey.avbpubkey",
3971 private_key: "testkey.pem",
3972 }
3973
3974 android_app {
3975 name: "app",
3976 srcs: ["foo/bar/MyClass.java"],
3977 package_name: "foo",
3978 sdk_version: "none",
3979 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003980 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003981 }
3982
3983 override_android_app {
3984 name: "override_app",
3985 base: "app",
3986 package_name: "bar",
3987 }
Jiyong Parka519c542020-03-03 11:45:41 +09003988 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09003989
Jiyong Park317645e2019-12-05 13:20:58 +09003990 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3991 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3992 if originalVariant.GetOverriddenBy() != "" {
3993 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3994 }
3995 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3996 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3997 }
3998
Jiyong Park5d790c32019-11-15 18:40:32 +09003999 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
4000 apexRule := module.Rule("apexRule")
4001 copyCmds := apexRule.Args["copy_commands"]
4002
4003 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han65cd0f02020-03-23 20:21:11 +09004004 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004005
4006 apexBundle := module.Module().(*apexBundle)
4007 name := apexBundle.Name()
4008 if name != "override_myapex" {
4009 t.Errorf("name should be \"override_myapex\", but was %q", name)
4010 }
4011
Baligh Uddin004d7172020-02-19 21:29:28 -08004012 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
4013 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
4014 }
4015
Jiyong Parka519c542020-03-03 11:45:41 +09004016 optFlags := apexRule.Args["opt_flags"]
Baligh Uddincb6aa122020-03-15 13:01:05 -07004017 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Parka519c542020-03-03 11:45:41 +09004018
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004019 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
4020 var builder strings.Builder
4021 data.Custom(&builder, name, "TARGET_", "", data)
4022 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09004023 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004024 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
4025 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004026 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004027 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09004028 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004029 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
4030 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09004031}
4032
Jooyung Han214bf372019-11-12 13:03:50 +09004033func TestLegacyAndroid10Support(t *testing.T) {
4034 ctx, _ := testApex(t, `
4035 apex {
4036 name: "myapex",
4037 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004038 native_shared_libs: ["mylib"],
Jooyung Han23b0adf2020-03-12 18:37:20 +09004039 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09004040 }
4041
4042 apex_key {
4043 name: "myapex.key",
4044 public_key: "testkey.avbpubkey",
4045 private_key: "testkey.pem",
4046 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004047
4048 cc_library {
4049 name: "mylib",
4050 srcs: ["mylib.cpp"],
4051 stl: "libc++",
4052 system_shared_libs: [],
4053 apex_available: [ "myapex" ],
4054 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004055 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09004056
4057 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4058 args := module.Rule("apexRule").Args
4059 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00004060 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004061
4062 // The copies of the libraries in the apex should have one more dependency than
4063 // the ones outside the apex, namely the unwinder. Ideally we should check
4064 // the dependency names directly here but for some reason the names are blank in
4065 // this test.
4066 for _, lib := range []string{"libc++", "mylib"} {
4067 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
4068 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
4069 if len(apexImplicits) != len(nonApexImplicits)+1 {
4070 t.Errorf("%q missing unwinder dep", lib)
4071 }
4072 }
Jooyung Han214bf372019-11-12 13:03:50 +09004073}
4074
Paul Duffinbf19a972020-05-26 13:21:35 +01004075var filesForSdkLibrary = map[string][]byte{
4076 "api/current.txt": nil,
4077 "api/removed.txt": nil,
4078 "api/system-current.txt": nil,
4079 "api/system-removed.txt": nil,
4080 "api/test-current.txt": nil,
4081 "api/test-removed.txt": nil,
Paul Duffinf642a312020-06-12 17:46:39 +01004082
4083 // For java_sdk_library_import
4084 "a.jar": nil,
Paul Duffinbf19a972020-05-26 13:21:35 +01004085}
4086
Jooyung Han58f26ab2019-12-18 15:34:32 +09004087func TestJavaSDKLibrary(t *testing.T) {
4088 ctx, _ := testApex(t, `
4089 apex {
4090 name: "myapex",
4091 key: "myapex.key",
4092 java_libs: ["foo"],
4093 }
4094
4095 apex_key {
4096 name: "myapex.key",
4097 public_key: "testkey.avbpubkey",
4098 private_key: "testkey.pem",
4099 }
4100
4101 java_sdk_library {
4102 name: "foo",
4103 srcs: ["a.java"],
4104 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004105 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09004106 }
Paul Duffinbf19a972020-05-26 13:21:35 +01004107 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09004108
4109 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00004110 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09004111 "javalib/foo.jar",
4112 "etc/permissions/foo.xml",
4113 })
4114 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09004115 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
4116 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09004117}
4118
Paul Duffinbf19a972020-05-26 13:21:35 +01004119func TestJavaSDKLibrary_WithinApex(t *testing.T) {
4120 ctx, _ := testApex(t, `
4121 apex {
4122 name: "myapex",
4123 key: "myapex.key",
4124 java_libs: ["foo", "bar"],
4125 }
4126
4127 apex_key {
4128 name: "myapex.key",
4129 public_key: "testkey.avbpubkey",
4130 private_key: "testkey.pem",
4131 }
4132
4133 java_sdk_library {
4134 name: "foo",
4135 srcs: ["a.java"],
4136 api_packages: ["foo"],
4137 apex_available: ["myapex"],
4138 sdk_version: "none",
4139 system_modules: "none",
4140 }
4141
4142 java_library {
4143 name: "bar",
4144 srcs: ["a.java"],
4145 libs: ["foo"],
4146 apex_available: ["myapex"],
4147 sdk_version: "none",
4148 system_modules: "none",
4149 }
4150 `, withFiles(filesForSdkLibrary))
4151
4152 // java_sdk_library installs both impl jar and permission XML
4153 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4154 "javalib/bar.jar",
4155 "javalib/foo.jar",
4156 "etc/permissions/foo.xml",
4157 })
4158
4159 // The bar library should depend on the implementation jar.
4160 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
4161 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
4162 t.Errorf("expected %q, found %#q", expected, actual)
4163 }
4164}
4165
4166func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
4167 ctx, _ := testApex(t, `
4168 apex {
4169 name: "myapex",
4170 key: "myapex.key",
4171 java_libs: ["foo"],
4172 }
4173
4174 apex_key {
4175 name: "myapex.key",
4176 public_key: "testkey.avbpubkey",
4177 private_key: "testkey.pem",
4178 }
4179
4180 java_sdk_library {
4181 name: "foo",
4182 srcs: ["a.java"],
4183 api_packages: ["foo"],
4184 apex_available: ["myapex"],
4185 sdk_version: "none",
4186 system_modules: "none",
4187 }
4188
4189 java_library {
4190 name: "bar",
4191 srcs: ["a.java"],
4192 libs: ["foo"],
4193 sdk_version: "none",
4194 system_modules: "none",
4195 }
4196 `, withFiles(filesForSdkLibrary))
4197
4198 // java_sdk_library installs both impl jar and permission XML
4199 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4200 "javalib/foo.jar",
4201 "etc/permissions/foo.xml",
4202 })
4203
4204 // The bar library should depend on the stubs jar.
4205 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
4206 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
4207 t.Errorf("expected %q, found %#q", expected, actual)
4208 }
4209}
4210
Paul Duffinf642a312020-06-12 17:46:39 +01004211func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
4212 ctx, _ := testApex(t, ``,
4213 withFiles(map[string][]byte{
4214 "apex/a.java": nil,
4215 "apex/apex_manifest.json": nil,
4216 "apex/Android.bp": []byte(`
4217 package {
4218 default_visibility: ["//visibility:private"],
4219 }
4220
4221 apex {
4222 name: "myapex",
4223 key: "myapex.key",
4224 java_libs: ["foo", "bar"],
4225 }
4226
4227 apex_key {
4228 name: "myapex.key",
4229 public_key: "testkey.avbpubkey",
4230 private_key: "testkey.pem",
4231 }
4232
4233 java_library {
4234 name: "bar",
4235 srcs: ["a.java"],
4236 libs: ["foo"],
4237 apex_available: ["myapex"],
4238 sdk_version: "none",
4239 system_modules: "none",
4240 }
4241`),
4242 "source/a.java": nil,
4243 "source/api/current.txt": nil,
4244 "source/api/removed.txt": nil,
4245 "source/Android.bp": []byte(`
4246 package {
4247 default_visibility: ["//visibility:private"],
4248 }
4249
4250 java_sdk_library {
4251 name: "foo",
4252 visibility: ["//apex"],
4253 srcs: ["a.java"],
4254 api_packages: ["foo"],
4255 apex_available: ["myapex"],
4256 sdk_version: "none",
4257 system_modules: "none",
4258 public: {
4259 enabled: true,
4260 },
4261 }
4262`),
4263 "prebuilt/a.jar": nil,
4264 "prebuilt/Android.bp": []byte(`
4265 package {
4266 default_visibility: ["//visibility:private"],
4267 }
4268
4269 java_sdk_library_import {
4270 name: "foo",
4271 visibility: ["//apex", "//source"],
4272 apex_available: ["myapex"],
4273 prefer: true,
4274 public: {
4275 jars: ["a.jar"],
4276 },
4277 }
4278`),
4279 }),
4280 )
4281
4282 // java_sdk_library installs both impl jar and permission XML
4283 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4284 "javalib/bar.jar",
4285 "javalib/foo.jar",
4286 "etc/permissions/foo.xml",
4287 })
4288
4289 // The bar library should depend on the implementation jar.
4290 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
4291 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
4292 t.Errorf("expected %q, found %#q", expected, actual)
4293 }
4294}
4295
4296func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
4297 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
4298 apex {
4299 name: "myapex",
4300 key: "myapex.key",
4301 java_libs: ["foo"],
4302 }
4303
4304 apex_key {
4305 name: "myapex.key",
4306 public_key: "testkey.avbpubkey",
4307 private_key: "testkey.pem",
4308 }
4309
4310 java_sdk_library_import {
4311 name: "foo",
4312 apex_available: ["myapex"],
4313 prefer: true,
4314 public: {
4315 jars: ["a.jar"],
4316 },
4317 }
4318
4319 `, withFiles(filesForSdkLibrary))
4320}
4321
atrost6e126252020-01-27 17:01:16 +00004322func TestCompatConfig(t *testing.T) {
4323 ctx, _ := testApex(t, `
4324 apex {
4325 name: "myapex",
4326 key: "myapex.key",
4327 prebuilts: ["myjar-platform-compat-config"],
4328 java_libs: ["myjar"],
4329 }
4330
4331 apex_key {
4332 name: "myapex.key",
4333 public_key: "testkey.avbpubkey",
4334 private_key: "testkey.pem",
4335 }
4336
4337 platform_compat_config {
4338 name: "myjar-platform-compat-config",
4339 src: ":myjar",
4340 }
4341
4342 java_library {
4343 name: "myjar",
4344 srcs: ["foo/bar/MyClass.java"],
4345 sdk_version: "none",
4346 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00004347 apex_available: [ "myapex" ],
4348 }
4349 `)
4350 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4351 "etc/compatconfig/myjar-platform-compat-config.xml",
4352 "javalib/myjar.jar",
4353 })
4354}
4355
Jiyong Park479321d2019-12-16 11:47:12 +09004356func TestRejectNonInstallableJavaLibrary(t *testing.T) {
4357 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
4358 apex {
4359 name: "myapex",
4360 key: "myapex.key",
4361 java_libs: ["myjar"],
4362 }
4363
4364 apex_key {
4365 name: "myapex.key",
4366 public_key: "testkey.avbpubkey",
4367 private_key: "testkey.pem",
4368 }
4369
4370 java_library {
4371 name: "myjar",
4372 srcs: ["foo/bar/MyClass.java"],
4373 sdk_version: "none",
4374 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09004375 compile_dex: false,
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09004376 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09004377 }
4378 `)
4379}
4380
Jiyong Park7afd1072019-12-30 16:56:33 +09004381func TestCarryRequiredModuleNames(t *testing.T) {
4382 ctx, config := testApex(t, `
4383 apex {
4384 name: "myapex",
4385 key: "myapex.key",
4386 native_shared_libs: ["mylib"],
4387 }
4388
4389 apex_key {
4390 name: "myapex.key",
4391 public_key: "testkey.avbpubkey",
4392 private_key: "testkey.pem",
4393 }
4394
4395 cc_library {
4396 name: "mylib",
4397 srcs: ["mylib.cpp"],
4398 system_shared_libs: [],
4399 stl: "none",
4400 required: ["a", "b"],
4401 host_required: ["c", "d"],
4402 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004403 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09004404 }
4405 `)
4406
4407 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4408 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
4409 name := apexBundle.BaseModuleName()
4410 prefix := "TARGET_"
4411 var builder strings.Builder
4412 data.Custom(&builder, name, prefix, "", data)
4413 androidMk := builder.String()
4414 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
4415 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
4416 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
4417}
4418
Jiyong Park7cd10e32020-01-14 09:22:18 +09004419func TestSymlinksFromApexToSystem(t *testing.T) {
4420 bp := `
4421 apex {
4422 name: "myapex",
4423 key: "myapex.key",
4424 native_shared_libs: ["mylib"],
4425 java_libs: ["myjar"],
4426 }
4427
Jiyong Park9d677202020-02-19 16:29:35 +09004428 apex {
4429 name: "myapex.updatable",
4430 key: "myapex.key",
4431 native_shared_libs: ["mylib"],
4432 java_libs: ["myjar"],
4433 updatable: true,
Jooyung Hanced674e2020-04-27 12:10:30 +09004434 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09004435 }
4436
Jiyong Park7cd10e32020-01-14 09:22:18 +09004437 apex_key {
4438 name: "myapex.key",
4439 public_key: "testkey.avbpubkey",
4440 private_key: "testkey.pem",
4441 }
4442
4443 cc_library {
4444 name: "mylib",
4445 srcs: ["mylib.cpp"],
4446 shared_libs: ["myotherlib"],
4447 system_shared_libs: [],
4448 stl: "none",
4449 apex_available: [
4450 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004451 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004452 "//apex_available:platform",
4453 ],
4454 }
4455
4456 cc_library {
4457 name: "myotherlib",
4458 srcs: ["mylib.cpp"],
4459 system_shared_libs: [],
4460 stl: "none",
4461 apex_available: [
4462 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004463 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004464 "//apex_available:platform",
4465 ],
4466 }
4467
4468 java_library {
4469 name: "myjar",
4470 srcs: ["foo/bar/MyClass.java"],
4471 sdk_version: "none",
4472 system_modules: "none",
4473 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09004474 apex_available: [
4475 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004476 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004477 "//apex_available:platform",
4478 ],
4479 }
4480
4481 java_library {
4482 name: "myotherjar",
4483 srcs: ["foo/bar/MyClass.java"],
4484 sdk_version: "none",
4485 system_modules: "none",
4486 apex_available: [
4487 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09004488 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09004489 "//apex_available:platform",
4490 ],
4491 }
4492 `
4493
4494 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
4495 for _, f := range files {
4496 if f.path == file {
4497 if f.isLink {
4498 t.Errorf("%q is not a real file", file)
4499 }
4500 return
4501 }
4502 }
4503 t.Errorf("%q is not found", file)
4504 }
4505
4506 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
4507 for _, f := range files {
4508 if f.path == file {
4509 if !f.isLink {
4510 t.Errorf("%q is not a symlink", file)
4511 }
4512 return
4513 }
4514 }
4515 t.Errorf("%q is not found", file)
4516 }
4517
Jiyong Park9d677202020-02-19 16:29:35 +09004518 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
4519 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09004520 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004521 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004522 ensureRealfileExists(t, files, "javalib/myjar.jar")
4523 ensureRealfileExists(t, files, "lib64/mylib.so")
4524 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4525
Jiyong Park9d677202020-02-19 16:29:35 +09004526 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4527 ensureRealfileExists(t, files, "javalib/myjar.jar")
4528 ensureRealfileExists(t, files, "lib64/mylib.so")
4529 ensureRealfileExists(t, files, "lib64/myotherlib.so")
4530
4531 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09004532 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00004533 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09004534 ensureRealfileExists(t, files, "javalib/myjar.jar")
4535 ensureRealfileExists(t, files, "lib64/mylib.so")
4536 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09004537
4538 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
4539 ensureRealfileExists(t, files, "javalib/myjar.jar")
4540 ensureRealfileExists(t, files, "lib64/mylib.so")
4541 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09004542}
4543
Jooyung Han40b286c2020-04-17 13:43:10 +09004544func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
4545 ctx, _ := testApex(t, `
4546 apex {
4547 name: "myapex",
4548 key: "myapex.key",
4549 }
4550 apex_key {
4551 name: "myapex.key",
4552 public_key: "testkey.avbpubkey",
4553 private_key: "testkey.pem",
4554 }
4555 `, func(fs map[string][]byte, config android.Config) {
4556 delete(config.Targets, android.Android)
4557 config.AndroidCommonTarget = android.Target{}
4558 })
4559
4560 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
4561 t.Errorf("Expected variants: %v, but got: %v", expected, got)
4562 }
4563}
4564
Jiyong Parkd93e1b12020-02-28 15:22:21 +09004565func TestAppBundle(t *testing.T) {
4566 ctx, _ := testApex(t, `
4567 apex {
4568 name: "myapex",
4569 key: "myapex.key",
4570 apps: ["AppFoo"],
4571 }
4572
4573 apex_key {
4574 name: "myapex.key",
4575 public_key: "testkey.avbpubkey",
4576 private_key: "testkey.pem",
4577 }
4578
4579 android_app {
4580 name: "AppFoo",
4581 srcs: ["foo/bar/MyClass.java"],
4582 sdk_version: "none",
4583 system_modules: "none",
4584 apex_available: [ "myapex" ],
4585 }
Jiyong Parkaf8998c2020-02-28 16:51:07 +09004586 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkd93e1b12020-02-28 15:22:21 +09004587
4588 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
4589 content := bundleConfigRule.Args["content"]
4590
4591 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkaf8998c2020-02-28 16:51:07 +09004592 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo/AppFoo.apk"}]}`)
Jiyong Parkd93e1b12020-02-28 15:22:21 +09004593}
4594
Sasha Smundakc4f0ff12020-05-27 16:36:07 -07004595func TestAppSetBundle(t *testing.T) {
4596 ctx, _ := testApex(t, `
4597 apex {
4598 name: "myapex",
4599 key: "myapex.key",
4600 apps: ["AppSet"],
4601 }
4602
4603 apex_key {
4604 name: "myapex.key",
4605 public_key: "testkey.avbpubkey",
4606 private_key: "testkey.pem",
4607 }
4608
4609 android_app_set {
4610 name: "AppSet",
4611 set: "AppSet.apks",
4612 }`)
4613 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4614 bundleConfigRule := mod.Description("Bundle Config")
4615 content := bundleConfigRule.Args["content"]
4616 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
4617 s := mod.Rule("apexRule").Args["copy_commands"]
4618 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
4619 if len(copyCmds) != 3 {
4620 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
4621 }
4622 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
4623 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
4624 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
4625}
4626
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004627func testNoUpdatableJarsInBootImage(t *testing.T, errmsg, bp string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
4628 t.Helper()
4629
4630 bp = bp + `
4631 filegroup {
4632 name: "some-updatable-apex-file_contexts",
4633 srcs: [
4634 "system/sepolicy/apex/some-updatable-apex-file_contexts",
4635 ],
4636 }
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +01004637
4638 filegroup {
4639 name: "some-non-updatable-apex-file_contexts",
4640 srcs: [
4641 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
4642 ],
4643 }
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004644 `
4645 bp += cc.GatherRequiredDepsForTest(android.Android)
4646 bp += java.GatherRequiredDepsForTest()
4647 bp += dexpreopt.BpToolModulesForTest()
4648
4649 fs := map[string][]byte{
4650 "a.java": nil,
4651 "a.jar": nil,
4652 "build/make/target/product/security": nil,
4653 "apex_manifest.json": nil,
4654 "AndroidManifest.xml": nil,
4655 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +01004656 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004657 "system/sepolicy/apex/com.android.art.something-file_contexts": nil,
4658 "framework/aidl/a.aidl": nil,
4659 }
4660 cc.GatherRequiredFilesForTest(fs)
4661
4662 ctx := android.NewTestArchContext()
4663 ctx.RegisterModuleType("apex", BundleFactory)
4664 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
4665 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffineedf3f12020-04-29 18:27:14 +01004666 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004667 cc.RegisterRequiredBuildComponentsForTest(ctx)
4668 java.RegisterJavaBuildComponents(ctx)
4669 java.RegisterSystemModulesBuildComponents(ctx)
4670 java.RegisterAppBuildComponents(ctx)
4671 java.RegisterDexpreoptBootJarsComponents(ctx)
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004672 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
4673 ctx.PreDepsMutators(RegisterPreDepsMutators)
4674 ctx.PostDepsMutators(RegisterPostDepsMutators)
4675
4676 config := android.TestArchConfig(buildDir, nil, bp, fs)
4677 ctx.Register(config)
4678
4679 _ = dexpreopt.GlobalSoongConfigForTests(config)
4680 dexpreopt.RegisterToolModulesForTest(ctx)
4681 pathCtx := android.PathContextForTesting(config)
4682 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
4683 transformDexpreoptConfig(dexpreoptConfig)
4684 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
4685
4686 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
4687 android.FailIfErrored(t, errs)
4688
4689 _, errs = ctx.PrepareBuildActions(config)
4690 if errmsg == "" {
4691 android.FailIfErrored(t, errs)
4692 } else if len(errs) > 0 {
4693 android.FailIfNoMatchingErrors(t, errmsg, errs)
4694 return
4695 } else {
4696 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
4697 }
4698}
4699
Jooyung Hanced674e2020-04-27 12:10:30 +09004700func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
4701 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
4702 apex {
4703 name: "myapex",
4704 key: "myapex.key",
4705 updatable: true,
4706 }
4707
4708 apex_key {
4709 name: "myapex.key",
4710 public_key: "testkey.avbpubkey",
4711 private_key: "testkey.pem",
4712 }
4713 `)
4714}
4715
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004716func TestNoUpdatableJarsInBootImage(t *testing.T) {
4717 bp := `
4718 java_library {
4719 name: "some-updatable-apex-lib",
4720 srcs: ["a.java"],
Artur Satayev2eedf622020-04-15 17:29:42 +01004721 sdk_version: "current",
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004722 apex_available: [
4723 "some-updatable-apex",
4724 ],
4725 }
4726
4727 java_library {
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +01004728 name: "some-non-updatable-apex-lib",
4729 srcs: ["a.java"],
4730 apex_available: [
4731 "some-non-updatable-apex",
4732 ],
4733 }
4734
4735 java_library {
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004736 name: "some-platform-lib",
4737 srcs: ["a.java"],
Artur Satayev2eedf622020-04-15 17:29:42 +01004738 sdk_version: "current",
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004739 installable: true,
4740 }
4741
4742 java_library {
4743 name: "some-art-lib",
4744 srcs: ["a.java"],
Artur Satayev2eedf622020-04-15 17:29:42 +01004745 sdk_version: "current",
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004746 apex_available: [
4747 "com.android.art.something",
4748 ],
4749 hostdex: true,
4750 }
4751
4752 apex {
4753 name: "some-updatable-apex",
4754 key: "some-updatable-apex.key",
4755 java_libs: ["some-updatable-apex-lib"],
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +01004756 updatable: true,
4757 min_sdk_version: "current",
4758 }
4759
4760 apex {
4761 name: "some-non-updatable-apex",
4762 key: "some-non-updatable-apex.key",
4763 java_libs: ["some-non-updatable-apex-lib"],
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004764 }
4765
4766 apex_key {
4767 name: "some-updatable-apex.key",
4768 }
4769
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +01004770 apex_key {
4771 name: "some-non-updatable-apex.key",
4772 }
4773
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004774 apex {
4775 name: "com.android.art.something",
4776 key: "com.android.art.something.key",
4777 java_libs: ["some-art-lib"],
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +01004778 updatable: true,
4779 min_sdk_version: "current",
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004780 }
4781
4782 apex_key {
4783 name: "com.android.art.something.key",
4784 }
4785 `
4786
4787 var error string
4788 var transform func(*dexpreopt.GlobalConfig)
4789
4790 // updatable jar from ART apex in the ART boot image => ok
4791 transform = func(config *dexpreopt.GlobalConfig) {
4792 config.ArtApexJars = []string{"some-art-lib"}
4793 }
4794 testNoUpdatableJarsInBootImage(t, "", bp, transform)
4795
4796 // updatable jar from ART apex in the framework boot image => error
4797 error = "module 'some-art-lib' from updatable apex 'com.android.art.something' is not allowed in the framework boot image"
4798 transform = func(config *dexpreopt.GlobalConfig) {
4799 config.BootJars = []string{"some-art-lib"}
4800 }
4801 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4802
4803 // updatable jar from some other apex in the ART boot image => error
4804 error = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the ART boot image"
4805 transform = func(config *dexpreopt.GlobalConfig) {
4806 config.ArtApexJars = []string{"some-updatable-apex-lib"}
4807 }
4808 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4809
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +01004810 // non-updatable jar from some other apex in the ART boot image => error
4811 error = "module 'some-non-updatable-apex-lib' is not allowed in the ART boot image"
4812 transform = func(config *dexpreopt.GlobalConfig) {
4813 config.ArtApexJars = []string{"some-non-updatable-apex-lib"}
4814 }
4815 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4816
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004817 // updatable jar from some other apex in the framework boot image => error
4818 error = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the framework boot image"
4819 transform = func(config *dexpreopt.GlobalConfig) {
4820 config.BootJars = []string{"some-updatable-apex-lib"}
4821 }
4822 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4823
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +01004824 // non-updatable jar from some other apex in the framework boot image => ok
4825 transform = func(config *dexpreopt.GlobalConfig) {
4826 config.BootJars = []string{"some-non-updatable-apex-lib"}
4827 }
4828 testNoUpdatableJarsInBootImage(t, "", bp, transform)
4829
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004830 // nonexistent jar in the ART boot image => error
4831 error = "failed to find a dex jar path for module 'nonexistent'"
4832 transform = func(config *dexpreopt.GlobalConfig) {
4833 config.ArtApexJars = []string{"nonexistent"}
4834 }
4835 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4836
4837 // nonexistent jar in the framework boot image => error
4838 error = "failed to find a dex jar path for module 'nonexistent'"
4839 transform = func(config *dexpreopt.GlobalConfig) {
4840 config.BootJars = []string{"nonexistent"}
4841 }
4842 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4843
4844 // platform jar in the ART boot image => error
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +01004845 error = "module 'some-platform-lib' is not allowed in the ART boot image"
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +00004846 transform = func(config *dexpreopt.GlobalConfig) {
4847 config.ArtApexJars = []string{"some-platform-lib"}
4848 }
4849 testNoUpdatableJarsInBootImage(t, error, bp, transform)
4850
4851 // platform jar in the framework boot image => ok
4852 transform = func(config *dexpreopt.GlobalConfig) {
4853 config.BootJars = []string{"some-platform-lib"}
4854 }
4855 testNoUpdatableJarsInBootImage(t, "", bp, transform)
4856}
4857
Andrei Onea115e7e72020-06-05 21:14:03 +01004858func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
4859 t.Helper()
4860 android.ClearApexDependency()
4861 bp += `
4862 apex_key {
4863 name: "myapex.key",
4864 public_key: "testkey.avbpubkey",
4865 private_key: "testkey.pem",
4866 }`
4867 fs := map[string][]byte{
4868 "lib1/src/A.java": nil,
4869 "lib2/src/B.java": nil,
4870 "system/sepolicy/apex/myapex-file_contexts": nil,
4871 }
4872
4873 ctx := android.NewTestArchContext()
4874 ctx.RegisterModuleType("apex", BundleFactory)
4875 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
4876 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
4877 cc.RegisterRequiredBuildComponentsForTest(ctx)
4878 java.RegisterJavaBuildComponents(ctx)
4879 java.RegisterSystemModulesBuildComponents(ctx)
4880 java.RegisterDexpreoptBootJarsComponents(ctx)
4881 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
4882 ctx.PreDepsMutators(RegisterPreDepsMutators)
4883 ctx.PostDepsMutators(RegisterPostDepsMutators)
4884 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
4885
4886 config := android.TestArchConfig(buildDir, nil, bp, fs)
4887 android.SetTestNeverallowRules(config, rules)
4888 updatableBootJars := make([]string, 0, len(apexBootJars))
4889 for _, apexBootJar := range apexBootJars {
4890 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
4891 }
4892 config.TestProductVariables.UpdatableBootJars = updatableBootJars
4893
4894 ctx.Register(config)
4895
4896 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
4897 android.FailIfErrored(t, errs)
4898
4899 _, errs = ctx.PrepareBuildActions(config)
4900 if errmsg == "" {
4901 android.FailIfErrored(t, errs)
4902 } else if len(errs) > 0 {
4903 android.FailIfNoMatchingErrors(t, errmsg, errs)
4904 return
4905 } else {
4906 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
4907 }
4908}
4909
4910func TestApexPermittedPackagesRules(t *testing.T) {
4911 testcases := []struct {
4912 name string
4913 expectedError string
4914 bp string
4915 bootJars []string
4916 modulesPackages map[string][]string
4917 }{
4918
4919 {
4920 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
4921 expectedError: "",
4922 bp: `
4923 java_library {
4924 name: "bcp_lib1",
4925 srcs: ["lib1/src/*.java"],
4926 permitted_packages: ["foo.bar"],
4927 apex_available: ["myapex"],
4928 sdk_version: "none",
4929 system_modules: "none",
4930 }
4931 java_library {
4932 name: "nonbcp_lib2",
4933 srcs: ["lib2/src/*.java"],
4934 apex_available: ["myapex"],
4935 permitted_packages: ["a.b"],
4936 sdk_version: "none",
4937 system_modules: "none",
4938 }
4939 apex {
4940 name: "myapex",
4941 key: "myapex.key",
4942 java_libs: ["bcp_lib1", "nonbcp_lib2"],
4943 }`,
4944 bootJars: []string{"bcp_lib1"},
4945 modulesPackages: map[string][]string{
4946 "myapex": []string{
4947 "foo.bar",
4948 },
4949 },
4950 },
4951 {
4952 name: "Bootclasspath apex jar not satisfying allowed module packages.",
4953 expectedError: `module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only allow these packages: foo.bar. Please jarjar or move code around.`,
4954 bp: `
4955 java_library {
4956 name: "bcp_lib1",
4957 srcs: ["lib1/src/*.java"],
4958 apex_available: ["myapex"],
4959 permitted_packages: ["foo.bar"],
4960 sdk_version: "none",
4961 system_modules: "none",
4962 }
4963 java_library {
4964 name: "bcp_lib2",
4965 srcs: ["lib2/src/*.java"],
4966 apex_available: ["myapex"],
4967 permitted_packages: ["foo.bar", "bar.baz"],
4968 sdk_version: "none",
4969 system_modules: "none",
4970 }
4971 apex {
4972 name: "myapex",
4973 key: "myapex.key",
4974 java_libs: ["bcp_lib1", "bcp_lib2"],
4975 }
4976 `,
4977 bootJars: []string{"bcp_lib1", "bcp_lib2"},
4978 modulesPackages: map[string][]string{
4979 "myapex": []string{
4980 "foo.bar",
4981 },
4982 },
4983 },
4984 }
4985 for _, tc := range testcases {
4986 t.Run(tc.name, func(t *testing.T) {
4987 rules := createApexPermittedPackagesRules(tc.modulesPackages)
4988 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
4989 })
4990 }
4991}
4992
Jiyong Parkf0d01b72020-04-13 16:19:48 +09004993func TestTestFor(t *testing.T) {
4994 ctx, _ := testApex(t, `
4995 apex {
4996 name: "myapex",
4997 key: "myapex.key",
4998 native_shared_libs: ["mylib", "myprivlib"],
4999 }
5000
5001 apex_key {
5002 name: "myapex.key",
5003 public_key: "testkey.avbpubkey",
5004 private_key: "testkey.pem",
5005 }
5006
5007 cc_library {
5008 name: "mylib",
5009 srcs: ["mylib.cpp"],
5010 system_shared_libs: [],
5011 stl: "none",
5012 stubs: {
5013 versions: ["1"],
5014 },
5015 apex_available: ["myapex"],
5016 }
5017
5018 cc_library {
5019 name: "myprivlib",
5020 srcs: ["mylib.cpp"],
5021 system_shared_libs: [],
5022 stl: "none",
5023 apex_available: ["myapex"],
5024 }
5025
5026
5027 cc_test {
5028 name: "mytest",
5029 gtest: false,
5030 srcs: ["mylib.cpp"],
5031 system_shared_libs: [],
5032 stl: "none",
5033 shared_libs: ["mylib", "myprivlib"],
5034 test_for: ["myapex"]
5035 }
5036 `)
5037
5038 // the test 'mytest' is a test for the apex, therefore is linked to the
5039 // actual implementation of mylib instead of its stub.
5040 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
5041 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
5042 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
5043}
5044
Jaewoong Jung8cf307e2020-05-14 14:15:24 -07005045// TODO(jungjw): Move this to proptools
5046func intPtr(i int) *int {
5047 return &i
5048}
5049
5050func TestApexSet(t *testing.T) {
5051 ctx, config := testApex(t, `
5052 apex_set {
5053 name: "myapex",
5054 set: "myapex.apks",
5055 filename: "foo_v2.apex",
5056 overrides: ["foo"],
5057 }
5058 `, func(fs map[string][]byte, config android.Config) {
5059 config.TestProductVariables.Platform_sdk_version = intPtr(30)
Jaewoong Jung829b7132020-06-10 12:23:32 -07005060 config.Targets[android.Android] = []android.Target{
5061 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
5062 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
5063 }
Jaewoong Jung8cf307e2020-05-14 14:15:24 -07005064 })
5065
5066 m := ctx.ModuleForTests("myapex", "android_common")
5067
5068 // Check extract_apks tool parameters.
5069 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
5070 actual := extractedApex.Args["abis"]
5071 expected := "ARMEABI_V7A,ARM64_V8A"
5072 if actual != expected {
5073 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
5074 }
5075 actual = extractedApex.Args["sdk-version"]
5076 expected = "30"
5077 if actual != expected {
5078 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
5079 }
5080
5081 a := m.Module().(*ApexSet)
5082 expectedOverrides := []string{"foo"}
5083 actualOverrides := android.AndroidMkEntriesForTest(t, config, "", a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
5084 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
5085 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
5086 }
5087}
5088
Jiyong Park8d6286b2020-06-12 17:26:31 +09005089func TestApexKeysTxt(t *testing.T) {
5090 ctx, _ := testApex(t, `
5091 apex {
5092 name: "myapex",
5093 key: "myapex.key",
5094 }
5095
5096 apex_key {
5097 name: "myapex.key",
5098 public_key: "testkey.avbpubkey",
5099 private_key: "testkey.pem",
5100 }
5101
5102 prebuilt_apex {
5103 name: "myapex",
5104 prefer: true,
5105 arch: {
5106 arm64: {
5107 src: "myapex-arm64.apex",
5108 },
5109 arm: {
5110 src: "myapex-arm.apex",
5111 },
5112 },
5113 }
5114
5115 apex_set {
5116 name: "myapex_set",
5117 set: "myapex.apks",
5118 filename: "myapex_set.apex",
5119 overrides: ["myapex"],
5120 }
5121 `)
5122
5123 apexKeysText := ctx.SingletonForTests("apex_keys_text")
5124 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
5125 ensureContains(t, content, `name="myapex_set.apex" public_key="PRESIGNED" private_key="PRESIGNED" container_certificate="PRESIGNED" container_private_key="PRESIGNED" partition="system"`)
5126 ensureNotContains(t, content, "myapex.apex")
5127}
5128
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07005129func TestMain(m *testing.M) {
5130 run := func() int {
5131 setUp()
5132 defer tearDown()
5133
5134 return m.Run()
5135 }
5136
5137 os.Exit(run())
5138}