blob: 71c4aa268a11e712aebcdf45e8cc72ae2f86bbfc [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090024 "strings"
25 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090026
27 "github.com/google/blueprint/proptools"
28
29 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080030 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090031 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000032 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070033 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090034 "android/soong/java"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090036)
37
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070038var buildDir string
39
Jooyung Hand3639552019-08-09 12:57:43 +090040// names returns name list from white space separated string
41func names(s string) (ns []string) {
42 for _, n := range strings.Split(s, " ") {
43 if len(n) > 0 {
44 ns = append(ns, n)
45 }
46 }
47 return
48}
49
Jooyung Han344d5432019-08-23 11:17:39 +090050func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
51 t.Helper()
52 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090053 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58 _, errs = ctx.PrepareBuildActions(config)
59 if len(errs) > 0 {
60 android.FailIfNoMatchingErrors(t, pattern, errs)
61 return
62 }
63
64 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
65}
66
Jooyung Han344d5432019-08-23 11:17:39 +090067func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
68 t.Helper()
69 ctx, config := testApexContext(t, bp, handlers...)
Paul Duffineedc5d52020-06-12 17:46:39 +010070 _, errs := ctx.ParseBlueprintsFiles(".")
Jooyung Han5c998b92019-06-27 11:30:33 +090071 android.FailIfErrored(t, errs)
72 _, errs = ctx.PrepareBuildActions(config)
73 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070074 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090075}
76
Jooyung Han344d5432019-08-23 11:17:39 +090077type testCustomizer func(fs map[string][]byte, config android.Config)
78
79func withFiles(files map[string][]byte) testCustomizer {
80 return func(fs map[string][]byte, config android.Config) {
81 for k, v := range files {
82 fs[k] = v
83 }
84 }
85}
86
87func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
88 return func(fs map[string][]byte, config android.Config) {
89 for k, v := range targets {
90 config.Targets[k] = v
91 }
92 }
93}
94
Jooyung Han35155c42020-02-06 17:33:20 +090095// withNativeBridgeTargets sets configuration with targets including:
96// - X86_64 (primary)
97// - X86 (secondary)
98// - Arm64 on X86_64 (native bridge)
99// - Arm on X86 (native bridge)
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700100func withNativeBridgeEnabled(_ map[string][]byte, config android.Config) {
Jooyung Han35155c42020-02-06 17:33:20 +0900101 config.Targets[android.Android] = []android.Target{
102 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
106 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
107 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
108 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
109 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
110 }
111}
112
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900113func withManifestPackageNameOverrides(specs []string) testCustomizer {
114 return func(fs map[string][]byte, config android.Config) {
115 config.TestProductVariables.ManifestPackageNameOverrides = specs
116 }
117}
118
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700119func withBinder32bit(_ map[string][]byte, config android.Config) {
Jooyung Han31c470b2019-10-18 16:26:59 +0900120 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
121}
122
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700123func withUnbundledBuild(_ map[string][]byte, config android.Config) {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900124 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
125}
126
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700127func testApexContext(_ *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +0900128 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900129
130 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900131 filegroup {
132 name: "myapex-file_contexts",
133 srcs: [
134 "system/sepolicy/apex/myapex-file_contexts",
135 ],
136 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900137 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800138
Colin Crossf9aabd72020-02-15 11:29:50 -0800139 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
140
Dario Frenicde2a032019-10-27 00:29:22 +0100141 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900142
Jooyung Han344d5432019-08-23 11:17:39 +0900143 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900144 "a.java": nil,
145 "PrebuiltAppFoo.apk": nil,
146 "PrebuiltAppFooPriv.apk": nil,
147 "build/make/target/product/security": nil,
148 "apex_manifest.json": nil,
149 "AndroidManifest.xml": nil,
150 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900151 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900152 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900153 "system/sepolicy/apex/otherapex-file_contexts": nil,
154 "system/sepolicy/apex/commonapex-file_contexts": nil,
155 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800156 "mylib.cpp": nil,
157 "mylib_common.cpp": nil,
158 "mytest.cpp": nil,
159 "mytest1.cpp": nil,
160 "mytest2.cpp": nil,
161 "mytest3.cpp": nil,
162 "myprebuilt": nil,
163 "my_include": nil,
164 "foo/bar/MyClass.java": nil,
165 "prebuilt.jar": nil,
Paul Duffindddd5462020-04-07 15:25:44 +0100166 "prebuilt.so": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800167 "vendor/foo/devkeys/test.x509.pem": nil,
168 "vendor/foo/devkeys/test.pk8": nil,
169 "testkey.x509.pem": nil,
170 "testkey.pk8": nil,
171 "testkey.override.x509.pem": nil,
172 "testkey.override.pk8": nil,
173 "vendor/foo/devkeys/testkey.avbpubkey": nil,
174 "vendor/foo/devkeys/testkey.pem": nil,
175 "NOTICE": nil,
176 "custom_notice": nil,
Jiyong Park9918e1a2020-03-17 19:16:40 +0900177 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800178 "testkey2.avbpubkey": nil,
179 "testkey2.pem": nil,
180 "myapex-arm64.apex": nil,
181 "myapex-arm.apex": nil,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700182 "myapex.apks": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800183 "frameworks/base/api/current.txt": nil,
184 "framework/aidl/a.aidl": nil,
185 "build/make/core/proguard.flags": nil,
186 "build/make/core/proguard_basic_keeps.flags": nil,
187 "dummy.txt": nil,
Liz Kammer1c14a212020-05-12 15:26:55 -0700188 "baz": nil,
189 "bar/baz": nil,
Liz Kammer5bd365f2020-05-27 15:15:11 -0700190 "testdata/baz": nil,
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700191 "AppSet.apks": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900192 }
193
Colin Crossf9aabd72020-02-15 11:29:50 -0800194 cc.GatherRequiredFilesForTest(fs)
195
Jooyung Han344d5432019-08-23 11:17:39 +0900196 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800197 // The fs now needs to be populated before creating the config, call handlers twice
198 // for now, once to get any fs changes, and later after the config was created to
199 // set product variables or targets.
200 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
201 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900202 }
203
Colin Cross98be1bb2019-12-13 20:41:13 -0800204 config := android.TestArchConfig(buildDir, nil, bp, fs)
205 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
206 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
207 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
208 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
209 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Dan Albert4f378d72020-07-23 17:32:15 -0700210 config.TestProductVariables.Platform_version_active_codenames = []string{"Q"}
Colin Cross98be1bb2019-12-13 20:41:13 -0800211 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
212
213 for _, handler := range handlers {
214 // The fs now needs to be populated before creating the config, call handlers twice
215 // for now, earlier to get any fs changes, and now after the config was created to
216 // set product variables or targets.
217 tempFS := map[string][]byte{}
218 handler(tempFS, config)
219 }
220
221 ctx := android.NewTestArchContext()
Paul Duffineedc5d52020-06-12 17:46:39 +0100222
223 // from android package
224 android.RegisterPackageBuildComponents(ctx)
225 ctx.PreArchMutators(android.RegisterVisibilityRuleChecker)
226
Colin Cross98be1bb2019-12-13 20:41:13 -0800227 ctx.RegisterModuleType("apex", BundleFactory)
228 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
229 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
230 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
231 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
232 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
233 ctx.RegisterModuleType("override_apex", overrideApexFactory)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700234 ctx.RegisterModuleType("apex_set", apexSetFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800235
Jooyung Hana57af4a2020-01-23 05:36:59 +0000236 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +0100237 ctx.PreArchMutators(android.RegisterComponentsMutator)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000238 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
239
Paul Duffin021f4e52020-07-30 16:04:17 +0100240 android.RegisterPrebuiltMutators(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +0100241
Paul Duffin021f4e52020-07-30 16:04:17 +0100242 // Register these after the prebuilt mutators have been registered to match what
243 // happens at runtime.
Paul Duffineedc5d52020-06-12 17:46:39 +0100244 ctx.PreArchMutators(android.RegisterVisibilityRuleGatherer)
245 ctx.PostDepsMutators(android.RegisterVisibilityRuleEnforcer)
246
Paul Duffin021f4e52020-07-30 16:04:17 +0100247 cc.RegisterRequiredBuildComponentsForTest(ctx)
248
Colin Cross98be1bb2019-12-13 20:41:13 -0800249 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800250 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
251 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Jooyung Han0703fd82020-08-26 22:11:53 +0900252 prebuilt_etc.RegisterPrebuiltEtcBuildComponents(ctx)
atrost6e126252020-01-27 17:01:16 +0000253 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700254 ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800255 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000256 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000257 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000258 java.RegisterAppBuildComponents(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +0100259 java.RegisterSdkLibraryBuildComponents(ctx)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900260 ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
markchien2f59ec92020-09-02 16:23:38 +0800261 ctx.RegisterModuleType("bpf", bpf.BpfFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800262
Colin Cross98be1bb2019-12-13 20:41:13 -0800263 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800264 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800265
266 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900267
Jooyung Han5c998b92019-06-27 11:30:33 +0900268 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900269}
270
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700271func setUp() {
272 var err error
273 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700275 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900276 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900277}
278
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700279func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700280 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281}
282
Jooyung Han643adc42020-02-27 13:50:06 +0900283// ensure that 'result' equals 'expected'
284func ensureEquals(t *testing.T, result string, expected string) {
285 t.Helper()
286 if result != expected {
287 t.Errorf("%q != %q", expected, result)
288 }
289}
290
Jiyong Park25fc6a92018-11-18 18:02:45 +0900291// ensure that 'result' contains 'expected'
292func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900293 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900294 if !strings.Contains(result, expected) {
295 t.Errorf("%q is not found in %q", expected, result)
296 }
297}
298
Liz Kammer5bd365f2020-05-27 15:15:11 -0700299// ensure that 'result' contains 'expected' exactly one time
300func ensureContainsOnce(t *testing.T, result string, expected string) {
301 t.Helper()
302 count := strings.Count(result, expected)
303 if count != 1 {
304 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
305 }
306}
307
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308// ensures that 'result' does not contain 'notExpected'
309func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900310 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900311 if strings.Contains(result, notExpected) {
312 t.Errorf("%q is found in %q", notExpected, result)
313 }
314}
315
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700316func ensureMatches(t *testing.T, result string, expectedRex string) {
317 ok, err := regexp.MatchString(expectedRex, result)
318 if err != nil {
319 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
320 return
321 }
322 if !ok {
323 t.Errorf("%s does not match regular expession %s", result, expectedRex)
324 }
325}
326
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900328 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900329 if !android.InList(expected, result) {
330 t.Errorf("%q is not found in %v", expected, result)
331 }
332}
333
334func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900335 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900336 if android.InList(notExpected, result) {
337 t.Errorf("%q is found in %v", notExpected, result)
338 }
339}
340
Jooyung Hane1633032019-08-01 17:41:43 +0900341func ensureListEmpty(t *testing.T, result []string) {
342 t.Helper()
343 if len(result) > 0 {
344 t.Errorf("%q is expected to be empty", result)
345 }
346}
347
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348// Minimal test
349func TestBasicApex(t *testing.T) {
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900350 ctx, config := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900351 apex_defaults {
352 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900353 manifest: ":myapex.manifest",
354 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 key: "myapex.key",
356 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800357 multilib: {
358 both: {
359 binaries: ["foo",],
360 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900361 },
Jiyong Park77acec62020-06-01 21:39:15 +0900362 java_libs: [
363 "myjar",
364 "myjar_dex",
365 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900366 }
367
Jiyong Park30ca9372019-02-07 16:27:23 +0900368 apex {
369 name: "myapex",
370 defaults: ["myapex-defaults"],
371 }
372
Jiyong Park25fc6a92018-11-18 18:02:45 +0900373 apex_key {
374 name: "myapex.key",
375 public_key: "testkey.avbpubkey",
376 private_key: "testkey.pem",
377 }
378
Jiyong Park809bb722019-02-13 21:33:49 +0900379 filegroup {
380 name: "myapex.manifest",
381 srcs: ["apex_manifest.json"],
382 }
383
384 filegroup {
385 name: "myapex.androidmanifest",
386 srcs: ["AndroidManifest.xml"],
387 }
388
Jiyong Park25fc6a92018-11-18 18:02:45 +0900389 cc_library {
390 name: "mylib",
391 srcs: ["mylib.cpp"],
392 shared_libs: ["mylib2"],
393 system_shared_libs: [],
394 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000395 // TODO: remove //apex_available:platform
396 apex_available: [
397 "//apex_available:platform",
398 "myapex",
399 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900400 }
401
Alex Light3d673592019-01-18 14:37:31 -0800402 cc_binary {
403 name: "foo",
404 srcs: ["mylib.cpp"],
405 compile_multilib: "both",
406 multilib: {
407 lib32: {
408 suffix: "32",
409 },
410 lib64: {
411 suffix: "64",
412 },
413 },
414 symlinks: ["foo_link_"],
415 symlink_preferred_arch: true,
416 system_shared_libs: [],
417 static_executable: true,
418 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700419 apex_available: [ "myapex", "com.android.gki.*" ],
420 }
421
422 apex {
423 name: "com.android.gki.fake",
424 binaries: ["foo"],
425 key: "myapex.key",
426 file_contexts: ":myapex-file_contexts",
Alex Light3d673592019-01-18 14:37:31 -0800427 }
428
Paul Duffindddd5462020-04-07 15:25:44 +0100429 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900430 name: "mylib2",
431 srcs: ["mylib.cpp"],
432 system_shared_libs: [],
433 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900434 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900435 static_libs: ["libstatic"],
436 // TODO: remove //apex_available:platform
437 apex_available: [
438 "//apex_available:platform",
439 "myapex",
440 ],
441 }
442
Paul Duffindddd5462020-04-07 15:25:44 +0100443 cc_prebuilt_library_shared {
444 name: "mylib2",
445 srcs: ["prebuilt.so"],
446 // TODO: remove //apex_available:platform
447 apex_available: [
448 "//apex_available:platform",
449 "myapex",
450 ],
451 }
452
Jiyong Park9918e1a2020-03-17 19:16:40 +0900453 cc_library_static {
454 name: "libstatic",
455 srcs: ["mylib.cpp"],
456 system_shared_libs: [],
457 stl: "none",
458 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000459 // TODO: remove //apex_available:platform
460 apex_available: [
461 "//apex_available:platform",
462 "myapex",
463 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900464 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900465
466 java_library {
467 name: "myjar",
468 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900469 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900470 sdk_version: "none",
471 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900472 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900473 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000474 // TODO: remove //apex_available:platform
475 apex_available: [
476 "//apex_available:platform",
477 "myapex",
478 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900479 }
480
Jiyong Park77acec62020-06-01 21:39:15 +0900481 dex_import {
482 name: "myjar_dex",
483 jars: ["prebuilt.jar"],
484 apex_available: [
485 "//apex_available:platform",
486 "myapex",
487 ],
488 }
489
Jiyong Park7f7766d2019-07-25 22:02:35 +0900490 java_library {
491 name: "myotherjar",
492 srcs: ["foo/bar/MyClass.java"],
493 sdk_version: "none",
494 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900495 // TODO: remove //apex_available:platform
496 apex_available: [
497 "//apex_available:platform",
498 "myapex",
499 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900501
502 java_library {
503 name: "mysharedjar",
504 srcs: ["foo/bar/MyClass.java"],
505 sdk_version: "none",
506 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900507 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900508 `)
509
Sundong Ahnabb64432019-10-22 13:58:29 +0900510 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900511
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900512 // Make sure that Android.mk is created
513 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
514 data := android.AndroidMkDataForTest(t, config, "", ab)
515 var builder strings.Builder
516 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
517
518 androidMk := builder.String()
519 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
520 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
521
Jiyong Park42cca6c2019-04-01 11:15:50 +0900522 optFlags := apexRule.Args["opt_flags"]
523 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700524 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900525 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900526
Jiyong Park25fc6a92018-11-18 18:02:45 +0900527 copyCmds := apexRule.Args["copy_commands"]
528
529 // Ensure that main rule creates an output
530 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
531
532 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700533 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
534 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
535 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900536
537 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700538 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
539 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900540
541 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800542 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
543 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900544 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900545 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900546 // .. but not for java libs
547 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900548 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800549
Colin Cross7113d202019-11-20 16:39:12 -0800550 // Ensure that the platform variant ends with _shared or _common
551 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
552 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900553 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
554 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
556
557 // Ensure that dynamic dependency to java libs are not included
558 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800559
560 // Ensure that all symlinks are present.
561 found_foo_link_64 := false
562 found_foo := false
563 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900564 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800565 if strings.HasSuffix(cmd, "bin/foo") {
566 found_foo = true
567 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
568 found_foo_link_64 = true
569 }
570 }
571 }
572 good := found_foo && found_foo_link_64
573 if !good {
574 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
575 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900576
Sundong Ahnabb64432019-10-22 13:58:29 +0900577 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700578 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900579 if len(noticeInputs) != 3 {
580 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900581 }
582 ensureListContains(t, noticeInputs, "NOTICE")
583 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900584 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900585
Artur Satayeva8bd1132020-04-27 18:07:06 +0100586 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100587 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
588 ensureListContains(t, fullDepsInfo, " mylib(minSdkVersion:(no version)) <- myapex")
589 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
590 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
591 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100592
593 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100594 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
595 ensureListContains(t, flatDepsInfo, "mylib(minSdkVersion:(no version))")
596 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
597 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
598 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800599}
600
Jooyung Hanf21c7972019-12-16 22:32:06 +0900601func TestDefaults(t *testing.T) {
602 ctx, _ := testApex(t, `
603 apex_defaults {
604 name: "myapex-defaults",
605 key: "myapex.key",
606 prebuilts: ["myetc"],
607 native_shared_libs: ["mylib"],
608 java_libs: ["myjar"],
609 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900610 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800611 bpfs: ["bpf"],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900612 }
613
614 prebuilt_etc {
615 name: "myetc",
616 src: "myprebuilt",
617 }
618
619 apex {
620 name: "myapex",
621 defaults: ["myapex-defaults"],
622 }
623
624 apex_key {
625 name: "myapex.key",
626 public_key: "testkey.avbpubkey",
627 private_key: "testkey.pem",
628 }
629
630 cc_library {
631 name: "mylib",
632 system_shared_libs: [],
633 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000634 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900635 }
636
637 java_library {
638 name: "myjar",
639 srcs: ["foo/bar/MyClass.java"],
640 sdk_version: "none",
641 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000642 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900643 }
644
645 android_app {
646 name: "AppFoo",
647 srcs: ["foo/bar/MyClass.java"],
648 sdk_version: "none",
649 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000650 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900651 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900652
653 runtime_resource_overlay {
654 name: "rro",
655 theme: "blue",
656 }
657
markchien2f59ec92020-09-02 16:23:38 +0800658 bpf {
659 name: "bpf",
660 srcs: ["bpf.c", "bpf2.c"],
661 }
662
Jooyung Hanf21c7972019-12-16 22:32:06 +0900663 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000664 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900665 "etc/myetc",
666 "javalib/myjar.jar",
667 "lib64/mylib.so",
668 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900669 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800670 "etc/bpf/bpf.o",
671 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900672 })
673}
674
Jooyung Han01a3ee22019-11-02 02:52:25 +0900675func TestApexManifest(t *testing.T) {
676 ctx, _ := testApex(t, `
677 apex {
678 name: "myapex",
679 key: "myapex.key",
680 }
681
682 apex_key {
683 name: "myapex.key",
684 public_key: "testkey.avbpubkey",
685 private_key: "testkey.pem",
686 }
687 `)
688
689 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900690 args := module.Rule("apexRule").Args
691 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
692 t.Error("manifest should be apex_manifest.pb, but " + manifest)
693 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900694}
695
Alex Light5098a612018-11-29 17:12:15 -0800696func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700697 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800698 apex {
699 name: "myapex",
700 key: "myapex.key",
701 payload_type: "zip",
702 native_shared_libs: ["mylib"],
703 }
704
705 apex_key {
706 name: "myapex.key",
707 public_key: "testkey.avbpubkey",
708 private_key: "testkey.pem",
709 }
710
711 cc_library {
712 name: "mylib",
713 srcs: ["mylib.cpp"],
714 shared_libs: ["mylib2"],
715 system_shared_libs: [],
716 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000717 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800718 }
719
720 cc_library {
721 name: "mylib2",
722 srcs: ["mylib.cpp"],
723 system_shared_libs: [],
724 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000725 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800726 }
727 `)
728
Sundong Ahnabb64432019-10-22 13:58:29 +0900729 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800730 copyCmds := zipApexRule.Args["copy_commands"]
731
732 // Ensure that main rule creates an output
733 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
734
735 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700736 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800737
738 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700739 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800740
741 // Ensure that both direct and indirect deps are copied into apex
742 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
743 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900744}
745
746func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700747 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900748 apex {
749 name: "myapex",
750 key: "myapex.key",
751 native_shared_libs: ["mylib", "mylib3"],
752 }
753
754 apex_key {
755 name: "myapex.key",
756 public_key: "testkey.avbpubkey",
757 private_key: "testkey.pem",
758 }
759
760 cc_library {
761 name: "mylib",
762 srcs: ["mylib.cpp"],
763 shared_libs: ["mylib2", "mylib3"],
764 system_shared_libs: [],
765 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000766 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900767 }
768
769 cc_library {
770 name: "mylib2",
771 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900772 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900773 system_shared_libs: [],
774 stl: "none",
775 stubs: {
776 versions: ["1", "2", "3"],
777 },
778 }
779
780 cc_library {
781 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900782 srcs: ["mylib.cpp"],
783 shared_libs: ["mylib4"],
784 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900785 stl: "none",
786 stubs: {
787 versions: ["10", "11", "12"],
788 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000789 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900790 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900791
792 cc_library {
793 name: "mylib4",
794 srcs: ["mylib.cpp"],
795 system_shared_libs: [],
796 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000797 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900798 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900799 `)
800
Sundong Ahnabb64432019-10-22 13:58:29 +0900801 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900802 copyCmds := apexRule.Args["copy_commands"]
803
804 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800805 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900806
807 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800808 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900809
810 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800811 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900812
Colin Crossaede88c2020-08-11 12:17:01 -0700813 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900814
815 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900816 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900817 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900818 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900819
820 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700821 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900822 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700823 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900824
825 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900826 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900827 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900828
829 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900830 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900831
Jooyung Hana57af4a2020-01-23 05:36:59 +0000832 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900833 "lib64/mylib.so",
834 "lib64/mylib3.so",
835 "lib64/mylib4.so",
836 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900837}
838
Colin Cross7812fd32020-09-25 12:35:10 -0700839func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
840 t.Parallel()
841 ctx, _ := testApex(t, `
842 apex {
843 name: "myapex",
844 key: "myapex.key",
845 native_shared_libs: ["mylib", "mylib3"],
846 min_sdk_version: "29",
847 }
848
849 apex_key {
850 name: "myapex.key",
851 public_key: "testkey.avbpubkey",
852 private_key: "testkey.pem",
853 }
854
855 cc_library {
856 name: "mylib",
857 srcs: ["mylib.cpp"],
858 shared_libs: ["mylib2", "mylib3"],
859 system_shared_libs: [],
860 stl: "none",
861 apex_available: [ "myapex" ],
862 min_sdk_version: "28",
863 }
864
865 cc_library {
866 name: "mylib2",
867 srcs: ["mylib.cpp"],
868 cflags: ["-include mylib.h"],
869 system_shared_libs: [],
870 stl: "none",
871 stubs: {
872 versions: ["28", "29", "30", "current"],
873 },
874 min_sdk_version: "28",
875 }
876
877 cc_library {
878 name: "mylib3",
879 srcs: ["mylib.cpp"],
880 shared_libs: ["mylib4"],
881 system_shared_libs: [],
882 stl: "none",
883 stubs: {
884 versions: ["28", "29", "30", "current"],
885 },
886 apex_available: [ "myapex" ],
887 min_sdk_version: "28",
888 }
889
890 cc_library {
891 name: "mylib4",
892 srcs: ["mylib.cpp"],
893 system_shared_libs: [],
894 stl: "none",
895 apex_available: [ "myapex" ],
896 min_sdk_version: "28",
897 }
898 `)
899
900 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
901 copyCmds := apexRule.Args["copy_commands"]
902
903 // Ensure that direct non-stubs dep is always included
904 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
905
906 // Ensure that indirect stubs dep is not included
907 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
908
909 // Ensure that direct stubs dep is included
910 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
911
912 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
913
914 // Ensure that mylib is linking with the version 29 stubs for mylib2
915 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_29/mylib2.so")
916 // ... and not linking to the non-stub (impl) variant of mylib2
917 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
918
919 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
920 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
921 // .. and not linking to the stubs variant of mylib3
922 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
923
924 // Ensure that stubs libs are built without -include flags
925 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
926 ensureNotContains(t, mylib2Cflags, "-include ")
927
928 // Ensure that genstub is invoked with --apex
929 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_29").Rule("genStubSrc").Args["flags"])
930
931 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
932 "lib64/mylib.so",
933 "lib64/mylib3.so",
934 "lib64/mylib4.so",
935 })
936}
937
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900938func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700939 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900940 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900941 name: "myapex2",
942 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900943 native_shared_libs: ["mylib"],
944 }
945
946 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900947 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900948 public_key: "testkey.avbpubkey",
949 private_key: "testkey.pem",
950 }
951
952 cc_library {
953 name: "mylib",
954 srcs: ["mylib.cpp"],
955 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900956 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900957 system_shared_libs: [],
958 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000959 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900960 }
961
962 cc_library {
963 name: "libfoo",
964 srcs: ["mylib.cpp"],
965 shared_libs: ["libbar"],
966 system_shared_libs: [],
967 stl: "none",
968 stubs: {
969 versions: ["10", "20", "30"],
970 },
971 }
972
973 cc_library {
974 name: "libbar",
975 srcs: ["mylib.cpp"],
976 system_shared_libs: [],
977 stl: "none",
978 }
979
Jiyong Park678c8812020-02-07 17:25:49 +0900980 cc_library_static {
981 name: "libbaz",
982 srcs: ["mylib.cpp"],
983 system_shared_libs: [],
984 stl: "none",
985 apex_available: [ "myapex2" ],
986 }
987
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900988 `)
989
Jiyong Park83dc74b2020-01-14 18:38:44 +0900990 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900991 copyCmds := apexRule.Args["copy_commands"]
992
993 // Ensure that direct non-stubs dep is always included
994 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
995
996 // Ensure that indirect stubs dep is not included
997 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
998
999 // Ensure that dependency of stubs is not included
1000 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1001
Colin Crossaede88c2020-08-11 12:17:01 -07001002 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001003
1004 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001005 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001006 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001007 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001008
Jiyong Park3ff16992019-12-27 14:11:47 +09001009 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001010
1011 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1012 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001013
Artur Satayeva8bd1132020-04-27 18:07:06 +01001014 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001015 ensureListContains(t, fullDepsInfo, " mylib(minSdkVersion:(no version)) <- myapex2")
1016 ensureListContains(t, fullDepsInfo, " libbaz(minSdkVersion:(no version)) <- mylib")
1017 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001018
Artur Satayeva8bd1132020-04-27 18:07:06 +01001019 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001020 ensureListContains(t, flatDepsInfo, "mylib(minSdkVersion:(no version))")
1021 ensureListContains(t, flatDepsInfo, "libbaz(minSdkVersion:(no version))")
1022 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001023}
1024
Jooyung Hand3639552019-08-09 12:57:43 +09001025func TestApexWithRuntimeLibsDependency(t *testing.T) {
1026 /*
1027 myapex
1028 |
1029 v (runtime_libs)
1030 mylib ------+------> libfoo [provides stub]
1031 |
1032 `------> libbar
1033 */
1034 ctx, _ := testApex(t, `
1035 apex {
1036 name: "myapex",
1037 key: "myapex.key",
1038 native_shared_libs: ["mylib"],
1039 }
1040
1041 apex_key {
1042 name: "myapex.key",
1043 public_key: "testkey.avbpubkey",
1044 private_key: "testkey.pem",
1045 }
1046
1047 cc_library {
1048 name: "mylib",
1049 srcs: ["mylib.cpp"],
1050 runtime_libs: ["libfoo", "libbar"],
1051 system_shared_libs: [],
1052 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001053 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001054 }
1055
1056 cc_library {
1057 name: "libfoo",
1058 srcs: ["mylib.cpp"],
1059 system_shared_libs: [],
1060 stl: "none",
1061 stubs: {
1062 versions: ["10", "20", "30"],
1063 },
1064 }
1065
1066 cc_library {
1067 name: "libbar",
1068 srcs: ["mylib.cpp"],
1069 system_shared_libs: [],
1070 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001071 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001072 }
1073
1074 `)
1075
Sundong Ahnabb64432019-10-22 13:58:29 +09001076 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001077 copyCmds := apexRule.Args["copy_commands"]
1078
1079 // Ensure that direct non-stubs dep is always included
1080 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1081
1082 // Ensure that indirect stubs dep is not included
1083 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1084
1085 // Ensure that runtime_libs dep in included
1086 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1087
Sundong Ahnabb64432019-10-22 13:58:29 +09001088 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001089 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1090 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001091
1092}
1093
Jooyung Han8ce8db92020-05-15 19:05:05 +09001094func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
1095 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
1096 bp := `
1097 apex {
1098 name: "com.android.runtime",
1099 key: "com.android.runtime.key",
1100 native_shared_libs: ["libc"],
1101 }
1102
1103 apex_key {
1104 name: "com.android.runtime.key",
1105 public_key: "testkey.avbpubkey",
1106 private_key: "testkey.pem",
1107 }
1108
1109 cc_library {
1110 name: "libc",
1111 no_libcrt: true,
1112 nocrt: true,
1113 stl: "none",
1114 system_shared_libs: [],
1115 stubs: { versions: ["1"] },
1116 apex_available: ["com.android.runtime"],
1117
1118 sanitize: {
1119 hwaddress: true,
1120 }
1121 }
1122
1123 cc_prebuilt_library_shared {
1124 name: "libclang_rt.hwasan-aarch64-android",
1125 no_libcrt: true,
1126 nocrt: true,
1127 stl: "none",
1128 system_shared_libs: [],
1129 srcs: [""],
1130 stubs: { versions: ["1"] },
1131
1132 sanitize: {
1133 never: true,
1134 },
1135 }
1136 `
1137 // override bp to use hard-coded names: com.android.runtime and libc
1138 fs["Android.bp"] = []byte(bp)
1139 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
1140 })
1141
1142 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1143 "lib64/bionic/libc.so",
1144 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1145 })
1146
1147 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1148
1149 installed := hwasan.Description("install libclang_rt.hwasan")
1150 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1151
1152 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1153 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1154 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1155}
1156
1157func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
1158 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
1159 bp := `
1160 apex {
1161 name: "com.android.runtime",
1162 key: "com.android.runtime.key",
1163 native_shared_libs: ["libc"],
1164 }
1165
1166 apex_key {
1167 name: "com.android.runtime.key",
1168 public_key: "testkey.avbpubkey",
1169 private_key: "testkey.pem",
1170 }
1171
1172 cc_library {
1173 name: "libc",
1174 no_libcrt: true,
1175 nocrt: true,
1176 stl: "none",
1177 system_shared_libs: [],
1178 stubs: { versions: ["1"] },
1179 apex_available: ["com.android.runtime"],
1180 }
1181
1182 cc_prebuilt_library_shared {
1183 name: "libclang_rt.hwasan-aarch64-android",
1184 no_libcrt: true,
1185 nocrt: true,
1186 stl: "none",
1187 system_shared_libs: [],
1188 srcs: [""],
1189 stubs: { versions: ["1"] },
1190
1191 sanitize: {
1192 never: true,
1193 },
1194 }
1195 `
1196 // override bp to use hard-coded names: com.android.runtime and libc
1197 fs["Android.bp"] = []byte(bp)
1198 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
1199
1200 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1201 })
1202
1203 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1204 "lib64/bionic/libc.so",
1205 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1206 })
1207
1208 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1209
1210 installed := hwasan.Description("install libclang_rt.hwasan")
1211 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1212
1213 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1214 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1215 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1216}
1217
Jooyung Han61b66e92020-03-21 14:21:46 +00001218func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1219 testcases := []struct {
1220 name string
1221 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001222 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001223 shouldLink string
1224 shouldNotLink []string
1225 }{
1226 {
Jooyung Han75568392020-03-20 04:29:24 +09001227 name: "should link to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001228 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001229 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001230 shouldLink: "30",
1231 shouldNotLink: []string{"29"},
1232 },
1233 {
1234 name: "should link to llndk#29",
Jooyung Han749dc692020-04-15 11:03:39 +09001235 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001236 apexVariant: "apex29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001237 shouldLink: "29",
1238 shouldNotLink: []string{"30"},
1239 },
1240 }
1241 for _, tc := range testcases {
1242 t.Run(tc.name, func(t *testing.T) {
1243 ctx, _ := testApex(t, `
1244 apex {
1245 name: "myapex",
1246 key: "myapex.key",
1247 use_vendor: true,
1248 native_shared_libs: ["mylib"],
Jooyung Han749dc692020-04-15 11:03:39 +09001249 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001250 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001251
Jooyung Han61b66e92020-03-21 14:21:46 +00001252 apex_key {
1253 name: "myapex.key",
1254 public_key: "testkey.avbpubkey",
1255 private_key: "testkey.pem",
1256 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001257
Jooyung Han61b66e92020-03-21 14:21:46 +00001258 cc_library {
1259 name: "mylib",
1260 srcs: ["mylib.cpp"],
1261 vendor_available: true,
1262 shared_libs: ["libbar"],
1263 system_shared_libs: [],
1264 stl: "none",
1265 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001266 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001267 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001268
Jooyung Han61b66e92020-03-21 14:21:46 +00001269 cc_library {
1270 name: "libbar",
1271 srcs: ["mylib.cpp"],
1272 system_shared_libs: [],
1273 stl: "none",
1274 stubs: { versions: ["29","30"] },
1275 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001276
Jooyung Han61b66e92020-03-21 14:21:46 +00001277 llndk_library {
1278 name: "libbar",
1279 symbol_file: "",
1280 }
1281 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07001282 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Han61b66e92020-03-21 14:21:46 +00001283 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +09001284
Jooyung Han61b66e92020-03-21 14:21:46 +00001285 // Ensure that LLNDK dep is not included
1286 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1287 "lib64/mylib.so",
1288 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001289
Jooyung Han61b66e92020-03-21 14:21:46 +00001290 // Ensure that LLNDK dep is required
1291 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1292 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1293 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001294
Colin Crossaede88c2020-08-11 12:17:01 -07001295 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001296 ensureContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
1297 for _, ver := range tc.shouldNotLink {
1298 ensureNotContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
1299 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001300
Colin Crossaede88c2020-08-11 12:17:01 -07001301 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001302 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1303 })
1304 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001305}
1306
Jiyong Park25fc6a92018-11-18 18:02:45 +09001307func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001308 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001309 apex {
1310 name: "myapex",
1311 key: "myapex.key",
1312 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
1313 }
1314
1315 apex_key {
1316 name: "myapex.key",
1317 public_key: "testkey.avbpubkey",
1318 private_key: "testkey.pem",
1319 }
1320
1321 cc_library {
1322 name: "mylib",
1323 srcs: ["mylib.cpp"],
1324 shared_libs: ["libdl#27"],
1325 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001326 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001327 }
1328
1329 cc_library_shared {
1330 name: "mylib_shared",
1331 srcs: ["mylib.cpp"],
1332 shared_libs: ["libdl#27"],
1333 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001334 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001335 }
1336
1337 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001338 name: "libBootstrap",
1339 srcs: ["mylib.cpp"],
1340 stl: "none",
1341 bootstrap: true,
1342 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001343 `)
1344
Sundong Ahnabb64432019-10-22 13:58:29 +09001345 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001346 copyCmds := apexRule.Args["copy_commands"]
1347
1348 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001349 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001350 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1351 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001352
1353 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001354 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001355
Colin Crossaede88c2020-08-11 12:17:01 -07001356 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1357 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1358 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001359
1360 // For dependency to libc
1361 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001362 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001363 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001364 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001365 // ... Cflags from stub is correctly exported to mylib
1366 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1367 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1368
1369 // For dependency to libm
1370 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001371 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001372 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001373 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001374 // ... and is not compiling with the stub
1375 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1376 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1377
1378 // For dependency to libdl
1379 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001380 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001381 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001382 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1383 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001384 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001385 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001386 // ... Cflags from stub is correctly exported to mylib
1387 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1388 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001389
1390 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001391 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1392 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1393 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1394 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001395}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001396
Jooyung Han749dc692020-04-15 11:03:39 +09001397func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001398 // there are three links between liba --> libz
Jooyung Han749dc692020-04-15 11:03:39 +09001399 // 1) myapex -> libx -> liba -> libz : this should be #29 link, but fallback to #28
1400 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001401 // 3) (platform) -> liba -> libz : this should be non-stub link
1402 ctx, _ := testApex(t, `
1403 apex {
1404 name: "myapex",
1405 key: "myapex.key",
1406 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001407 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001408 }
1409
1410 apex {
1411 name: "otherapex",
1412 key: "myapex.key",
1413 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001414 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001415 }
1416
1417 apex_key {
1418 name: "myapex.key",
1419 public_key: "testkey.avbpubkey",
1420 private_key: "testkey.pem",
1421 }
1422
1423 cc_library {
1424 name: "libx",
1425 shared_libs: ["liba"],
1426 system_shared_libs: [],
1427 stl: "none",
1428 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001429 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001430 }
1431
1432 cc_library {
1433 name: "liby",
1434 shared_libs: ["liba"],
1435 system_shared_libs: [],
1436 stl: "none",
1437 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001438 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001439 }
1440
1441 cc_library {
1442 name: "liba",
1443 shared_libs: ["libz"],
1444 system_shared_libs: [],
1445 stl: "none",
1446 apex_available: [
1447 "//apex_available:anyapex",
1448 "//apex_available:platform",
1449 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001450 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001451 }
1452
1453 cc_library {
1454 name: "libz",
1455 system_shared_libs: [],
1456 stl: "none",
1457 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001458 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001459 },
1460 }
Jooyung Han749dc692020-04-15 11:03:39 +09001461 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001462
1463 expectLink := func(from, from_variant, to, to_variant string) {
1464 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1465 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1466 }
1467 expectNoLink := func(from, from_variant, to, to_variant string) {
1468 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1469 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1470 }
1471 // platform liba is linked to non-stub version
1472 expectLink("liba", "shared", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001473 // liba in myapex is linked to #28
Colin Crossaede88c2020-08-11 12:17:01 -07001474 expectLink("liba", "shared_apex29", "libz", "shared_28")
1475 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
1476 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001477 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001478 expectLink("liba", "shared_apex30", "libz", "shared_30")
1479 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1480 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001481}
1482
Jooyung Hanaed150d2020-04-02 01:41:41 +09001483func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
1484 ctx, _ := testApex(t, `
1485 apex {
1486 name: "myapex",
1487 key: "myapex.key",
1488 native_shared_libs: ["libx"],
1489 min_sdk_version: "R",
1490 }
1491
1492 apex_key {
1493 name: "myapex.key",
1494 public_key: "testkey.avbpubkey",
1495 private_key: "testkey.pem",
1496 }
1497
1498 cc_library {
1499 name: "libx",
1500 shared_libs: ["libz"],
1501 system_shared_libs: [],
1502 stl: "none",
1503 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001504 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001505 }
1506
1507 cc_library {
1508 name: "libz",
1509 system_shared_libs: [],
1510 stl: "none",
1511 stubs: {
1512 versions: ["29", "R"],
1513 },
1514 }
1515 `, func(fs map[string][]byte, config android.Config) {
1516 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
1517 })
1518
1519 expectLink := func(from, from_variant, to, to_variant string) {
1520 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1521 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1522 }
1523 expectNoLink := func(from, from_variant, to, to_variant string) {
1524 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1525 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1526 }
Dan Albertc8060532020-07-22 22:32:17 -07001527 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001528 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1529 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001530}
1531
Jooyung Han749dc692020-04-15 11:03:39 +09001532func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001533 ctx, _ := testApex(t, `
1534 apex {
1535 name: "myapex",
1536 key: "myapex.key",
1537 native_shared_libs: ["libx"],
1538 }
1539
1540 apex_key {
1541 name: "myapex.key",
1542 public_key: "testkey.avbpubkey",
1543 private_key: "testkey.pem",
1544 }
1545
1546 cc_library {
1547 name: "libx",
1548 shared_libs: ["libz"],
1549 system_shared_libs: [],
1550 stl: "none",
1551 apex_available: [ "myapex" ],
1552 }
1553
1554 cc_library {
1555 name: "libz",
1556 system_shared_libs: [],
1557 stl: "none",
1558 stubs: {
1559 versions: ["1", "2"],
1560 },
1561 }
1562 `)
1563
1564 expectLink := func(from, from_variant, to, to_variant string) {
1565 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1566 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1567 }
1568 expectNoLink := func(from, from_variant, to, to_variant string) {
1569 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1570 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1571 }
Colin Crossaede88c2020-08-11 12:17:01 -07001572 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1573 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1574 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001575}
1576
1577func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1578 ctx, _ := testApex(t, `
1579 apex {
1580 name: "myapex",
1581 key: "myapex.key",
1582 native_shared_libs: ["libx"],
1583 }
1584
1585 apex_key {
1586 name: "myapex.key",
1587 public_key: "testkey.avbpubkey",
1588 private_key: "testkey.pem",
1589 }
1590
1591 cc_library {
1592 name: "libx",
1593 system_shared_libs: [],
1594 stl: "none",
1595 apex_available: [ "myapex" ],
1596 stubs: {
1597 versions: ["1", "2"],
1598 },
1599 }
1600
1601 cc_library {
1602 name: "libz",
1603 shared_libs: ["libx"],
1604 system_shared_libs: [],
1605 stl: "none",
1606 }
1607 `)
1608
1609 expectLink := func(from, from_variant, to, to_variant string) {
1610 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1611 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1612 }
1613 expectNoLink := func(from, from_variant, to, to_variant string) {
1614 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1615 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1616 }
1617 expectLink("libz", "shared", "libx", "shared_2")
1618 expectNoLink("libz", "shared", "libz", "shared_1")
1619 expectNoLink("libz", "shared", "libz", "shared")
1620}
1621
Jooyung Han75568392020-03-20 04:29:24 +09001622func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001623 ctx, _ := testApex(t, `
1624 apex {
1625 name: "myapex",
1626 key: "myapex.key",
1627 native_shared_libs: ["libx"],
1628 min_sdk_version: "29",
1629 }
1630
1631 apex_key {
1632 name: "myapex.key",
1633 public_key: "testkey.avbpubkey",
1634 private_key: "testkey.pem",
1635 }
1636
1637 cc_library {
1638 name: "libx",
1639 shared_libs: ["libbar"],
1640 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001641 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001642 }
1643
1644 cc_library {
1645 name: "libbar",
1646 stubs: {
1647 versions: ["29", "30"],
1648 },
1649 }
Jooyung Han75568392020-03-20 04:29:24 +09001650 `, func(fs map[string][]byte, config android.Config) {
1651 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1652 })
Jooyung Han03b51852020-02-26 22:45:42 +09001653 expectLink := func(from, from_variant, to, to_variant string) {
1654 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1655 libFlags := ld.Args["libFlags"]
1656 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1657 }
Colin Crossaede88c2020-08-11 12:17:01 -07001658 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001659}
1660
Jooyung Han75568392020-03-20 04:29:24 +09001661func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001662 ctx, _ := testApex(t, `
1663 apex {
1664 name: "myapex",
1665 key: "myapex.key",
1666 native_shared_libs: ["libx"],
1667 min_sdk_version: "29",
1668 }
1669
1670 apex_key {
1671 name: "myapex.key",
1672 public_key: "testkey.avbpubkey",
1673 private_key: "testkey.pem",
1674 }
1675
1676 cc_library {
1677 name: "libx",
1678 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001679 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001680 }
Jooyung Han75568392020-03-20 04:29:24 +09001681 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001682
1683 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001684 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Jooyung Han03b51852020-02-26 22:45:42 +09001685 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1686 // note that platform variant is not.
1687 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1688 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Han03b51852020-02-26 22:45:42 +09001689}
1690
Jooyung Han749dc692020-04-15 11:03:39 +09001691func TestApexMinSdkVersion_ErrorIfIncompatibleStubs(t *testing.T) {
Jooyung Han75568392020-03-20 04:29:24 +09001692 testApexError(t, `"libz" .*: not found a version\(<=29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001693 apex {
1694 name: "myapex",
1695 key: "myapex.key",
1696 native_shared_libs: ["libx"],
1697 min_sdk_version: "29",
1698 }
1699
1700 apex_key {
1701 name: "myapex.key",
1702 public_key: "testkey.avbpubkey",
1703 private_key: "testkey.pem",
1704 }
1705
1706 cc_library {
1707 name: "libx",
1708 shared_libs: ["libz"],
1709 system_shared_libs: [],
1710 stl: "none",
1711 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001712 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001713 }
1714
1715 cc_library {
1716 name: "libz",
1717 system_shared_libs: [],
1718 stl: "none",
1719 stubs: {
1720 versions: ["30"],
1721 },
1722 }
Jooyung Han75568392020-03-20 04:29:24 +09001723 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001724}
Jooyung Han03b51852020-02-26 22:45:42 +09001725
Jooyung Han749dc692020-04-15 11:03:39 +09001726func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1727 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001728 apex {
1729 name: "myapex",
1730 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001731 native_shared_libs: ["mylib"],
1732 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001733 }
1734
1735 apex_key {
1736 name: "myapex.key",
1737 public_key: "testkey.avbpubkey",
1738 private_key: "testkey.pem",
1739 }
Jooyung Han749dc692020-04-15 11:03:39 +09001740
1741 cc_library {
1742 name: "mylib",
1743 srcs: ["mylib.cpp"],
1744 system_shared_libs: [],
1745 stl: "none",
1746 apex_available: [
1747 "myapex",
1748 ],
1749 min_sdk_version: "30",
1750 }
1751 `)
1752}
1753
1754func TestApexMinSdkVersion_Okay(t *testing.T) {
1755 testApex(t, `
1756 apex {
1757 name: "myapex",
1758 key: "myapex.key",
1759 native_shared_libs: ["libfoo"],
1760 java_libs: ["libbar"],
1761 min_sdk_version: "29",
1762 }
1763
1764 apex_key {
1765 name: "myapex.key",
1766 public_key: "testkey.avbpubkey",
1767 private_key: "testkey.pem",
1768 }
1769
1770 cc_library {
1771 name: "libfoo",
1772 srcs: ["mylib.cpp"],
1773 shared_libs: ["libfoo_dep"],
1774 apex_available: ["myapex"],
1775 min_sdk_version: "29",
1776 }
1777
1778 cc_library {
1779 name: "libfoo_dep",
1780 srcs: ["mylib.cpp"],
1781 apex_available: ["myapex"],
1782 min_sdk_version: "29",
1783 }
1784
1785 java_library {
1786 name: "libbar",
1787 sdk_version: "current",
1788 srcs: ["a.java"],
1789 static_libs: ["libbar_dep"],
1790 apex_available: ["myapex"],
1791 min_sdk_version: "29",
1792 }
1793
1794 java_library {
1795 name: "libbar_dep",
1796 sdk_version: "current",
1797 srcs: ["a.java"],
1798 apex_available: ["myapex"],
1799 min_sdk_version: "29",
1800 }
Jooyung Han03b51852020-02-26 22:45:42 +09001801 `)
1802}
1803
Artur Satayev8cf899a2020-04-15 17:29:42 +01001804func TestJavaStableSdkVersion(t *testing.T) {
1805 testCases := []struct {
1806 name string
1807 expectedError string
1808 bp string
1809 }{
1810 {
1811 name: "Non-updatable apex with non-stable dep",
1812 bp: `
1813 apex {
1814 name: "myapex",
1815 java_libs: ["myjar"],
1816 key: "myapex.key",
1817 }
1818 apex_key {
1819 name: "myapex.key",
1820 public_key: "testkey.avbpubkey",
1821 private_key: "testkey.pem",
1822 }
1823 java_library {
1824 name: "myjar",
1825 srcs: ["foo/bar/MyClass.java"],
1826 sdk_version: "core_platform",
1827 apex_available: ["myapex"],
1828 }
1829 `,
1830 },
1831 {
1832 name: "Updatable apex with stable dep",
1833 bp: `
1834 apex {
1835 name: "myapex",
1836 java_libs: ["myjar"],
1837 key: "myapex.key",
1838 updatable: true,
1839 min_sdk_version: "29",
1840 }
1841 apex_key {
1842 name: "myapex.key",
1843 public_key: "testkey.avbpubkey",
1844 private_key: "testkey.pem",
1845 }
1846 java_library {
1847 name: "myjar",
1848 srcs: ["foo/bar/MyClass.java"],
1849 sdk_version: "current",
1850 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001851 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001852 }
1853 `,
1854 },
1855 {
1856 name: "Updatable apex with non-stable dep",
1857 expectedError: "cannot depend on \"myjar\"",
1858 bp: `
1859 apex {
1860 name: "myapex",
1861 java_libs: ["myjar"],
1862 key: "myapex.key",
1863 updatable: true,
1864 }
1865 apex_key {
1866 name: "myapex.key",
1867 public_key: "testkey.avbpubkey",
1868 private_key: "testkey.pem",
1869 }
1870 java_library {
1871 name: "myjar",
1872 srcs: ["foo/bar/MyClass.java"],
1873 sdk_version: "core_platform",
1874 apex_available: ["myapex"],
1875 }
1876 `,
1877 },
1878 {
1879 name: "Updatable apex with non-stable transitive dep",
1880 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against non-public Android API.",
1881 bp: `
1882 apex {
1883 name: "myapex",
1884 java_libs: ["myjar"],
1885 key: "myapex.key",
1886 updatable: true,
1887 }
1888 apex_key {
1889 name: "myapex.key",
1890 public_key: "testkey.avbpubkey",
1891 private_key: "testkey.pem",
1892 }
1893 java_library {
1894 name: "myjar",
1895 srcs: ["foo/bar/MyClass.java"],
1896 sdk_version: "current",
1897 apex_available: ["myapex"],
1898 static_libs: ["transitive-jar"],
1899 }
1900 java_library {
1901 name: "transitive-jar",
1902 srcs: ["foo/bar/MyClass.java"],
1903 sdk_version: "core_platform",
1904 apex_available: ["myapex"],
1905 }
1906 `,
1907 },
1908 }
1909
1910 for _, test := range testCases {
1911 t.Run(test.name, func(t *testing.T) {
1912 if test.expectedError == "" {
1913 testApex(t, test.bp)
1914 } else {
1915 testApexError(t, test.expectedError, test.bp)
1916 }
1917 })
1918 }
1919}
1920
Jooyung Han749dc692020-04-15 11:03:39 +09001921func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
1922 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
1923 apex {
1924 name: "myapex",
1925 key: "myapex.key",
1926 native_shared_libs: ["mylib"],
1927 min_sdk_version: "29",
1928 }
1929
1930 apex_key {
1931 name: "myapex.key",
1932 public_key: "testkey.avbpubkey",
1933 private_key: "testkey.pem",
1934 }
1935
1936 cc_library {
1937 name: "mylib",
1938 srcs: ["mylib.cpp"],
1939 shared_libs: ["mylib2"],
1940 system_shared_libs: [],
1941 stl: "none",
1942 apex_available: [
1943 "myapex",
1944 ],
1945 min_sdk_version: "29",
1946 }
1947
1948 // indirect part of the apex
1949 cc_library {
1950 name: "mylib2",
1951 srcs: ["mylib.cpp"],
1952 system_shared_libs: [],
1953 stl: "none",
1954 apex_available: [
1955 "myapex",
1956 ],
1957 min_sdk_version: "30",
1958 }
1959 `)
1960}
1961
1962func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
1963 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
1964 apex {
1965 name: "myapex",
1966 key: "myapex.key",
1967 apps: ["AppFoo"],
1968 min_sdk_version: "29",
1969 }
1970
1971 apex_key {
1972 name: "myapex.key",
1973 public_key: "testkey.avbpubkey",
1974 private_key: "testkey.pem",
1975 }
1976
1977 android_app {
1978 name: "AppFoo",
1979 srcs: ["foo/bar/MyClass.java"],
1980 sdk_version: "current",
1981 min_sdk_version: "29",
1982 system_modules: "none",
1983 stl: "none",
1984 static_libs: ["bar"],
1985 apex_available: [ "myapex" ],
1986 }
1987
1988 java_library {
1989 name: "bar",
1990 sdk_version: "current",
1991 srcs: ["a.java"],
1992 apex_available: [ "myapex" ],
1993 }
1994 `)
1995}
1996
1997func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
1998 ctx, _ := testApex(t, `
1999 apex {
2000 name: "myapex",
2001 key: "myapex.key",
2002 native_shared_libs: ["mylib"],
2003 min_sdk_version: "29",
2004 }
2005
2006 apex_key {
2007 name: "myapex.key",
2008 public_key: "testkey.avbpubkey",
2009 private_key: "testkey.pem",
2010 }
2011
2012 // mylib in myapex will link to mylib2#29
2013 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2014 cc_library {
2015 name: "mylib",
2016 srcs: ["mylib.cpp"],
2017 shared_libs: ["mylib2"],
2018 system_shared_libs: [],
2019 stl: "none",
2020 apex_available: ["myapex", "otherapex"],
2021 min_sdk_version: "29",
2022 }
2023
2024 cc_library {
2025 name: "mylib2",
2026 srcs: ["mylib.cpp"],
2027 system_shared_libs: [],
2028 stl: "none",
2029 apex_available: ["otherapex"],
2030 stubs: { versions: ["29", "30"] },
2031 min_sdk_version: "30",
2032 }
2033
2034 apex {
2035 name: "otherapex",
2036 key: "myapex.key",
2037 native_shared_libs: ["mylib", "mylib2"],
2038 min_sdk_version: "30",
2039 }
2040 `)
2041 expectLink := func(from, from_variant, to, to_variant string) {
2042 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2043 libFlags := ld.Args["libFlags"]
2044 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2045 }
Colin Crossaede88c2020-08-11 12:17:01 -07002046 expectLink("mylib", "shared_apex29", "mylib2", "shared_29")
2047 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002048}
2049
Jiyong Park7c2ee712018-12-07 00:42:25 +09002050func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002051 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002052 apex {
2053 name: "myapex",
2054 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002055 native_shared_libs: ["mylib"],
2056 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002057 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002058 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09002059 }
2060
2061 apex_key {
2062 name: "myapex.key",
2063 public_key: "testkey.avbpubkey",
2064 private_key: "testkey.pem",
2065 }
2066
2067 prebuilt_etc {
2068 name: "myetc",
2069 src: "myprebuilt",
2070 sub_dir: "foo/bar",
2071 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002072
2073 cc_library {
2074 name: "mylib",
2075 srcs: ["mylib.cpp"],
2076 relative_install_path: "foo/bar",
2077 system_shared_libs: [],
2078 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002079 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002080 }
2081
2082 cc_binary {
2083 name: "mybin",
2084 srcs: ["mylib.cpp"],
2085 relative_install_path: "foo/bar",
2086 system_shared_libs: [],
2087 static_executable: true,
2088 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002089 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002090 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002091 `)
2092
Sundong Ahnabb64432019-10-22 13:58:29 +09002093 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002094 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2095
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002096 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002097 ensureListContains(t, dirs, "etc")
2098 ensureListContains(t, dirs, "etc/foo")
2099 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002100 ensureListContains(t, dirs, "lib64")
2101 ensureListContains(t, dirs, "lib64/foo")
2102 ensureListContains(t, dirs, "lib64/foo/bar")
2103 ensureListContains(t, dirs, "lib")
2104 ensureListContains(t, dirs, "lib/foo")
2105 ensureListContains(t, dirs, "lib/foo/bar")
2106
Jiyong Parkbd13e442019-03-15 18:10:35 +09002107 ensureListContains(t, dirs, "bin")
2108 ensureListContains(t, dirs, "bin/foo")
2109 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002110}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002111
Jooyung Han35155c42020-02-06 17:33:20 +09002112func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
2113 ctx, _ := testApex(t, `
2114 apex {
2115 name: "myapex",
2116 key: "myapex.key",
2117 multilib: {
2118 both: {
2119 native_shared_libs: ["mylib"],
2120 binaries: ["mybin"],
2121 },
2122 },
2123 compile_multilib: "both",
2124 native_bridge_supported: true,
2125 }
2126
2127 apex_key {
2128 name: "myapex.key",
2129 public_key: "testkey.avbpubkey",
2130 private_key: "testkey.pem",
2131 }
2132
2133 cc_library {
2134 name: "mylib",
2135 relative_install_path: "foo/bar",
2136 system_shared_libs: [],
2137 stl: "none",
2138 apex_available: [ "myapex" ],
2139 native_bridge_supported: true,
2140 }
2141
2142 cc_binary {
2143 name: "mybin",
2144 relative_install_path: "foo/bar",
2145 system_shared_libs: [],
2146 static_executable: true,
2147 stl: "none",
2148 apex_available: [ "myapex" ],
2149 native_bridge_supported: true,
2150 compile_multilib: "both", // default is "first" for binary
2151 multilib: {
2152 lib64: {
2153 suffix: "64",
2154 },
2155 },
2156 }
2157 `, withNativeBridgeEnabled)
2158 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2159 "bin/foo/bar/mybin",
2160 "bin/foo/bar/mybin64",
2161 "bin/arm/foo/bar/mybin",
2162 "bin/arm64/foo/bar/mybin64",
2163 "lib/foo/bar/mylib.so",
2164 "lib/arm/foo/bar/mylib.so",
2165 "lib64/foo/bar/mylib.so",
2166 "lib64/arm64/foo/bar/mylib.so",
2167 })
2168}
2169
Jiyong Parkda6eb592018-12-19 17:12:36 +09002170func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002171 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002172 apex {
2173 name: "myapex",
2174 key: "myapex.key",
2175 native_shared_libs: ["mylib"],
2176 use_vendor: true,
2177 }
2178
2179 apex_key {
2180 name: "myapex.key",
2181 public_key: "testkey.avbpubkey",
2182 private_key: "testkey.pem",
2183 }
2184
2185 cc_library {
2186 name: "mylib",
2187 srcs: ["mylib.cpp"],
2188 shared_libs: ["mylib2"],
2189 system_shared_libs: [],
2190 vendor_available: true,
2191 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002192 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002193 }
2194
2195 cc_library {
2196 name: "mylib2",
2197 srcs: ["mylib.cpp"],
2198 system_shared_libs: [],
2199 vendor_available: true,
2200 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002201 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002202 }
Jooyung Handc782442019-11-01 03:14:38 +09002203 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002204 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002205 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09002206
2207 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002208 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002209 for _, implicit := range i.Implicits {
2210 inputsList = append(inputsList, implicit.String())
2211 }
2212 }
2213 inputsString := strings.Join(inputsList, " ")
2214
2215 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002216 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2217 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002218
2219 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002220 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2221 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002222}
Jiyong Park16e91a02018-12-20 18:18:08 +09002223
Jooyung Han85d61762020-06-24 23:50:26 +09002224func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002225 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2226 apex {
2227 name: "myapex",
2228 key: "myapex.key",
2229 use_vendor: true,
2230 }
2231 apex_key {
2232 name: "myapex.key",
2233 public_key: "testkey.avbpubkey",
2234 private_key: "testkey.pem",
2235 }
2236 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002237 setUseVendorAllowListForTest(config, []string{""})
Jooyung Handc782442019-11-01 03:14:38 +09002238 })
Colin Cross440e0d02020-06-11 11:32:11 -07002239 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002240 testApex(t, `
2241 apex {
2242 name: "myapex",
2243 key: "myapex.key",
2244 use_vendor: true,
2245 }
2246 apex_key {
2247 name: "myapex.key",
2248 public_key: "testkey.avbpubkey",
2249 private_key: "testkey.pem",
2250 }
2251 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002252 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002253 })
2254}
2255
Jooyung Han5c998b92019-06-27 11:30:33 +09002256func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2257 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2258 apex {
2259 name: "myapex",
2260 key: "myapex.key",
2261 native_shared_libs: ["mylib"],
2262 use_vendor: true,
2263 }
2264
2265 apex_key {
2266 name: "myapex.key",
2267 public_key: "testkey.avbpubkey",
2268 private_key: "testkey.pem",
2269 }
2270
2271 cc_library {
2272 name: "mylib",
2273 srcs: ["mylib.cpp"],
2274 system_shared_libs: [],
2275 stl: "none",
2276 }
2277 `)
2278}
2279
Jooyung Han85d61762020-06-24 23:50:26 +09002280func TestVendorApex(t *testing.T) {
2281 ctx, config := testApex(t, `
2282 apex {
2283 name: "myapex",
2284 key: "myapex.key",
2285 binaries: ["mybin"],
2286 vendor: true,
2287 }
2288 apex_key {
2289 name: "myapex.key",
2290 public_key: "testkey.avbpubkey",
2291 private_key: "testkey.pem",
2292 }
2293 cc_binary {
2294 name: "mybin",
2295 vendor: true,
2296 shared_libs: ["libfoo"],
2297 }
2298 cc_library {
2299 name: "libfoo",
2300 proprietary: true,
2301 }
2302 `)
2303
2304 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2305 "bin/mybin",
2306 "lib64/libfoo.so",
2307 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2308 "lib64/libc++.so",
2309 })
2310
2311 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2312 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2313 name := apexBundle.BaseModuleName()
2314 prefix := "TARGET_"
2315 var builder strings.Builder
2316 data.Custom(&builder, name, prefix, "", data)
2317 androidMk := builder.String()
2318 ensureContains(t, androidMk, `LOCAL_MODULE_PATH := /tmp/target/product/test_device/vendor/apex`)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002319
2320 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2321 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2322 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002323}
2324
Jooyung Handf78e212020-07-22 15:54:47 +09002325func TestVendorApex_use_vndk_as_stable(t *testing.T) {
2326 ctx, _ := testApex(t, `
2327 apex {
2328 name: "myapex",
2329 key: "myapex.key",
2330 binaries: ["mybin"],
2331 vendor: true,
2332 use_vndk_as_stable: true,
2333 }
2334 apex_key {
2335 name: "myapex.key",
2336 public_key: "testkey.avbpubkey",
2337 private_key: "testkey.pem",
2338 }
2339 cc_binary {
2340 name: "mybin",
2341 vendor: true,
2342 shared_libs: ["libvndk", "libvendor"],
2343 }
2344 cc_library {
2345 name: "libvndk",
2346 vndk: {
2347 enabled: true,
2348 },
2349 vendor_available: true,
2350 }
2351 cc_library {
2352 name: "libvendor",
2353 vendor: true,
2354 }
2355 `)
2356
2357 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2358
Colin Crossaede88c2020-08-11 12:17:01 -07002359 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002360 libs := names(ldRule.Args["libFlags"])
2361 // VNDK libs(libvndk/libc++) as they are
2362 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2363 ensureListContains(t, libs, buildDir+"/.intermediates/libc++/"+vendorVariant+"_shared/libc++.so")
2364 // non-stable Vendor libs as APEX variants
Colin Crossaede88c2020-08-11 12:17:01 -07002365 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002366
2367 // VNDK libs are not included when use_vndk_as_stable: true
2368 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2369 "bin/mybin",
2370 "lib64/libvendor.so",
2371 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002372
2373 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2374 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2375 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002376}
2377
Jooyung Han8e5685d2020-09-21 11:02:57 +09002378func TestApex_withPrebuiltFirmware(t *testing.T) {
2379 testCases := []struct {
2380 name string
2381 additionalProp string
2382 }{
2383 {"system apex with prebuilt_firmware", ""},
2384 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2385 }
2386 for _, tc := range testCases {
2387 t.Run(tc.name, func(t *testing.T) {
2388 ctx, _ := testApex(t, `
2389 apex {
2390 name: "myapex",
2391 key: "myapex.key",
2392 prebuilts: ["myfirmware"],
2393 `+tc.additionalProp+`
2394 }
2395 apex_key {
2396 name: "myapex.key",
2397 public_key: "testkey.avbpubkey",
2398 private_key: "testkey.pem",
2399 }
2400 prebuilt_firmware {
2401 name: "myfirmware",
2402 src: "myfirmware.bin",
2403 filename_from_src: true,
2404 `+tc.additionalProp+`
2405 }
2406 `)
2407 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2408 "etc/firmware/myfirmware.bin",
2409 })
2410 })
2411 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002412}
2413
Jooyung Hanefb184e2020-06-25 17:14:25 +09002414func TestAndroidMk_UseVendorRequired(t *testing.T) {
2415 ctx, config := testApex(t, `
2416 apex {
2417 name: "myapex",
2418 key: "myapex.key",
2419 use_vendor: true,
2420 native_shared_libs: ["mylib"],
2421 }
2422
2423 apex_key {
2424 name: "myapex.key",
2425 public_key: "testkey.avbpubkey",
2426 private_key: "testkey.pem",
2427 }
2428
2429 cc_library {
2430 name: "mylib",
2431 vendor_available: true,
2432 apex_available: ["myapex"],
2433 }
2434 `, func(fs map[string][]byte, config android.Config) {
2435 setUseVendorAllowListForTest(config, []string{"myapex"})
2436 })
2437
2438 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2439 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2440 name := apexBundle.BaseModuleName()
2441 prefix := "TARGET_"
2442 var builder strings.Builder
2443 data.Custom(&builder, name, prefix, "", data)
2444 androidMk := builder.String()
2445 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2446}
2447
2448func TestAndroidMk_VendorApexRequired(t *testing.T) {
2449 ctx, config := testApex(t, `
2450 apex {
2451 name: "myapex",
2452 key: "myapex.key",
2453 vendor: true,
2454 native_shared_libs: ["mylib"],
2455 }
2456
2457 apex_key {
2458 name: "myapex.key",
2459 public_key: "testkey.avbpubkey",
2460 private_key: "testkey.pem",
2461 }
2462
2463 cc_library {
2464 name: "mylib",
2465 vendor_available: true,
2466 }
2467 `)
2468
2469 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2470 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2471 name := apexBundle.BaseModuleName()
2472 prefix := "TARGET_"
2473 var builder strings.Builder
2474 data.Custom(&builder, name, prefix, "", data)
2475 androidMk := builder.String()
2476 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2477}
2478
Jooyung Han2ed99d02020-06-24 23:26:26 +09002479func TestAndroidMkWritesCommonProperties(t *testing.T) {
2480 ctx, config := testApex(t, `
2481 apex {
2482 name: "myapex",
2483 key: "myapex.key",
2484 vintf_fragments: ["fragment.xml"],
2485 init_rc: ["init.rc"],
2486 }
2487 apex_key {
2488 name: "myapex.key",
2489 public_key: "testkey.avbpubkey",
2490 private_key: "testkey.pem",
2491 }
2492 cc_binary {
2493 name: "mybin",
2494 }
2495 `)
2496
2497 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2498 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2499 name := apexBundle.BaseModuleName()
2500 prefix := "TARGET_"
2501 var builder strings.Builder
2502 data.Custom(&builder, name, prefix, "", data)
2503 androidMk := builder.String()
2504 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2505 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2506}
2507
Jiyong Park16e91a02018-12-20 18:18:08 +09002508func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002509 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002510 apex {
2511 name: "myapex",
2512 key: "myapex.key",
2513 native_shared_libs: ["mylib"],
2514 }
2515
2516 apex_key {
2517 name: "myapex.key",
2518 public_key: "testkey.avbpubkey",
2519 private_key: "testkey.pem",
2520 }
2521
2522 cc_library {
2523 name: "mylib",
2524 srcs: ["mylib.cpp"],
2525 system_shared_libs: [],
2526 stl: "none",
2527 stubs: {
2528 versions: ["1", "2", "3"],
2529 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002530 apex_available: [
2531 "//apex_available:platform",
2532 "myapex",
2533 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002534 }
2535
2536 cc_binary {
2537 name: "not_in_apex",
2538 srcs: ["mylib.cpp"],
2539 static_libs: ["mylib"],
2540 static_executable: true,
2541 system_shared_libs: [],
2542 stl: "none",
2543 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002544 `)
2545
Colin Cross7113d202019-11-20 16:39:12 -08002546 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002547
2548 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002549 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002550}
Jiyong Park9335a262018-12-24 11:31:58 +09002551
2552func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002553 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002554 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002555 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002556 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002557 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002558 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002559 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09002560 }
2561
2562 cc_library {
2563 name: "mylib",
2564 srcs: ["mylib.cpp"],
2565 system_shared_libs: [],
2566 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002567 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002568 }
2569
2570 apex_key {
2571 name: "myapex.key",
2572 public_key: "testkey.avbpubkey",
2573 private_key: "testkey.pem",
2574 }
2575
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002576 android_app_certificate {
2577 name: "myapex.certificate",
2578 certificate: "testkey",
2579 }
2580
2581 android_app_certificate {
2582 name: "myapex.certificate.override",
2583 certificate: "testkey.override",
2584 }
2585
Jiyong Park9335a262018-12-24 11:31:58 +09002586 `)
2587
2588 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002589 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002590
2591 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2592 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
2593 "vendor/foo/devkeys/testkey.avbpubkey")
2594 }
2595 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
2596 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
2597 "vendor/foo/devkeys/testkey.pem")
2598 }
2599
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002600 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002601 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002602 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002603 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002604 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002605 }
2606}
Jiyong Park58e364a2019-01-19 19:24:06 +09002607
Jooyung Hanf121a652019-12-17 14:30:11 +09002608func TestCertificate(t *testing.T) {
2609 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
2610 ctx, _ := testApex(t, `
2611 apex {
2612 name: "myapex",
2613 key: "myapex.key",
2614 }
2615 apex_key {
2616 name: "myapex.key",
2617 public_key: "testkey.avbpubkey",
2618 private_key: "testkey.pem",
2619 }`)
2620 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2621 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2622 if actual := rule.Args["certificates"]; actual != expected {
2623 t.Errorf("certificates should be %q, not %q", expected, actual)
2624 }
2625 })
2626 t.Run("override when unspecified", func(t *testing.T) {
2627 ctx, _ := testApex(t, `
2628 apex {
2629 name: "myapex_keytest",
2630 key: "myapex.key",
2631 file_contexts: ":myapex-file_contexts",
2632 }
2633 apex_key {
2634 name: "myapex.key",
2635 public_key: "testkey.avbpubkey",
2636 private_key: "testkey.pem",
2637 }
2638 android_app_certificate {
2639 name: "myapex.certificate.override",
2640 certificate: "testkey.override",
2641 }`)
2642 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2643 expected := "testkey.override.x509.pem testkey.override.pk8"
2644 if actual := rule.Args["certificates"]; actual != expected {
2645 t.Errorf("certificates should be %q, not %q", expected, actual)
2646 }
2647 })
2648 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
2649 ctx, _ := testApex(t, `
2650 apex {
2651 name: "myapex",
2652 key: "myapex.key",
2653 certificate: ":myapex.certificate",
2654 }
2655 apex_key {
2656 name: "myapex.key",
2657 public_key: "testkey.avbpubkey",
2658 private_key: "testkey.pem",
2659 }
2660 android_app_certificate {
2661 name: "myapex.certificate",
2662 certificate: "testkey",
2663 }`)
2664 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2665 expected := "testkey.x509.pem testkey.pk8"
2666 if actual := rule.Args["certificates"]; actual != expected {
2667 t.Errorf("certificates should be %q, not %q", expected, actual)
2668 }
2669 })
2670 t.Run("override when specifiec as <:module>", func(t *testing.T) {
2671 ctx, _ := testApex(t, `
2672 apex {
2673 name: "myapex_keytest",
2674 key: "myapex.key",
2675 file_contexts: ":myapex-file_contexts",
2676 certificate: ":myapex.certificate",
2677 }
2678 apex_key {
2679 name: "myapex.key",
2680 public_key: "testkey.avbpubkey",
2681 private_key: "testkey.pem",
2682 }
2683 android_app_certificate {
2684 name: "myapex.certificate.override",
2685 certificate: "testkey.override",
2686 }`)
2687 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2688 expected := "testkey.override.x509.pem testkey.override.pk8"
2689 if actual := rule.Args["certificates"]; actual != expected {
2690 t.Errorf("certificates should be %q, not %q", expected, actual)
2691 }
2692 })
2693 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
2694 ctx, _ := testApex(t, `
2695 apex {
2696 name: "myapex",
2697 key: "myapex.key",
2698 certificate: "testkey",
2699 }
2700 apex_key {
2701 name: "myapex.key",
2702 public_key: "testkey.avbpubkey",
2703 private_key: "testkey.pem",
2704 }`)
2705 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2706 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2707 if actual := rule.Args["certificates"]; actual != expected {
2708 t.Errorf("certificates should be %q, not %q", expected, actual)
2709 }
2710 })
2711 t.Run("override when specified as <name>", func(t *testing.T) {
2712 ctx, _ := testApex(t, `
2713 apex {
2714 name: "myapex_keytest",
2715 key: "myapex.key",
2716 file_contexts: ":myapex-file_contexts",
2717 certificate: "testkey",
2718 }
2719 apex_key {
2720 name: "myapex.key",
2721 public_key: "testkey.avbpubkey",
2722 private_key: "testkey.pem",
2723 }
2724 android_app_certificate {
2725 name: "myapex.certificate.override",
2726 certificate: "testkey.override",
2727 }`)
2728 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2729 expected := "testkey.override.x509.pem testkey.override.pk8"
2730 if actual := rule.Args["certificates"]; actual != expected {
2731 t.Errorf("certificates should be %q, not %q", expected, actual)
2732 }
2733 })
2734}
2735
Jiyong Park58e364a2019-01-19 19:24:06 +09002736func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002737 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002738 apex {
2739 name: "myapex",
2740 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002741 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09002742 }
2743
2744 apex {
2745 name: "otherapex",
2746 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002747 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002748 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002749 }
2750
2751 apex_key {
2752 name: "myapex.key",
2753 public_key: "testkey.avbpubkey",
2754 private_key: "testkey.pem",
2755 }
2756
2757 cc_library {
2758 name: "mylib",
2759 srcs: ["mylib.cpp"],
2760 system_shared_libs: [],
2761 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002762 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002763 "myapex",
2764 "otherapex",
2765 ],
Jooyung Han24282772020-03-21 23:20:55 +09002766 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002767 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002768 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002769 cc_library {
2770 name: "mylib2",
2771 srcs: ["mylib.cpp"],
2772 system_shared_libs: [],
2773 stl: "none",
2774 apex_available: [
2775 "myapex",
2776 "otherapex",
2777 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002778 static_libs: ["mylib3"],
2779 recovery_available: true,
2780 min_sdk_version: "29",
2781 }
2782 cc_library {
2783 name: "mylib3",
2784 srcs: ["mylib.cpp"],
2785 system_shared_libs: [],
2786 stl: "none",
2787 apex_available: [
2788 "myapex",
2789 "otherapex",
2790 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002791 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002792 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002793 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002794 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002795 `)
2796
Jooyung Hanc87a0592020-03-02 17:44:33 +09002797 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002798 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002799 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han75568392020-03-20 04:29:24 +09002800 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002801
Jooyung Hanccce2f22020-03-07 03:45:53 +09002802 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002803 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002804 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09002805 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09002806 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002807
Jooyung Hanccce2f22020-03-07 03:45:53 +09002808 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002809 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002810 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09002811 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09002812 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002813
Colin Crossaede88c2020-08-11 12:17:01 -07002814 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
2815 // each variant defines additional macros to distinguish which apex variant it is built for
2816
2817 // non-APEX variant does not have __ANDROID_APEX__ defined
2818 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2819 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2820
2821 // APEX variant has __ANDROID_APEX__ defined
2822 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
2823 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2824 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2825 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2826
2827 // APEX variant has __ANDROID_APEX__ defined
2828 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
2829 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2830 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2831 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2832
2833 // recovery variant does not set __ANDROID_SDK_VERSION__
2834 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2835 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2836 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
2837
2838 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
2839 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09002840
2841 // non-APEX variant does not have __ANDROID_APEX__ defined
2842 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2843 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2844
2845 // APEX variant has __ANDROID_APEX__ defined
2846 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002847 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002848 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002849 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002850
Jooyung Hanc87a0592020-03-02 17:44:33 +09002851 // APEX variant has __ANDROID_APEX__ defined
2852 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002853 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002854 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002855 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09002856
2857 // recovery variant does not set __ANDROID_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07002858 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09002859 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2860 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002861}
Jiyong Park7e636d02019-01-28 16:16:54 +09002862
2863func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002864 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09002865 apex {
2866 name: "myapex",
2867 key: "myapex.key",
2868 native_shared_libs: ["mylib"],
2869 }
2870
2871 apex_key {
2872 name: "myapex.key",
2873 public_key: "testkey.avbpubkey",
2874 private_key: "testkey.pem",
2875 }
2876
2877 cc_library_headers {
2878 name: "mylib_headers",
2879 export_include_dirs: ["my_include"],
2880 system_shared_libs: [],
2881 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09002882 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002883 }
2884
2885 cc_library {
2886 name: "mylib",
2887 srcs: ["mylib.cpp"],
2888 system_shared_libs: [],
2889 stl: "none",
2890 header_libs: ["mylib_headers"],
2891 export_header_lib_headers: ["mylib_headers"],
2892 stubs: {
2893 versions: ["1", "2", "3"],
2894 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002895 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002896 }
2897
2898 cc_library {
2899 name: "otherlib",
2900 srcs: ["mylib.cpp"],
2901 system_shared_libs: [],
2902 stl: "none",
2903 shared_libs: ["mylib"],
2904 }
2905 `)
2906
Colin Cross7113d202019-11-20 16:39:12 -08002907 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09002908
2909 // Ensure that the include path of the header lib is exported to 'otherlib'
2910 ensureContains(t, cFlags, "-Imy_include")
2911}
Alex Light9670d332019-01-29 18:07:33 -08002912
Jiyong Park7cd10e32020-01-14 09:22:18 +09002913type fileInApex struct {
2914 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00002915 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09002916 isLink bool
2917}
2918
Jooyung Hana57af4a2020-01-23 05:36:59 +00002919func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09002920 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00002921 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09002922 copyCmds := apexRule.Args["copy_commands"]
2923 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09002924 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09002925 for _, cmd := range strings.Split(copyCmds, "&&") {
2926 cmd = strings.TrimSpace(cmd)
2927 if cmd == "" {
2928 continue
2929 }
2930 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00002931 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09002932 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09002933 switch terms[0] {
2934 case "mkdir":
2935 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09002936 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09002937 t.Fatal("copyCmds contains invalid cp command", cmd)
2938 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002939 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002940 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002941 isLink = false
2942 case "ln":
2943 if len(terms) != 3 && len(terms) != 4 {
2944 // ln LINK TARGET or ln -s LINK TARGET
2945 t.Fatal("copyCmds contains invalid ln command", cmd)
2946 }
2947 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002948 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002949 isLink = true
2950 default:
2951 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
2952 }
2953 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09002954 index := strings.Index(dst, imageApexDir)
2955 if index == -1 {
2956 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
2957 }
2958 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002959 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09002960 }
2961 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002962 return ret
2963}
2964
Jooyung Hana57af4a2020-01-23 05:36:59 +00002965func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
2966 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09002967 var failed bool
2968 var surplus []string
2969 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00002970 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09002971 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09002972 for _, expected := range files {
2973 if matched, _ := path.Match(expected, file.path); matched {
2974 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09002975 mactchFound = true
2976 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09002977 }
2978 }
Jooyung Hane6436d72020-02-27 13:31:56 +09002979 if !mactchFound {
2980 surplus = append(surplus, file.path)
2981 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002982 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002983
Jooyung Han31c470b2019-10-18 16:26:59 +09002984 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002985 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09002986 t.Log("surplus files", surplus)
2987 failed = true
2988 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002989
2990 if len(files) > len(filesMatched) {
2991 var missing []string
2992 for _, expected := range files {
2993 if !filesMatched[expected] {
2994 missing = append(missing, expected)
2995 }
2996 }
2997 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09002998 t.Log("missing files", missing)
2999 failed = true
3000 }
3001 if failed {
3002 t.Fail()
3003 }
3004}
3005
Jooyung Han344d5432019-08-23 11:17:39 +09003006func TestVndkApexCurrent(t *testing.T) {
3007 ctx, _ := testApex(t, `
3008 apex_vndk {
3009 name: "myapex",
3010 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003011 }
3012
3013 apex_key {
3014 name: "myapex.key",
3015 public_key: "testkey.avbpubkey",
3016 private_key: "testkey.pem",
3017 }
3018
3019 cc_library {
3020 name: "libvndk",
3021 srcs: ["mylib.cpp"],
3022 vendor_available: true,
3023 vndk: {
3024 enabled: true,
3025 },
3026 system_shared_libs: [],
3027 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003028 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003029 }
3030
3031 cc_library {
3032 name: "libvndksp",
3033 srcs: ["mylib.cpp"],
3034 vendor_available: true,
3035 vndk: {
3036 enabled: true,
3037 support_system_process: true,
3038 },
3039 system_shared_libs: [],
3040 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003041 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003042 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003043 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003044
Jooyung Hana57af4a2020-01-23 05:36:59 +00003045 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003046 "lib/libvndk.so",
3047 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003048 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003049 "lib64/libvndk.so",
3050 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003051 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003052 "etc/llndk.libraries.VER.txt",
3053 "etc/vndkcore.libraries.VER.txt",
3054 "etc/vndksp.libraries.VER.txt",
3055 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003056 })
Jooyung Han344d5432019-08-23 11:17:39 +09003057}
3058
3059func TestVndkApexWithPrebuilt(t *testing.T) {
3060 ctx, _ := testApex(t, `
3061 apex_vndk {
3062 name: "myapex",
3063 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003064 }
3065
3066 apex_key {
3067 name: "myapex.key",
3068 public_key: "testkey.avbpubkey",
3069 private_key: "testkey.pem",
3070 }
3071
3072 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003073 name: "libvndk",
3074 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003075 vendor_available: true,
3076 vndk: {
3077 enabled: true,
3078 },
3079 system_shared_libs: [],
3080 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003081 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003082 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003083
3084 cc_prebuilt_library_shared {
3085 name: "libvndk.arm",
3086 srcs: ["libvndk.arm.so"],
3087 vendor_available: true,
3088 vndk: {
3089 enabled: true,
3090 },
3091 enabled: false,
3092 arch: {
3093 arm: {
3094 enabled: true,
3095 },
3096 },
3097 system_shared_libs: [],
3098 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003099 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003100 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003101 `+vndkLibrariesTxtFiles("current"),
3102 withFiles(map[string][]byte{
3103 "libvndk.so": nil,
3104 "libvndk.arm.so": nil,
3105 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003106
Jooyung Hana57af4a2020-01-23 05:36:59 +00003107 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003108 "lib/libvndk.so",
3109 "lib/libvndk.arm.so",
3110 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003111 "lib/libc++.so",
3112 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003113 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003114 })
Jooyung Han344d5432019-08-23 11:17:39 +09003115}
3116
Jooyung Han39edb6c2019-11-06 16:53:07 +09003117func vndkLibrariesTxtFiles(vers ...string) (result string) {
3118 for _, v := range vers {
3119 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09003120 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003121 result += `
3122 vndk_libraries_txt {
3123 name: "` + txt + `.libraries.txt",
3124 }
3125 `
3126 }
3127 } else {
3128 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
3129 result += `
3130 prebuilt_etc {
3131 name: "` + txt + `.libraries.` + v + `.txt",
3132 src: "dummy.txt",
3133 }
3134 `
3135 }
3136 }
3137 }
3138 return
3139}
3140
Jooyung Han344d5432019-08-23 11:17:39 +09003141func TestVndkApexVersion(t *testing.T) {
3142 ctx, _ := testApex(t, `
3143 apex_vndk {
3144 name: "myapex_v27",
3145 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003146 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003147 vndk_version: "27",
3148 }
3149
3150 apex_key {
3151 name: "myapex.key",
3152 public_key: "testkey.avbpubkey",
3153 private_key: "testkey.pem",
3154 }
3155
Jooyung Han31c470b2019-10-18 16:26:59 +09003156 vndk_prebuilt_shared {
3157 name: "libvndk27",
3158 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003159 vendor_available: true,
3160 vndk: {
3161 enabled: true,
3162 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003163 target_arch: "arm64",
3164 arch: {
3165 arm: {
3166 srcs: ["libvndk27_arm.so"],
3167 },
3168 arm64: {
3169 srcs: ["libvndk27_arm64.so"],
3170 },
3171 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003172 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003173 }
3174
3175 vndk_prebuilt_shared {
3176 name: "libvndk27",
3177 version: "27",
3178 vendor_available: true,
3179 vndk: {
3180 enabled: true,
3181 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003182 target_arch: "x86_64",
3183 arch: {
3184 x86: {
3185 srcs: ["libvndk27_x86.so"],
3186 },
3187 x86_64: {
3188 srcs: ["libvndk27_x86_64.so"],
3189 },
3190 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003191 }
3192 `+vndkLibrariesTxtFiles("27"),
3193 withFiles(map[string][]byte{
3194 "libvndk27_arm.so": nil,
3195 "libvndk27_arm64.so": nil,
3196 "libvndk27_x86.so": nil,
3197 "libvndk27_x86_64.so": nil,
3198 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003199
Jooyung Hana57af4a2020-01-23 05:36:59 +00003200 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003201 "lib/libvndk27_arm.so",
3202 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003203 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003204 })
Jooyung Han344d5432019-08-23 11:17:39 +09003205}
3206
3207func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
3208 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
3209 apex_vndk {
3210 name: "myapex_v27",
3211 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003212 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003213 vndk_version: "27",
3214 }
3215 apex_vndk {
3216 name: "myapex_v27_other",
3217 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003218 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003219 vndk_version: "27",
3220 }
3221
3222 apex_key {
3223 name: "myapex.key",
3224 public_key: "testkey.avbpubkey",
3225 private_key: "testkey.pem",
3226 }
3227
3228 cc_library {
3229 name: "libvndk",
3230 srcs: ["mylib.cpp"],
3231 vendor_available: true,
3232 vndk: {
3233 enabled: true,
3234 },
3235 system_shared_libs: [],
3236 stl: "none",
3237 }
3238
3239 vndk_prebuilt_shared {
3240 name: "libvndk",
3241 version: "27",
3242 vendor_available: true,
3243 vndk: {
3244 enabled: true,
3245 },
3246 srcs: ["libvndk.so"],
3247 }
3248 `, withFiles(map[string][]byte{
3249 "libvndk.so": nil,
3250 }))
3251}
3252
Jooyung Han90eee022019-10-01 20:02:42 +09003253func TestVndkApexNameRule(t *testing.T) {
3254 ctx, _ := testApex(t, `
3255 apex_vndk {
3256 name: "myapex",
3257 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003258 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003259 }
3260 apex_vndk {
3261 name: "myapex_v28",
3262 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003263 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003264 vndk_version: "28",
3265 }
3266 apex_key {
3267 name: "myapex.key",
3268 public_key: "testkey.avbpubkey",
3269 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003270 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003271
3272 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003273 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003274 actual := proptools.String(bundle.properties.Apex_name)
3275 if !reflect.DeepEqual(actual, expected) {
3276 t.Errorf("Got '%v', expected '%v'", actual, expected)
3277 }
3278 }
3279
3280 assertApexName("com.android.vndk.vVER", "myapex")
3281 assertApexName("com.android.vndk.v28", "myapex_v28")
3282}
3283
Jooyung Han344d5432019-08-23 11:17:39 +09003284func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
3285 ctx, _ := testApex(t, `
3286 apex_vndk {
3287 name: "myapex",
3288 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003289 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003290 }
3291
3292 apex_key {
3293 name: "myapex.key",
3294 public_key: "testkey.avbpubkey",
3295 private_key: "testkey.pem",
3296 }
3297
3298 cc_library {
3299 name: "libvndk",
3300 srcs: ["mylib.cpp"],
3301 vendor_available: true,
3302 native_bridge_supported: true,
3303 host_supported: true,
3304 vndk: {
3305 enabled: true,
3306 },
3307 system_shared_libs: [],
3308 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003309 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003310 }
Jooyung Han35155c42020-02-06 17:33:20 +09003311 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003312
Jooyung Hana57af4a2020-01-23 05:36:59 +00003313 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003314 "lib/libvndk.so",
3315 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003316 "lib/libc++.so",
3317 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003318 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003319 })
Jooyung Han344d5432019-08-23 11:17:39 +09003320}
3321
3322func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
3323 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
3324 apex_vndk {
3325 name: "myapex",
3326 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003327 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003328 native_bridge_supported: true,
3329 }
3330
3331 apex_key {
3332 name: "myapex.key",
3333 public_key: "testkey.avbpubkey",
3334 private_key: "testkey.pem",
3335 }
3336
3337 cc_library {
3338 name: "libvndk",
3339 srcs: ["mylib.cpp"],
3340 vendor_available: true,
3341 native_bridge_supported: true,
3342 host_supported: true,
3343 vndk: {
3344 enabled: true,
3345 },
3346 system_shared_libs: [],
3347 stl: "none",
3348 }
3349 `)
3350}
3351
Jooyung Han31c470b2019-10-18 16:26:59 +09003352func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003353 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003354 apex_vndk {
3355 name: "myapex_v27",
3356 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003357 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003358 vndk_version: "27",
3359 }
3360
3361 apex_key {
3362 name: "myapex.key",
3363 public_key: "testkey.avbpubkey",
3364 private_key: "testkey.pem",
3365 }
3366
3367 vndk_prebuilt_shared {
3368 name: "libvndk27",
3369 version: "27",
3370 target_arch: "arm",
3371 vendor_available: true,
3372 vndk: {
3373 enabled: true,
3374 },
3375 arch: {
3376 arm: {
3377 srcs: ["libvndk27.so"],
3378 }
3379 },
3380 }
3381
3382 vndk_prebuilt_shared {
3383 name: "libvndk27",
3384 version: "27",
3385 target_arch: "arm",
3386 binder32bit: true,
3387 vendor_available: true,
3388 vndk: {
3389 enabled: true,
3390 },
3391 arch: {
3392 arm: {
3393 srcs: ["libvndk27binder32.so"],
3394 }
3395 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003396 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003397 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003398 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003399 withFiles(map[string][]byte{
3400 "libvndk27.so": nil,
3401 "libvndk27binder32.so": nil,
3402 }),
3403 withBinder32bit,
3404 withTargets(map[android.OsType][]android.Target{
3405 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003406 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3407 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003408 },
3409 }),
3410 )
3411
Jooyung Hana57af4a2020-01-23 05:36:59 +00003412 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003413 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003414 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003415 })
3416}
3417
Jooyung Han45a96772020-06-15 14:59:42 +09003418func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
3419 ctx, _ := testApex(t, `
3420 apex_vndk {
3421 name: "myapex",
3422 key: "myapex.key",
3423 file_contexts: ":myapex-file_contexts",
3424 }
3425
3426 apex_key {
3427 name: "myapex.key",
3428 public_key: "testkey.avbpubkey",
3429 private_key: "testkey.pem",
3430 }
3431
3432 cc_library {
3433 name: "libz",
3434 vendor_available: true,
3435 vndk: {
3436 enabled: true,
3437 },
3438 stubs: {
3439 symbol_file: "libz.map.txt",
3440 versions: ["30"],
3441 }
3442 }
3443 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3444 "libz.map.txt": nil,
3445 }))
3446
3447 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_image").Rule("apexManifestRule")
3448 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3449 ensureListEmpty(t, provideNativeLibs)
3450}
3451
Jooyung Hane1633032019-08-01 17:41:43 +09003452func TestDependenciesInApexManifest(t *testing.T) {
3453 ctx, _ := testApex(t, `
3454 apex {
3455 name: "myapex_nodep",
3456 key: "myapex.key",
3457 native_shared_libs: ["lib_nodep"],
3458 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003459 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003460 }
3461
3462 apex {
3463 name: "myapex_dep",
3464 key: "myapex.key",
3465 native_shared_libs: ["lib_dep"],
3466 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003467 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003468 }
3469
3470 apex {
3471 name: "myapex_provider",
3472 key: "myapex.key",
3473 native_shared_libs: ["libfoo"],
3474 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003475 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003476 }
3477
3478 apex {
3479 name: "myapex_selfcontained",
3480 key: "myapex.key",
3481 native_shared_libs: ["lib_dep", "libfoo"],
3482 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003483 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003484 }
3485
3486 apex_key {
3487 name: "myapex.key",
3488 public_key: "testkey.avbpubkey",
3489 private_key: "testkey.pem",
3490 }
3491
3492 cc_library {
3493 name: "lib_nodep",
3494 srcs: ["mylib.cpp"],
3495 system_shared_libs: [],
3496 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003497 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003498 }
3499
3500 cc_library {
3501 name: "lib_dep",
3502 srcs: ["mylib.cpp"],
3503 shared_libs: ["libfoo"],
3504 system_shared_libs: [],
3505 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003506 apex_available: [
3507 "myapex_dep",
3508 "myapex_provider",
3509 "myapex_selfcontained",
3510 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003511 }
3512
3513 cc_library {
3514 name: "libfoo",
3515 srcs: ["mytest.cpp"],
3516 stubs: {
3517 versions: ["1"],
3518 },
3519 system_shared_libs: [],
3520 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003521 apex_available: [
3522 "myapex_provider",
3523 "myapex_selfcontained",
3524 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003525 }
3526 `)
3527
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003528 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003529 var provideNativeLibs, requireNativeLibs []string
3530
Sundong Ahnabb64432019-10-22 13:58:29 +09003531 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003532 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3533 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003534 ensureListEmpty(t, provideNativeLibs)
3535 ensureListEmpty(t, requireNativeLibs)
3536
Sundong Ahnabb64432019-10-22 13:58:29 +09003537 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003538 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3539 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003540 ensureListEmpty(t, provideNativeLibs)
3541 ensureListContains(t, requireNativeLibs, "libfoo.so")
3542
Sundong Ahnabb64432019-10-22 13:58:29 +09003543 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003544 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3545 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003546 ensureListContains(t, provideNativeLibs, "libfoo.so")
3547 ensureListEmpty(t, requireNativeLibs)
3548
Sundong Ahnabb64432019-10-22 13:58:29 +09003549 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003550 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3551 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003552 ensureListContains(t, provideNativeLibs, "libfoo.so")
3553 ensureListEmpty(t, requireNativeLibs)
3554}
3555
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003556func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09003557 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003558 apex {
3559 name: "myapex",
3560 key: "myapex.key",
3561 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003562 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003563 }
3564
3565 apex_key {
3566 name: "myapex.key",
3567 public_key: "testkey.avbpubkey",
3568 private_key: "testkey.pem",
3569 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003570
3571 cc_library {
3572 name: "mylib",
3573 srcs: ["mylib.cpp"],
3574 system_shared_libs: [],
3575 stl: "none",
3576 apex_available: [
3577 "//apex_available:platform",
3578 "myapex",
3579 ],
3580 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003581 `)
3582
Sundong Ahnabb64432019-10-22 13:58:29 +09003583 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003584 apexManifestRule := module.Rule("apexManifestRule")
3585 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3586 apexRule := module.Rule("apexRule")
3587 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003588
3589 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3590 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3591 name := apexBundle.BaseModuleName()
3592 prefix := "TARGET_"
3593 var builder strings.Builder
3594 data.Custom(&builder, name, prefix, "", data)
3595 androidMk := builder.String()
3596 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3597 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003598}
3599
Alex Light0851b882019-02-07 13:20:53 -08003600func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003601 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003602 apex {
3603 name: "myapex",
3604 key: "myapex.key",
3605 native_shared_libs: ["mylib_common"],
3606 }
3607
3608 apex_key {
3609 name: "myapex.key",
3610 public_key: "testkey.avbpubkey",
3611 private_key: "testkey.pem",
3612 }
3613
3614 cc_library {
3615 name: "mylib_common",
3616 srcs: ["mylib.cpp"],
3617 system_shared_libs: [],
3618 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003619 apex_available: [
3620 "//apex_available:platform",
3621 "myapex",
3622 ],
Alex Light0851b882019-02-07 13:20:53 -08003623 }
3624 `)
3625
Sundong Ahnabb64432019-10-22 13:58:29 +09003626 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003627 apexRule := module.Rule("apexRule")
3628 copyCmds := apexRule.Args["copy_commands"]
3629
3630 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3631 t.Log("Apex was a test apex!")
3632 t.Fail()
3633 }
3634 // Ensure that main rule creates an output
3635 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3636
3637 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003638 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003639
3640 // Ensure that both direct and indirect deps are copied into apex
3641 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3642
Colin Cross7113d202019-11-20 16:39:12 -08003643 // Ensure that the platform variant ends with _shared
3644 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003645
3646 if !android.InAnyApex("mylib_common") {
3647 t.Log("Found mylib_common not in any apex!")
3648 t.Fail()
3649 }
3650}
3651
3652func TestTestApex(t *testing.T) {
3653 if android.InAnyApex("mylib_common_test") {
3654 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!")
3655 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003656 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003657 apex_test {
3658 name: "myapex",
3659 key: "myapex.key",
3660 native_shared_libs: ["mylib_common_test"],
3661 }
3662
3663 apex_key {
3664 name: "myapex.key",
3665 public_key: "testkey.avbpubkey",
3666 private_key: "testkey.pem",
3667 }
3668
3669 cc_library {
3670 name: "mylib_common_test",
3671 srcs: ["mylib.cpp"],
3672 system_shared_libs: [],
3673 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003674 // TODO: remove //apex_available:platform
3675 apex_available: [
3676 "//apex_available:platform",
3677 "myapex",
3678 ],
Alex Light0851b882019-02-07 13:20:53 -08003679 }
3680 `)
3681
Sundong Ahnabb64432019-10-22 13:58:29 +09003682 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003683 apexRule := module.Rule("apexRule")
3684 copyCmds := apexRule.Args["copy_commands"]
3685
3686 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3687 t.Log("Apex was not a test apex!")
3688 t.Fail()
3689 }
3690 // Ensure that main rule creates an output
3691 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3692
3693 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003694 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003695
3696 // Ensure that both direct and indirect deps are copied into apex
3697 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3698
Colin Cross7113d202019-11-20 16:39:12 -08003699 // Ensure that the platform variant ends with _shared
3700 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003701}
3702
Alex Light9670d332019-01-29 18:07:33 -08003703func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003704 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003705 apex {
3706 name: "myapex",
3707 key: "myapex.key",
3708 multilib: {
3709 first: {
3710 native_shared_libs: ["mylib_common"],
3711 }
3712 },
3713 target: {
3714 android: {
3715 multilib: {
3716 first: {
3717 native_shared_libs: ["mylib"],
3718 }
3719 }
3720 },
3721 host: {
3722 multilib: {
3723 first: {
3724 native_shared_libs: ["mylib2"],
3725 }
3726 }
3727 }
3728 }
3729 }
3730
3731 apex_key {
3732 name: "myapex.key",
3733 public_key: "testkey.avbpubkey",
3734 private_key: "testkey.pem",
3735 }
3736
3737 cc_library {
3738 name: "mylib",
3739 srcs: ["mylib.cpp"],
3740 system_shared_libs: [],
3741 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003742 // TODO: remove //apex_available:platform
3743 apex_available: [
3744 "//apex_available:platform",
3745 "myapex",
3746 ],
Alex Light9670d332019-01-29 18:07:33 -08003747 }
3748
3749 cc_library {
3750 name: "mylib_common",
3751 srcs: ["mylib.cpp"],
3752 system_shared_libs: [],
3753 stl: "none",
3754 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003755 // TODO: remove //apex_available:platform
3756 apex_available: [
3757 "//apex_available:platform",
3758 "myapex",
3759 ],
Alex Light9670d332019-01-29 18:07:33 -08003760 }
3761
3762 cc_library {
3763 name: "mylib2",
3764 srcs: ["mylib.cpp"],
3765 system_shared_libs: [],
3766 stl: "none",
3767 compile_multilib: "first",
3768 }
3769 `)
3770
Sundong Ahnabb64432019-10-22 13:58:29 +09003771 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003772 copyCmds := apexRule.Args["copy_commands"]
3773
3774 // Ensure that main rule creates an output
3775 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3776
3777 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003778 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3779 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3780 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003781
3782 // Ensure that both direct and indirect deps are copied into apex
3783 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3784 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3785 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3786
Colin Cross7113d202019-11-20 16:39:12 -08003787 // Ensure that the platform variant ends with _shared
3788 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3789 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3790 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003791}
Jiyong Park04480cf2019-02-06 00:16:29 +09003792
3793func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003794 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09003795 apex {
3796 name: "myapex",
3797 key: "myapex.key",
3798 binaries: ["myscript"],
3799 }
3800
3801 apex_key {
3802 name: "myapex.key",
3803 public_key: "testkey.avbpubkey",
3804 private_key: "testkey.pem",
3805 }
3806
3807 sh_binary {
3808 name: "myscript",
3809 src: "mylib.cpp",
3810 filename: "myscript.sh",
3811 sub_dir: "script",
3812 }
3813 `)
3814
Sundong Ahnabb64432019-10-22 13:58:29 +09003815 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09003816 copyCmds := apexRule.Args["copy_commands"]
3817
3818 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
3819}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003820
Jooyung Han91df2082019-11-20 01:49:42 +09003821func TestApexInVariousPartition(t *testing.T) {
3822 testcases := []struct {
3823 propName, parition, flattenedPartition string
3824 }{
3825 {"", "system", "system_ext"},
3826 {"product_specific: true", "product", "product"},
3827 {"soc_specific: true", "vendor", "vendor"},
3828 {"proprietary: true", "vendor", "vendor"},
3829 {"vendor: true", "vendor", "vendor"},
3830 {"system_ext_specific: true", "system_ext", "system_ext"},
3831 }
3832 for _, tc := range testcases {
3833 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
3834 ctx, _ := testApex(t, `
3835 apex {
3836 name: "myapex",
3837 key: "myapex.key",
3838 `+tc.propName+`
3839 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003840
Jooyung Han91df2082019-11-20 01:49:42 +09003841 apex_key {
3842 name: "myapex.key",
3843 public_key: "testkey.avbpubkey",
3844 private_key: "testkey.pem",
3845 }
3846 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003847
Jooyung Han91df2082019-11-20 01:49:42 +09003848 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3849 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
3850 actual := apex.installDir.String()
3851 if actual != expected {
3852 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
3853 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003854
Jooyung Han91df2082019-11-20 01:49:42 +09003855 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
3856 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
3857 actual = flattened.installDir.String()
3858 if actual != expected {
3859 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
3860 }
3861 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003862 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003863}
Jiyong Park67882562019-03-21 01:11:21 +09003864
Jooyung Han580eb4f2020-06-24 19:33:06 +09003865func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09003866 ctx, _ := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09003867 apex {
3868 name: "myapex",
3869 key: "myapex.key",
3870 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003871
Jooyung Han580eb4f2020-06-24 19:33:06 +09003872 apex_key {
3873 name: "myapex.key",
3874 public_key: "testkey.avbpubkey",
3875 private_key: "testkey.pem",
3876 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003877 `)
3878 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09003879 rule := module.Output("file_contexts")
3880 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
3881}
Jooyung Han54aca7b2019-11-20 02:26:02 +09003882
Jooyung Han580eb4f2020-06-24 19:33:06 +09003883func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09003884 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09003885 apex {
3886 name: "myapex",
3887 key: "myapex.key",
3888 file_contexts: "my_own_file_contexts",
3889 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003890
Jooyung Han580eb4f2020-06-24 19:33:06 +09003891 apex_key {
3892 name: "myapex.key",
3893 public_key: "testkey.avbpubkey",
3894 private_key: "testkey.pem",
3895 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003896 `, withFiles(map[string][]byte{
3897 "my_own_file_contexts": nil,
3898 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09003899}
Jooyung Han54aca7b2019-11-20 02:26:02 +09003900
Jooyung Han580eb4f2020-06-24 19:33:06 +09003901func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09003902 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09003903 apex {
3904 name: "myapex",
3905 key: "myapex.key",
3906 product_specific: true,
3907 file_contexts: "product_specific_file_contexts",
3908 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003909
Jooyung Han580eb4f2020-06-24 19:33:06 +09003910 apex_key {
3911 name: "myapex.key",
3912 public_key: "testkey.avbpubkey",
3913 private_key: "testkey.pem",
3914 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003915 `)
3916
Jooyung Han580eb4f2020-06-24 19:33:06 +09003917 ctx, _ := testApex(t, `
3918 apex {
3919 name: "myapex",
3920 key: "myapex.key",
3921 product_specific: true,
3922 file_contexts: "product_specific_file_contexts",
3923 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003924
Jooyung Han580eb4f2020-06-24 19:33:06 +09003925 apex_key {
3926 name: "myapex.key",
3927 public_key: "testkey.avbpubkey",
3928 private_key: "testkey.pem",
3929 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003930 `, withFiles(map[string][]byte{
3931 "product_specific_file_contexts": nil,
3932 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09003933 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3934 rule := module.Output("file_contexts")
3935 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
3936}
Jooyung Han54aca7b2019-11-20 02:26:02 +09003937
Jooyung Han580eb4f2020-06-24 19:33:06 +09003938func TestFileContexts_SetViaFileGroup(t *testing.T) {
3939 ctx, _ := testApex(t, `
3940 apex {
3941 name: "myapex",
3942 key: "myapex.key",
3943 product_specific: true,
3944 file_contexts: ":my-file-contexts",
3945 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003946
Jooyung Han580eb4f2020-06-24 19:33:06 +09003947 apex_key {
3948 name: "myapex.key",
3949 public_key: "testkey.avbpubkey",
3950 private_key: "testkey.pem",
3951 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003952
Jooyung Han580eb4f2020-06-24 19:33:06 +09003953 filegroup {
3954 name: "my-file-contexts",
3955 srcs: ["product_specific_file_contexts"],
3956 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003957 `, withFiles(map[string][]byte{
3958 "product_specific_file_contexts": nil,
3959 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09003960 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3961 rule := module.Output("file_contexts")
3962 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09003963}
3964
Jiyong Park67882562019-03-21 01:11:21 +09003965func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003966 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09003967 apex_key {
3968 name: "myapex.key",
3969 public_key: ":my.avbpubkey",
3970 private_key: ":my.pem",
3971 product_specific: true,
3972 }
3973
3974 filegroup {
3975 name: "my.avbpubkey",
3976 srcs: ["testkey2.avbpubkey"],
3977 }
3978
3979 filegroup {
3980 name: "my.pem",
3981 srcs: ["testkey2.pem"],
3982 }
3983 `)
3984
3985 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
3986 expected_pubkey := "testkey2.avbpubkey"
3987 actual_pubkey := apex_key.public_key_file.String()
3988 if actual_pubkey != expected_pubkey {
3989 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
3990 }
3991 expected_privkey := "testkey2.pem"
3992 actual_privkey := apex_key.private_key_file.String()
3993 if actual_privkey != expected_privkey {
3994 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
3995 }
3996}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003997
3998func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003999 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004000 prebuilt_apex {
4001 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004002 arch: {
4003 arm64: {
4004 src: "myapex-arm64.apex",
4005 },
4006 arm: {
4007 src: "myapex-arm.apex",
4008 },
4009 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004010 }
4011 `)
4012
4013 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4014
Jiyong Parkc95714e2019-03-29 14:23:10 +09004015 expectedInput := "myapex-arm64.apex"
4016 if prebuilt.inputApex.String() != expectedInput {
4017 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4018 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004019}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004020
4021func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004022 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004023 prebuilt_apex {
4024 name: "myapex",
4025 src: "myapex-arm.apex",
4026 filename: "notmyapex.apex",
4027 }
4028 `)
4029
4030 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4031
4032 expected := "notmyapex.apex"
4033 if p.installFilename != expected {
4034 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4035 }
4036}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004037
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004038func TestPrebuiltOverrides(t *testing.T) {
4039 ctx, config := testApex(t, `
4040 prebuilt_apex {
4041 name: "myapex.prebuilt",
4042 src: "myapex-arm.apex",
4043 overrides: [
4044 "myapex",
4045 ],
4046 }
4047 `)
4048
4049 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4050
4051 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09004052 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004053 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004054 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004055 }
4056}
4057
Roland Levillain630846d2019-06-26 12:48:34 +01004058func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01004059 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004060 apex_test {
4061 name: "myapex",
4062 key: "myapex.key",
4063 tests: [
4064 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004065 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004066 ],
4067 }
4068
4069 apex_key {
4070 name: "myapex.key",
4071 public_key: "testkey.avbpubkey",
4072 private_key: "testkey.pem",
4073 }
4074
Liz Kammer1c14a212020-05-12 15:26:55 -07004075 filegroup {
4076 name: "fg",
4077 srcs: [
4078 "baz",
4079 "bar/baz"
4080 ],
4081 }
4082
Roland Levillain630846d2019-06-26 12:48:34 +01004083 cc_test {
4084 name: "mytest",
4085 gtest: false,
4086 srcs: ["mytest.cpp"],
4087 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004088 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004089 system_shared_libs: [],
4090 static_executable: true,
4091 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004092 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004093 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004094
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004095 cc_library {
4096 name: "mylib",
4097 srcs: ["mylib.cpp"],
4098 system_shared_libs: [],
4099 stl: "none",
4100 }
4101
Liz Kammer5bd365f2020-05-27 15:15:11 -07004102 filegroup {
4103 name: "fg2",
4104 srcs: [
4105 "testdata/baz"
4106 ],
4107 }
4108
Roland Levillain9b5fde92019-06-28 15:41:19 +01004109 cc_test {
4110 name: "mytests",
4111 gtest: false,
4112 srcs: [
4113 "mytest1.cpp",
4114 "mytest2.cpp",
4115 "mytest3.cpp",
4116 ],
4117 test_per_src: true,
4118 relative_install_path: "test",
4119 system_shared_libs: [],
4120 static_executable: true,
4121 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004122 data: [
4123 ":fg",
4124 ":fg2",
4125 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004126 }
Roland Levillain630846d2019-06-26 12:48:34 +01004127 `)
4128
Sundong Ahnabb64432019-10-22 13:58:29 +09004129 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004130 copyCmds := apexRule.Args["copy_commands"]
4131
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004132 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004133 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004134 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004135
Liz Kammer1c14a212020-05-12 15:26:55 -07004136 //Ensure that test data are copied into apex.
4137 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4138 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4139
Roland Levillain9b5fde92019-06-28 15:41:19 +01004140 // Ensure that test deps built with `test_per_src` are copied into apex.
4141 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4142 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4143 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004144
4145 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004146 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4147 data := android.AndroidMkDataForTest(t, config, "", bundle)
4148 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004149 prefix := "TARGET_"
4150 var builder strings.Builder
4151 data.Custom(&builder, name, prefix, "", data)
4152 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004153 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4154 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4155 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4156 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004157 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004158 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004159 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004160
4161 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4162 data = android.AndroidMkDataForTest(t, config, "", flatBundle)
4163 data.Custom(&builder, name, prefix, "", data)
4164 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004165 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4166 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004167}
4168
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004169func TestInstallExtraFlattenedApexes(t *testing.T) {
4170 ctx, config := testApex(t, `
4171 apex {
4172 name: "myapex",
4173 key: "myapex.key",
4174 }
4175 apex_key {
4176 name: "myapex.key",
4177 public_key: "testkey.avbpubkey",
4178 private_key: "testkey.pem",
4179 }
4180 `, func(fs map[string][]byte, config android.Config) {
4181 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4182 })
4183 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004184 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004185 mk := android.AndroidMkDataForTest(t, config, "", ab)
4186 var builder strings.Builder
4187 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4188 androidMk := builder.String()
4189 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4190}
4191
Jooyung Han5c998b92019-06-27 11:30:33 +09004192func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004193 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09004194 apex {
4195 name: "myapex",
4196 key: "myapex.key",
4197 native_shared_libs: ["mylib"],
4198 uses: ["commonapex"],
4199 }
4200
4201 apex {
4202 name: "commonapex",
4203 key: "myapex.key",
4204 native_shared_libs: ["libcommon"],
4205 provide_cpp_shared_libs: true,
4206 }
4207
4208 apex_key {
4209 name: "myapex.key",
4210 public_key: "testkey.avbpubkey",
4211 private_key: "testkey.pem",
4212 }
4213
4214 cc_library {
4215 name: "mylib",
4216 srcs: ["mylib.cpp"],
4217 shared_libs: ["libcommon"],
4218 system_shared_libs: [],
4219 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004220 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09004221 }
4222
4223 cc_library {
4224 name: "libcommon",
4225 srcs: ["mylib_common.cpp"],
4226 system_shared_libs: [],
4227 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004228 // TODO: remove //apex_available:platform
4229 apex_available: [
4230 "//apex_available:platform",
4231 "commonapex",
4232 "myapex",
4233 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09004234 }
4235 `)
4236
Sundong Ahnabb64432019-10-22 13:58:29 +09004237 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09004238 apexRule1 := module1.Rule("apexRule")
4239 copyCmds1 := apexRule1.Args["copy_commands"]
4240
Sundong Ahnabb64432019-10-22 13:58:29 +09004241 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09004242 apexRule2 := module2.Rule("apexRule")
4243 copyCmds2 := apexRule2.Args["copy_commands"]
4244
Colin Crossaede88c2020-08-11 12:17:01 -07004245 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4246 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_apex10000")
Jooyung Han5c998b92019-06-27 11:30:33 +09004247 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
4248 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
4249 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
4250}
4251
4252func TestApexUsesFailsIfNotProvided(t *testing.T) {
4253 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
4254 apex {
4255 name: "myapex",
4256 key: "myapex.key",
4257 uses: ["commonapex"],
4258 }
4259
4260 apex {
4261 name: "commonapex",
4262 key: "myapex.key",
4263 }
4264
4265 apex_key {
4266 name: "myapex.key",
4267 public_key: "testkey.avbpubkey",
4268 private_key: "testkey.pem",
4269 }
4270 `)
4271 testApexError(t, `uses: "commonapex" is not a provider`, `
4272 apex {
4273 name: "myapex",
4274 key: "myapex.key",
4275 uses: ["commonapex"],
4276 }
4277
4278 cc_library {
4279 name: "commonapex",
4280 system_shared_libs: [],
4281 stl: "none",
4282 }
4283
4284 apex_key {
4285 name: "myapex.key",
4286 public_key: "testkey.avbpubkey",
4287 private_key: "testkey.pem",
4288 }
4289 `)
4290}
4291
4292func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
4293 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
4294 apex {
4295 name: "myapex",
4296 key: "myapex.key",
4297 use_vendor: true,
4298 uses: ["commonapex"],
4299 }
4300
4301 apex {
4302 name: "commonapex",
4303 key: "myapex.key",
4304 provide_cpp_shared_libs: true,
4305 }
4306
4307 apex_key {
4308 name: "myapex.key",
4309 public_key: "testkey.avbpubkey",
4310 private_key: "testkey.pem",
4311 }
Jooyung Handc782442019-11-01 03:14:38 +09004312 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07004313 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09004314 })
Jooyung Han5c998b92019-06-27 11:30:33 +09004315}
4316
Jooyung Hand48f3c32019-08-23 11:18:57 +09004317func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4318 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4319 apex {
4320 name: "myapex",
4321 key: "myapex.key",
4322 native_shared_libs: ["libfoo"],
4323 }
4324
4325 apex_key {
4326 name: "myapex.key",
4327 public_key: "testkey.avbpubkey",
4328 private_key: "testkey.pem",
4329 }
4330
4331 cc_library {
4332 name: "libfoo",
4333 stl: "none",
4334 system_shared_libs: [],
4335 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004336 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004337 }
4338 `)
4339 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4340 apex {
4341 name: "myapex",
4342 key: "myapex.key",
4343 java_libs: ["myjar"],
4344 }
4345
4346 apex_key {
4347 name: "myapex.key",
4348 public_key: "testkey.avbpubkey",
4349 private_key: "testkey.pem",
4350 }
4351
4352 java_library {
4353 name: "myjar",
4354 srcs: ["foo/bar/MyClass.java"],
4355 sdk_version: "none",
4356 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004357 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004358 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004359 }
4360 `)
4361}
4362
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004363func TestApexWithApps(t *testing.T) {
4364 ctx, _ := testApex(t, `
4365 apex {
4366 name: "myapex",
4367 key: "myapex.key",
4368 apps: [
4369 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09004370 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004371 ],
4372 }
4373
4374 apex_key {
4375 name: "myapex.key",
4376 public_key: "testkey.avbpubkey",
4377 private_key: "testkey.pem",
4378 }
4379
4380 android_app {
4381 name: "AppFoo",
4382 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004383 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004384 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09004385 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08004386 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004387 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004388 }
Jiyong Parkf7487312019-10-17 12:54:30 +09004389
4390 android_app {
4391 name: "AppFooPriv",
4392 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004393 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09004394 system_modules: "none",
4395 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08004396 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004397 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09004398 }
Jiyong Park8be103b2019-11-08 15:53:48 +09004399
4400 cc_library_shared {
4401 name: "libjni",
4402 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004403 shared_libs: ["libfoo"],
4404 stl: "none",
4405 system_shared_libs: [],
4406 apex_available: [ "myapex" ],
4407 sdk_version: "current",
4408 }
4409
4410 cc_library_shared {
4411 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09004412 stl: "none",
4413 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09004414 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08004415 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09004416 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004417 `)
4418
Sundong Ahnabb64432019-10-22 13:58:29 +09004419 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004420 apexRule := module.Rule("apexRule")
4421 copyCmds := apexRule.Args["copy_commands"]
4422
4423 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09004424 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004425
Colin Crossaede88c2020-08-11 12:17:01 -07004426 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004427 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09004428 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004429 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004430 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004431 // JNI libraries including transitive deps are
4432 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07004433 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004434 // ... embedded inside APK (jnilibs.zip)
4435 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
4436 // ... and not directly inside the APEX
4437 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
4438 }
Dario Frenicde2a032019-10-27 00:29:22 +01004439}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004440
Dario Frenicde2a032019-10-27 00:29:22 +01004441func TestApexWithAppImports(t *testing.T) {
4442 ctx, _ := testApex(t, `
4443 apex {
4444 name: "myapex",
4445 key: "myapex.key",
4446 apps: [
4447 "AppFooPrebuilt",
4448 "AppFooPrivPrebuilt",
4449 ],
4450 }
4451
4452 apex_key {
4453 name: "myapex.key",
4454 public_key: "testkey.avbpubkey",
4455 private_key: "testkey.pem",
4456 }
4457
4458 android_app_import {
4459 name: "AppFooPrebuilt",
4460 apk: "PrebuiltAppFoo.apk",
4461 presigned: true,
4462 dex_preopt: {
4463 enabled: false,
4464 },
Jiyong Park592a6a42020-04-21 22:34:28 +09004465 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01004466 }
4467
4468 android_app_import {
4469 name: "AppFooPrivPrebuilt",
4470 apk: "PrebuiltAppFooPriv.apk",
4471 privileged: true,
4472 presigned: true,
4473 dex_preopt: {
4474 enabled: false,
4475 },
Jooyung Han39ee1192020-03-23 20:21:11 +09004476 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09004477 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01004478 }
4479 `)
4480
Sundong Ahnabb64432019-10-22 13:58:29 +09004481 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01004482 apexRule := module.Rule("apexRule")
4483 copyCmds := apexRule.Args["copy_commands"]
4484
4485 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09004486 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
4487}
4488
4489func TestApexWithAppImportsPrefer(t *testing.T) {
4490 ctx, _ := testApex(t, `
4491 apex {
4492 name: "myapex",
4493 key: "myapex.key",
4494 apps: [
4495 "AppFoo",
4496 ],
4497 }
4498
4499 apex_key {
4500 name: "myapex.key",
4501 public_key: "testkey.avbpubkey",
4502 private_key: "testkey.pem",
4503 }
4504
4505 android_app {
4506 name: "AppFoo",
4507 srcs: ["foo/bar/MyClass.java"],
4508 sdk_version: "none",
4509 system_modules: "none",
4510 apex_available: [ "myapex" ],
4511 }
4512
4513 android_app_import {
4514 name: "AppFoo",
4515 apk: "AppFooPrebuilt.apk",
4516 filename: "AppFooPrebuilt.apk",
4517 presigned: true,
4518 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09004519 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09004520 }
4521 `, withFiles(map[string][]byte{
4522 "AppFooPrebuilt.apk": nil,
4523 }))
4524
4525 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4526 "app/AppFoo/AppFooPrebuilt.apk",
4527 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004528}
4529
Dario Freni6f3937c2019-12-20 22:58:03 +00004530func TestApexWithTestHelperApp(t *testing.T) {
4531 ctx, _ := testApex(t, `
4532 apex {
4533 name: "myapex",
4534 key: "myapex.key",
4535 apps: [
4536 "TesterHelpAppFoo",
4537 ],
4538 }
4539
4540 apex_key {
4541 name: "myapex.key",
4542 public_key: "testkey.avbpubkey",
4543 private_key: "testkey.pem",
4544 }
4545
4546 android_test_helper_app {
4547 name: "TesterHelpAppFoo",
4548 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004549 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00004550 }
4551
4552 `)
4553
4554 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4555 apexRule := module.Rule("apexRule")
4556 copyCmds := apexRule.Args["copy_commands"]
4557
4558 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
4559}
4560
Jooyung Han18020ea2019-11-13 10:50:48 +09004561func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
4562 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09004563 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09004564 apex {
4565 name: "myapex",
4566 key: "myapex.key",
4567 native_shared_libs: ["libfoo"],
4568 }
4569
4570 apex_key {
4571 name: "myapex.key",
4572 public_key: "testkey.avbpubkey",
4573 private_key: "testkey.pem",
4574 }
4575
4576 apex {
4577 name: "otherapex",
4578 key: "myapex.key",
4579 native_shared_libs: ["libfoo"],
4580 }
4581
4582 cc_defaults {
4583 name: "libfoo-defaults",
4584 apex_available: ["otherapex"],
4585 }
4586
4587 cc_library {
4588 name: "libfoo",
4589 defaults: ["libfoo-defaults"],
4590 stl: "none",
4591 system_shared_libs: [],
4592 }`)
4593}
4594
Paul Duffine52e66f2020-03-30 17:54:29 +01004595func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09004596 // libfoo is not available to myapex, but only to otherapex
4597 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
4598 apex {
4599 name: "myapex",
4600 key: "myapex.key",
4601 native_shared_libs: ["libfoo"],
4602 }
4603
4604 apex_key {
4605 name: "myapex.key",
4606 public_key: "testkey.avbpubkey",
4607 private_key: "testkey.pem",
4608 }
4609
4610 apex {
4611 name: "otherapex",
4612 key: "otherapex.key",
4613 native_shared_libs: ["libfoo"],
4614 }
4615
4616 apex_key {
4617 name: "otherapex.key",
4618 public_key: "testkey.avbpubkey",
4619 private_key: "testkey.pem",
4620 }
4621
4622 cc_library {
4623 name: "libfoo",
4624 stl: "none",
4625 system_shared_libs: [],
4626 apex_available: ["otherapex"],
4627 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004628}
Jiyong Park127b40b2019-09-30 16:04:35 +09004629
Paul Duffine52e66f2020-03-30 17:54:29 +01004630func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09004631 // libbbaz is an indirect dep
Paul Duffindf915ff2020-03-30 17:58:21 +01004632 testApexError(t, `requires "libbaz" that is not available for the APEX. Dependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07004633.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01004634.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07004635.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01004636.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07004637.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01004638.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004639 apex {
4640 name: "myapex",
4641 key: "myapex.key",
4642 native_shared_libs: ["libfoo"],
4643 }
4644
4645 apex_key {
4646 name: "myapex.key",
4647 public_key: "testkey.avbpubkey",
4648 private_key: "testkey.pem",
4649 }
4650
Jiyong Park127b40b2019-09-30 16:04:35 +09004651 cc_library {
4652 name: "libfoo",
4653 stl: "none",
4654 shared_libs: ["libbar"],
4655 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09004656 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004657 }
4658
4659 cc_library {
4660 name: "libbar",
4661 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09004662 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004663 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09004664 apex_available: ["myapex"],
4665 }
4666
4667 cc_library {
4668 name: "libbaz",
4669 stl: "none",
4670 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09004671 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004672}
Jiyong Park127b40b2019-09-30 16:04:35 +09004673
Paul Duffine52e66f2020-03-30 17:54:29 +01004674func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09004675 testApexError(t, "\"otherapex\" is not a valid module name", `
4676 apex {
4677 name: "myapex",
4678 key: "myapex.key",
4679 native_shared_libs: ["libfoo"],
4680 }
4681
4682 apex_key {
4683 name: "myapex.key",
4684 public_key: "testkey.avbpubkey",
4685 private_key: "testkey.pem",
4686 }
4687
4688 cc_library {
4689 name: "libfoo",
4690 stl: "none",
4691 system_shared_libs: [],
4692 apex_available: ["otherapex"],
4693 }`)
4694
Paul Duffine52e66f2020-03-30 17:54:29 +01004695 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004696 apex {
4697 name: "myapex",
4698 key: "myapex.key",
4699 native_shared_libs: ["libfoo", "libbar"],
4700 }
4701
4702 apex_key {
4703 name: "myapex.key",
4704 public_key: "testkey.avbpubkey",
4705 private_key: "testkey.pem",
4706 }
4707
4708 cc_library {
4709 name: "libfoo",
4710 stl: "none",
4711 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09004712 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004713 apex_available: ["myapex"],
4714 }
4715
4716 cc_library {
4717 name: "libbar",
4718 stl: "none",
4719 system_shared_libs: [],
4720 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09004721 }
4722
4723 cc_library {
4724 name: "libbaz",
4725 stl: "none",
4726 system_shared_libs: [],
4727 stubs: {
4728 versions: ["10", "20", "30"],
4729 },
Jiyong Park127b40b2019-09-30 16:04:35 +09004730 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004731}
Jiyong Park127b40b2019-09-30 16:04:35 +09004732
Jiyong Park89e850a2020-04-07 16:37:39 +09004733func TestApexAvailable_CheckForPlatform(t *testing.T) {
Paul Duffine52e66f2020-03-30 17:54:29 +01004734 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004735 apex {
4736 name: "myapex",
4737 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09004738 native_shared_libs: ["libbar", "libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004739 }
4740
4741 apex_key {
4742 name: "myapex.key",
4743 public_key: "testkey.avbpubkey",
4744 private_key: "testkey.pem",
4745 }
4746
4747 cc_library {
4748 name: "libfoo",
4749 stl: "none",
4750 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09004751 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004752 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09004753 }
4754
4755 cc_library {
4756 name: "libfoo2",
4757 stl: "none",
4758 system_shared_libs: [],
4759 shared_libs: ["libbaz"],
4760 apex_available: ["//apex_available:platform"],
4761 }
4762
4763 cc_library {
4764 name: "libbar",
4765 stl: "none",
4766 system_shared_libs: [],
4767 apex_available: ["myapex"],
4768 }
4769
4770 cc_library {
4771 name: "libbaz",
4772 stl: "none",
4773 system_shared_libs: [],
4774 apex_available: ["myapex"],
4775 stubs: {
4776 versions: ["1"],
4777 },
Jiyong Park127b40b2019-09-30 16:04:35 +09004778 }`)
4779
Jiyong Park89e850a2020-04-07 16:37:39 +09004780 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
4781 // because it depends on libbar which isn't available to platform
4782 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4783 if libfoo.NotAvailableForPlatform() != true {
4784 t.Errorf("%q shouldn't be available to platform", libfoo.String())
4785 }
4786
4787 // libfoo2 however can be available to platform because it depends on libbaz which provides
4788 // stubs
4789 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4790 if libfoo2.NotAvailableForPlatform() == true {
4791 t.Errorf("%q should be available to platform", libfoo2.String())
4792 }
Paul Duffine52e66f2020-03-30 17:54:29 +01004793}
Jiyong Parka90ca002019-10-07 15:47:24 +09004794
Paul Duffine52e66f2020-03-30 17:54:29 +01004795func TestApexAvailable_CreatedForApex(t *testing.T) {
Jiyong Park89e850a2020-04-07 16:37:39 +09004796 ctx, _ := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09004797 apex {
4798 name: "myapex",
4799 key: "myapex.key",
4800 native_shared_libs: ["libfoo"],
4801 }
4802
4803 apex_key {
4804 name: "myapex.key",
4805 public_key: "testkey.avbpubkey",
4806 private_key: "testkey.pem",
4807 }
4808
4809 cc_library {
4810 name: "libfoo",
4811 stl: "none",
4812 system_shared_libs: [],
4813 apex_available: ["myapex"],
4814 static: {
4815 apex_available: ["//apex_available:platform"],
4816 },
4817 }`)
4818
Jiyong Park89e850a2020-04-07 16:37:39 +09004819 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4820 if libfooShared.NotAvailableForPlatform() != true {
4821 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
4822 }
4823 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
4824 if libfooStatic.NotAvailableForPlatform() != false {
4825 t.Errorf("%q should be available to platform", libfooStatic.String())
4826 }
Jiyong Park127b40b2019-09-30 16:04:35 +09004827}
4828
Jiyong Park5d790c32019-11-15 18:40:32 +09004829func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004830 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09004831 apex {
4832 name: "myapex",
4833 key: "myapex.key",
4834 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004835 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09004836 }
4837
4838 override_apex {
4839 name: "override_myapex",
4840 base: "myapex",
4841 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004842 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08004843 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07004844 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09004845 }
4846
4847 apex_key {
4848 name: "myapex.key",
4849 public_key: "testkey.avbpubkey",
4850 private_key: "testkey.pem",
4851 }
4852
4853 android_app {
4854 name: "app",
4855 srcs: ["foo/bar/MyClass.java"],
4856 package_name: "foo",
4857 sdk_version: "none",
4858 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004859 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09004860 }
4861
4862 override_android_app {
4863 name: "override_app",
4864 base: "app",
4865 package_name: "bar",
4866 }
Jiyong Park20bacab2020-03-03 11:45:41 +09004867 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09004868
Jiyong Park317645e2019-12-05 13:20:58 +09004869 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
4870 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
4871 if originalVariant.GetOverriddenBy() != "" {
4872 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
4873 }
4874 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
4875 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
4876 }
4877
Jiyong Park5d790c32019-11-15 18:40:32 +09004878 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
4879 apexRule := module.Rule("apexRule")
4880 copyCmds := apexRule.Args["copy_commands"]
4881
4882 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09004883 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004884
4885 apexBundle := module.Module().(*apexBundle)
4886 name := apexBundle.Name()
4887 if name != "override_myapex" {
4888 t.Errorf("name should be \"override_myapex\", but was %q", name)
4889 }
4890
Baligh Uddin004d7172020-02-19 21:29:28 -08004891 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
4892 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
4893 }
4894
Jiyong Park20bacab2020-03-03 11:45:41 +09004895 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07004896 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09004897
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004898 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
4899 var builder strings.Builder
4900 data.Custom(&builder, name, "TARGET_", "", data)
4901 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09004902 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004903 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
4904 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004905 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004906 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09004907 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004908 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
4909 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09004910}
4911
Jooyung Han214bf372019-11-12 13:03:50 +09004912func TestLegacyAndroid10Support(t *testing.T) {
4913 ctx, _ := testApex(t, `
4914 apex {
4915 name: "myapex",
4916 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004917 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09004918 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09004919 }
4920
4921 apex_key {
4922 name: "myapex.key",
4923 public_key: "testkey.avbpubkey",
4924 private_key: "testkey.pem",
4925 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004926
4927 cc_library {
4928 name: "mylib",
4929 srcs: ["mylib.cpp"],
4930 stl: "libc++",
4931 system_shared_libs: [],
4932 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09004933 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004934 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004935 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09004936
4937 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4938 args := module.Rule("apexRule").Args
4939 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00004940 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004941
4942 // The copies of the libraries in the apex should have one more dependency than
4943 // the ones outside the apex, namely the unwinder. Ideally we should check
4944 // the dependency names directly here but for some reason the names are blank in
4945 // this test.
4946 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07004947 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004948 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
4949 if len(apexImplicits) != len(nonApexImplicits)+1 {
4950 t.Errorf("%q missing unwinder dep", lib)
4951 }
4952 }
Jooyung Han214bf372019-11-12 13:03:50 +09004953}
4954
Paul Duffin9b879592020-05-26 13:21:35 +01004955var filesForSdkLibrary = map[string][]byte{
4956 "api/current.txt": nil,
4957 "api/removed.txt": nil,
4958 "api/system-current.txt": nil,
4959 "api/system-removed.txt": nil,
4960 "api/test-current.txt": nil,
4961 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01004962
4963 // For java_sdk_library_import
4964 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01004965}
4966
Jooyung Han58f26ab2019-12-18 15:34:32 +09004967func TestJavaSDKLibrary(t *testing.T) {
4968 ctx, _ := testApex(t, `
4969 apex {
4970 name: "myapex",
4971 key: "myapex.key",
4972 java_libs: ["foo"],
4973 }
4974
4975 apex_key {
4976 name: "myapex.key",
4977 public_key: "testkey.avbpubkey",
4978 private_key: "testkey.pem",
4979 }
4980
4981 java_sdk_library {
4982 name: "foo",
4983 srcs: ["a.java"],
4984 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004985 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09004986 }
Paul Duffin9b879592020-05-26 13:21:35 +01004987 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09004988
4989 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00004990 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09004991 "javalib/foo.jar",
4992 "etc/permissions/foo.xml",
4993 })
4994 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09004995 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
4996 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09004997}
4998
Paul Duffin9b879592020-05-26 13:21:35 +01004999func TestJavaSDKLibrary_WithinApex(t *testing.T) {
5000 ctx, _ := testApex(t, `
5001 apex {
5002 name: "myapex",
5003 key: "myapex.key",
5004 java_libs: ["foo", "bar"],
5005 }
5006
5007 apex_key {
5008 name: "myapex.key",
5009 public_key: "testkey.avbpubkey",
5010 private_key: "testkey.pem",
5011 }
5012
5013 java_sdk_library {
5014 name: "foo",
5015 srcs: ["a.java"],
5016 api_packages: ["foo"],
5017 apex_available: ["myapex"],
5018 sdk_version: "none",
5019 system_modules: "none",
5020 }
5021
5022 java_library {
5023 name: "bar",
5024 srcs: ["a.java"],
5025 libs: ["foo"],
5026 apex_available: ["myapex"],
5027 sdk_version: "none",
5028 system_modules: "none",
5029 }
5030 `, withFiles(filesForSdkLibrary))
5031
5032 // java_sdk_library installs both impl jar and permission XML
5033 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5034 "javalib/bar.jar",
5035 "javalib/foo.jar",
5036 "etc/permissions/foo.xml",
5037 })
5038
5039 // The bar library should depend on the implementation jar.
5040 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5041 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5042 t.Errorf("expected %q, found %#q", expected, actual)
5043 }
5044}
5045
5046func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
5047 ctx, _ := testApex(t, `
5048 apex {
5049 name: "myapex",
5050 key: "myapex.key",
5051 java_libs: ["foo"],
5052 }
5053
5054 apex_key {
5055 name: "myapex.key",
5056 public_key: "testkey.avbpubkey",
5057 private_key: "testkey.pem",
5058 }
5059
5060 java_sdk_library {
5061 name: "foo",
5062 srcs: ["a.java"],
5063 api_packages: ["foo"],
5064 apex_available: ["myapex"],
5065 sdk_version: "none",
5066 system_modules: "none",
5067 }
5068
5069 java_library {
5070 name: "bar",
5071 srcs: ["a.java"],
5072 libs: ["foo"],
5073 sdk_version: "none",
5074 system_modules: "none",
5075 }
5076 `, withFiles(filesForSdkLibrary))
5077
5078 // java_sdk_library installs both impl jar and permission XML
5079 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5080 "javalib/foo.jar",
5081 "etc/permissions/foo.xml",
5082 })
5083
5084 // The bar library should depend on the stubs jar.
5085 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5086 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5087 t.Errorf("expected %q, found %#q", expected, actual)
5088 }
5089}
5090
Paul Duffineedc5d52020-06-12 17:46:39 +01005091func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
5092 ctx, _ := testApex(t, ``,
5093 withFiles(map[string][]byte{
5094 "apex/a.java": nil,
5095 "apex/apex_manifest.json": nil,
5096 "apex/Android.bp": []byte(`
5097 package {
5098 default_visibility: ["//visibility:private"],
5099 }
5100
5101 apex {
5102 name: "myapex",
5103 key: "myapex.key",
5104 java_libs: ["foo", "bar"],
5105 }
5106
5107 apex_key {
5108 name: "myapex.key",
5109 public_key: "testkey.avbpubkey",
5110 private_key: "testkey.pem",
5111 }
5112
5113 java_library {
5114 name: "bar",
5115 srcs: ["a.java"],
5116 libs: ["foo"],
5117 apex_available: ["myapex"],
5118 sdk_version: "none",
5119 system_modules: "none",
5120 }
5121`),
5122 "source/a.java": nil,
5123 "source/api/current.txt": nil,
5124 "source/api/removed.txt": nil,
5125 "source/Android.bp": []byte(`
5126 package {
5127 default_visibility: ["//visibility:private"],
5128 }
5129
5130 java_sdk_library {
5131 name: "foo",
5132 visibility: ["//apex"],
5133 srcs: ["a.java"],
5134 api_packages: ["foo"],
5135 apex_available: ["myapex"],
5136 sdk_version: "none",
5137 system_modules: "none",
5138 public: {
5139 enabled: true,
5140 },
5141 }
5142`),
5143 "prebuilt/a.jar": nil,
5144 "prebuilt/Android.bp": []byte(`
5145 package {
5146 default_visibility: ["//visibility:private"],
5147 }
5148
5149 java_sdk_library_import {
5150 name: "foo",
5151 visibility: ["//apex", "//source"],
5152 apex_available: ["myapex"],
5153 prefer: true,
5154 public: {
5155 jars: ["a.jar"],
5156 },
5157 }
5158`),
5159 }),
5160 )
5161
5162 // java_sdk_library installs both impl jar and permission XML
5163 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5164 "javalib/bar.jar",
5165 "javalib/foo.jar",
5166 "etc/permissions/foo.xml",
5167 })
5168
5169 // The bar library should depend on the implementation jar.
5170 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5171 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5172 t.Errorf("expected %q, found %#q", expected, actual)
5173 }
5174}
5175
5176func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5177 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5178 apex {
5179 name: "myapex",
5180 key: "myapex.key",
5181 java_libs: ["foo"],
5182 }
5183
5184 apex_key {
5185 name: "myapex.key",
5186 public_key: "testkey.avbpubkey",
5187 private_key: "testkey.pem",
5188 }
5189
5190 java_sdk_library_import {
5191 name: "foo",
5192 apex_available: ["myapex"],
5193 prefer: true,
5194 public: {
5195 jars: ["a.jar"],
5196 },
5197 }
5198
5199 `, withFiles(filesForSdkLibrary))
5200}
5201
atrost6e126252020-01-27 17:01:16 +00005202func TestCompatConfig(t *testing.T) {
5203 ctx, _ := testApex(t, `
5204 apex {
5205 name: "myapex",
5206 key: "myapex.key",
5207 prebuilts: ["myjar-platform-compat-config"],
5208 java_libs: ["myjar"],
5209 }
5210
5211 apex_key {
5212 name: "myapex.key",
5213 public_key: "testkey.avbpubkey",
5214 private_key: "testkey.pem",
5215 }
5216
5217 platform_compat_config {
5218 name: "myjar-platform-compat-config",
5219 src: ":myjar",
5220 }
5221
5222 java_library {
5223 name: "myjar",
5224 srcs: ["foo/bar/MyClass.java"],
5225 sdk_version: "none",
5226 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005227 apex_available: [ "myapex" ],
5228 }
5229 `)
5230 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5231 "etc/compatconfig/myjar-platform-compat-config.xml",
5232 "javalib/myjar.jar",
5233 })
5234}
5235
Jiyong Park479321d2019-12-16 11:47:12 +09005236func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5237 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5238 apex {
5239 name: "myapex",
5240 key: "myapex.key",
5241 java_libs: ["myjar"],
5242 }
5243
5244 apex_key {
5245 name: "myapex.key",
5246 public_key: "testkey.avbpubkey",
5247 private_key: "testkey.pem",
5248 }
5249
5250 java_library {
5251 name: "myjar",
5252 srcs: ["foo/bar/MyClass.java"],
5253 sdk_version: "none",
5254 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005255 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005256 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005257 }
5258 `)
5259}
5260
Jiyong Park7afd1072019-12-30 16:56:33 +09005261func TestCarryRequiredModuleNames(t *testing.T) {
5262 ctx, config := testApex(t, `
5263 apex {
5264 name: "myapex",
5265 key: "myapex.key",
5266 native_shared_libs: ["mylib"],
5267 }
5268
5269 apex_key {
5270 name: "myapex.key",
5271 public_key: "testkey.avbpubkey",
5272 private_key: "testkey.pem",
5273 }
5274
5275 cc_library {
5276 name: "mylib",
5277 srcs: ["mylib.cpp"],
5278 system_shared_libs: [],
5279 stl: "none",
5280 required: ["a", "b"],
5281 host_required: ["c", "d"],
5282 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005283 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005284 }
5285 `)
5286
5287 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
5288 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
5289 name := apexBundle.BaseModuleName()
5290 prefix := "TARGET_"
5291 var builder strings.Builder
5292 data.Custom(&builder, name, prefix, "", data)
5293 androidMk := builder.String()
5294 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
5295 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
5296 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
5297}
5298
Jiyong Park7cd10e32020-01-14 09:22:18 +09005299func TestSymlinksFromApexToSystem(t *testing.T) {
5300 bp := `
5301 apex {
5302 name: "myapex",
5303 key: "myapex.key",
5304 native_shared_libs: ["mylib"],
5305 java_libs: ["myjar"],
5306 }
5307
Jiyong Park9d677202020-02-19 16:29:35 +09005308 apex {
5309 name: "myapex.updatable",
5310 key: "myapex.key",
5311 native_shared_libs: ["mylib"],
5312 java_libs: ["myjar"],
5313 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09005314 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09005315 }
5316
Jiyong Park7cd10e32020-01-14 09:22:18 +09005317 apex_key {
5318 name: "myapex.key",
5319 public_key: "testkey.avbpubkey",
5320 private_key: "testkey.pem",
5321 }
5322
5323 cc_library {
5324 name: "mylib",
5325 srcs: ["mylib.cpp"],
5326 shared_libs: ["myotherlib"],
5327 system_shared_libs: [],
5328 stl: "none",
5329 apex_available: [
5330 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005331 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005332 "//apex_available:platform",
5333 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005334 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005335 }
5336
5337 cc_library {
5338 name: "myotherlib",
5339 srcs: ["mylib.cpp"],
5340 system_shared_libs: [],
5341 stl: "none",
5342 apex_available: [
5343 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005344 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005345 "//apex_available:platform",
5346 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005347 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005348 }
5349
5350 java_library {
5351 name: "myjar",
5352 srcs: ["foo/bar/MyClass.java"],
5353 sdk_version: "none",
5354 system_modules: "none",
5355 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09005356 apex_available: [
5357 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005358 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005359 "//apex_available:platform",
5360 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005361 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005362 }
5363
5364 java_library {
5365 name: "myotherjar",
5366 srcs: ["foo/bar/MyClass.java"],
5367 sdk_version: "none",
5368 system_modules: "none",
5369 apex_available: [
5370 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005371 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005372 "//apex_available:platform",
5373 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005374 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005375 }
5376 `
5377
5378 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
5379 for _, f := range files {
5380 if f.path == file {
5381 if f.isLink {
5382 t.Errorf("%q is not a real file", file)
5383 }
5384 return
5385 }
5386 }
5387 t.Errorf("%q is not found", file)
5388 }
5389
5390 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
5391 for _, f := range files {
5392 if f.path == file {
5393 if !f.isLink {
5394 t.Errorf("%q is not a symlink", file)
5395 }
5396 return
5397 }
5398 }
5399 t.Errorf("%q is not found", file)
5400 }
5401
Jiyong Park9d677202020-02-19 16:29:35 +09005402 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
5403 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09005404 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005405 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005406 ensureRealfileExists(t, files, "javalib/myjar.jar")
5407 ensureRealfileExists(t, files, "lib64/mylib.so")
5408 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5409
Jiyong Park9d677202020-02-19 16:29:35 +09005410 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5411 ensureRealfileExists(t, files, "javalib/myjar.jar")
5412 ensureRealfileExists(t, files, "lib64/mylib.so")
5413 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5414
5415 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09005416 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005417 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005418 ensureRealfileExists(t, files, "javalib/myjar.jar")
5419 ensureRealfileExists(t, files, "lib64/mylib.so")
5420 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09005421
5422 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5423 ensureRealfileExists(t, files, "javalib/myjar.jar")
5424 ensureRealfileExists(t, files, "lib64/mylib.so")
5425 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09005426}
5427
Yo Chiange8128052020-07-23 20:09:18 +08005428func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
5429 ctx, config := testApex(t, `
5430 apex {
5431 name: "myapex",
5432 key: "myapex.key",
5433 native_shared_libs: ["mylib"],
5434 }
5435
5436 apex_key {
5437 name: "myapex.key",
5438 public_key: "testkey.avbpubkey",
5439 private_key: "testkey.pem",
5440 }
5441
5442 cc_library_shared {
5443 name: "mylib",
5444 srcs: ["mylib.cpp"],
5445 shared_libs: ["myotherlib"],
5446 system_shared_libs: [],
5447 stl: "none",
5448 apex_available: [
5449 "myapex",
5450 "//apex_available:platform",
5451 ],
5452 }
5453
5454 cc_prebuilt_library_shared {
5455 name: "myotherlib",
5456 srcs: ["prebuilt.so"],
5457 system_shared_libs: [],
5458 stl: "none",
5459 apex_available: [
5460 "myapex",
5461 "//apex_available:platform",
5462 ],
5463 }
5464 `)
5465
5466 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
5467 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
5468 var builder strings.Builder
5469 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
5470 androidMk := builder.String()
5471 // `myotherlib` is added to `myapex` as symlink
5472 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
5473 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
5474 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
5475 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
5476 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += mylib.myapex myotherlib apex_manifest.pb.myapex apex_pubkey.myapex\n")
5477}
5478
Jooyung Han643adc42020-02-27 13:50:06 +09005479func TestApexWithJniLibs(t *testing.T) {
5480 ctx, _ := testApex(t, `
5481 apex {
5482 name: "myapex",
5483 key: "myapex.key",
5484 jni_libs: ["mylib"],
5485 }
5486
5487 apex_key {
5488 name: "myapex.key",
5489 public_key: "testkey.avbpubkey",
5490 private_key: "testkey.pem",
5491 }
5492
5493 cc_library {
5494 name: "mylib",
5495 srcs: ["mylib.cpp"],
5496 shared_libs: ["mylib2"],
5497 system_shared_libs: [],
5498 stl: "none",
5499 apex_available: [ "myapex" ],
5500 }
5501
5502 cc_library {
5503 name: "mylib2",
5504 srcs: ["mylib.cpp"],
5505 system_shared_libs: [],
5506 stl: "none",
5507 apex_available: [ "myapex" ],
5508 }
5509 `)
5510
5511 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
5512 // Notice mylib2.so (transitive dep) is not added as a jni_lib
5513 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
5514 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5515 "lib64/mylib.so",
5516 "lib64/mylib2.so",
5517 })
5518}
5519
Jooyung Han49f67012020-04-17 13:43:10 +09005520func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
5521 ctx, _ := testApex(t, `
5522 apex {
5523 name: "myapex",
5524 key: "myapex.key",
5525 }
5526 apex_key {
5527 name: "myapex.key",
5528 public_key: "testkey.avbpubkey",
5529 private_key: "testkey.pem",
5530 }
5531 `, func(fs map[string][]byte, config android.Config) {
5532 delete(config.Targets, android.Android)
5533 config.AndroidCommonTarget = android.Target{}
5534 })
5535
5536 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
5537 t.Errorf("Expected variants: %v, but got: %v", expected, got)
5538 }
5539}
5540
Jiyong Parkbd159612020-02-28 15:22:21 +09005541func TestAppBundle(t *testing.T) {
5542 ctx, _ := testApex(t, `
5543 apex {
5544 name: "myapex",
5545 key: "myapex.key",
5546 apps: ["AppFoo"],
5547 }
5548
5549 apex_key {
5550 name: "myapex.key",
5551 public_key: "testkey.avbpubkey",
5552 private_key: "testkey.pem",
5553 }
5554
5555 android_app {
5556 name: "AppFoo",
5557 srcs: ["foo/bar/MyClass.java"],
5558 sdk_version: "none",
5559 system_modules: "none",
5560 apex_available: [ "myapex" ],
5561 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09005562 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09005563
5564 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
5565 content := bundleConfigRule.Args["content"]
5566
5567 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09005568 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo/AppFoo.apk"}]}`)
Jiyong Parkbd159612020-02-28 15:22:21 +09005569}
5570
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005571func TestAppSetBundle(t *testing.T) {
5572 ctx, _ := testApex(t, `
5573 apex {
5574 name: "myapex",
5575 key: "myapex.key",
5576 apps: ["AppSet"],
5577 }
5578
5579 apex_key {
5580 name: "myapex.key",
5581 public_key: "testkey.avbpubkey",
5582 private_key: "testkey.pem",
5583 }
5584
5585 android_app_set {
5586 name: "AppSet",
5587 set: "AppSet.apks",
5588 }`)
5589 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5590 bundleConfigRule := mod.Description("Bundle Config")
5591 content := bundleConfigRule.Args["content"]
5592 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
5593 s := mod.Rule("apexRule").Args["copy_commands"]
5594 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
5595 if len(copyCmds) != 3 {
5596 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
5597 }
5598 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
5599 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
5600 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
5601}
5602
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005603func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005604 t.Helper()
5605
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005606 bp := `
5607 java_library {
5608 name: "some-updatable-apex-lib",
5609 srcs: ["a.java"],
5610 sdk_version: "current",
5611 apex_available: [
5612 "some-updatable-apex",
5613 ],
5614 }
5615
5616 java_library {
5617 name: "some-non-updatable-apex-lib",
5618 srcs: ["a.java"],
5619 apex_available: [
5620 "some-non-updatable-apex",
5621 ],
5622 }
5623
5624 java_library {
5625 name: "some-platform-lib",
5626 srcs: ["a.java"],
5627 sdk_version: "current",
5628 installable: true,
5629 }
5630
5631 java_library {
5632 name: "some-art-lib",
5633 srcs: ["a.java"],
5634 sdk_version: "current",
5635 apex_available: [
5636 "com.android.art.something",
5637 ],
5638 hostdex: true,
5639 }
5640
5641 apex {
5642 name: "some-updatable-apex",
5643 key: "some-updatable-apex.key",
5644 java_libs: ["some-updatable-apex-lib"],
5645 updatable: true,
5646 min_sdk_version: "current",
5647 }
5648
5649 apex {
5650 name: "some-non-updatable-apex",
5651 key: "some-non-updatable-apex.key",
5652 java_libs: ["some-non-updatable-apex-lib"],
5653 }
5654
5655 apex_key {
5656 name: "some-updatable-apex.key",
5657 }
5658
5659 apex_key {
5660 name: "some-non-updatable-apex.key",
5661 }
5662
5663 apex {
5664 name: "com.android.art.something",
5665 key: "com.android.art.something.key",
5666 java_libs: ["some-art-lib"],
5667 updatable: true,
5668 min_sdk_version: "current",
5669 }
5670
5671 apex_key {
5672 name: "com.android.art.something.key",
5673 }
5674
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005675 filegroup {
5676 name: "some-updatable-apex-file_contexts",
5677 srcs: [
5678 "system/sepolicy/apex/some-updatable-apex-file_contexts",
5679 ],
5680 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005681
5682 filegroup {
5683 name: "some-non-updatable-apex-file_contexts",
5684 srcs: [
5685 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
5686 ],
5687 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005688 `
5689 bp += cc.GatherRequiredDepsForTest(android.Android)
5690 bp += java.GatherRequiredDepsForTest()
5691 bp += dexpreopt.BpToolModulesForTest()
5692
5693 fs := map[string][]byte{
5694 "a.java": nil,
5695 "a.jar": nil,
5696 "build/make/target/product/security": nil,
5697 "apex_manifest.json": nil,
5698 "AndroidManifest.xml": nil,
5699 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005700 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005701 "system/sepolicy/apex/com.android.art.something-file_contexts": nil,
5702 "framework/aidl/a.aidl": nil,
5703 }
5704 cc.GatherRequiredFilesForTest(fs)
5705
5706 ctx := android.NewTestArchContext()
5707 ctx.RegisterModuleType("apex", BundleFactory)
5708 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
5709 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01005710 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin021f4e52020-07-30 16:04:17 +01005711 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005712 cc.RegisterRequiredBuildComponentsForTest(ctx)
5713 java.RegisterJavaBuildComponents(ctx)
5714 java.RegisterSystemModulesBuildComponents(ctx)
5715 java.RegisterAppBuildComponents(ctx)
5716 java.RegisterDexpreoptBootJarsComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005717 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
5718 ctx.PreDepsMutators(RegisterPreDepsMutators)
5719 ctx.PostDepsMutators(RegisterPostDepsMutators)
5720
5721 config := android.TestArchConfig(buildDir, nil, bp, fs)
5722 ctx.Register(config)
5723
5724 _ = dexpreopt.GlobalSoongConfigForTests(config)
5725 dexpreopt.RegisterToolModulesForTest(ctx)
5726 pathCtx := android.PathContextForTesting(config)
5727 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
5728 transformDexpreoptConfig(dexpreoptConfig)
5729 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
5730
5731 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
5732 android.FailIfErrored(t, errs)
5733
5734 _, errs = ctx.PrepareBuildActions(config)
5735 if errmsg == "" {
5736 android.FailIfErrored(t, errs)
5737 } else if len(errs) > 0 {
5738 android.FailIfNoMatchingErrors(t, errmsg, errs)
5739 return
5740 } else {
5741 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
5742 }
5743}
5744
Jooyung Han548640b2020-04-27 12:10:30 +09005745func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
5746 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
5747 apex {
5748 name: "myapex",
5749 key: "myapex.key",
5750 updatable: true,
5751 }
5752
5753 apex_key {
5754 name: "myapex.key",
5755 public_key: "testkey.avbpubkey",
5756 private_key: "testkey.pem",
5757 }
5758 `)
5759}
5760
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005761func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005762 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005763 var transform func(*dexpreopt.GlobalConfig)
5764
Ulya Trafimovich249386a2020-07-01 14:31:13 +01005765 config := android.TestArchConfig(buildDir, nil, "", nil)
5766 ctx := android.PathContextForTesting(config)
5767
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005768 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
5769 transform = func(config *dexpreopt.GlobalConfig) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01005770 config.ArtApexJars = android.CreateConfiguredJarList(ctx, []string{"com.android.art.something:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005771 }
5772 testNoUpdatableJarsInBootImage(t, "", transform)
5773 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005774
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005775 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07005776 err = `module "some-art-lib" from updatable apexes \["com.android.art.something"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005777 transform = func(config *dexpreopt.GlobalConfig) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01005778 config.BootJars = android.CreateConfiguredJarList(ctx, []string{"com.android.art.something:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005779 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005780 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005781 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005782
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005783 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07005784 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005785 transform = func(config *dexpreopt.GlobalConfig) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01005786 config.ArtApexJars = android.CreateConfiguredJarList(ctx, []string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005787 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005788 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005789 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005790
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005791 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07005792 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005793 transform = func(config *dexpreopt.GlobalConfig) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01005794 config.ArtApexJars = android.CreateConfiguredJarList(ctx, []string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005795 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005796 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005797 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005798
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005799 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07005800 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005801 transform = func(config *dexpreopt.GlobalConfig) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01005802 config.BootJars = android.CreateConfiguredJarList(ctx, []string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005803 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005804 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005805 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005806
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005807 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
5808 transform = func(config *dexpreopt.GlobalConfig) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01005809 config.BootJars = android.CreateConfiguredJarList(ctx, []string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005810 }
5811 testNoUpdatableJarsInBootImage(t, "", transform)
5812 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005813
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005814 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005815 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005816 transform = func(config *dexpreopt.GlobalConfig) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01005817 config.ArtApexJars = android.CreateConfiguredJarList(ctx, []string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005818 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005819 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005820 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005821
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005822 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005823 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005824 transform = func(config *dexpreopt.GlobalConfig) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01005825 config.BootJars = android.CreateConfiguredJarList(ctx, []string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005826 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005827 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005828 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005829
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005830 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07005831 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005832 transform = func(config *dexpreopt.GlobalConfig) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01005833 config.ArtApexJars = android.CreateConfiguredJarList(ctx, []string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005834 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005835 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005836 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005837
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005838 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
5839 transform = func(config *dexpreopt.GlobalConfig) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +01005840 config.BootJars = android.CreateConfiguredJarList(ctx, []string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005841 }
5842 testNoUpdatableJarsInBootImage(t, "", transform)
5843 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005844}
5845
Andrei Onea115e7e72020-06-05 21:14:03 +01005846func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
5847 t.Helper()
5848 android.ClearApexDependency()
5849 bp += `
5850 apex_key {
5851 name: "myapex.key",
5852 public_key: "testkey.avbpubkey",
5853 private_key: "testkey.pem",
5854 }`
5855 fs := map[string][]byte{
5856 "lib1/src/A.java": nil,
5857 "lib2/src/B.java": nil,
5858 "system/sepolicy/apex/myapex-file_contexts": nil,
5859 }
5860
5861 ctx := android.NewTestArchContext()
5862 ctx.RegisterModuleType("apex", BundleFactory)
5863 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
5864 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
5865 cc.RegisterRequiredBuildComponentsForTest(ctx)
5866 java.RegisterJavaBuildComponents(ctx)
5867 java.RegisterSystemModulesBuildComponents(ctx)
5868 java.RegisterDexpreoptBootJarsComponents(ctx)
5869 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
5870 ctx.PreDepsMutators(RegisterPreDepsMutators)
5871 ctx.PostDepsMutators(RegisterPostDepsMutators)
5872 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
5873
5874 config := android.TestArchConfig(buildDir, nil, bp, fs)
5875 android.SetTestNeverallowRules(config, rules)
5876 updatableBootJars := make([]string, 0, len(apexBootJars))
5877 for _, apexBootJar := range apexBootJars {
5878 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
5879 }
5880 config.TestProductVariables.UpdatableBootJars = updatableBootJars
5881
5882 ctx.Register(config)
5883
5884 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
5885 android.FailIfErrored(t, errs)
5886
5887 _, errs = ctx.PrepareBuildActions(config)
5888 if errmsg == "" {
5889 android.FailIfErrored(t, errs)
5890 } else if len(errs) > 0 {
5891 android.FailIfNoMatchingErrors(t, errmsg, errs)
5892 return
5893 } else {
5894 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
5895 }
5896}
5897
5898func TestApexPermittedPackagesRules(t *testing.T) {
5899 testcases := []struct {
5900 name string
5901 expectedError string
5902 bp string
5903 bootJars []string
5904 modulesPackages map[string][]string
5905 }{
5906
5907 {
5908 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
5909 expectedError: "",
5910 bp: `
5911 java_library {
5912 name: "bcp_lib1",
5913 srcs: ["lib1/src/*.java"],
5914 permitted_packages: ["foo.bar"],
5915 apex_available: ["myapex"],
5916 sdk_version: "none",
5917 system_modules: "none",
5918 }
5919 java_library {
5920 name: "nonbcp_lib2",
5921 srcs: ["lib2/src/*.java"],
5922 apex_available: ["myapex"],
5923 permitted_packages: ["a.b"],
5924 sdk_version: "none",
5925 system_modules: "none",
5926 }
5927 apex {
5928 name: "myapex",
5929 key: "myapex.key",
5930 java_libs: ["bcp_lib1", "nonbcp_lib2"],
5931 }`,
5932 bootJars: []string{"bcp_lib1"},
5933 modulesPackages: map[string][]string{
5934 "myapex": []string{
5935 "foo.bar",
5936 },
5937 },
5938 },
5939 {
5940 name: "Bootclasspath apex jar not satisfying allowed module packages.",
5941 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.`,
5942 bp: `
5943 java_library {
5944 name: "bcp_lib1",
5945 srcs: ["lib1/src/*.java"],
5946 apex_available: ["myapex"],
5947 permitted_packages: ["foo.bar"],
5948 sdk_version: "none",
5949 system_modules: "none",
5950 }
5951 java_library {
5952 name: "bcp_lib2",
5953 srcs: ["lib2/src/*.java"],
5954 apex_available: ["myapex"],
5955 permitted_packages: ["foo.bar", "bar.baz"],
5956 sdk_version: "none",
5957 system_modules: "none",
5958 }
5959 apex {
5960 name: "myapex",
5961 key: "myapex.key",
5962 java_libs: ["bcp_lib1", "bcp_lib2"],
5963 }
5964 `,
5965 bootJars: []string{"bcp_lib1", "bcp_lib2"},
5966 modulesPackages: map[string][]string{
5967 "myapex": []string{
5968 "foo.bar",
5969 },
5970 },
5971 },
5972 }
5973 for _, tc := range testcases {
5974 t.Run(tc.name, func(t *testing.T) {
5975 rules := createApexPermittedPackagesRules(tc.modulesPackages)
5976 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
5977 })
5978 }
5979}
5980
Jiyong Park62304bb2020-04-13 16:19:48 +09005981func TestTestFor(t *testing.T) {
5982 ctx, _ := testApex(t, `
5983 apex {
5984 name: "myapex",
5985 key: "myapex.key",
5986 native_shared_libs: ["mylib", "myprivlib"],
5987 }
5988
5989 apex_key {
5990 name: "myapex.key",
5991 public_key: "testkey.avbpubkey",
5992 private_key: "testkey.pem",
5993 }
5994
5995 cc_library {
5996 name: "mylib",
5997 srcs: ["mylib.cpp"],
5998 system_shared_libs: [],
5999 stl: "none",
6000 stubs: {
6001 versions: ["1"],
6002 },
6003 apex_available: ["myapex"],
6004 }
6005
6006 cc_library {
6007 name: "myprivlib",
6008 srcs: ["mylib.cpp"],
6009 system_shared_libs: [],
6010 stl: "none",
6011 apex_available: ["myapex"],
6012 }
6013
6014
6015 cc_test {
6016 name: "mytest",
6017 gtest: false,
6018 srcs: ["mylib.cpp"],
6019 system_shared_libs: [],
6020 stl: "none",
6021 shared_libs: ["mylib", "myprivlib"],
6022 test_for: ["myapex"]
6023 }
6024 `)
6025
6026 // the test 'mytest' is a test for the apex, therefore is linked to the
6027 // actual implementation of mylib instead of its stub.
6028 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6029 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6030 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
6031}
6032
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006033// TODO(jungjw): Move this to proptools
6034func intPtr(i int) *int {
6035 return &i
6036}
6037
6038func TestApexSet(t *testing.T) {
6039 ctx, config := testApex(t, `
6040 apex_set {
6041 name: "myapex",
6042 set: "myapex.apks",
6043 filename: "foo_v2.apex",
6044 overrides: ["foo"],
6045 }
6046 `, func(fs map[string][]byte, config android.Config) {
6047 config.TestProductVariables.Platform_sdk_version = intPtr(30)
Jaewoong Jung829b7132020-06-10 12:23:32 -07006048 config.Targets[android.Android] = []android.Target{
6049 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6050 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6051 }
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006052 })
6053
6054 m := ctx.ModuleForTests("myapex", "android_common")
6055
6056 // Check extract_apks tool parameters.
6057 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6058 actual := extractedApex.Args["abis"]
6059 expected := "ARMEABI_V7A,ARM64_V8A"
6060 if actual != expected {
6061 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6062 }
6063 actual = extractedApex.Args["sdk-version"]
6064 expected = "30"
6065 if actual != expected {
6066 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6067 }
6068
6069 a := m.Module().(*ApexSet)
6070 expectedOverrides := []string{"foo"}
6071 actualOverrides := android.AndroidMkEntriesForTest(t, config, "", a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
6072 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6073 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6074 }
6075}
6076
Jiyong Park7d95a512020-05-10 15:16:24 +09006077func TestNoStaticLinkingToStubsLib(t *testing.T) {
6078 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6079 apex {
6080 name: "myapex",
6081 key: "myapex.key",
6082 native_shared_libs: ["mylib"],
6083 }
6084
6085 apex_key {
6086 name: "myapex.key",
6087 public_key: "testkey.avbpubkey",
6088 private_key: "testkey.pem",
6089 }
6090
6091 cc_library {
6092 name: "mylib",
6093 srcs: ["mylib.cpp"],
6094 static_libs: ["otherlib"],
6095 system_shared_libs: [],
6096 stl: "none",
6097 apex_available: [ "myapex" ],
6098 }
6099
6100 cc_library {
6101 name: "otherlib",
6102 srcs: ["mylib.cpp"],
6103 system_shared_libs: [],
6104 stl: "none",
6105 stubs: {
6106 versions: ["1", "2", "3"],
6107 },
6108 apex_available: [ "myapex" ],
6109 }
6110 `)
6111}
6112
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006113func TestApexKeysTxt(t *testing.T) {
6114 ctx, _ := testApex(t, `
6115 apex {
6116 name: "myapex",
6117 key: "myapex.key",
6118 }
6119
6120 apex_key {
6121 name: "myapex.key",
6122 public_key: "testkey.avbpubkey",
6123 private_key: "testkey.pem",
6124 }
6125
6126 prebuilt_apex {
6127 name: "myapex",
6128 prefer: true,
6129 arch: {
6130 arm64: {
6131 src: "myapex-arm64.apex",
6132 },
6133 arm: {
6134 src: "myapex-arm.apex",
6135 },
6136 },
6137 }
6138
6139 apex_set {
6140 name: "myapex_set",
6141 set: "myapex.apks",
6142 filename: "myapex_set.apex",
6143 overrides: ["myapex"],
6144 }
6145 `)
6146
6147 apexKeysText := ctx.SingletonForTests("apex_keys_text")
6148 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
6149 ensureContains(t, content, `name="myapex_set.apex" public_key="PRESIGNED" private_key="PRESIGNED" container_certificate="PRESIGNED" container_private_key="PRESIGNED" partition="system"`)
Jiyong Park03a7f3e2020-06-18 19:34:42 +09006150 ensureContains(t, content, `name="myapex.apex" public_key="PRESIGNED" private_key="PRESIGNED" container_certificate="PRESIGNED" container_private_key="PRESIGNED" partition="system"`)
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006151}
6152
Jooyung Han938b5932020-06-20 12:47:47 +09006153func TestAllowedFiles(t *testing.T) {
6154 ctx, _ := testApex(t, `
6155 apex {
6156 name: "myapex",
6157 key: "myapex.key",
6158 apps: ["app"],
6159 allowed_files: "allowed.txt",
6160 }
6161
6162 apex_key {
6163 name: "myapex.key",
6164 public_key: "testkey.avbpubkey",
6165 private_key: "testkey.pem",
6166 }
6167
6168 android_app {
6169 name: "app",
6170 srcs: ["foo/bar/MyClass.java"],
6171 package_name: "foo",
6172 sdk_version: "none",
6173 system_modules: "none",
6174 apex_available: [ "myapex" ],
6175 }
6176 `, withFiles(map[string][]byte{
6177 "sub/Android.bp": []byte(`
6178 override_apex {
6179 name: "override_myapex",
6180 base: "myapex",
6181 apps: ["override_app"],
6182 allowed_files: ":allowed",
6183 }
6184 // Overridable "path" property should be referenced indirectly
6185 filegroup {
6186 name: "allowed",
6187 srcs: ["allowed.txt"],
6188 }
6189 override_android_app {
6190 name: "override_app",
6191 base: "app",
6192 package_name: "bar",
6193 }
6194 `),
6195 }))
6196
6197 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
6198 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
6199 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
6200 }
6201
6202 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
6203 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
6204 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
6205 }
6206}
6207
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006208func TestNonPreferredPrebuiltDependency(t *testing.T) {
6209 _, _ = testApex(t, `
6210 apex {
6211 name: "myapex",
6212 key: "myapex.key",
6213 native_shared_libs: ["mylib"],
6214 }
6215
6216 apex_key {
6217 name: "myapex.key",
6218 public_key: "testkey.avbpubkey",
6219 private_key: "testkey.pem",
6220 }
6221
6222 cc_library {
6223 name: "mylib",
6224 srcs: ["mylib.cpp"],
6225 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07006226 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006227 },
6228 apex_available: ["myapex"],
6229 }
6230
6231 cc_prebuilt_library_shared {
6232 name: "mylib",
6233 prefer: false,
6234 srcs: ["prebuilt.so"],
6235 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07006236 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006237 },
6238 apex_available: ["myapex"],
6239 }
6240 `)
6241}
6242
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07006243func TestMain(m *testing.M) {
6244 run := func() int {
6245 setUp()
6246 defer tearDown()
6247
6248 return m.Run()
6249 }
6250
6251 os.Exit(run())
6252}