blob: 4bcbf72355838b5c0216a4e664d8fa21c0ce1cfa [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 Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "io/ioutil"
20 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090021 "path"
Paul Duffin37856732021-02-26 14:24:15 +000022 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070023 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010024 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090025 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090026 "strings"
27 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090028
29 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070041var buildDir string
42
Jooyung Hand3639552019-08-09 12:57:43 +090043// names returns name list from white space separated string
44func names(s string) (ns []string) {
45 for _, n := range strings.Split(s, " ") {
46 if len(n) > 0 {
47 ns = append(ns, n)
48 }
49 }
50 return
51}
52
Jooyung Han344d5432019-08-23 11:17:39 +090053func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
54 t.Helper()
55 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090056 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
57 if len(errs) > 0 {
58 android.FailIfNoMatchingErrors(t, pattern, errs)
59 return
60 }
61 _, errs = ctx.PrepareBuildActions(config)
62 if len(errs) > 0 {
63 android.FailIfNoMatchingErrors(t, pattern, errs)
64 return
65 }
66
67 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
68}
69
Jooyung Han344d5432019-08-23 11:17:39 +090070func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
71 t.Helper()
72 ctx, config := testApexContext(t, bp, handlers...)
Paul Duffineedc5d52020-06-12 17:46:39 +010073 _, errs := ctx.ParseBlueprintsFiles(".")
Jooyung Han5c998b92019-06-27 11:30:33 +090074 android.FailIfErrored(t, errs)
75 _, errs = ctx.PrepareBuildActions(config)
76 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070077 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090078}
79
Jooyung Han344d5432019-08-23 11:17:39 +090080type testCustomizer func(fs map[string][]byte, config android.Config)
81
82func withFiles(files map[string][]byte) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range files {
85 fs[k] = v
86 }
87 }
88}
89
90func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
91 return func(fs map[string][]byte, config android.Config) {
92 for k, v := range targets {
93 config.Targets[k] = v
94 }
95 }
96}
97
Jooyung Han35155c42020-02-06 17:33:20 +090098// withNativeBridgeTargets sets configuration with targets including:
99// - X86_64 (primary)
100// - X86 (secondary)
101// - Arm64 on X86_64 (native bridge)
102// - Arm on X86 (native bridge)
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700103func withNativeBridgeEnabled(_ map[string][]byte, config android.Config) {
Jooyung Han35155c42020-02-06 17:33:20 +0900104 config.Targets[android.Android] = []android.Target{
105 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
106 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
107 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
108 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
109 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
110 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
111 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
112 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
113 }
114}
115
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900116func withManifestPackageNameOverrides(specs []string) testCustomizer {
117 return func(fs map[string][]byte, config android.Config) {
118 config.TestProductVariables.ManifestPackageNameOverrides = specs
119 }
120}
121
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700122func withBinder32bit(_ map[string][]byte, config android.Config) {
Jooyung Han31c470b2019-10-18 16:26:59 +0900123 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
124}
125
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700126func withUnbundledBuild(_ map[string][]byte, config android.Config) {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900127 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
128}
129
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700130func testApexContext(_ *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900131 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900132 filegroup {
133 name: "myapex-file_contexts",
134 srcs: [
135 "system/sepolicy/apex/myapex-file_contexts",
136 ],
137 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900138 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800139
Colin Crossf9aabd72020-02-15 11:29:50 -0800140 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
141
Jiyong Park99644e92020-11-17 22:21:02 +0900142 bp = bp + rust.GatherRequiredDepsForTest()
143
Dario Frenicde2a032019-10-27 00:29:22 +0100144 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900145
Jooyung Han344d5432019-08-23 11:17:39 +0900146 fs := map[string][]byte{
Colin Cross2807f002021-03-02 10:15:29 -0800147 "a.java": nil,
148 "PrebuiltAppFoo.apk": nil,
149 "PrebuiltAppFooPriv.apk": nil,
150 "build/make/target/product/security": nil,
151 "apex_manifest.json": nil,
152 "AndroidManifest.xml": nil,
153 "system/sepolicy/apex/myapex-file_contexts": nil,
154 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
155 "system/sepolicy/apex/myapex2-file_contexts": nil,
156 "system/sepolicy/apex/otherapex-file_contexts": nil,
157 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
158 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800159 "mylib.cpp": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800160 "mytest.cpp": nil,
161 "mytest1.cpp": nil,
162 "mytest2.cpp": nil,
163 "mytest3.cpp": nil,
164 "myprebuilt": nil,
165 "my_include": nil,
166 "foo/bar/MyClass.java": nil,
167 "prebuilt.jar": nil,
Paul Duffindddd5462020-04-07 15:25:44 +0100168 "prebuilt.so": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800169 "vendor/foo/devkeys/test.x509.pem": nil,
170 "vendor/foo/devkeys/test.pk8": nil,
171 "testkey.x509.pem": nil,
172 "testkey.pk8": nil,
173 "testkey.override.x509.pem": nil,
174 "testkey.override.pk8": nil,
175 "vendor/foo/devkeys/testkey.avbpubkey": nil,
176 "vendor/foo/devkeys/testkey.pem": nil,
177 "NOTICE": nil,
178 "custom_notice": nil,
Jiyong Park9918e1a2020-03-17 19:16:40 +0900179 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800180 "testkey2.avbpubkey": nil,
181 "testkey2.pem": nil,
182 "myapex-arm64.apex": nil,
183 "myapex-arm.apex": nil,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700184 "myapex.apks": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800185 "frameworks/base/api/current.txt": nil,
186 "framework/aidl/a.aidl": nil,
187 "build/make/core/proguard.flags": nil,
188 "build/make/core/proguard_basic_keeps.flags": nil,
189 "dummy.txt": nil,
Liz Kammer1c14a212020-05-12 15:26:55 -0700190 "baz": nil,
191 "bar/baz": nil,
Liz Kammer5bd365f2020-05-27 15:15:11 -0700192 "testdata/baz": nil,
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700193 "AppSet.apks": nil,
Jiyong Park99644e92020-11-17 22:21:02 +0900194 "foo.rs": nil,
Paul Duffin064b70c2020-11-02 17:32:38 +0000195 "libfoo.jar": nil,
Paul Duffin39853512021-02-26 11:09:39 +0000196 "libbar.jar": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900197 }
198
Colin Crossf9aabd72020-02-15 11:29:50 -0800199 cc.GatherRequiredFilesForTest(fs)
200
Jooyung Han344d5432019-08-23 11:17:39 +0900201 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800202 // The fs now needs to be populated before creating the config, call handlers twice
203 // for now, once to get any fs changes, and later after the config was created to
204 // set product variables or targets.
205 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
206 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900207 }
208
Colin Cross98be1bb2019-12-13 20:41:13 -0800209 config := android.TestArchConfig(buildDir, nil, bp, fs)
210 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
211 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
212 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
213 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
214 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Dan Albert4f378d72020-07-23 17:32:15 -0700215 config.TestProductVariables.Platform_version_active_codenames = []string{"Q"}
Colin Cross98be1bb2019-12-13 20:41:13 -0800216 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
217
218 for _, handler := range handlers {
219 // The fs now needs to be populated before creating the config, call handlers twice
220 // for now, earlier to get any fs changes, and now after the config was created to
221 // set product variables or targets.
222 tempFS := map[string][]byte{}
223 handler(tempFS, config)
224 }
225
Colin Crossae8600b2020-10-29 17:09:13 -0700226 ctx := android.NewTestArchContext(config)
Paul Duffineedc5d52020-06-12 17:46:39 +0100227
228 // from android package
229 android.RegisterPackageBuildComponents(ctx)
230 ctx.PreArchMutators(android.RegisterVisibilityRuleChecker)
231
Colin Cross98be1bb2019-12-13 20:41:13 -0800232 ctx.RegisterModuleType("apex", BundleFactory)
233 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
234 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
235 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
236 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
237 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
238 ctx.RegisterModuleType("override_apex", overrideApexFactory)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700239 ctx.RegisterModuleType("apex_set", apexSetFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800240
Jooyung Hana57af4a2020-01-23 05:36:59 +0000241 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +0100242 ctx.PreArchMutators(android.RegisterComponentsMutator)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000243 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
244
Paul Duffin021f4e52020-07-30 16:04:17 +0100245 android.RegisterPrebuiltMutators(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +0100246
Paul Duffin021f4e52020-07-30 16:04:17 +0100247 // Register these after the prebuilt mutators have been registered to match what
248 // happens at runtime.
Paul Duffineedc5d52020-06-12 17:46:39 +0100249 ctx.PreArchMutators(android.RegisterVisibilityRuleGatherer)
250 ctx.PostDepsMutators(android.RegisterVisibilityRuleEnforcer)
251
Paul Duffin021f4e52020-07-30 16:04:17 +0100252 cc.RegisterRequiredBuildComponentsForTest(ctx)
Jiyong Park99644e92020-11-17 22:21:02 +0900253 rust.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000254 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffin021f4e52020-07-30 16:04:17 +0100255
Colin Cross98be1bb2019-12-13 20:41:13 -0800256 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800257 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
Colin Crosse4e44bc2020-12-28 13:50:21 -0800258 cc.RegisterVndkLibraryTxtTypes(ctx)
Jooyung Han0703fd82020-08-26 22:11:53 +0900259 prebuilt_etc.RegisterPrebuiltEtcBuildComponents(ctx)
atrost6e126252020-01-27 17:01:16 +0000260 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700261 ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800262 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900263 ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
markchien2f59ec92020-09-02 16:23:38 +0800264 ctx.RegisterModuleType("bpf", bpf.BpfFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800265
Colin Cross98be1bb2019-12-13 20:41:13 -0800266 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800267 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800268
Colin Crossae8600b2020-10-29 17:09:13 -0700269 ctx.Register()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900270
Jooyung Han5c998b92019-06-27 11:30:33 +0900271 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900272}
273
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700274func setUp() {
275 var err error
276 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900277 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700278 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900279 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280}
281
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700282func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700283 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900284}
285
Jooyung Han643adc42020-02-27 13:50:06 +0900286// ensure that 'result' equals 'expected'
287func ensureEquals(t *testing.T, result string, expected string) {
288 t.Helper()
289 if result != expected {
290 t.Errorf("%q != %q", expected, result)
291 }
292}
293
Jiyong Park25fc6a92018-11-18 18:02:45 +0900294// ensure that 'result' contains 'expected'
295func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900296 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900297 if !strings.Contains(result, expected) {
298 t.Errorf("%q is not found in %q", expected, result)
299 }
300}
301
Liz Kammer5bd365f2020-05-27 15:15:11 -0700302// ensure that 'result' contains 'expected' exactly one time
303func ensureContainsOnce(t *testing.T, result string, expected string) {
304 t.Helper()
305 count := strings.Count(result, expected)
306 if count != 1 {
307 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
308 }
309}
310
Jiyong Park25fc6a92018-11-18 18:02:45 +0900311// ensures that 'result' does not contain 'notExpected'
312func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900313 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314 if strings.Contains(result, notExpected) {
315 t.Errorf("%q is found in %q", notExpected, result)
316 }
317}
318
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700319func ensureMatches(t *testing.T, result string, expectedRex string) {
320 ok, err := regexp.MatchString(expectedRex, result)
321 if err != nil {
322 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
323 return
324 }
325 if !ok {
326 t.Errorf("%s does not match regular expession %s", result, expectedRex)
327 }
328}
329
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900331 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900332 if !android.InList(expected, result) {
333 t.Errorf("%q is not found in %v", expected, result)
334 }
335}
336
337func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900338 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900339 if android.InList(notExpected, result) {
340 t.Errorf("%q is found in %v", notExpected, result)
341 }
342}
343
Jooyung Hane1633032019-08-01 17:41:43 +0900344func ensureListEmpty(t *testing.T, result []string) {
345 t.Helper()
346 if len(result) > 0 {
347 t.Errorf("%q is expected to be empty", result)
348 }
349}
350
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000351func ensureListNotEmpty(t *testing.T, result []string) {
352 t.Helper()
353 if len(result) == 0 {
354 t.Errorf("%q is expected to be not empty", result)
355 }
356}
357
Jiyong Park25fc6a92018-11-18 18:02:45 +0900358// Minimal test
359func TestBasicApex(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700360 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900361 apex_defaults {
362 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900363 manifest: ":myapex.manifest",
364 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900365 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900366 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900367 native_shared_libs: [
368 "mylib",
369 "libfoo.ffi",
370 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900371 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800372 multilib: {
373 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900374 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800375 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900376 },
Jiyong Park77acec62020-06-01 21:39:15 +0900377 java_libs: [
378 "myjar",
379 "myjar_dex",
380 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900381 }
382
Jiyong Park30ca9372019-02-07 16:27:23 +0900383 apex {
384 name: "myapex",
385 defaults: ["myapex-defaults"],
386 }
387
Jiyong Park25fc6a92018-11-18 18:02:45 +0900388 apex_key {
389 name: "myapex.key",
390 public_key: "testkey.avbpubkey",
391 private_key: "testkey.pem",
392 }
393
Jiyong Park809bb722019-02-13 21:33:49 +0900394 filegroup {
395 name: "myapex.manifest",
396 srcs: ["apex_manifest.json"],
397 }
398
399 filegroup {
400 name: "myapex.androidmanifest",
401 srcs: ["AndroidManifest.xml"],
402 }
403
Jiyong Park25fc6a92018-11-18 18:02:45 +0900404 cc_library {
405 name: "mylib",
406 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900407 shared_libs: [
408 "mylib2",
409 "libbar.ffi",
410 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900411 system_shared_libs: [],
412 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000413 // TODO: remove //apex_available:platform
414 apex_available: [
415 "//apex_available:platform",
416 "myapex",
417 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900418 }
419
Alex Light3d673592019-01-18 14:37:31 -0800420 cc_binary {
421 name: "foo",
422 srcs: ["mylib.cpp"],
423 compile_multilib: "both",
424 multilib: {
425 lib32: {
426 suffix: "32",
427 },
428 lib64: {
429 suffix: "64",
430 },
431 },
432 symlinks: ["foo_link_"],
433 symlink_preferred_arch: true,
434 system_shared_libs: [],
435 static_executable: true,
436 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700437 apex_available: [ "myapex", "com.android.gki.*" ],
438 }
439
Jiyong Park99644e92020-11-17 22:21:02 +0900440 rust_binary {
441 name: "foo.rust",
442 srcs: ["foo.rs"],
443 rlibs: ["libfoo.rlib.rust"],
444 dylibs: ["libfoo.dylib.rust"],
445 apex_available: ["myapex"],
446 }
447
448 rust_library_rlib {
449 name: "libfoo.rlib.rust",
450 srcs: ["foo.rs"],
451 crate_name: "foo",
452 apex_available: ["myapex"],
453 }
454
455 rust_library_dylib {
456 name: "libfoo.dylib.rust",
457 srcs: ["foo.rs"],
458 crate_name: "foo",
459 apex_available: ["myapex"],
460 }
461
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900462 rust_ffi_shared {
463 name: "libfoo.ffi",
464 srcs: ["foo.rs"],
465 crate_name: "foo",
466 apex_available: ["myapex"],
467 }
468
469 rust_ffi_shared {
470 name: "libbar.ffi",
471 srcs: ["foo.rs"],
472 crate_name: "bar",
473 apex_available: ["myapex"],
474 }
475
Yifan Hongd22a84a2020-07-28 17:37:46 -0700476 apex {
477 name: "com.android.gki.fake",
478 binaries: ["foo"],
479 key: "myapex.key",
480 file_contexts: ":myapex-file_contexts",
Alex Light3d673592019-01-18 14:37:31 -0800481 }
482
Paul Duffindddd5462020-04-07 15:25:44 +0100483 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900484 name: "mylib2",
485 srcs: ["mylib.cpp"],
486 system_shared_libs: [],
487 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900488 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900489 static_libs: ["libstatic"],
490 // TODO: remove //apex_available:platform
491 apex_available: [
492 "//apex_available:platform",
493 "myapex",
494 ],
495 }
496
Paul Duffindddd5462020-04-07 15:25:44 +0100497 cc_prebuilt_library_shared {
498 name: "mylib2",
499 srcs: ["prebuilt.so"],
500 // TODO: remove //apex_available:platform
501 apex_available: [
502 "//apex_available:platform",
503 "myapex",
504 ],
505 }
506
Jiyong Park9918e1a2020-03-17 19:16:40 +0900507 cc_library_static {
508 name: "libstatic",
509 srcs: ["mylib.cpp"],
510 system_shared_libs: [],
511 stl: "none",
512 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000513 // TODO: remove //apex_available:platform
514 apex_available: [
515 "//apex_available:platform",
516 "myapex",
517 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900518 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900519
520 java_library {
521 name: "myjar",
522 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900523 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900524 sdk_version: "none",
525 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900526 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900527 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000528 // TODO: remove //apex_available:platform
529 apex_available: [
530 "//apex_available:platform",
531 "myapex",
532 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900533 }
534
Jiyong Park77acec62020-06-01 21:39:15 +0900535 dex_import {
536 name: "myjar_dex",
537 jars: ["prebuilt.jar"],
538 apex_available: [
539 "//apex_available:platform",
540 "myapex",
541 ],
542 }
543
Jiyong Park7f7766d2019-07-25 22:02:35 +0900544 java_library {
545 name: "myotherjar",
546 srcs: ["foo/bar/MyClass.java"],
547 sdk_version: "none",
548 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900549 // TODO: remove //apex_available:platform
550 apex_available: [
551 "//apex_available:platform",
552 "myapex",
553 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900554 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900555
556 java_library {
557 name: "mysharedjar",
558 srcs: ["foo/bar/MyClass.java"],
559 sdk_version: "none",
560 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900561 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900562 `)
563
Sundong Ahnabb64432019-10-22 13:58:29 +0900564 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900565
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900566 // Make sure that Android.mk is created
567 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700568 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900569 var builder strings.Builder
570 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
571
572 androidMk := builder.String()
573 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
574 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
575
Jiyong Park42cca6c2019-04-01 11:15:50 +0900576 optFlags := apexRule.Args["opt_flags"]
577 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700578 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900579 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900580
Jiyong Park25fc6a92018-11-18 18:02:45 +0900581 copyCmds := apexRule.Args["copy_commands"]
582
583 // Ensure that main rule creates an output
584 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
585
586 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700587 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
588 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
589 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900590 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900591 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900592
593 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700594 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
595 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900596 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
597 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900598 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900599
600 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800601 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
602 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900603 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900604 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900605 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900606 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
607 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900608 // .. but not for java libs
609 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900610 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800611
Colin Cross7113d202019-11-20 16:39:12 -0800612 // Ensure that the platform variant ends with _shared or _common
613 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
614 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900615 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
616 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900617 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
618
619 // Ensure that dynamic dependency to java libs are not included
620 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800621
622 // Ensure that all symlinks are present.
623 found_foo_link_64 := false
624 found_foo := false
625 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900626 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800627 if strings.HasSuffix(cmd, "bin/foo") {
628 found_foo = true
629 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
630 found_foo_link_64 = true
631 }
632 }
633 }
634 good := found_foo && found_foo_link_64
635 if !good {
636 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
637 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900638
Sundong Ahnabb64432019-10-22 13:58:29 +0900639 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700640 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900641 if len(noticeInputs) != 3 {
642 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900643 }
644 ensureListContains(t, noticeInputs, "NOTICE")
645 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900646 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900647
Artur Satayeva8bd1132020-04-27 18:07:06 +0100648 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100649 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
650 ensureListContains(t, fullDepsInfo, " mylib(minSdkVersion:(no version)) <- myapex")
651 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
652 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
653 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100654
655 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100656 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
657 ensureListContains(t, flatDepsInfo, "mylib(minSdkVersion:(no version))")
658 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
659 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
660 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800661}
662
Jooyung Hanf21c7972019-12-16 22:32:06 +0900663func TestDefaults(t *testing.T) {
664 ctx, _ := testApex(t, `
665 apex_defaults {
666 name: "myapex-defaults",
667 key: "myapex.key",
668 prebuilts: ["myetc"],
669 native_shared_libs: ["mylib"],
670 java_libs: ["myjar"],
671 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900672 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800673 bpfs: ["bpf"],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900674 }
675
676 prebuilt_etc {
677 name: "myetc",
678 src: "myprebuilt",
679 }
680
681 apex {
682 name: "myapex",
683 defaults: ["myapex-defaults"],
684 }
685
686 apex_key {
687 name: "myapex.key",
688 public_key: "testkey.avbpubkey",
689 private_key: "testkey.pem",
690 }
691
692 cc_library {
693 name: "mylib",
694 system_shared_libs: [],
695 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000696 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900697 }
698
699 java_library {
700 name: "myjar",
701 srcs: ["foo/bar/MyClass.java"],
702 sdk_version: "none",
703 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000704 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900705 }
706
707 android_app {
708 name: "AppFoo",
709 srcs: ["foo/bar/MyClass.java"],
710 sdk_version: "none",
711 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000712 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900713 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900714
715 runtime_resource_overlay {
716 name: "rro",
717 theme: "blue",
718 }
719
markchien2f59ec92020-09-02 16:23:38 +0800720 bpf {
721 name: "bpf",
722 srcs: ["bpf.c", "bpf2.c"],
723 }
724
Jooyung Hanf21c7972019-12-16 22:32:06 +0900725 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000726 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900727 "etc/myetc",
728 "javalib/myjar.jar",
729 "lib64/mylib.so",
730 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900731 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800732 "etc/bpf/bpf.o",
733 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900734 })
735}
736
Jooyung Han01a3ee22019-11-02 02:52:25 +0900737func TestApexManifest(t *testing.T) {
738 ctx, _ := testApex(t, `
739 apex {
740 name: "myapex",
741 key: "myapex.key",
742 }
743
744 apex_key {
745 name: "myapex.key",
746 public_key: "testkey.avbpubkey",
747 private_key: "testkey.pem",
748 }
749 `)
750
751 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900752 args := module.Rule("apexRule").Args
753 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
754 t.Error("manifest should be apex_manifest.pb, but " + manifest)
755 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900756}
757
Alex Light5098a612018-11-29 17:12:15 -0800758func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700759 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800760 apex {
761 name: "myapex",
762 key: "myapex.key",
763 payload_type: "zip",
764 native_shared_libs: ["mylib"],
765 }
766
767 apex_key {
768 name: "myapex.key",
769 public_key: "testkey.avbpubkey",
770 private_key: "testkey.pem",
771 }
772
773 cc_library {
774 name: "mylib",
775 srcs: ["mylib.cpp"],
776 shared_libs: ["mylib2"],
777 system_shared_libs: [],
778 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000779 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800780 }
781
782 cc_library {
783 name: "mylib2",
784 srcs: ["mylib.cpp"],
785 system_shared_libs: [],
786 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000787 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800788 }
789 `)
790
Sundong Ahnabb64432019-10-22 13:58:29 +0900791 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800792 copyCmds := zipApexRule.Args["copy_commands"]
793
794 // Ensure that main rule creates an output
795 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
796
797 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700798 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800799
800 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700801 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800802
803 // Ensure that both direct and indirect deps are copied into apex
804 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
805 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900806}
807
808func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700809 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900810 apex {
811 name: "myapex",
812 key: "myapex.key",
813 native_shared_libs: ["mylib", "mylib3"],
814 }
815
816 apex_key {
817 name: "myapex.key",
818 public_key: "testkey.avbpubkey",
819 private_key: "testkey.pem",
820 }
821
822 cc_library {
823 name: "mylib",
824 srcs: ["mylib.cpp"],
825 shared_libs: ["mylib2", "mylib3"],
826 system_shared_libs: [],
827 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000828 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900829 }
830
831 cc_library {
832 name: "mylib2",
833 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900834 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900835 system_shared_libs: [],
836 stl: "none",
837 stubs: {
838 versions: ["1", "2", "3"],
839 },
840 }
841
842 cc_library {
843 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900844 srcs: ["mylib.cpp"],
845 shared_libs: ["mylib4"],
846 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900847 stl: "none",
848 stubs: {
849 versions: ["10", "11", "12"],
850 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000851 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900852 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900853
854 cc_library {
855 name: "mylib4",
856 srcs: ["mylib.cpp"],
857 system_shared_libs: [],
858 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000859 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900860 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900861 `)
862
Sundong Ahnabb64432019-10-22 13:58:29 +0900863 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900864 copyCmds := apexRule.Args["copy_commands"]
865
866 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800867 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900868
869 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800870 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900871
872 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800873 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900874
Colin Crossaede88c2020-08-11 12:17:01 -0700875 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900876
877 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900878 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900879 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900880 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900881
882 // 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 -0700883 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900884 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700885 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900886
887 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900888 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900889 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900890
891 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700892 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900893
Jooyung Hana57af4a2020-01-23 05:36:59 +0000894 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900895 "lib64/mylib.so",
896 "lib64/mylib3.so",
897 "lib64/mylib4.so",
898 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900899}
900
Colin Cross7812fd32020-09-25 12:35:10 -0700901func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
902 t.Parallel()
903 ctx, _ := testApex(t, `
904 apex {
905 name: "myapex",
906 key: "myapex.key",
907 native_shared_libs: ["mylib", "mylib3"],
908 min_sdk_version: "29",
909 }
910
911 apex_key {
912 name: "myapex.key",
913 public_key: "testkey.avbpubkey",
914 private_key: "testkey.pem",
915 }
916
917 cc_library {
918 name: "mylib",
919 srcs: ["mylib.cpp"],
920 shared_libs: ["mylib2", "mylib3"],
921 system_shared_libs: [],
922 stl: "none",
923 apex_available: [ "myapex" ],
924 min_sdk_version: "28",
925 }
926
927 cc_library {
928 name: "mylib2",
929 srcs: ["mylib.cpp"],
930 cflags: ["-include mylib.h"],
931 system_shared_libs: [],
932 stl: "none",
933 stubs: {
934 versions: ["28", "29", "30", "current"],
935 },
936 min_sdk_version: "28",
937 }
938
939 cc_library {
940 name: "mylib3",
941 srcs: ["mylib.cpp"],
942 shared_libs: ["mylib4"],
943 system_shared_libs: [],
944 stl: "none",
945 stubs: {
946 versions: ["28", "29", "30", "current"],
947 },
948 apex_available: [ "myapex" ],
949 min_sdk_version: "28",
950 }
951
952 cc_library {
953 name: "mylib4",
954 srcs: ["mylib.cpp"],
955 system_shared_libs: [],
956 stl: "none",
957 apex_available: [ "myapex" ],
958 min_sdk_version: "28",
959 }
960 `)
961
962 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
963 copyCmds := apexRule.Args["copy_commands"]
964
965 // Ensure that direct non-stubs dep is always included
966 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
967
968 // Ensure that indirect stubs dep is not included
969 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
970
971 // Ensure that direct stubs dep is included
972 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
973
974 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
975
Jiyong Park55549df2021-02-26 23:57:23 +0900976 // Ensure that mylib is linking with the latest version of stub for mylib2
977 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700978 // ... and not linking to the non-stub (impl) variant of mylib2
979 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
980
981 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
982 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
983 // .. and not linking to the stubs variant of mylib3
984 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
985
986 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700987 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700988 ensureNotContains(t, mylib2Cflags, "-include ")
989
990 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700991 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700992
993 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
994 "lib64/mylib.so",
995 "lib64/mylib3.so",
996 "lib64/mylib4.so",
997 })
998}
999
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001000func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1001 t.Parallel()
1002 // myapex (Z)
1003 // mylib -----------------.
1004 // |
1005 // otherapex (29) |
1006 // libstub's versions: 29 Z current
1007 // |
1008 // <platform> |
1009 // libplatform ----------------'
1010 ctx, _ := testApex(t, `
1011 apex {
1012 name: "myapex",
1013 key: "myapex.key",
1014 native_shared_libs: ["mylib"],
1015 min_sdk_version: "Z", // non-final
1016 }
1017
1018 cc_library {
1019 name: "mylib",
1020 srcs: ["mylib.cpp"],
1021 shared_libs: ["libstub"],
1022 apex_available: ["myapex"],
1023 min_sdk_version: "Z",
1024 }
1025
1026 apex_key {
1027 name: "myapex.key",
1028 public_key: "testkey.avbpubkey",
1029 private_key: "testkey.pem",
1030 }
1031
1032 apex {
1033 name: "otherapex",
1034 key: "myapex.key",
1035 native_shared_libs: ["libstub"],
1036 min_sdk_version: "29",
1037 }
1038
1039 cc_library {
1040 name: "libstub",
1041 srcs: ["mylib.cpp"],
1042 stubs: {
1043 versions: ["29", "Z", "current"],
1044 },
1045 apex_available: ["otherapex"],
1046 min_sdk_version: "29",
1047 }
1048
1049 // platform module depending on libstub from otherapex should use the latest stub("current")
1050 cc_library {
1051 name: "libplatform",
1052 srcs: ["mylib.cpp"],
1053 shared_libs: ["libstub"],
1054 }
1055 `, func(fs map[string][]byte, config android.Config) {
1056 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Z")
1057 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
1058 config.TestProductVariables.Platform_version_active_codenames = []string{"Z"}
1059 })
1060
Jiyong Park55549df2021-02-26 23:57:23 +09001061 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001062 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001063 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001064 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001065 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001066
1067 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1068 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1069 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1070 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1071 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1072}
1073
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001074func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001075 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001076 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001077 name: "myapex2",
1078 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001079 native_shared_libs: ["mylib"],
1080 }
1081
1082 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001083 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001084 public_key: "testkey.avbpubkey",
1085 private_key: "testkey.pem",
1086 }
1087
1088 cc_library {
1089 name: "mylib",
1090 srcs: ["mylib.cpp"],
1091 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001092 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001093 system_shared_libs: [],
1094 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001095 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001096 }
1097
1098 cc_library {
1099 name: "libfoo",
1100 srcs: ["mylib.cpp"],
1101 shared_libs: ["libbar"],
1102 system_shared_libs: [],
1103 stl: "none",
1104 stubs: {
1105 versions: ["10", "20", "30"],
1106 },
1107 }
1108
1109 cc_library {
1110 name: "libbar",
1111 srcs: ["mylib.cpp"],
1112 system_shared_libs: [],
1113 stl: "none",
1114 }
1115
Jiyong Park678c8812020-02-07 17:25:49 +09001116 cc_library_static {
1117 name: "libbaz",
1118 srcs: ["mylib.cpp"],
1119 system_shared_libs: [],
1120 stl: "none",
1121 apex_available: [ "myapex2" ],
1122 }
1123
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001124 `)
1125
Jiyong Park83dc74b2020-01-14 18:38:44 +09001126 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001127 copyCmds := apexRule.Args["copy_commands"]
1128
1129 // Ensure that direct non-stubs dep is always included
1130 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1131
1132 // Ensure that indirect stubs dep is not included
1133 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1134
1135 // Ensure that dependency of stubs is not included
1136 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1137
Colin Crossaede88c2020-08-11 12:17:01 -07001138 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001139
1140 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001141 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001142 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001143 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001144
Jiyong Park3ff16992019-12-27 14:11:47 +09001145 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001146
1147 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1148 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001149
Artur Satayeva8bd1132020-04-27 18:07:06 +01001150 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001151 ensureListContains(t, fullDepsInfo, " mylib(minSdkVersion:(no version)) <- myapex2")
1152 ensureListContains(t, fullDepsInfo, " libbaz(minSdkVersion:(no version)) <- mylib")
1153 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001154
Artur Satayeva8bd1132020-04-27 18:07:06 +01001155 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001156 ensureListContains(t, flatDepsInfo, "mylib(minSdkVersion:(no version))")
1157 ensureListContains(t, flatDepsInfo, "libbaz(minSdkVersion:(no version))")
1158 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001159}
1160
Jooyung Hand3639552019-08-09 12:57:43 +09001161func TestApexWithRuntimeLibsDependency(t *testing.T) {
1162 /*
1163 myapex
1164 |
1165 v (runtime_libs)
1166 mylib ------+------> libfoo [provides stub]
1167 |
1168 `------> libbar
1169 */
1170 ctx, _ := testApex(t, `
1171 apex {
1172 name: "myapex",
1173 key: "myapex.key",
1174 native_shared_libs: ["mylib"],
1175 }
1176
1177 apex_key {
1178 name: "myapex.key",
1179 public_key: "testkey.avbpubkey",
1180 private_key: "testkey.pem",
1181 }
1182
1183 cc_library {
1184 name: "mylib",
1185 srcs: ["mylib.cpp"],
1186 runtime_libs: ["libfoo", "libbar"],
1187 system_shared_libs: [],
1188 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001189 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001190 }
1191
1192 cc_library {
1193 name: "libfoo",
1194 srcs: ["mylib.cpp"],
1195 system_shared_libs: [],
1196 stl: "none",
1197 stubs: {
1198 versions: ["10", "20", "30"],
1199 },
1200 }
1201
1202 cc_library {
1203 name: "libbar",
1204 srcs: ["mylib.cpp"],
1205 system_shared_libs: [],
1206 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001207 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001208 }
1209
1210 `)
1211
Sundong Ahnabb64432019-10-22 13:58:29 +09001212 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001213 copyCmds := apexRule.Args["copy_commands"]
1214
1215 // Ensure that direct non-stubs dep is always included
1216 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1217
1218 // Ensure that indirect stubs dep is not included
1219 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1220
1221 // Ensure that runtime_libs dep in included
1222 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1223
Sundong Ahnabb64432019-10-22 13:58:29 +09001224 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001225 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1226 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001227
1228}
1229
Jooyung Han8ce8db92020-05-15 19:05:05 +09001230func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
1231 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
1232 bp := `
1233 apex {
1234 name: "com.android.runtime",
1235 key: "com.android.runtime.key",
1236 native_shared_libs: ["libc"],
1237 }
1238
1239 apex_key {
1240 name: "com.android.runtime.key",
1241 public_key: "testkey.avbpubkey",
1242 private_key: "testkey.pem",
1243 }
1244
1245 cc_library {
1246 name: "libc",
1247 no_libcrt: true,
1248 nocrt: true,
1249 stl: "none",
1250 system_shared_libs: [],
1251 stubs: { versions: ["1"] },
1252 apex_available: ["com.android.runtime"],
1253
1254 sanitize: {
1255 hwaddress: true,
1256 }
1257 }
1258
1259 cc_prebuilt_library_shared {
1260 name: "libclang_rt.hwasan-aarch64-android",
1261 no_libcrt: true,
1262 nocrt: true,
1263 stl: "none",
1264 system_shared_libs: [],
1265 srcs: [""],
1266 stubs: { versions: ["1"] },
1267
1268 sanitize: {
1269 never: true,
1270 },
1271 }
1272 `
1273 // override bp to use hard-coded names: com.android.runtime and libc
1274 fs["Android.bp"] = []byte(bp)
1275 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
1276 })
1277
1278 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1279 "lib64/bionic/libc.so",
1280 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1281 })
1282
1283 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1284
1285 installed := hwasan.Description("install libclang_rt.hwasan")
1286 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1287
1288 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1289 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1290 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1291}
1292
1293func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
1294 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
1295 bp := `
1296 apex {
1297 name: "com.android.runtime",
1298 key: "com.android.runtime.key",
1299 native_shared_libs: ["libc"],
1300 }
1301
1302 apex_key {
1303 name: "com.android.runtime.key",
1304 public_key: "testkey.avbpubkey",
1305 private_key: "testkey.pem",
1306 }
1307
1308 cc_library {
1309 name: "libc",
1310 no_libcrt: true,
1311 nocrt: true,
1312 stl: "none",
1313 system_shared_libs: [],
1314 stubs: { versions: ["1"] },
1315 apex_available: ["com.android.runtime"],
1316 }
1317
1318 cc_prebuilt_library_shared {
1319 name: "libclang_rt.hwasan-aarch64-android",
1320 no_libcrt: true,
1321 nocrt: true,
1322 stl: "none",
1323 system_shared_libs: [],
1324 srcs: [""],
1325 stubs: { versions: ["1"] },
1326
1327 sanitize: {
1328 never: true,
1329 },
1330 }
1331 `
1332 // override bp to use hard-coded names: com.android.runtime and libc
1333 fs["Android.bp"] = []byte(bp)
1334 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
1335
1336 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1337 })
1338
1339 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1340 "lib64/bionic/libc.so",
1341 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1342 })
1343
1344 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1345
1346 installed := hwasan.Description("install libclang_rt.hwasan")
1347 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1348
1349 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1350 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1351 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1352}
1353
Jooyung Han61b66e92020-03-21 14:21:46 +00001354func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1355 testcases := []struct {
1356 name string
1357 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001358 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001359 shouldLink string
1360 shouldNotLink []string
1361 }{
1362 {
Jiyong Park55549df2021-02-26 23:57:23 +09001363 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001364 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001365 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001366 shouldLink: "30",
1367 shouldNotLink: []string{"29"},
1368 },
1369 {
Jiyong Park55549df2021-02-26 23:57:23 +09001370 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001371 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001372 apexVariant: "apex29",
Jiyong Park55549df2021-02-26 23:57:23 +09001373 shouldLink: "30",
1374 shouldNotLink: []string{"29"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001375 },
1376 }
1377 for _, tc := range testcases {
1378 t.Run(tc.name, func(t *testing.T) {
1379 ctx, _ := testApex(t, `
1380 apex {
1381 name: "myapex",
1382 key: "myapex.key",
1383 use_vendor: true,
1384 native_shared_libs: ["mylib"],
Jooyung Han749dc692020-04-15 11:03:39 +09001385 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001386 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001387
Jooyung Han61b66e92020-03-21 14:21:46 +00001388 apex_key {
1389 name: "myapex.key",
1390 public_key: "testkey.avbpubkey",
1391 private_key: "testkey.pem",
1392 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001393
Jooyung Han61b66e92020-03-21 14:21:46 +00001394 cc_library {
1395 name: "mylib",
1396 srcs: ["mylib.cpp"],
1397 vendor_available: true,
1398 shared_libs: ["libbar"],
1399 system_shared_libs: [],
1400 stl: "none",
1401 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001402 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001403 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001404
Jooyung Han61b66e92020-03-21 14:21:46 +00001405 cc_library {
1406 name: "libbar",
1407 srcs: ["mylib.cpp"],
1408 system_shared_libs: [],
1409 stl: "none",
1410 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001411 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001412 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001413
Jooyung Han61b66e92020-03-21 14:21:46 +00001414 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001415 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001416 symbol_file: "",
1417 }
1418 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07001419 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Han61b66e92020-03-21 14:21:46 +00001420 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +09001421
Jooyung Han61b66e92020-03-21 14:21:46 +00001422 // Ensure that LLNDK dep is not included
1423 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1424 "lib64/mylib.so",
1425 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001426
Jooyung Han61b66e92020-03-21 14:21:46 +00001427 // Ensure that LLNDK dep is required
1428 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1429 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1430 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001431
Colin Crossaede88c2020-08-11 12:17:01 -07001432 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001433 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001434 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001435 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001436 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001437
Colin Crossaede88c2020-08-11 12:17:01 -07001438 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001439 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1440 })
1441 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001442}
1443
Jiyong Park25fc6a92018-11-18 18:02:45 +09001444func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001445 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001446 apex {
1447 name: "myapex",
1448 key: "myapex.key",
1449 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
1450 }
1451
1452 apex_key {
1453 name: "myapex.key",
1454 public_key: "testkey.avbpubkey",
1455 private_key: "testkey.pem",
1456 }
1457
1458 cc_library {
1459 name: "mylib",
1460 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001461 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001462 shared_libs: ["libdl#27"],
1463 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001464 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001465 }
1466
1467 cc_library_shared {
1468 name: "mylib_shared",
1469 srcs: ["mylib.cpp"],
1470 shared_libs: ["libdl#27"],
1471 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001472 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001473 }
1474
1475 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001476 name: "libBootstrap",
1477 srcs: ["mylib.cpp"],
1478 stl: "none",
1479 bootstrap: true,
1480 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001481 `)
1482
Sundong Ahnabb64432019-10-22 13:58:29 +09001483 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001484 copyCmds := apexRule.Args["copy_commands"]
1485
1486 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001487 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001488 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1489 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001490
1491 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001492 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001493
Colin Crossaede88c2020-08-11 12:17:01 -07001494 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1495 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1496 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001497
1498 // For dependency to libc
1499 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001500 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001501 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001502 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001503 // ... Cflags from stub is correctly exported to mylib
1504 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1505 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1506
1507 // For dependency to libm
1508 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001509 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001510 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001511 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001512 // ... and is not compiling with the stub
1513 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1514 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1515
1516 // For dependency to libdl
1517 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001518 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001519 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001520 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1521 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001522 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001523 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001524 // ... Cflags from stub is correctly exported to mylib
1525 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1526 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001527
1528 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001529 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1530 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1531 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1532 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001533}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001534
Jooyung Han749dc692020-04-15 11:03:39 +09001535func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001536 // there are three links between liba --> libz.
1537 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001538 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001539 // 3) (platform) -> liba -> libz : this should be non-stub link
1540 ctx, _ := testApex(t, `
1541 apex {
1542 name: "myapex",
1543 key: "myapex.key",
1544 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001545 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001546 }
1547
1548 apex {
1549 name: "otherapex",
1550 key: "myapex.key",
1551 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001552 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001553 }
1554
1555 apex_key {
1556 name: "myapex.key",
1557 public_key: "testkey.avbpubkey",
1558 private_key: "testkey.pem",
1559 }
1560
1561 cc_library {
1562 name: "libx",
1563 shared_libs: ["liba"],
1564 system_shared_libs: [],
1565 stl: "none",
1566 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001567 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001568 }
1569
1570 cc_library {
1571 name: "liby",
1572 shared_libs: ["liba"],
1573 system_shared_libs: [],
1574 stl: "none",
1575 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001576 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001577 }
1578
1579 cc_library {
1580 name: "liba",
1581 shared_libs: ["libz"],
1582 system_shared_libs: [],
1583 stl: "none",
1584 apex_available: [
1585 "//apex_available:anyapex",
1586 "//apex_available:platform",
1587 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001588 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001589 }
1590
1591 cc_library {
1592 name: "libz",
1593 system_shared_libs: [],
1594 stl: "none",
1595 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001596 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001597 },
1598 }
Jooyung Han749dc692020-04-15 11:03:39 +09001599 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001600
1601 expectLink := func(from, from_variant, to, to_variant string) {
1602 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1603 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1604 }
1605 expectNoLink := func(from, from_variant, to, to_variant string) {
1606 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1607 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1608 }
1609 // platform liba is linked to non-stub version
1610 expectLink("liba", "shared", "libz", "shared")
Jiyong Park55549df2021-02-26 23:57:23 +09001611 // liba in myapex is linked to #30
1612 expectLink("liba", "shared_apex29", "libz", "shared_30")
1613 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001614 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001615 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001616 expectLink("liba", "shared_apex30", "libz", "shared_30")
1617 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1618 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001619}
1620
Jooyung Hanaed150d2020-04-02 01:41:41 +09001621func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
1622 ctx, _ := testApex(t, `
1623 apex {
1624 name: "myapex",
1625 key: "myapex.key",
1626 native_shared_libs: ["libx"],
1627 min_sdk_version: "R",
1628 }
1629
1630 apex_key {
1631 name: "myapex.key",
1632 public_key: "testkey.avbpubkey",
1633 private_key: "testkey.pem",
1634 }
1635
1636 cc_library {
1637 name: "libx",
1638 shared_libs: ["libz"],
1639 system_shared_libs: [],
1640 stl: "none",
1641 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001642 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001643 }
1644
1645 cc_library {
1646 name: "libz",
1647 system_shared_libs: [],
1648 stl: "none",
1649 stubs: {
1650 versions: ["29", "R"],
1651 },
1652 }
1653 `, func(fs map[string][]byte, config android.Config) {
1654 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
1655 })
1656
1657 expectLink := func(from, from_variant, to, to_variant string) {
1658 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1659 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1660 }
1661 expectNoLink := func(from, from_variant, to, to_variant string) {
1662 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1663 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1664 }
Dan Albertc8060532020-07-22 22:32:17 -07001665 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001666 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1667 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001668}
1669
Jooyung Han749dc692020-04-15 11:03:39 +09001670func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001671 ctx, _ := testApex(t, `
1672 apex {
1673 name: "myapex",
1674 key: "myapex.key",
1675 native_shared_libs: ["libx"],
1676 }
1677
1678 apex_key {
1679 name: "myapex.key",
1680 public_key: "testkey.avbpubkey",
1681 private_key: "testkey.pem",
1682 }
1683
1684 cc_library {
1685 name: "libx",
1686 shared_libs: ["libz"],
1687 system_shared_libs: [],
1688 stl: "none",
1689 apex_available: [ "myapex" ],
1690 }
1691
1692 cc_library {
1693 name: "libz",
1694 system_shared_libs: [],
1695 stl: "none",
1696 stubs: {
1697 versions: ["1", "2"],
1698 },
1699 }
1700 `)
1701
1702 expectLink := func(from, from_variant, to, to_variant string) {
1703 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1704 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1705 }
1706 expectNoLink := func(from, from_variant, to, to_variant string) {
1707 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1708 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1709 }
Colin Crossaede88c2020-08-11 12:17:01 -07001710 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1711 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1712 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001713}
1714
1715func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1716 ctx, _ := testApex(t, `
1717 apex {
1718 name: "myapex",
1719 key: "myapex.key",
1720 native_shared_libs: ["libx"],
1721 }
1722
1723 apex_key {
1724 name: "myapex.key",
1725 public_key: "testkey.avbpubkey",
1726 private_key: "testkey.pem",
1727 }
1728
1729 cc_library {
1730 name: "libx",
1731 system_shared_libs: [],
1732 stl: "none",
1733 apex_available: [ "myapex" ],
1734 stubs: {
1735 versions: ["1", "2"],
1736 },
1737 }
1738
1739 cc_library {
1740 name: "libz",
1741 shared_libs: ["libx"],
1742 system_shared_libs: [],
1743 stl: "none",
1744 }
1745 `)
1746
1747 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001748 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001749 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1750 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1751 }
1752 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001753 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001754 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1755 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1756 }
1757 expectLink("libz", "shared", "libx", "shared_2")
1758 expectNoLink("libz", "shared", "libz", "shared_1")
1759 expectNoLink("libz", "shared", "libz", "shared")
1760}
1761
Jooyung Han75568392020-03-20 04:29:24 +09001762func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001763 ctx, _ := testApex(t, `
1764 apex {
1765 name: "myapex",
1766 key: "myapex.key",
1767 native_shared_libs: ["libx"],
1768 min_sdk_version: "29",
1769 }
1770
1771 apex_key {
1772 name: "myapex.key",
1773 public_key: "testkey.avbpubkey",
1774 private_key: "testkey.pem",
1775 }
1776
1777 cc_library {
1778 name: "libx",
1779 shared_libs: ["libbar"],
1780 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001781 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001782 }
1783
1784 cc_library {
1785 name: "libbar",
1786 stubs: {
1787 versions: ["29", "30"],
1788 },
1789 }
Jooyung Han75568392020-03-20 04:29:24 +09001790 `, func(fs map[string][]byte, config android.Config) {
1791 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1792 })
Jooyung Han03b51852020-02-26 22:45:42 +09001793 expectLink := func(from, from_variant, to, to_variant string) {
1794 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1795 libFlags := ld.Args["libFlags"]
1796 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1797 }
Colin Crossaede88c2020-08-11 12:17:01 -07001798 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001799}
1800
Jooyung Han75568392020-03-20 04:29:24 +09001801func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001802 ctx, _ := testApex(t, `
1803 apex {
1804 name: "myapex",
1805 key: "myapex.key",
1806 native_shared_libs: ["libx"],
1807 min_sdk_version: "29",
1808 }
1809
1810 apex_key {
1811 name: "myapex.key",
1812 public_key: "testkey.avbpubkey",
1813 private_key: "testkey.pem",
1814 }
1815
1816 cc_library {
1817 name: "libx",
1818 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001819 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001820 }
Jooyung Han75568392020-03-20 04:29:24 +09001821 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001822
1823 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001824 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001825 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001826 // note that platform variant is not.
1827 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001828 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001829}
1830
Jooyung Han749dc692020-04-15 11:03:39 +09001831func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1832 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001833 apex {
1834 name: "myapex",
1835 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001836 native_shared_libs: ["mylib"],
1837 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001838 }
1839
1840 apex_key {
1841 name: "myapex.key",
1842 public_key: "testkey.avbpubkey",
1843 private_key: "testkey.pem",
1844 }
Jooyung Han749dc692020-04-15 11:03:39 +09001845
1846 cc_library {
1847 name: "mylib",
1848 srcs: ["mylib.cpp"],
1849 system_shared_libs: [],
1850 stl: "none",
1851 apex_available: [
1852 "myapex",
1853 ],
1854 min_sdk_version: "30",
1855 }
1856 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001857
1858 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1859 apex {
1860 name: "myapex",
1861 key: "myapex.key",
1862 native_shared_libs: ["libfoo.ffi"],
1863 min_sdk_version: "29",
1864 }
1865
1866 apex_key {
1867 name: "myapex.key",
1868 public_key: "testkey.avbpubkey",
1869 private_key: "testkey.pem",
1870 }
1871
1872 rust_ffi_shared {
1873 name: "libfoo.ffi",
1874 srcs: ["foo.rs"],
1875 crate_name: "foo",
1876 apex_available: [
1877 "myapex",
1878 ],
1879 min_sdk_version: "30",
1880 }
1881 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001882}
1883
1884func TestApexMinSdkVersion_Okay(t *testing.T) {
1885 testApex(t, `
1886 apex {
1887 name: "myapex",
1888 key: "myapex.key",
1889 native_shared_libs: ["libfoo"],
1890 java_libs: ["libbar"],
1891 min_sdk_version: "29",
1892 }
1893
1894 apex_key {
1895 name: "myapex.key",
1896 public_key: "testkey.avbpubkey",
1897 private_key: "testkey.pem",
1898 }
1899
1900 cc_library {
1901 name: "libfoo",
1902 srcs: ["mylib.cpp"],
1903 shared_libs: ["libfoo_dep"],
1904 apex_available: ["myapex"],
1905 min_sdk_version: "29",
1906 }
1907
1908 cc_library {
1909 name: "libfoo_dep",
1910 srcs: ["mylib.cpp"],
1911 apex_available: ["myapex"],
1912 min_sdk_version: "29",
1913 }
1914
1915 java_library {
1916 name: "libbar",
1917 sdk_version: "current",
1918 srcs: ["a.java"],
1919 static_libs: ["libbar_dep"],
1920 apex_available: ["myapex"],
1921 min_sdk_version: "29",
1922 }
1923
1924 java_library {
1925 name: "libbar_dep",
1926 sdk_version: "current",
1927 srcs: ["a.java"],
1928 apex_available: ["myapex"],
1929 min_sdk_version: "29",
1930 }
Jooyung Han03b51852020-02-26 22:45:42 +09001931 `)
1932}
1933
Artur Satayev8cf899a2020-04-15 17:29:42 +01001934func TestJavaStableSdkVersion(t *testing.T) {
1935 testCases := []struct {
1936 name string
1937 expectedError string
1938 bp string
1939 }{
1940 {
1941 name: "Non-updatable apex with non-stable dep",
1942 bp: `
1943 apex {
1944 name: "myapex",
1945 java_libs: ["myjar"],
1946 key: "myapex.key",
1947 }
1948 apex_key {
1949 name: "myapex.key",
1950 public_key: "testkey.avbpubkey",
1951 private_key: "testkey.pem",
1952 }
1953 java_library {
1954 name: "myjar",
1955 srcs: ["foo/bar/MyClass.java"],
1956 sdk_version: "core_platform",
1957 apex_available: ["myapex"],
1958 }
1959 `,
1960 },
1961 {
1962 name: "Updatable apex with stable dep",
1963 bp: `
1964 apex {
1965 name: "myapex",
1966 java_libs: ["myjar"],
1967 key: "myapex.key",
1968 updatable: true,
1969 min_sdk_version: "29",
1970 }
1971 apex_key {
1972 name: "myapex.key",
1973 public_key: "testkey.avbpubkey",
1974 private_key: "testkey.pem",
1975 }
1976 java_library {
1977 name: "myjar",
1978 srcs: ["foo/bar/MyClass.java"],
1979 sdk_version: "current",
1980 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001981 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001982 }
1983 `,
1984 },
1985 {
1986 name: "Updatable apex with non-stable dep",
1987 expectedError: "cannot depend on \"myjar\"",
1988 bp: `
1989 apex {
1990 name: "myapex",
1991 java_libs: ["myjar"],
1992 key: "myapex.key",
1993 updatable: true,
1994 }
1995 apex_key {
1996 name: "myapex.key",
1997 public_key: "testkey.avbpubkey",
1998 private_key: "testkey.pem",
1999 }
2000 java_library {
2001 name: "myjar",
2002 srcs: ["foo/bar/MyClass.java"],
2003 sdk_version: "core_platform",
2004 apex_available: ["myapex"],
2005 }
2006 `,
2007 },
2008 {
2009 name: "Updatable apex with non-stable transitive dep",
Jiyong Park670e0f62021-02-18 13:10:18 +09002010 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002011 bp: `
2012 apex {
2013 name: "myapex",
2014 java_libs: ["myjar"],
2015 key: "myapex.key",
2016 updatable: true,
2017 }
2018 apex_key {
2019 name: "myapex.key",
2020 public_key: "testkey.avbpubkey",
2021 private_key: "testkey.pem",
2022 }
2023 java_library {
2024 name: "myjar",
2025 srcs: ["foo/bar/MyClass.java"],
2026 sdk_version: "current",
2027 apex_available: ["myapex"],
2028 static_libs: ["transitive-jar"],
2029 }
2030 java_library {
2031 name: "transitive-jar",
2032 srcs: ["foo/bar/MyClass.java"],
2033 sdk_version: "core_platform",
2034 apex_available: ["myapex"],
2035 }
2036 `,
2037 },
2038 }
2039
2040 for _, test := range testCases {
2041 t.Run(test.name, func(t *testing.T) {
2042 if test.expectedError == "" {
2043 testApex(t, test.bp)
2044 } else {
2045 testApexError(t, test.expectedError, test.bp)
2046 }
2047 })
2048 }
2049}
2050
Jooyung Han749dc692020-04-15 11:03:39 +09002051func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2052 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2053 apex {
2054 name: "myapex",
2055 key: "myapex.key",
2056 native_shared_libs: ["mylib"],
2057 min_sdk_version: "29",
2058 }
2059
2060 apex_key {
2061 name: "myapex.key",
2062 public_key: "testkey.avbpubkey",
2063 private_key: "testkey.pem",
2064 }
2065
2066 cc_library {
2067 name: "mylib",
2068 srcs: ["mylib.cpp"],
2069 shared_libs: ["mylib2"],
2070 system_shared_libs: [],
2071 stl: "none",
2072 apex_available: [
2073 "myapex",
2074 ],
2075 min_sdk_version: "29",
2076 }
2077
2078 // indirect part of the apex
2079 cc_library {
2080 name: "mylib2",
2081 srcs: ["mylib.cpp"],
2082 system_shared_libs: [],
2083 stl: "none",
2084 apex_available: [
2085 "myapex",
2086 ],
2087 min_sdk_version: "30",
2088 }
2089 `)
2090}
2091
2092func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2093 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2094 apex {
2095 name: "myapex",
2096 key: "myapex.key",
2097 apps: ["AppFoo"],
2098 min_sdk_version: "29",
2099 }
2100
2101 apex_key {
2102 name: "myapex.key",
2103 public_key: "testkey.avbpubkey",
2104 private_key: "testkey.pem",
2105 }
2106
2107 android_app {
2108 name: "AppFoo",
2109 srcs: ["foo/bar/MyClass.java"],
2110 sdk_version: "current",
2111 min_sdk_version: "29",
2112 system_modules: "none",
2113 stl: "none",
2114 static_libs: ["bar"],
2115 apex_available: [ "myapex" ],
2116 }
2117
2118 java_library {
2119 name: "bar",
2120 sdk_version: "current",
2121 srcs: ["a.java"],
2122 apex_available: [ "myapex" ],
2123 }
2124 `)
2125}
2126
2127func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
2128 ctx, _ := testApex(t, `
2129 apex {
2130 name: "myapex",
2131 key: "myapex.key",
2132 native_shared_libs: ["mylib"],
2133 min_sdk_version: "29",
2134 }
2135
2136 apex_key {
2137 name: "myapex.key",
2138 public_key: "testkey.avbpubkey",
2139 private_key: "testkey.pem",
2140 }
2141
Jiyong Park55549df2021-02-26 23:57:23 +09002142 // mylib in myapex will link to mylib2#30
Jooyung Han749dc692020-04-15 11:03:39 +09002143 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2144 cc_library {
2145 name: "mylib",
2146 srcs: ["mylib.cpp"],
2147 shared_libs: ["mylib2"],
2148 system_shared_libs: [],
2149 stl: "none",
2150 apex_available: ["myapex", "otherapex"],
2151 min_sdk_version: "29",
2152 }
2153
2154 cc_library {
2155 name: "mylib2",
2156 srcs: ["mylib.cpp"],
2157 system_shared_libs: [],
2158 stl: "none",
2159 apex_available: ["otherapex"],
2160 stubs: { versions: ["29", "30"] },
2161 min_sdk_version: "30",
2162 }
2163
2164 apex {
2165 name: "otherapex",
2166 key: "myapex.key",
2167 native_shared_libs: ["mylib", "mylib2"],
2168 min_sdk_version: "30",
2169 }
2170 `)
2171 expectLink := func(from, from_variant, to, to_variant string) {
2172 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2173 libFlags := ld.Args["libFlags"]
2174 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2175 }
Jiyong Park55549df2021-02-26 23:57:23 +09002176 expectLink("mylib", "shared_apex29", "mylib2", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07002177 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002178}
2179
Jooyung Haned124c32021-01-26 11:43:46 +09002180func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
2181 withSAsActiveCodeNames := func(fs map[string][]byte, config android.Config) {
2182 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("S")
2183 config.TestProductVariables.Platform_version_active_codenames = []string{"S"}
2184 }
2185 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2186 apex {
2187 name: "myapex",
2188 key: "myapex.key",
2189 native_shared_libs: ["libfoo"],
2190 min_sdk_version: "S",
2191 }
2192 apex_key {
2193 name: "myapex.key",
2194 public_key: "testkey.avbpubkey",
2195 private_key: "testkey.pem",
2196 }
2197 cc_library {
2198 name: "libfoo",
2199 shared_libs: ["libbar"],
2200 apex_available: ["myapex"],
2201 min_sdk_version: "29",
2202 }
2203 cc_library {
2204 name: "libbar",
2205 apex_available: ["myapex"],
2206 }
2207 `, withSAsActiveCodeNames)
2208}
2209
2210func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
2211 withSAsActiveCodeNames := func(fs map[string][]byte, config android.Config) {
2212 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("S")
2213 config.TestProductVariables.Platform_version_active_codenames = []string{"S", "T"}
2214 }
2215 ctx, _ := testApex(t, `
2216 apex {
2217 name: "myapex",
2218 key: "myapex.key",
2219 native_shared_libs: ["libfoo"],
2220 min_sdk_version: "S",
2221 }
2222 apex_key {
2223 name: "myapex.key",
2224 public_key: "testkey.avbpubkey",
2225 private_key: "testkey.pem",
2226 }
2227 cc_library {
2228 name: "libfoo",
2229 shared_libs: ["libbar"],
2230 apex_available: ["myapex"],
2231 min_sdk_version: "S",
2232 }
2233 cc_library {
2234 name: "libbar",
2235 stubs: {
2236 symbol_file: "libbar.map.txt",
2237 versions: ["30", "S", "T"],
2238 },
2239 }
2240 `, withSAsActiveCodeNames)
2241
2242 // ensure libfoo is linked with "S" version of libbar stub
2243 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2244 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09002245 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_T/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002246}
2247
Jiyong Park7c2ee712018-12-07 00:42:25 +09002248func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002249 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002250 apex {
2251 name: "myapex",
2252 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002253 native_shared_libs: ["mylib"],
2254 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002255 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002256 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09002257 }
2258
2259 apex_key {
2260 name: "myapex.key",
2261 public_key: "testkey.avbpubkey",
2262 private_key: "testkey.pem",
2263 }
2264
2265 prebuilt_etc {
2266 name: "myetc",
2267 src: "myprebuilt",
2268 sub_dir: "foo/bar",
2269 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002270
2271 cc_library {
2272 name: "mylib",
2273 srcs: ["mylib.cpp"],
2274 relative_install_path: "foo/bar",
2275 system_shared_libs: [],
2276 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002277 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002278 }
2279
2280 cc_binary {
2281 name: "mybin",
2282 srcs: ["mylib.cpp"],
2283 relative_install_path: "foo/bar",
2284 system_shared_libs: [],
2285 static_executable: true,
2286 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002287 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002288 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002289 `)
2290
Sundong Ahnabb64432019-10-22 13:58:29 +09002291 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002292 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2293
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002294 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002295 ensureListContains(t, dirs, "etc")
2296 ensureListContains(t, dirs, "etc/foo")
2297 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002298 ensureListContains(t, dirs, "lib64")
2299 ensureListContains(t, dirs, "lib64/foo")
2300 ensureListContains(t, dirs, "lib64/foo/bar")
2301 ensureListContains(t, dirs, "lib")
2302 ensureListContains(t, dirs, "lib/foo")
2303 ensureListContains(t, dirs, "lib/foo/bar")
2304
Jiyong Parkbd13e442019-03-15 18:10:35 +09002305 ensureListContains(t, dirs, "bin")
2306 ensureListContains(t, dirs, "bin/foo")
2307 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002308}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002309
Jooyung Han35155c42020-02-06 17:33:20 +09002310func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
2311 ctx, _ := testApex(t, `
2312 apex {
2313 name: "myapex",
2314 key: "myapex.key",
2315 multilib: {
2316 both: {
2317 native_shared_libs: ["mylib"],
2318 binaries: ["mybin"],
2319 },
2320 },
2321 compile_multilib: "both",
2322 native_bridge_supported: true,
2323 }
2324
2325 apex_key {
2326 name: "myapex.key",
2327 public_key: "testkey.avbpubkey",
2328 private_key: "testkey.pem",
2329 }
2330
2331 cc_library {
2332 name: "mylib",
2333 relative_install_path: "foo/bar",
2334 system_shared_libs: [],
2335 stl: "none",
2336 apex_available: [ "myapex" ],
2337 native_bridge_supported: true,
2338 }
2339
2340 cc_binary {
2341 name: "mybin",
2342 relative_install_path: "foo/bar",
2343 system_shared_libs: [],
2344 static_executable: true,
2345 stl: "none",
2346 apex_available: [ "myapex" ],
2347 native_bridge_supported: true,
2348 compile_multilib: "both", // default is "first" for binary
2349 multilib: {
2350 lib64: {
2351 suffix: "64",
2352 },
2353 },
2354 }
2355 `, withNativeBridgeEnabled)
2356 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2357 "bin/foo/bar/mybin",
2358 "bin/foo/bar/mybin64",
2359 "bin/arm/foo/bar/mybin",
2360 "bin/arm64/foo/bar/mybin64",
2361 "lib/foo/bar/mylib.so",
2362 "lib/arm/foo/bar/mylib.so",
2363 "lib64/foo/bar/mylib.so",
2364 "lib64/arm64/foo/bar/mylib.so",
2365 })
2366}
2367
Jiyong Parkda6eb592018-12-19 17:12:36 +09002368func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002369 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002370 apex {
2371 name: "myapex",
2372 key: "myapex.key",
2373 native_shared_libs: ["mylib"],
2374 use_vendor: true,
2375 }
2376
2377 apex_key {
2378 name: "myapex.key",
2379 public_key: "testkey.avbpubkey",
2380 private_key: "testkey.pem",
2381 }
2382
2383 cc_library {
2384 name: "mylib",
2385 srcs: ["mylib.cpp"],
2386 shared_libs: ["mylib2"],
2387 system_shared_libs: [],
2388 vendor_available: true,
2389 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002390 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002391 }
2392
2393 cc_library {
2394 name: "mylib2",
2395 srcs: ["mylib.cpp"],
2396 system_shared_libs: [],
2397 vendor_available: true,
2398 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002399 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002400 }
Jooyung Handc782442019-11-01 03:14:38 +09002401 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002402 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002403 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09002404
2405 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002406 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002407 for _, implicit := range i.Implicits {
2408 inputsList = append(inputsList, implicit.String())
2409 }
2410 }
2411 inputsString := strings.Join(inputsList, " ")
2412
2413 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002414 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2415 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002416
2417 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002418 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2419 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002420}
Jiyong Park16e91a02018-12-20 18:18:08 +09002421
Jooyung Han85d61762020-06-24 23:50:26 +09002422func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002423 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2424 apex {
2425 name: "myapex",
2426 key: "myapex.key",
2427 use_vendor: true,
2428 }
2429 apex_key {
2430 name: "myapex.key",
2431 public_key: "testkey.avbpubkey",
2432 private_key: "testkey.pem",
2433 }
2434 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002435 setUseVendorAllowListForTest(config, []string{""})
Jooyung Handc782442019-11-01 03:14:38 +09002436 })
Colin Cross440e0d02020-06-11 11:32:11 -07002437 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002438 testApex(t, `
2439 apex {
2440 name: "myapex",
2441 key: "myapex.key",
2442 use_vendor: true,
2443 }
2444 apex_key {
2445 name: "myapex.key",
2446 public_key: "testkey.avbpubkey",
2447 private_key: "testkey.pem",
2448 }
2449 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002450 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002451 })
2452}
2453
Jooyung Han5c998b92019-06-27 11:30:33 +09002454func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2455 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2456 apex {
2457 name: "myapex",
2458 key: "myapex.key",
2459 native_shared_libs: ["mylib"],
2460 use_vendor: true,
2461 }
2462
2463 apex_key {
2464 name: "myapex.key",
2465 public_key: "testkey.avbpubkey",
2466 private_key: "testkey.pem",
2467 }
2468
2469 cc_library {
2470 name: "mylib",
2471 srcs: ["mylib.cpp"],
2472 system_shared_libs: [],
2473 stl: "none",
2474 }
2475 `)
2476}
2477
Jooyung Han85d61762020-06-24 23:50:26 +09002478func TestVendorApex(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002479 ctx, _ := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002480 apex {
2481 name: "myapex",
2482 key: "myapex.key",
2483 binaries: ["mybin"],
2484 vendor: true,
2485 }
2486 apex_key {
2487 name: "myapex.key",
2488 public_key: "testkey.avbpubkey",
2489 private_key: "testkey.pem",
2490 }
2491 cc_binary {
2492 name: "mybin",
2493 vendor: true,
2494 shared_libs: ["libfoo"],
2495 }
2496 cc_library {
2497 name: "libfoo",
2498 proprietary: true,
2499 }
2500 `)
2501
2502 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2503 "bin/mybin",
2504 "lib64/libfoo.so",
2505 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2506 "lib64/libc++.so",
2507 })
2508
2509 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002510 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002511 name := apexBundle.BaseModuleName()
2512 prefix := "TARGET_"
2513 var builder strings.Builder
2514 data.Custom(&builder, name, prefix, "", data)
2515 androidMk := builder.String()
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002516 installPath := path.Join(buildDir, "../target/product/test_device/vendor/apex")
2517 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002518
2519 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2520 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2521 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002522}
2523
Jooyung Handf78e212020-07-22 15:54:47 +09002524func TestVendorApex_use_vndk_as_stable(t *testing.T) {
2525 ctx, _ := testApex(t, `
2526 apex {
2527 name: "myapex",
2528 key: "myapex.key",
2529 binaries: ["mybin"],
2530 vendor: true,
2531 use_vndk_as_stable: true,
2532 }
2533 apex_key {
2534 name: "myapex.key",
2535 public_key: "testkey.avbpubkey",
2536 private_key: "testkey.pem",
2537 }
2538 cc_binary {
2539 name: "mybin",
2540 vendor: true,
2541 shared_libs: ["libvndk", "libvendor"],
2542 }
2543 cc_library {
2544 name: "libvndk",
2545 vndk: {
2546 enabled: true,
2547 },
2548 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002549 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002550 }
2551 cc_library {
2552 name: "libvendor",
2553 vendor: true,
2554 }
2555 `)
2556
2557 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2558
Colin Crossaede88c2020-08-11 12:17:01 -07002559 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002560 libs := names(ldRule.Args["libFlags"])
2561 // VNDK libs(libvndk/libc++) as they are
2562 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2563 ensureListContains(t, libs, buildDir+"/.intermediates/libc++/"+vendorVariant+"_shared/libc++.so")
2564 // non-stable Vendor libs as APEX variants
Colin Crossaede88c2020-08-11 12:17:01 -07002565 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002566
2567 // VNDK libs are not included when use_vndk_as_stable: true
2568 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2569 "bin/mybin",
2570 "lib64/libvendor.so",
2571 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002572
2573 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2574 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2575 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002576}
2577
Jooyung Han8e5685d2020-09-21 11:02:57 +09002578func TestApex_withPrebuiltFirmware(t *testing.T) {
2579 testCases := []struct {
2580 name string
2581 additionalProp string
2582 }{
2583 {"system apex with prebuilt_firmware", ""},
2584 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2585 }
2586 for _, tc := range testCases {
2587 t.Run(tc.name, func(t *testing.T) {
2588 ctx, _ := testApex(t, `
2589 apex {
2590 name: "myapex",
2591 key: "myapex.key",
2592 prebuilts: ["myfirmware"],
2593 `+tc.additionalProp+`
2594 }
2595 apex_key {
2596 name: "myapex.key",
2597 public_key: "testkey.avbpubkey",
2598 private_key: "testkey.pem",
2599 }
2600 prebuilt_firmware {
2601 name: "myfirmware",
2602 src: "myfirmware.bin",
2603 filename_from_src: true,
2604 `+tc.additionalProp+`
2605 }
2606 `)
2607 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2608 "etc/firmware/myfirmware.bin",
2609 })
2610 })
2611 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002612}
2613
Jooyung Hanefb184e2020-06-25 17:14:25 +09002614func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002615 ctx, _ := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002616 apex {
2617 name: "myapex",
2618 key: "myapex.key",
2619 use_vendor: true,
2620 native_shared_libs: ["mylib"],
2621 }
2622
2623 apex_key {
2624 name: "myapex.key",
2625 public_key: "testkey.avbpubkey",
2626 private_key: "testkey.pem",
2627 }
2628
2629 cc_library {
2630 name: "mylib",
2631 vendor_available: true,
2632 apex_available: ["myapex"],
2633 }
2634 `, func(fs map[string][]byte, config android.Config) {
2635 setUseVendorAllowListForTest(config, []string{"myapex"})
2636 })
2637
2638 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002639 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002640 name := apexBundle.BaseModuleName()
2641 prefix := "TARGET_"
2642 var builder strings.Builder
2643 data.Custom(&builder, name, prefix, "", data)
2644 androidMk := builder.String()
2645 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2646}
2647
2648func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002649 ctx, _ := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002650 apex {
2651 name: "myapex",
2652 key: "myapex.key",
2653 vendor: true,
2654 native_shared_libs: ["mylib"],
2655 }
2656
2657 apex_key {
2658 name: "myapex.key",
2659 public_key: "testkey.avbpubkey",
2660 private_key: "testkey.pem",
2661 }
2662
2663 cc_library {
2664 name: "mylib",
2665 vendor_available: true,
2666 }
2667 `)
2668
2669 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002670 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002671 name := apexBundle.BaseModuleName()
2672 prefix := "TARGET_"
2673 var builder strings.Builder
2674 data.Custom(&builder, name, prefix, "", data)
2675 androidMk := builder.String()
2676 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2677}
2678
Jooyung Han2ed99d02020-06-24 23:26:26 +09002679func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002680 ctx, _ := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002681 apex {
2682 name: "myapex",
2683 key: "myapex.key",
2684 vintf_fragments: ["fragment.xml"],
2685 init_rc: ["init.rc"],
2686 }
2687 apex_key {
2688 name: "myapex.key",
2689 public_key: "testkey.avbpubkey",
2690 private_key: "testkey.pem",
2691 }
2692 cc_binary {
2693 name: "mybin",
2694 }
2695 `)
2696
2697 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002698 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002699 name := apexBundle.BaseModuleName()
2700 prefix := "TARGET_"
2701 var builder strings.Builder
2702 data.Custom(&builder, name, prefix, "", data)
2703 androidMk := builder.String()
2704 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2705 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2706}
2707
Jiyong Park16e91a02018-12-20 18:18:08 +09002708func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002709 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002710 apex {
2711 name: "myapex",
2712 key: "myapex.key",
2713 native_shared_libs: ["mylib"],
2714 }
2715
2716 apex_key {
2717 name: "myapex.key",
2718 public_key: "testkey.avbpubkey",
2719 private_key: "testkey.pem",
2720 }
2721
2722 cc_library {
2723 name: "mylib",
2724 srcs: ["mylib.cpp"],
2725 system_shared_libs: [],
2726 stl: "none",
2727 stubs: {
2728 versions: ["1", "2", "3"],
2729 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002730 apex_available: [
2731 "//apex_available:platform",
2732 "myapex",
2733 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002734 }
2735
2736 cc_binary {
2737 name: "not_in_apex",
2738 srcs: ["mylib.cpp"],
2739 static_libs: ["mylib"],
2740 static_executable: true,
2741 system_shared_libs: [],
2742 stl: "none",
2743 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002744 `)
2745
Colin Cross7113d202019-11-20 16:39:12 -08002746 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002747
2748 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002749 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002750}
Jiyong Park9335a262018-12-24 11:31:58 +09002751
2752func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002753 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002754 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002755 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002756 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002757 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002758 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002759 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09002760 }
2761
2762 cc_library {
2763 name: "mylib",
2764 srcs: ["mylib.cpp"],
2765 system_shared_libs: [],
2766 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002767 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002768 }
2769
2770 apex_key {
2771 name: "myapex.key",
2772 public_key: "testkey.avbpubkey",
2773 private_key: "testkey.pem",
2774 }
2775
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002776 android_app_certificate {
2777 name: "myapex.certificate",
2778 certificate: "testkey",
2779 }
2780
2781 android_app_certificate {
2782 name: "myapex.certificate.override",
2783 certificate: "testkey.override",
2784 }
2785
Jiyong Park9335a262018-12-24 11:31:58 +09002786 `)
2787
2788 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002789 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002790
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002791 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2792 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002793 "vendor/foo/devkeys/testkey.avbpubkey")
2794 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002795 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2796 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002797 "vendor/foo/devkeys/testkey.pem")
2798 }
2799
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002800 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002801 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002802 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002803 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002804 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002805 }
2806}
Jiyong Park58e364a2019-01-19 19:24:06 +09002807
Jooyung Hanf121a652019-12-17 14:30:11 +09002808func TestCertificate(t *testing.T) {
2809 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
2810 ctx, _ := testApex(t, `
2811 apex {
2812 name: "myapex",
2813 key: "myapex.key",
2814 }
2815 apex_key {
2816 name: "myapex.key",
2817 public_key: "testkey.avbpubkey",
2818 private_key: "testkey.pem",
2819 }`)
2820 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2821 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2822 if actual := rule.Args["certificates"]; actual != expected {
2823 t.Errorf("certificates should be %q, not %q", expected, actual)
2824 }
2825 })
2826 t.Run("override when unspecified", func(t *testing.T) {
2827 ctx, _ := testApex(t, `
2828 apex {
2829 name: "myapex_keytest",
2830 key: "myapex.key",
2831 file_contexts: ":myapex-file_contexts",
2832 }
2833 apex_key {
2834 name: "myapex.key",
2835 public_key: "testkey.avbpubkey",
2836 private_key: "testkey.pem",
2837 }
2838 android_app_certificate {
2839 name: "myapex.certificate.override",
2840 certificate: "testkey.override",
2841 }`)
2842 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2843 expected := "testkey.override.x509.pem testkey.override.pk8"
2844 if actual := rule.Args["certificates"]; actual != expected {
2845 t.Errorf("certificates should be %q, not %q", expected, actual)
2846 }
2847 })
2848 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
2849 ctx, _ := testApex(t, `
2850 apex {
2851 name: "myapex",
2852 key: "myapex.key",
2853 certificate: ":myapex.certificate",
2854 }
2855 apex_key {
2856 name: "myapex.key",
2857 public_key: "testkey.avbpubkey",
2858 private_key: "testkey.pem",
2859 }
2860 android_app_certificate {
2861 name: "myapex.certificate",
2862 certificate: "testkey",
2863 }`)
2864 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2865 expected := "testkey.x509.pem testkey.pk8"
2866 if actual := rule.Args["certificates"]; actual != expected {
2867 t.Errorf("certificates should be %q, not %q", expected, actual)
2868 }
2869 })
2870 t.Run("override when specifiec as <:module>", func(t *testing.T) {
2871 ctx, _ := testApex(t, `
2872 apex {
2873 name: "myapex_keytest",
2874 key: "myapex.key",
2875 file_contexts: ":myapex-file_contexts",
2876 certificate: ":myapex.certificate",
2877 }
2878 apex_key {
2879 name: "myapex.key",
2880 public_key: "testkey.avbpubkey",
2881 private_key: "testkey.pem",
2882 }
2883 android_app_certificate {
2884 name: "myapex.certificate.override",
2885 certificate: "testkey.override",
2886 }`)
2887 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2888 expected := "testkey.override.x509.pem testkey.override.pk8"
2889 if actual := rule.Args["certificates"]; actual != expected {
2890 t.Errorf("certificates should be %q, not %q", expected, actual)
2891 }
2892 })
2893 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
2894 ctx, _ := testApex(t, `
2895 apex {
2896 name: "myapex",
2897 key: "myapex.key",
2898 certificate: "testkey",
2899 }
2900 apex_key {
2901 name: "myapex.key",
2902 public_key: "testkey.avbpubkey",
2903 private_key: "testkey.pem",
2904 }`)
2905 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2906 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2907 if actual := rule.Args["certificates"]; actual != expected {
2908 t.Errorf("certificates should be %q, not %q", expected, actual)
2909 }
2910 })
2911 t.Run("override when specified as <name>", func(t *testing.T) {
2912 ctx, _ := testApex(t, `
2913 apex {
2914 name: "myapex_keytest",
2915 key: "myapex.key",
2916 file_contexts: ":myapex-file_contexts",
2917 certificate: "testkey",
2918 }
2919 apex_key {
2920 name: "myapex.key",
2921 public_key: "testkey.avbpubkey",
2922 private_key: "testkey.pem",
2923 }
2924 android_app_certificate {
2925 name: "myapex.certificate.override",
2926 certificate: "testkey.override",
2927 }`)
2928 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2929 expected := "testkey.override.x509.pem testkey.override.pk8"
2930 if actual := rule.Args["certificates"]; actual != expected {
2931 t.Errorf("certificates should be %q, not %q", expected, actual)
2932 }
2933 })
2934}
2935
Jiyong Park58e364a2019-01-19 19:24:06 +09002936func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002937 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002938 apex {
2939 name: "myapex",
2940 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002941 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09002942 }
2943
2944 apex {
2945 name: "otherapex",
2946 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002947 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002948 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002949 }
2950
2951 apex_key {
2952 name: "myapex.key",
2953 public_key: "testkey.avbpubkey",
2954 private_key: "testkey.pem",
2955 }
2956
2957 cc_library {
2958 name: "mylib",
2959 srcs: ["mylib.cpp"],
2960 system_shared_libs: [],
2961 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002962 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002963 "myapex",
2964 "otherapex",
2965 ],
Jooyung Han24282772020-03-21 23:20:55 +09002966 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002967 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002968 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002969 cc_library {
2970 name: "mylib2",
2971 srcs: ["mylib.cpp"],
2972 system_shared_libs: [],
2973 stl: "none",
2974 apex_available: [
2975 "myapex",
2976 "otherapex",
2977 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002978 static_libs: ["mylib3"],
2979 recovery_available: true,
2980 min_sdk_version: "29",
2981 }
2982 cc_library {
2983 name: "mylib3",
2984 srcs: ["mylib.cpp"],
2985 system_shared_libs: [],
2986 stl: "none",
2987 apex_available: [
2988 "myapex",
2989 "otherapex",
2990 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002991 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002992 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002993 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002994 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002995 `)
2996
Jooyung Hanc87a0592020-03-02 17:44:33 +09002997 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002998 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002999 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003000 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003001
Jooyung Hanccce2f22020-03-07 03:45:53 +09003002 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003003 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003004 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003005 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003006 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003007
Jooyung Hanccce2f22020-03-07 03:45:53 +09003008 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003009 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003010 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003011 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003012 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003013
Colin Crossaede88c2020-08-11 12:17:01 -07003014 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3015 // each variant defines additional macros to distinguish which apex variant it is built for
3016
3017 // non-APEX variant does not have __ANDROID_APEX__ defined
3018 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3019 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3020
3021 // APEX variant has __ANDROID_APEX__ defined
3022 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3023 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3024 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3025 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3026
3027 // APEX variant has __ANDROID_APEX__ defined
3028 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3029 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3030 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3031 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3032
Dan Albertb19953d2020-11-17 15:29:36 -08003033 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003034 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3035 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003036 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003037
3038 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3039 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003040
3041 // non-APEX variant does not have __ANDROID_APEX__ defined
3042 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3043 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3044
3045 // APEX variant has __ANDROID_APEX__ defined
3046 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003047 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003048 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003049 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003050
Jooyung Hanc87a0592020-03-02 17:44:33 +09003051 // APEX variant has __ANDROID_APEX__ defined
3052 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003053 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003054 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003055 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003056
Dan Albertb19953d2020-11-17 15:29:36 -08003057 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003058 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003059 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003060 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003061}
Jiyong Park7e636d02019-01-28 16:16:54 +09003062
3063func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003064 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003065 apex {
3066 name: "myapex",
3067 key: "myapex.key",
3068 native_shared_libs: ["mylib"],
3069 }
3070
3071 apex_key {
3072 name: "myapex.key",
3073 public_key: "testkey.avbpubkey",
3074 private_key: "testkey.pem",
3075 }
3076
3077 cc_library_headers {
3078 name: "mylib_headers",
3079 export_include_dirs: ["my_include"],
3080 system_shared_libs: [],
3081 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003082 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003083 }
3084
3085 cc_library {
3086 name: "mylib",
3087 srcs: ["mylib.cpp"],
3088 system_shared_libs: [],
3089 stl: "none",
3090 header_libs: ["mylib_headers"],
3091 export_header_lib_headers: ["mylib_headers"],
3092 stubs: {
3093 versions: ["1", "2", "3"],
3094 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003095 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003096 }
3097
3098 cc_library {
3099 name: "otherlib",
3100 srcs: ["mylib.cpp"],
3101 system_shared_libs: [],
3102 stl: "none",
3103 shared_libs: ["mylib"],
3104 }
3105 `)
3106
Colin Cross7113d202019-11-20 16:39:12 -08003107 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003108
3109 // Ensure that the include path of the header lib is exported to 'otherlib'
3110 ensureContains(t, cFlags, "-Imy_include")
3111}
Alex Light9670d332019-01-29 18:07:33 -08003112
Jiyong Park7cd10e32020-01-14 09:22:18 +09003113type fileInApex struct {
3114 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003115 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003116 isLink bool
3117}
3118
Jooyung Hana57af4a2020-01-23 05:36:59 +00003119func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003120 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003121 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003122 copyCmds := apexRule.Args["copy_commands"]
3123 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003124 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003125 for _, cmd := range strings.Split(copyCmds, "&&") {
3126 cmd = strings.TrimSpace(cmd)
3127 if cmd == "" {
3128 continue
3129 }
3130 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003131 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003132 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003133 switch terms[0] {
3134 case "mkdir":
3135 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003136 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003137 t.Fatal("copyCmds contains invalid cp command", cmd)
3138 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003139 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003140 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003141 isLink = false
3142 case "ln":
3143 if len(terms) != 3 && len(terms) != 4 {
3144 // ln LINK TARGET or ln -s LINK TARGET
3145 t.Fatal("copyCmds contains invalid ln command", cmd)
3146 }
3147 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003148 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003149 isLink = true
3150 default:
3151 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3152 }
3153 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003154 index := strings.Index(dst, imageApexDir)
3155 if index == -1 {
3156 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3157 }
3158 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003159 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003160 }
3161 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003162 return ret
3163}
3164
Jooyung Hana57af4a2020-01-23 05:36:59 +00003165func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3166 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003167 var failed bool
3168 var surplus []string
3169 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003170 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003171 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003172 for _, expected := range files {
3173 if matched, _ := path.Match(expected, file.path); matched {
3174 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003175 mactchFound = true
3176 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003177 }
3178 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003179 if !mactchFound {
3180 surplus = append(surplus, file.path)
3181 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003182 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003183
Jooyung Han31c470b2019-10-18 16:26:59 +09003184 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003185 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003186 t.Log("surplus files", surplus)
3187 failed = true
3188 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003189
3190 if len(files) > len(filesMatched) {
3191 var missing []string
3192 for _, expected := range files {
3193 if !filesMatched[expected] {
3194 missing = append(missing, expected)
3195 }
3196 }
3197 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003198 t.Log("missing files", missing)
3199 failed = true
3200 }
3201 if failed {
3202 t.Fail()
3203 }
3204}
3205
Jooyung Han344d5432019-08-23 11:17:39 +09003206func TestVndkApexCurrent(t *testing.T) {
3207 ctx, _ := testApex(t, `
3208 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003209 name: "com.android.vndk.current",
3210 key: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003211 }
3212
3213 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003214 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003215 public_key: "testkey.avbpubkey",
3216 private_key: "testkey.pem",
3217 }
3218
3219 cc_library {
3220 name: "libvndk",
3221 srcs: ["mylib.cpp"],
3222 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003223 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003224 vndk: {
3225 enabled: true,
3226 },
3227 system_shared_libs: [],
3228 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003229 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003230 }
3231
3232 cc_library {
3233 name: "libvndksp",
3234 srcs: ["mylib.cpp"],
3235 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003236 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003237 vndk: {
3238 enabled: true,
3239 support_system_process: true,
3240 },
3241 system_shared_libs: [],
3242 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003243 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003244 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003245 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003246
Colin Cross2807f002021-03-02 10:15:29 -08003247 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003248 "lib/libvndk.so",
3249 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003250 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003251 "lib64/libvndk.so",
3252 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003253 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003254 "etc/llndk.libraries.VER.txt",
3255 "etc/vndkcore.libraries.VER.txt",
3256 "etc/vndksp.libraries.VER.txt",
3257 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003258 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003259 })
Jooyung Han344d5432019-08-23 11:17:39 +09003260}
3261
3262func TestVndkApexWithPrebuilt(t *testing.T) {
3263 ctx, _ := testApex(t, `
3264 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003265 name: "com.android.vndk.current",
3266 key: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003267 }
3268
3269 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003270 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003271 public_key: "testkey.avbpubkey",
3272 private_key: "testkey.pem",
3273 }
3274
3275 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003276 name: "libvndk",
3277 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003278 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003279 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003280 vndk: {
3281 enabled: true,
3282 },
3283 system_shared_libs: [],
3284 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003285 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003286 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003287
3288 cc_prebuilt_library_shared {
3289 name: "libvndk.arm",
3290 srcs: ["libvndk.arm.so"],
3291 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003292 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003293 vndk: {
3294 enabled: true,
3295 },
3296 enabled: false,
3297 arch: {
3298 arm: {
3299 enabled: true,
3300 },
3301 },
3302 system_shared_libs: [],
3303 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003304 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003305 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003306 `+vndkLibrariesTxtFiles("current"),
3307 withFiles(map[string][]byte{
3308 "libvndk.so": nil,
3309 "libvndk.arm.so": nil,
3310 }))
Colin Cross2807f002021-03-02 10:15:29 -08003311 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003312 "lib/libvndk.so",
3313 "lib/libvndk.arm.so",
3314 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003315 "lib/libc++.so",
3316 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003317 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003318 })
Jooyung Han344d5432019-08-23 11:17:39 +09003319}
3320
Jooyung Han39edb6c2019-11-06 16:53:07 +09003321func vndkLibrariesTxtFiles(vers ...string) (result string) {
3322 for _, v := range vers {
3323 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003324 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003325 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003326 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003327 name: "` + txt + `.libraries.txt",
3328 }
3329 `
3330 }
3331 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003332 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003333 result += `
3334 prebuilt_etc {
3335 name: "` + txt + `.libraries.` + v + `.txt",
3336 src: "dummy.txt",
3337 }
3338 `
3339 }
3340 }
3341 }
3342 return
3343}
3344
Jooyung Han344d5432019-08-23 11:17:39 +09003345func TestVndkApexVersion(t *testing.T) {
3346 ctx, _ := testApex(t, `
3347 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003348 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003349 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003350 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003351 vndk_version: "27",
3352 }
3353
3354 apex_key {
3355 name: "myapex.key",
3356 public_key: "testkey.avbpubkey",
3357 private_key: "testkey.pem",
3358 }
3359
Jooyung Han31c470b2019-10-18 16:26:59 +09003360 vndk_prebuilt_shared {
3361 name: "libvndk27",
3362 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003363 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003364 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003365 vndk: {
3366 enabled: true,
3367 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003368 target_arch: "arm64",
3369 arch: {
3370 arm: {
3371 srcs: ["libvndk27_arm.so"],
3372 },
3373 arm64: {
3374 srcs: ["libvndk27_arm64.so"],
3375 },
3376 },
Colin Cross2807f002021-03-02 10:15:29 -08003377 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003378 }
3379
3380 vndk_prebuilt_shared {
3381 name: "libvndk27",
3382 version: "27",
3383 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003384 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003385 vndk: {
3386 enabled: true,
3387 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003388 target_arch: "x86_64",
3389 arch: {
3390 x86: {
3391 srcs: ["libvndk27_x86.so"],
3392 },
3393 x86_64: {
3394 srcs: ["libvndk27_x86_64.so"],
3395 },
3396 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003397 }
3398 `+vndkLibrariesTxtFiles("27"),
3399 withFiles(map[string][]byte{
3400 "libvndk27_arm.so": nil,
3401 "libvndk27_arm64.so": nil,
3402 "libvndk27_x86.so": nil,
3403 "libvndk27_x86_64.so": nil,
3404 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003405
Colin Cross2807f002021-03-02 10:15:29 -08003406 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003407 "lib/libvndk27_arm.so",
3408 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003409 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003410 })
Jooyung Han344d5432019-08-23 11:17:39 +09003411}
3412
Jooyung Han90eee022019-10-01 20:02:42 +09003413func TestVndkApexNameRule(t *testing.T) {
3414 ctx, _ := testApex(t, `
3415 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003416 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003417 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003418 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003419 }
3420 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003421 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003422 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003423 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003424 vndk_version: "28",
3425 }
3426 apex_key {
3427 name: "myapex.key",
3428 public_key: "testkey.avbpubkey",
3429 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003430 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003431
3432 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003433 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003434 actual := proptools.String(bundle.properties.Apex_name)
3435 if !reflect.DeepEqual(actual, expected) {
3436 t.Errorf("Got '%v', expected '%v'", actual, expected)
3437 }
3438 }
3439
Colin Cross2807f002021-03-02 10:15:29 -08003440 assertApexName("com.android.vndk.vVER", "com.android.vndk.current")
3441 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003442}
3443
Jooyung Han344d5432019-08-23 11:17:39 +09003444func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
3445 ctx, _ := testApex(t, `
3446 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003447 name: "com.android.vndk.current",
3448 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003449 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003450 }
3451
3452 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003453 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003454 public_key: "testkey.avbpubkey",
3455 private_key: "testkey.pem",
3456 }
3457
3458 cc_library {
3459 name: "libvndk",
3460 srcs: ["mylib.cpp"],
3461 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003462 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003463 native_bridge_supported: true,
3464 host_supported: true,
3465 vndk: {
3466 enabled: true,
3467 },
3468 system_shared_libs: [],
3469 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003470 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003471 }
Colin Cross2807f002021-03-02 10:15:29 -08003472 `+vndkLibrariesTxtFiles("current"),
3473 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003474
Colin Cross2807f002021-03-02 10:15:29 -08003475 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003476 "lib/libvndk.so",
3477 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003478 "lib/libc++.so",
3479 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003480 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003481 })
Jooyung Han344d5432019-08-23 11:17:39 +09003482}
3483
3484func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003485 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003486 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003487 name: "com.android.vndk.current",
3488 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003489 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003490 native_bridge_supported: true,
3491 }
3492
3493 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003494 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003495 public_key: "testkey.avbpubkey",
3496 private_key: "testkey.pem",
3497 }
3498
3499 cc_library {
3500 name: "libvndk",
3501 srcs: ["mylib.cpp"],
3502 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003503 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003504 native_bridge_supported: true,
3505 host_supported: true,
3506 vndk: {
3507 enabled: true,
3508 },
3509 system_shared_libs: [],
3510 stl: "none",
3511 }
3512 `)
3513}
3514
Jooyung Han31c470b2019-10-18 16:26:59 +09003515func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003516 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003517 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003518 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003519 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003520 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003521 vndk_version: "27",
3522 }
3523
3524 apex_key {
3525 name: "myapex.key",
3526 public_key: "testkey.avbpubkey",
3527 private_key: "testkey.pem",
3528 }
3529
3530 vndk_prebuilt_shared {
3531 name: "libvndk27",
3532 version: "27",
3533 target_arch: "arm",
3534 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003535 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003536 vndk: {
3537 enabled: true,
3538 },
3539 arch: {
3540 arm: {
3541 srcs: ["libvndk27.so"],
3542 }
3543 },
3544 }
3545
3546 vndk_prebuilt_shared {
3547 name: "libvndk27",
3548 version: "27",
3549 target_arch: "arm",
3550 binder32bit: true,
3551 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003552 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003553 vndk: {
3554 enabled: true,
3555 },
3556 arch: {
3557 arm: {
3558 srcs: ["libvndk27binder32.so"],
3559 }
3560 },
Colin Cross2807f002021-03-02 10:15:29 -08003561 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003562 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003563 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003564 withFiles(map[string][]byte{
3565 "libvndk27.so": nil,
3566 "libvndk27binder32.so": nil,
3567 }),
3568 withBinder32bit,
3569 withTargets(map[android.OsType][]android.Target{
3570 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003571 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3572 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003573 },
3574 }),
3575 )
3576
Colin Cross2807f002021-03-02 10:15:29 -08003577 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003578 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003579 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003580 })
3581}
3582
Jooyung Han45a96772020-06-15 14:59:42 +09003583func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
3584 ctx, _ := testApex(t, `
3585 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003586 name: "com.android.vndk.current",
3587 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003588 file_contexts: ":myapex-file_contexts",
3589 }
3590
3591 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003592 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003593 public_key: "testkey.avbpubkey",
3594 private_key: "testkey.pem",
3595 }
3596
3597 cc_library {
3598 name: "libz",
3599 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003600 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003601 vndk: {
3602 enabled: true,
3603 },
3604 stubs: {
3605 symbol_file: "libz.map.txt",
3606 versions: ["30"],
3607 }
3608 }
3609 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3610 "libz.map.txt": nil,
3611 }))
3612
Colin Cross2807f002021-03-02 10:15:29 -08003613 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003614 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3615 ensureListEmpty(t, provideNativeLibs)
3616}
3617
Jooyung Hane1633032019-08-01 17:41:43 +09003618func TestDependenciesInApexManifest(t *testing.T) {
3619 ctx, _ := testApex(t, `
3620 apex {
3621 name: "myapex_nodep",
3622 key: "myapex.key",
3623 native_shared_libs: ["lib_nodep"],
3624 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003625 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003626 }
3627
3628 apex {
3629 name: "myapex_dep",
3630 key: "myapex.key",
3631 native_shared_libs: ["lib_dep"],
3632 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003633 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003634 }
3635
3636 apex {
3637 name: "myapex_provider",
3638 key: "myapex.key",
3639 native_shared_libs: ["libfoo"],
3640 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003641 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003642 }
3643
3644 apex {
3645 name: "myapex_selfcontained",
3646 key: "myapex.key",
3647 native_shared_libs: ["lib_dep", "libfoo"],
3648 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003649 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003650 }
3651
3652 apex_key {
3653 name: "myapex.key",
3654 public_key: "testkey.avbpubkey",
3655 private_key: "testkey.pem",
3656 }
3657
3658 cc_library {
3659 name: "lib_nodep",
3660 srcs: ["mylib.cpp"],
3661 system_shared_libs: [],
3662 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003663 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003664 }
3665
3666 cc_library {
3667 name: "lib_dep",
3668 srcs: ["mylib.cpp"],
3669 shared_libs: ["libfoo"],
3670 system_shared_libs: [],
3671 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003672 apex_available: [
3673 "myapex_dep",
3674 "myapex_provider",
3675 "myapex_selfcontained",
3676 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003677 }
3678
3679 cc_library {
3680 name: "libfoo",
3681 srcs: ["mytest.cpp"],
3682 stubs: {
3683 versions: ["1"],
3684 },
3685 system_shared_libs: [],
3686 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003687 apex_available: [
3688 "myapex_provider",
3689 "myapex_selfcontained",
3690 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003691 }
3692 `)
3693
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003694 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003695 var provideNativeLibs, requireNativeLibs []string
3696
Sundong Ahnabb64432019-10-22 13:58:29 +09003697 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003698 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3699 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003700 ensureListEmpty(t, provideNativeLibs)
3701 ensureListEmpty(t, requireNativeLibs)
3702
Sundong Ahnabb64432019-10-22 13:58:29 +09003703 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003704 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3705 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003706 ensureListEmpty(t, provideNativeLibs)
3707 ensureListContains(t, requireNativeLibs, "libfoo.so")
3708
Sundong Ahnabb64432019-10-22 13:58:29 +09003709 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003710 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3711 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003712 ensureListContains(t, provideNativeLibs, "libfoo.so")
3713 ensureListEmpty(t, requireNativeLibs)
3714
Sundong Ahnabb64432019-10-22 13:58:29 +09003715 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003716 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3717 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003718 ensureListContains(t, provideNativeLibs, "libfoo.so")
3719 ensureListEmpty(t, requireNativeLibs)
3720}
3721
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003722func TestApexName(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07003723 ctx, _ := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003724 apex {
3725 name: "myapex",
3726 key: "myapex.key",
3727 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003728 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003729 }
3730
3731 apex_key {
3732 name: "myapex.key",
3733 public_key: "testkey.avbpubkey",
3734 private_key: "testkey.pem",
3735 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003736
3737 cc_library {
3738 name: "mylib",
3739 srcs: ["mylib.cpp"],
3740 system_shared_libs: [],
3741 stl: "none",
3742 apex_available: [
3743 "//apex_available:platform",
3744 "myapex",
3745 ],
3746 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003747 `)
3748
Sundong Ahnabb64432019-10-22 13:58:29 +09003749 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003750 apexManifestRule := module.Rule("apexManifestRule")
3751 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3752 apexRule := module.Rule("apexRule")
3753 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003754
3755 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003756 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003757 name := apexBundle.BaseModuleName()
3758 prefix := "TARGET_"
3759 var builder strings.Builder
3760 data.Custom(&builder, name, prefix, "", data)
3761 androidMk := builder.String()
3762 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3763 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003764}
3765
Alex Light0851b882019-02-07 13:20:53 -08003766func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003767 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003768 apex {
3769 name: "myapex",
3770 key: "myapex.key",
3771 native_shared_libs: ["mylib_common"],
3772 }
3773
3774 apex_key {
3775 name: "myapex.key",
3776 public_key: "testkey.avbpubkey",
3777 private_key: "testkey.pem",
3778 }
3779
3780 cc_library {
3781 name: "mylib_common",
3782 srcs: ["mylib.cpp"],
3783 system_shared_libs: [],
3784 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003785 apex_available: [
3786 "//apex_available:platform",
3787 "myapex",
3788 ],
Alex Light0851b882019-02-07 13:20:53 -08003789 }
3790 `)
3791
Sundong Ahnabb64432019-10-22 13:58:29 +09003792 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003793 apexRule := module.Rule("apexRule")
3794 copyCmds := apexRule.Args["copy_commands"]
3795
3796 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3797 t.Log("Apex was a test apex!")
3798 t.Fail()
3799 }
3800 // Ensure that main rule creates an output
3801 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3802
3803 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003804 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003805
3806 // Ensure that both direct and indirect deps are copied into apex
3807 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3808
Colin Cross7113d202019-11-20 16:39:12 -08003809 // Ensure that the platform variant ends with _shared
3810 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003811
Colin Cross56a83212020-09-15 18:30:11 -07003812 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003813 t.Log("Found mylib_common not in any apex!")
3814 t.Fail()
3815 }
3816}
3817
3818func TestTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003819 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003820 apex_test {
3821 name: "myapex",
3822 key: "myapex.key",
3823 native_shared_libs: ["mylib_common_test"],
3824 }
3825
3826 apex_key {
3827 name: "myapex.key",
3828 public_key: "testkey.avbpubkey",
3829 private_key: "testkey.pem",
3830 }
3831
3832 cc_library {
3833 name: "mylib_common_test",
3834 srcs: ["mylib.cpp"],
3835 system_shared_libs: [],
3836 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003837 // TODO: remove //apex_available:platform
3838 apex_available: [
3839 "//apex_available:platform",
3840 "myapex",
3841 ],
Alex Light0851b882019-02-07 13:20:53 -08003842 }
3843 `)
3844
Sundong Ahnabb64432019-10-22 13:58:29 +09003845 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003846 apexRule := module.Rule("apexRule")
3847 copyCmds := apexRule.Args["copy_commands"]
3848
3849 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3850 t.Log("Apex was not a test apex!")
3851 t.Fail()
3852 }
3853 // Ensure that main rule creates an output
3854 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3855
3856 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003857 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003858
3859 // Ensure that both direct and indirect deps are copied into apex
3860 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3861
Colin Cross7113d202019-11-20 16:39:12 -08003862 // Ensure that the platform variant ends with _shared
3863 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003864}
3865
Alex Light9670d332019-01-29 18:07:33 -08003866func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003867 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003868 apex {
3869 name: "myapex",
3870 key: "myapex.key",
3871 multilib: {
3872 first: {
3873 native_shared_libs: ["mylib_common"],
3874 }
3875 },
3876 target: {
3877 android: {
3878 multilib: {
3879 first: {
3880 native_shared_libs: ["mylib"],
3881 }
3882 }
3883 },
3884 host: {
3885 multilib: {
3886 first: {
3887 native_shared_libs: ["mylib2"],
3888 }
3889 }
3890 }
3891 }
3892 }
3893
3894 apex_key {
3895 name: "myapex.key",
3896 public_key: "testkey.avbpubkey",
3897 private_key: "testkey.pem",
3898 }
3899
3900 cc_library {
3901 name: "mylib",
3902 srcs: ["mylib.cpp"],
3903 system_shared_libs: [],
3904 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003905 // TODO: remove //apex_available:platform
3906 apex_available: [
3907 "//apex_available:platform",
3908 "myapex",
3909 ],
Alex Light9670d332019-01-29 18:07:33 -08003910 }
3911
3912 cc_library {
3913 name: "mylib_common",
3914 srcs: ["mylib.cpp"],
3915 system_shared_libs: [],
3916 stl: "none",
3917 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003918 // TODO: remove //apex_available:platform
3919 apex_available: [
3920 "//apex_available:platform",
3921 "myapex",
3922 ],
Alex Light9670d332019-01-29 18:07:33 -08003923 }
3924
3925 cc_library {
3926 name: "mylib2",
3927 srcs: ["mylib.cpp"],
3928 system_shared_libs: [],
3929 stl: "none",
3930 compile_multilib: "first",
3931 }
3932 `)
3933
Sundong Ahnabb64432019-10-22 13:58:29 +09003934 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003935 copyCmds := apexRule.Args["copy_commands"]
3936
3937 // Ensure that main rule creates an output
3938 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3939
3940 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003941 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3942 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3943 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003944
3945 // Ensure that both direct and indirect deps are copied into apex
3946 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3947 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3948 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3949
Colin Cross7113d202019-11-20 16:39:12 -08003950 // Ensure that the platform variant ends with _shared
3951 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3952 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3953 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003954}
Jiyong Park04480cf2019-02-06 00:16:29 +09003955
Jiyong Park59140302020-12-14 18:44:04 +09003956func TestApexWithArch(t *testing.T) {
3957 ctx, _ := testApex(t, `
3958 apex {
3959 name: "myapex",
3960 key: "myapex.key",
3961 arch: {
3962 arm64: {
3963 native_shared_libs: ["mylib.arm64"],
3964 },
3965 x86_64: {
3966 native_shared_libs: ["mylib.x64"],
3967 },
3968 }
3969 }
3970
3971 apex_key {
3972 name: "myapex.key",
3973 public_key: "testkey.avbpubkey",
3974 private_key: "testkey.pem",
3975 }
3976
3977 cc_library {
3978 name: "mylib.arm64",
3979 srcs: ["mylib.cpp"],
3980 system_shared_libs: [],
3981 stl: "none",
3982 // TODO: remove //apex_available:platform
3983 apex_available: [
3984 "//apex_available:platform",
3985 "myapex",
3986 ],
3987 }
3988
3989 cc_library {
3990 name: "mylib.x64",
3991 srcs: ["mylib.cpp"],
3992 system_shared_libs: [],
3993 stl: "none",
3994 // TODO: remove //apex_available:platform
3995 apex_available: [
3996 "//apex_available:platform",
3997 "myapex",
3998 ],
3999 }
4000 `)
4001
4002 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4003 copyCmds := apexRule.Args["copy_commands"]
4004
4005 // Ensure that apex variant is created for the direct dep
4006 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4007 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4008
4009 // Ensure that both direct and indirect deps are copied into apex
4010 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4011 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4012}
4013
Jiyong Park04480cf2019-02-06 00:16:29 +09004014func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004015 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004016 apex {
4017 name: "myapex",
4018 key: "myapex.key",
4019 binaries: ["myscript"],
4020 }
4021
4022 apex_key {
4023 name: "myapex.key",
4024 public_key: "testkey.avbpubkey",
4025 private_key: "testkey.pem",
4026 }
4027
4028 sh_binary {
4029 name: "myscript",
4030 src: "mylib.cpp",
4031 filename: "myscript.sh",
4032 sub_dir: "script",
4033 }
4034 `)
4035
Sundong Ahnabb64432019-10-22 13:58:29 +09004036 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004037 copyCmds := apexRule.Args["copy_commands"]
4038
4039 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4040}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004041
Jooyung Han91df2082019-11-20 01:49:42 +09004042func TestApexInVariousPartition(t *testing.T) {
4043 testcases := []struct {
4044 propName, parition, flattenedPartition string
4045 }{
4046 {"", "system", "system_ext"},
4047 {"product_specific: true", "product", "product"},
4048 {"soc_specific: true", "vendor", "vendor"},
4049 {"proprietary: true", "vendor", "vendor"},
4050 {"vendor: true", "vendor", "vendor"},
4051 {"system_ext_specific: true", "system_ext", "system_ext"},
4052 }
4053 for _, tc := range testcases {
4054 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
4055 ctx, _ := testApex(t, `
4056 apex {
4057 name: "myapex",
4058 key: "myapex.key",
4059 `+tc.propName+`
4060 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004061
Jooyung Han91df2082019-11-20 01:49:42 +09004062 apex_key {
4063 name: "myapex.key",
4064 public_key: "testkey.avbpubkey",
4065 private_key: "testkey.pem",
4066 }
4067 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004068
Jooyung Han91df2082019-11-20 01:49:42 +09004069 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4070 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
4071 actual := apex.installDir.String()
4072 if actual != expected {
4073 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4074 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004075
Jooyung Han91df2082019-11-20 01:49:42 +09004076 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4077 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
4078 actual = flattened.installDir.String()
4079 if actual != expected {
4080 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4081 }
4082 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004083 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004084}
Jiyong Park67882562019-03-21 01:11:21 +09004085
Jooyung Han580eb4f2020-06-24 19:33:06 +09004086func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004087 ctx, _ := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004088 apex {
4089 name: "myapex",
4090 key: "myapex.key",
4091 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004092
Jooyung Han580eb4f2020-06-24 19:33:06 +09004093 apex_key {
4094 name: "myapex.key",
4095 public_key: "testkey.avbpubkey",
4096 private_key: "testkey.pem",
4097 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004098 `)
4099 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004100 rule := module.Output("file_contexts")
4101 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4102}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004103
Jooyung Han580eb4f2020-06-24 19:33:06 +09004104func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004105 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004106 apex {
4107 name: "myapex",
4108 key: "myapex.key",
4109 file_contexts: "my_own_file_contexts",
4110 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004111
Jooyung Han580eb4f2020-06-24 19:33:06 +09004112 apex_key {
4113 name: "myapex.key",
4114 public_key: "testkey.avbpubkey",
4115 private_key: "testkey.pem",
4116 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004117 `, withFiles(map[string][]byte{
4118 "my_own_file_contexts": nil,
4119 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004120}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004121
Jooyung Han580eb4f2020-06-24 19:33:06 +09004122func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004123 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004124 apex {
4125 name: "myapex",
4126 key: "myapex.key",
4127 product_specific: true,
4128 file_contexts: "product_specific_file_contexts",
4129 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004130
Jooyung Han580eb4f2020-06-24 19:33:06 +09004131 apex_key {
4132 name: "myapex.key",
4133 public_key: "testkey.avbpubkey",
4134 private_key: "testkey.pem",
4135 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004136 `)
4137
Jooyung Han580eb4f2020-06-24 19:33:06 +09004138 ctx, _ := testApex(t, `
4139 apex {
4140 name: "myapex",
4141 key: "myapex.key",
4142 product_specific: true,
4143 file_contexts: "product_specific_file_contexts",
4144 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004145
Jooyung Han580eb4f2020-06-24 19:33:06 +09004146 apex_key {
4147 name: "myapex.key",
4148 public_key: "testkey.avbpubkey",
4149 private_key: "testkey.pem",
4150 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004151 `, withFiles(map[string][]byte{
4152 "product_specific_file_contexts": nil,
4153 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004154 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4155 rule := module.Output("file_contexts")
4156 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4157}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004158
Jooyung Han580eb4f2020-06-24 19:33:06 +09004159func TestFileContexts_SetViaFileGroup(t *testing.T) {
4160 ctx, _ := testApex(t, `
4161 apex {
4162 name: "myapex",
4163 key: "myapex.key",
4164 product_specific: true,
4165 file_contexts: ":my-file-contexts",
4166 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004167
Jooyung Han580eb4f2020-06-24 19:33:06 +09004168 apex_key {
4169 name: "myapex.key",
4170 public_key: "testkey.avbpubkey",
4171 private_key: "testkey.pem",
4172 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004173
Jooyung Han580eb4f2020-06-24 19:33:06 +09004174 filegroup {
4175 name: "my-file-contexts",
4176 srcs: ["product_specific_file_contexts"],
4177 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004178 `, withFiles(map[string][]byte{
4179 "product_specific_file_contexts": nil,
4180 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004181 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4182 rule := module.Output("file_contexts")
4183 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004184}
4185
Jiyong Park67882562019-03-21 01:11:21 +09004186func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004187 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004188 apex_key {
4189 name: "myapex.key",
4190 public_key: ":my.avbpubkey",
4191 private_key: ":my.pem",
4192 product_specific: true,
4193 }
4194
4195 filegroup {
4196 name: "my.avbpubkey",
4197 srcs: ["testkey2.avbpubkey"],
4198 }
4199
4200 filegroup {
4201 name: "my.pem",
4202 srcs: ["testkey2.pem"],
4203 }
4204 `)
4205
4206 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4207 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004208 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004209 if actual_pubkey != expected_pubkey {
4210 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4211 }
4212 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004213 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004214 if actual_privkey != expected_privkey {
4215 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4216 }
4217}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004218
4219func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004220 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004221 prebuilt_apex {
4222 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004223 arch: {
4224 arm64: {
4225 src: "myapex-arm64.apex",
4226 },
4227 arm: {
4228 src: "myapex-arm.apex",
4229 },
4230 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004231 }
4232 `)
4233
4234 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4235
Jiyong Parkc95714e2019-03-29 14:23:10 +09004236 expectedInput := "myapex-arm64.apex"
4237 if prebuilt.inputApex.String() != expectedInput {
4238 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4239 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004240}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004241
4242func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004243 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004244 prebuilt_apex {
4245 name: "myapex",
4246 src: "myapex-arm.apex",
4247 filename: "notmyapex.apex",
4248 }
4249 `)
4250
4251 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4252
4253 expected := "notmyapex.apex"
4254 if p.installFilename != expected {
4255 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4256 }
4257}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004258
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004259func TestPrebuiltOverrides(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07004260 ctx, _ := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004261 prebuilt_apex {
4262 name: "myapex.prebuilt",
4263 src: "myapex-arm.apex",
4264 overrides: [
4265 "myapex",
4266 ],
4267 }
4268 `)
4269
4270 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4271
4272 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004273 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004274 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004275 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004276 }
4277}
4278
Paul Duffin092153d2021-01-26 11:42:39 +00004279// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4280// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004281func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4282 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004283 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004284 }
4285
Paul Duffin89886cb2021-02-05 16:44:03 +00004286 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004287 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004288 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004289 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004290 stem := android.RemoveOptionalPrebuiltPrefix(name)
4291 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004292 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4293 }
4294 }
4295
Paul Duffin39853512021-02-26 11:09:39 +00004296 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004297 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004298 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004299 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4300 }
4301 }
4302
4303 t.Run("prebuilt only", func(t *testing.T) {
4304 bp := `
4305 prebuilt_apex {
4306 name: "myapex",
4307 arch: {
4308 arm64: {
4309 src: "myapex-arm64.apex",
4310 },
4311 arm: {
4312 src: "myapex-arm.apex",
4313 },
4314 },
Paul Duffin39853512021-02-26 11:09:39 +00004315 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004316 }
4317
4318 java_import {
4319 name: "libfoo",
4320 jars: ["libfoo.jar"],
4321 }
Paul Duffin39853512021-02-26 11:09:39 +00004322
4323 java_sdk_library_import {
4324 name: "libbar",
4325 public: {
4326 jars: ["libbar.jar"],
4327 },
4328 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004329 `
4330
4331 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4332 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4333
Paul Duffinf6932af2021-02-26 18:21:56 +00004334 // Make sure that the deapexer has the correct input APEX.
4335 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4336 rule := deapexer.Rule("deapexer")
4337 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4338 t.Errorf("expected: %q, found: %q", expected, actual)
4339 }
4340
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004341 // Make sure that the prebuilt_apex has the correct input APEX.
4342 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4343 rule = prebuiltApex.Rule("android/soong/android.Cp")
4344 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4345 t.Errorf("expected: %q, found: %q", expected, actual)
4346 }
4347
Paul Duffin89886cb2021-02-05 16:44:03 +00004348 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004349
4350 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004351 })
4352
4353 t.Run("prebuilt with source preferred", func(t *testing.T) {
4354
4355 bp := `
4356 prebuilt_apex {
4357 name: "myapex",
4358 arch: {
4359 arm64: {
4360 src: "myapex-arm64.apex",
4361 },
4362 arm: {
4363 src: "myapex-arm.apex",
4364 },
4365 },
Paul Duffin39853512021-02-26 11:09:39 +00004366 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004367 }
4368
4369 java_import {
4370 name: "libfoo",
4371 jars: ["libfoo.jar"],
4372 }
4373
4374 java_library {
4375 name: "libfoo",
4376 }
Paul Duffin39853512021-02-26 11:09:39 +00004377
4378 java_sdk_library_import {
4379 name: "libbar",
4380 public: {
4381 jars: ["libbar.jar"],
4382 },
4383 }
4384
4385 java_sdk_library {
4386 name: "libbar",
4387 srcs: ["foo/bar/MyClass.java"],
4388 unsafe_ignore_missing_latest_api: true,
4389 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004390 `
4391
4392 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4393 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4394
Paul Duffin89886cb2021-02-05 16:44:03 +00004395 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004396 ensureNoSourceVariant(t, ctx, "libfoo")
4397
4398 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4399 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004400 })
4401
4402 t.Run("prebuilt preferred with source", func(t *testing.T) {
4403 bp := `
4404 prebuilt_apex {
4405 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004406 arch: {
4407 arm64: {
4408 src: "myapex-arm64.apex",
4409 },
4410 arm: {
4411 src: "myapex-arm.apex",
4412 },
4413 },
Paul Duffin39853512021-02-26 11:09:39 +00004414 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004415 }
4416
4417 java_import {
4418 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004419 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004420 jars: ["libfoo.jar"],
4421 }
4422
4423 java_library {
4424 name: "libfoo",
4425 }
Paul Duffin39853512021-02-26 11:09:39 +00004426
4427 java_sdk_library_import {
4428 name: "libbar",
4429 prefer: true,
4430 public: {
4431 jars: ["libbar.jar"],
4432 },
4433 }
4434
4435 java_sdk_library {
4436 name: "libbar",
4437 srcs: ["foo/bar/MyClass.java"],
4438 unsafe_ignore_missing_latest_api: true,
4439 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004440 `
4441
4442 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4443 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4444
Paul Duffin89886cb2021-02-05 16:44:03 +00004445 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004446 ensureNoSourceVariant(t, ctx, "libfoo")
4447
4448 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4449 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004450 })
4451}
4452
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004453func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4454 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004455 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004456 }
4457
Paul Duffin37856732021-02-26 14:24:15 +00004458 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4459 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004460 s := ctx.SingletonForTests("dex_bootjars")
4461 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004462 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004463 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004464 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004465 foundLibfooJar = true
4466 buildRule := s.Output(output)
4467 actual := android.NormalizePathForTesting(buildRule.Input)
4468 if actual != bootDexJarPath {
4469 t.Errorf("Incorrect boot dex jar path '%s', expected '%s'", actual, bootDexJarPath)
4470 }
4471 }
4472 }
4473 if !foundLibfooJar {
4474 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs")
4475 }
4476 }
4477
Paul Duffin4fd997b2021-02-03 20:06:33 +00004478 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004479 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004480 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4481 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4482 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4483 }
4484
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004485 t.Run("prebuilt only", func(t *testing.T) {
4486 bp := `
4487 prebuilt_apex {
4488 name: "myapex",
4489 arch: {
4490 arm64: {
4491 src: "myapex-arm64.apex",
4492 },
4493 arm: {
4494 src: "myapex-arm.apex",
4495 },
4496 },
Paul Duffin37856732021-02-26 14:24:15 +00004497 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004498 }
4499
4500 java_import {
4501 name: "libfoo",
4502 jars: ["libfoo.jar"],
4503 apex_available: ["myapex"],
4504 }
Paul Duffin37856732021-02-26 14:24:15 +00004505
4506 java_sdk_library_import {
4507 name: "libbar",
4508 public: {
4509 jars: ["libbar.jar"],
4510 },
4511 apex_available: ["myapex"],
4512 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004513 `
4514
4515 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004516 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4517 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004518
Paul Duffin9d67ca62021-02-03 20:06:33 +00004519 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4520 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004521.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004522.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4523`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004524 })
4525
4526 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4527 bp := `
4528 prebuilt_apex {
4529 name: "myapex",
4530 arch: {
4531 arm64: {
4532 src: "myapex-arm64.apex",
4533 },
4534 arm: {
4535 src: "myapex-arm.apex",
4536 },
4537 },
Paul Duffin37856732021-02-26 14:24:15 +00004538 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004539 }
4540
4541 java_import {
4542 name: "libfoo",
4543 jars: ["libfoo.jar"],
4544 apex_available: ["myapex"],
4545 }
4546
4547 java_library {
4548 name: "libfoo",
4549 srcs: ["foo/bar/MyClass.java"],
4550 apex_available: ["myapex"],
4551 }
Paul Duffin37856732021-02-26 14:24:15 +00004552
4553 java_sdk_library_import {
4554 name: "libbar",
4555 public: {
4556 jars: ["libbar.jar"],
4557 },
4558 apex_available: ["myapex"],
4559 }
4560
4561 java_sdk_library {
4562 name: "libbar",
4563 srcs: ["foo/bar/MyClass.java"],
4564 unsafe_ignore_missing_latest_api: true,
4565 apex_available: ["myapex"],
4566 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004567 `
4568
4569 // In this test the source (java_library) libfoo is active since the
4570 // prebuilt (java_import) defaults to prefer:false. However the
4571 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4572 // find the dex boot jar in it. We either need to disable the source libfoo
4573 // or make the prebuilt libfoo preferred.
4574 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4575 })
4576
4577 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4578 bp := `
4579 prebuilt_apex {
4580 name: "myapex",
4581 arch: {
4582 arm64: {
4583 src: "myapex-arm64.apex",
4584 },
4585 arm: {
4586 src: "myapex-arm.apex",
4587 },
4588 },
Paul Duffin37856732021-02-26 14:24:15 +00004589 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004590 }
4591
4592 java_import {
4593 name: "libfoo",
4594 prefer: true,
4595 jars: ["libfoo.jar"],
4596 apex_available: ["myapex"],
4597 }
4598
4599 java_library {
4600 name: "libfoo",
4601 srcs: ["foo/bar/MyClass.java"],
4602 apex_available: ["myapex"],
4603 }
Paul Duffin37856732021-02-26 14:24:15 +00004604
4605 java_sdk_library_import {
4606 name: "libbar",
4607 prefer: true,
4608 public: {
4609 jars: ["libbar.jar"],
4610 },
4611 apex_available: ["myapex"],
4612 }
4613
4614 java_sdk_library {
4615 name: "libbar",
4616 srcs: ["foo/bar/MyClass.java"],
4617 unsafe_ignore_missing_latest_api: true,
4618 apex_available: ["myapex"],
4619 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004620 `
4621
4622 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004623 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4624 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004625
Paul Duffin9d67ca62021-02-03 20:06:33 +00004626 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4627 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004628.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004629.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4630`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004631 })
4632
4633 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4634 bp := `
4635 apex {
4636 name: "myapex",
4637 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004638 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004639 }
4640
4641 apex_key {
4642 name: "myapex.key",
4643 public_key: "testkey.avbpubkey",
4644 private_key: "testkey.pem",
4645 }
4646
4647 prebuilt_apex {
4648 name: "myapex",
4649 arch: {
4650 arm64: {
4651 src: "myapex-arm64.apex",
4652 },
4653 arm: {
4654 src: "myapex-arm.apex",
4655 },
4656 },
Paul Duffin37856732021-02-26 14:24:15 +00004657 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004658 }
4659
4660 java_import {
4661 name: "libfoo",
4662 jars: ["libfoo.jar"],
4663 apex_available: ["myapex"],
4664 }
4665
4666 java_library {
4667 name: "libfoo",
4668 srcs: ["foo/bar/MyClass.java"],
4669 apex_available: ["myapex"],
4670 }
Paul Duffin37856732021-02-26 14:24:15 +00004671
4672 java_sdk_library_import {
4673 name: "libbar",
4674 public: {
4675 jars: ["libbar.jar"],
4676 },
4677 apex_available: ["myapex"],
4678 }
4679
4680 java_sdk_library {
4681 name: "libbar",
4682 srcs: ["foo/bar/MyClass.java"],
4683 unsafe_ignore_missing_latest_api: true,
4684 apex_available: ["myapex"],
4685 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004686 `
4687
4688 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004689 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4690 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004691
4692 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4693 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004694.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004695.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4696`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004697 })
4698
4699 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4700 bp := `
4701 apex {
4702 name: "myapex",
4703 enabled: false,
4704 key: "myapex.key",
4705 java_libs: ["libfoo"],
4706 }
4707
4708 apex_key {
4709 name: "myapex.key",
4710 public_key: "testkey.avbpubkey",
4711 private_key: "testkey.pem",
4712 }
4713
4714 prebuilt_apex {
4715 name: "myapex",
4716 arch: {
4717 arm64: {
4718 src: "myapex-arm64.apex",
4719 },
4720 arm: {
4721 src: "myapex-arm.apex",
4722 },
4723 },
Paul Duffin37856732021-02-26 14:24:15 +00004724 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004725 }
4726
4727 java_import {
4728 name: "libfoo",
4729 prefer: true,
4730 jars: ["libfoo.jar"],
4731 apex_available: ["myapex"],
4732 }
4733
4734 java_library {
4735 name: "libfoo",
4736 srcs: ["foo/bar/MyClass.java"],
4737 apex_available: ["myapex"],
4738 }
Paul Duffin37856732021-02-26 14:24:15 +00004739
4740 java_sdk_library_import {
4741 name: "libbar",
4742 prefer: true,
4743 public: {
4744 jars: ["libbar.jar"],
4745 },
4746 apex_available: ["myapex"],
4747 }
4748
4749 java_sdk_library {
4750 name: "libbar",
4751 srcs: ["foo/bar/MyClass.java"],
4752 unsafe_ignore_missing_latest_api: true,
4753 apex_available: ["myapex"],
4754 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004755 `
4756
4757 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004758 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4759 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004760
Paul Duffin9d67ca62021-02-03 20:06:33 +00004761 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4762 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004763.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004764.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4765`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004766 })
4767}
4768
Roland Levillain630846d2019-06-26 12:48:34 +01004769func TestApexWithTests(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07004770 ctx, _ := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004771 apex_test {
4772 name: "myapex",
4773 key: "myapex.key",
4774 tests: [
4775 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004776 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004777 ],
4778 }
4779
4780 apex_key {
4781 name: "myapex.key",
4782 public_key: "testkey.avbpubkey",
4783 private_key: "testkey.pem",
4784 }
4785
Liz Kammer1c14a212020-05-12 15:26:55 -07004786 filegroup {
4787 name: "fg",
4788 srcs: [
4789 "baz",
4790 "bar/baz"
4791 ],
4792 }
4793
Roland Levillain630846d2019-06-26 12:48:34 +01004794 cc_test {
4795 name: "mytest",
4796 gtest: false,
4797 srcs: ["mytest.cpp"],
4798 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004799 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004800 system_shared_libs: [],
4801 static_executable: true,
4802 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004803 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004804 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004805
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004806 cc_library {
4807 name: "mylib",
4808 srcs: ["mylib.cpp"],
4809 system_shared_libs: [],
4810 stl: "none",
4811 }
4812
Liz Kammer5bd365f2020-05-27 15:15:11 -07004813 filegroup {
4814 name: "fg2",
4815 srcs: [
4816 "testdata/baz"
4817 ],
4818 }
4819
Roland Levillain9b5fde92019-06-28 15:41:19 +01004820 cc_test {
4821 name: "mytests",
4822 gtest: false,
4823 srcs: [
4824 "mytest1.cpp",
4825 "mytest2.cpp",
4826 "mytest3.cpp",
4827 ],
4828 test_per_src: true,
4829 relative_install_path: "test",
4830 system_shared_libs: [],
4831 static_executable: true,
4832 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004833 data: [
4834 ":fg",
4835 ":fg2",
4836 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004837 }
Roland Levillain630846d2019-06-26 12:48:34 +01004838 `)
4839
Sundong Ahnabb64432019-10-22 13:58:29 +09004840 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004841 copyCmds := apexRule.Args["copy_commands"]
4842
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004843 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004844 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004845 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004846
Liz Kammer1c14a212020-05-12 15:26:55 -07004847 //Ensure that test data are copied into apex.
4848 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4849 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4850
Roland Levillain9b5fde92019-06-28 15:41:19 +01004851 // Ensure that test deps built with `test_per_src` are copied into apex.
4852 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4853 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4854 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004855
4856 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004857 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004858 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004859 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004860 prefix := "TARGET_"
4861 var builder strings.Builder
4862 data.Custom(&builder, name, prefix, "", data)
4863 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004864 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4865 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4866 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4867 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004868 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004869 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004870 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004871
4872 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004873 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004874 data.Custom(&builder, name, prefix, "", data)
4875 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004876 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4877 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004878}
4879
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004880func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07004881 ctx, _ := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004882 apex {
4883 name: "myapex",
4884 key: "myapex.key",
4885 }
4886 apex_key {
4887 name: "myapex.key",
4888 public_key: "testkey.avbpubkey",
4889 private_key: "testkey.pem",
4890 }
4891 `, func(fs map[string][]byte, config android.Config) {
4892 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4893 })
4894 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004895 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004896 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004897 var builder strings.Builder
4898 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4899 androidMk := builder.String()
4900 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4901}
4902
Jooyung Hand48f3c32019-08-23 11:18:57 +09004903func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4904 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4905 apex {
4906 name: "myapex",
4907 key: "myapex.key",
4908 native_shared_libs: ["libfoo"],
4909 }
4910
4911 apex_key {
4912 name: "myapex.key",
4913 public_key: "testkey.avbpubkey",
4914 private_key: "testkey.pem",
4915 }
4916
4917 cc_library {
4918 name: "libfoo",
4919 stl: "none",
4920 system_shared_libs: [],
4921 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004922 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004923 }
4924 `)
4925 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4926 apex {
4927 name: "myapex",
4928 key: "myapex.key",
4929 java_libs: ["myjar"],
4930 }
4931
4932 apex_key {
4933 name: "myapex.key",
4934 public_key: "testkey.avbpubkey",
4935 private_key: "testkey.pem",
4936 }
4937
4938 java_library {
4939 name: "myjar",
4940 srcs: ["foo/bar/MyClass.java"],
4941 sdk_version: "none",
4942 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004943 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004944 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004945 }
4946 `)
4947}
4948
Bill Peckhama41a6962021-01-11 10:58:54 -08004949func TestApexWithJavaImport(t *testing.T) {
4950 ctx, _ := testApex(t, `
4951 apex {
4952 name: "myapex",
4953 key: "myapex.key",
4954 java_libs: ["myjavaimport"],
4955 }
4956
4957 apex_key {
4958 name: "myapex.key",
4959 public_key: "testkey.avbpubkey",
4960 private_key: "testkey.pem",
4961 }
4962
4963 java_import {
4964 name: "myjavaimport",
4965 apex_available: ["myapex"],
4966 jars: ["my.jar"],
4967 compile_dex: true,
4968 }
4969 `)
4970
4971 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4972 apexRule := module.Rule("apexRule")
4973 copyCmds := apexRule.Args["copy_commands"]
4974 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
4975}
4976
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004977func TestApexWithApps(t *testing.T) {
4978 ctx, _ := testApex(t, `
4979 apex {
4980 name: "myapex",
4981 key: "myapex.key",
4982 apps: [
4983 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09004984 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004985 ],
4986 }
4987
4988 apex_key {
4989 name: "myapex.key",
4990 public_key: "testkey.avbpubkey",
4991 private_key: "testkey.pem",
4992 }
4993
4994 android_app {
4995 name: "AppFoo",
4996 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004997 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004998 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09004999 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005000 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005001 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005002 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005003
5004 android_app {
5005 name: "AppFooPriv",
5006 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005007 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005008 system_modules: "none",
5009 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005010 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005011 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005012 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005013
5014 cc_library_shared {
5015 name: "libjni",
5016 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005017 shared_libs: ["libfoo"],
5018 stl: "none",
5019 system_shared_libs: [],
5020 apex_available: [ "myapex" ],
5021 sdk_version: "current",
5022 }
5023
5024 cc_library_shared {
5025 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005026 stl: "none",
5027 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005028 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005029 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005030 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005031 `)
5032
Sundong Ahnabb64432019-10-22 13:58:29 +09005033 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005034 apexRule := module.Rule("apexRule")
5035 copyCmds := apexRule.Args["copy_commands"]
5036
5037 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005038 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005039
Colin Crossaede88c2020-08-11 12:17:01 -07005040 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005041 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005042 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005043 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005044 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005045 // JNI libraries including transitive deps are
5046 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005047 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005048 // ... embedded inside APK (jnilibs.zip)
5049 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5050 // ... and not directly inside the APEX
5051 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5052 }
Dario Frenicde2a032019-10-27 00:29:22 +01005053}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005054
Dario Frenicde2a032019-10-27 00:29:22 +01005055func TestApexWithAppImports(t *testing.T) {
5056 ctx, _ := testApex(t, `
5057 apex {
5058 name: "myapex",
5059 key: "myapex.key",
5060 apps: [
5061 "AppFooPrebuilt",
5062 "AppFooPrivPrebuilt",
5063 ],
5064 }
5065
5066 apex_key {
5067 name: "myapex.key",
5068 public_key: "testkey.avbpubkey",
5069 private_key: "testkey.pem",
5070 }
5071
5072 android_app_import {
5073 name: "AppFooPrebuilt",
5074 apk: "PrebuiltAppFoo.apk",
5075 presigned: true,
5076 dex_preopt: {
5077 enabled: false,
5078 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005079 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005080 }
5081
5082 android_app_import {
5083 name: "AppFooPrivPrebuilt",
5084 apk: "PrebuiltAppFooPriv.apk",
5085 privileged: true,
5086 presigned: true,
5087 dex_preopt: {
5088 enabled: false,
5089 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005090 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005091 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005092 }
5093 `)
5094
Sundong Ahnabb64432019-10-22 13:58:29 +09005095 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005096 apexRule := module.Rule("apexRule")
5097 copyCmds := apexRule.Args["copy_commands"]
5098
5099 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005100 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5101}
5102
5103func TestApexWithAppImportsPrefer(t *testing.T) {
5104 ctx, _ := testApex(t, `
5105 apex {
5106 name: "myapex",
5107 key: "myapex.key",
5108 apps: [
5109 "AppFoo",
5110 ],
5111 }
5112
5113 apex_key {
5114 name: "myapex.key",
5115 public_key: "testkey.avbpubkey",
5116 private_key: "testkey.pem",
5117 }
5118
5119 android_app {
5120 name: "AppFoo",
5121 srcs: ["foo/bar/MyClass.java"],
5122 sdk_version: "none",
5123 system_modules: "none",
5124 apex_available: [ "myapex" ],
5125 }
5126
5127 android_app_import {
5128 name: "AppFoo",
5129 apk: "AppFooPrebuilt.apk",
5130 filename: "AppFooPrebuilt.apk",
5131 presigned: true,
5132 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005133 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005134 }
5135 `, withFiles(map[string][]byte{
5136 "AppFooPrebuilt.apk": nil,
5137 }))
5138
5139 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5140 "app/AppFoo/AppFooPrebuilt.apk",
5141 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005142}
5143
Dario Freni6f3937c2019-12-20 22:58:03 +00005144func TestApexWithTestHelperApp(t *testing.T) {
5145 ctx, _ := testApex(t, `
5146 apex {
5147 name: "myapex",
5148 key: "myapex.key",
5149 apps: [
5150 "TesterHelpAppFoo",
5151 ],
5152 }
5153
5154 apex_key {
5155 name: "myapex.key",
5156 public_key: "testkey.avbpubkey",
5157 private_key: "testkey.pem",
5158 }
5159
5160 android_test_helper_app {
5161 name: "TesterHelpAppFoo",
5162 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005163 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005164 }
5165
5166 `)
5167
5168 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5169 apexRule := module.Rule("apexRule")
5170 copyCmds := apexRule.Args["copy_commands"]
5171
5172 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5173}
5174
Jooyung Han18020ea2019-11-13 10:50:48 +09005175func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5176 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005177 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005178 apex {
5179 name: "myapex",
5180 key: "myapex.key",
5181 native_shared_libs: ["libfoo"],
5182 }
5183
5184 apex_key {
5185 name: "myapex.key",
5186 public_key: "testkey.avbpubkey",
5187 private_key: "testkey.pem",
5188 }
5189
5190 apex {
5191 name: "otherapex",
5192 key: "myapex.key",
5193 native_shared_libs: ["libfoo"],
5194 }
5195
5196 cc_defaults {
5197 name: "libfoo-defaults",
5198 apex_available: ["otherapex"],
5199 }
5200
5201 cc_library {
5202 name: "libfoo",
5203 defaults: ["libfoo-defaults"],
5204 stl: "none",
5205 system_shared_libs: [],
5206 }`)
5207}
5208
Paul Duffine52e66f2020-03-30 17:54:29 +01005209func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005210 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005211 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005212 apex {
5213 name: "myapex",
5214 key: "myapex.key",
5215 native_shared_libs: ["libfoo"],
5216 }
5217
5218 apex_key {
5219 name: "myapex.key",
5220 public_key: "testkey.avbpubkey",
5221 private_key: "testkey.pem",
5222 }
5223
5224 apex {
5225 name: "otherapex",
5226 key: "otherapex.key",
5227 native_shared_libs: ["libfoo"],
5228 }
5229
5230 apex_key {
5231 name: "otherapex.key",
5232 public_key: "testkey.avbpubkey",
5233 private_key: "testkey.pem",
5234 }
5235
5236 cc_library {
5237 name: "libfoo",
5238 stl: "none",
5239 system_shared_libs: [],
5240 apex_available: ["otherapex"],
5241 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005242}
Jiyong Park127b40b2019-09-30 16:04:35 +09005243
Paul Duffine52e66f2020-03-30 17:54:29 +01005244func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005245 // libbbaz is an indirect dep
Steven Moreland6e36cd62020-10-22 01:08:35 +00005246 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'. Dependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005247.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005248.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005249.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005250.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005251.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005252.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005253 apex {
5254 name: "myapex",
5255 key: "myapex.key",
5256 native_shared_libs: ["libfoo"],
5257 }
5258
5259 apex_key {
5260 name: "myapex.key",
5261 public_key: "testkey.avbpubkey",
5262 private_key: "testkey.pem",
5263 }
5264
Jiyong Park127b40b2019-09-30 16:04:35 +09005265 cc_library {
5266 name: "libfoo",
5267 stl: "none",
5268 shared_libs: ["libbar"],
5269 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005270 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005271 }
5272
5273 cc_library {
5274 name: "libbar",
5275 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005276 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005277 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005278 apex_available: ["myapex"],
5279 }
5280
5281 cc_library {
5282 name: "libbaz",
5283 stl: "none",
5284 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005285 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005286}
Jiyong Park127b40b2019-09-30 16:04:35 +09005287
Paul Duffine52e66f2020-03-30 17:54:29 +01005288func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005289 testApexError(t, "\"otherapex\" is not a valid module name", `
5290 apex {
5291 name: "myapex",
5292 key: "myapex.key",
5293 native_shared_libs: ["libfoo"],
5294 }
5295
5296 apex_key {
5297 name: "myapex.key",
5298 public_key: "testkey.avbpubkey",
5299 private_key: "testkey.pem",
5300 }
5301
5302 cc_library {
5303 name: "libfoo",
5304 stl: "none",
5305 system_shared_libs: [],
5306 apex_available: ["otherapex"],
5307 }`)
5308
Paul Duffine52e66f2020-03-30 17:54:29 +01005309 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005310 apex {
5311 name: "myapex",
5312 key: "myapex.key",
5313 native_shared_libs: ["libfoo", "libbar"],
5314 }
5315
5316 apex_key {
5317 name: "myapex.key",
5318 public_key: "testkey.avbpubkey",
5319 private_key: "testkey.pem",
5320 }
5321
5322 cc_library {
5323 name: "libfoo",
5324 stl: "none",
5325 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005326 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005327 apex_available: ["myapex"],
5328 }
5329
5330 cc_library {
5331 name: "libbar",
5332 stl: "none",
5333 system_shared_libs: [],
5334 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005335 }
5336
5337 cc_library {
5338 name: "libbaz",
5339 stl: "none",
5340 system_shared_libs: [],
5341 stubs: {
5342 versions: ["10", "20", "30"],
5343 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005344 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005345}
Jiyong Park127b40b2019-09-30 16:04:35 +09005346
Jiyong Park89e850a2020-04-07 16:37:39 +09005347func TestApexAvailable_CheckForPlatform(t *testing.T) {
Paul Duffine52e66f2020-03-30 17:54:29 +01005348 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005349 apex {
5350 name: "myapex",
5351 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005352 native_shared_libs: ["libbar", "libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005353 }
5354
5355 apex_key {
5356 name: "myapex.key",
5357 public_key: "testkey.avbpubkey",
5358 private_key: "testkey.pem",
5359 }
5360
5361 cc_library {
5362 name: "libfoo",
5363 stl: "none",
5364 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005365 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005366 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005367 }
5368
5369 cc_library {
5370 name: "libfoo2",
5371 stl: "none",
5372 system_shared_libs: [],
5373 shared_libs: ["libbaz"],
5374 apex_available: ["//apex_available:platform"],
5375 }
5376
5377 cc_library {
5378 name: "libbar",
5379 stl: "none",
5380 system_shared_libs: [],
5381 apex_available: ["myapex"],
5382 }
5383
5384 cc_library {
5385 name: "libbaz",
5386 stl: "none",
5387 system_shared_libs: [],
5388 apex_available: ["myapex"],
5389 stubs: {
5390 versions: ["1"],
5391 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005392 }`)
5393
Jiyong Park89e850a2020-04-07 16:37:39 +09005394 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5395 // because it depends on libbar which isn't available to platform
5396 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5397 if libfoo.NotAvailableForPlatform() != true {
5398 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5399 }
5400
5401 // libfoo2 however can be available to platform because it depends on libbaz which provides
5402 // stubs
5403 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5404 if libfoo2.NotAvailableForPlatform() == true {
5405 t.Errorf("%q should be available to platform", libfoo2.String())
5406 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005407}
Jiyong Parka90ca002019-10-07 15:47:24 +09005408
Paul Duffine52e66f2020-03-30 17:54:29 +01005409func TestApexAvailable_CreatedForApex(t *testing.T) {
Jiyong Park89e850a2020-04-07 16:37:39 +09005410 ctx, _ := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005411 apex {
5412 name: "myapex",
5413 key: "myapex.key",
5414 native_shared_libs: ["libfoo"],
5415 }
5416
5417 apex_key {
5418 name: "myapex.key",
5419 public_key: "testkey.avbpubkey",
5420 private_key: "testkey.pem",
5421 }
5422
5423 cc_library {
5424 name: "libfoo",
5425 stl: "none",
5426 system_shared_libs: [],
5427 apex_available: ["myapex"],
5428 static: {
5429 apex_available: ["//apex_available:platform"],
5430 },
5431 }`)
5432
Jiyong Park89e850a2020-04-07 16:37:39 +09005433 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5434 if libfooShared.NotAvailableForPlatform() != true {
5435 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5436 }
5437 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5438 if libfooStatic.NotAvailableForPlatform() != false {
5439 t.Errorf("%q should be available to platform", libfooStatic.String())
5440 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005441}
5442
Jiyong Park5d790c32019-11-15 18:40:32 +09005443func TestOverrideApex(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07005444 ctx, _ := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005445 apex {
5446 name: "myapex",
5447 key: "myapex.key",
5448 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005449 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09005450 }
5451
5452 override_apex {
5453 name: "override_myapex",
5454 base: "myapex",
5455 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005456 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005457 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005458 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005459 }
5460
5461 apex_key {
5462 name: "myapex.key",
5463 public_key: "testkey.avbpubkey",
5464 private_key: "testkey.pem",
5465 }
5466
5467 android_app {
5468 name: "app",
5469 srcs: ["foo/bar/MyClass.java"],
5470 package_name: "foo",
5471 sdk_version: "none",
5472 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005473 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005474 }
5475
5476 override_android_app {
5477 name: "override_app",
5478 base: "app",
5479 package_name: "bar",
5480 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005481 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005482
Jiyong Park317645e2019-12-05 13:20:58 +09005483 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5484 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5485 if originalVariant.GetOverriddenBy() != "" {
5486 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5487 }
5488 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5489 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5490 }
5491
Jiyong Park5d790c32019-11-15 18:40:32 +09005492 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5493 apexRule := module.Rule("apexRule")
5494 copyCmds := apexRule.Args["copy_commands"]
5495
5496 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005497 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005498
5499 apexBundle := module.Module().(*apexBundle)
5500 name := apexBundle.Name()
5501 if name != "override_myapex" {
5502 t.Errorf("name should be \"override_myapex\", but was %q", name)
5503 }
5504
Baligh Uddin004d7172020-02-19 21:29:28 -08005505 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5506 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5507 }
5508
Jiyong Park20bacab2020-03-03 11:45:41 +09005509 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005510 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005511
Colin Crossaa255532020-07-03 13:18:24 -07005512 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005513 var builder strings.Builder
5514 data.Custom(&builder, name, "TARGET_", "", data)
5515 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005516 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005517 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5518 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005519 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005520 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005521 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005522 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5523 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005524}
5525
Jooyung Han214bf372019-11-12 13:03:50 +09005526func TestLegacyAndroid10Support(t *testing.T) {
5527 ctx, _ := testApex(t, `
5528 apex {
5529 name: "myapex",
5530 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005531 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005532 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005533 }
5534
5535 apex_key {
5536 name: "myapex.key",
5537 public_key: "testkey.avbpubkey",
5538 private_key: "testkey.pem",
5539 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005540
5541 cc_library {
5542 name: "mylib",
5543 srcs: ["mylib.cpp"],
5544 stl: "libc++",
5545 system_shared_libs: [],
5546 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005547 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005548 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005549 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005550
5551 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5552 args := module.Rule("apexRule").Args
5553 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005554 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005555
5556 // The copies of the libraries in the apex should have one more dependency than
5557 // the ones outside the apex, namely the unwinder. Ideally we should check
5558 // the dependency names directly here but for some reason the names are blank in
5559 // this test.
5560 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005561 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005562 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5563 if len(apexImplicits) != len(nonApexImplicits)+1 {
5564 t.Errorf("%q missing unwinder dep", lib)
5565 }
5566 }
Jooyung Han214bf372019-11-12 13:03:50 +09005567}
5568
Paul Duffin9b879592020-05-26 13:21:35 +01005569var filesForSdkLibrary = map[string][]byte{
5570 "api/current.txt": nil,
5571 "api/removed.txt": nil,
5572 "api/system-current.txt": nil,
5573 "api/system-removed.txt": nil,
5574 "api/test-current.txt": nil,
5575 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005576
Anton Hanssondff2c782020-12-21 17:10:01 +00005577 "100/public/api/foo.txt": nil,
5578 "100/public/api/foo-removed.txt": nil,
5579 "100/system/api/foo.txt": nil,
5580 "100/system/api/foo-removed.txt": nil,
5581
Paul Duffineedc5d52020-06-12 17:46:39 +01005582 // For java_sdk_library_import
5583 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005584}
5585
Jooyung Han58f26ab2019-12-18 15:34:32 +09005586func TestJavaSDKLibrary(t *testing.T) {
5587 ctx, _ := testApex(t, `
5588 apex {
5589 name: "myapex",
5590 key: "myapex.key",
5591 java_libs: ["foo"],
5592 }
5593
5594 apex_key {
5595 name: "myapex.key",
5596 public_key: "testkey.avbpubkey",
5597 private_key: "testkey.pem",
5598 }
5599
5600 java_sdk_library {
5601 name: "foo",
5602 srcs: ["a.java"],
5603 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005604 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005605 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005606
5607 prebuilt_apis {
5608 name: "sdk",
5609 api_dirs: ["100"],
5610 }
Paul Duffin9b879592020-05-26 13:21:35 +01005611 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005612
5613 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005614 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005615 "javalib/foo.jar",
5616 "etc/permissions/foo.xml",
5617 })
5618 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005619 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5620 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005621}
5622
Paul Duffin9b879592020-05-26 13:21:35 +01005623func TestJavaSDKLibrary_WithinApex(t *testing.T) {
5624 ctx, _ := testApex(t, `
5625 apex {
5626 name: "myapex",
5627 key: "myapex.key",
5628 java_libs: ["foo", "bar"],
5629 }
5630
5631 apex_key {
5632 name: "myapex.key",
5633 public_key: "testkey.avbpubkey",
5634 private_key: "testkey.pem",
5635 }
5636
5637 java_sdk_library {
5638 name: "foo",
5639 srcs: ["a.java"],
5640 api_packages: ["foo"],
5641 apex_available: ["myapex"],
5642 sdk_version: "none",
5643 system_modules: "none",
5644 }
5645
5646 java_library {
5647 name: "bar",
5648 srcs: ["a.java"],
5649 libs: ["foo"],
5650 apex_available: ["myapex"],
5651 sdk_version: "none",
5652 system_modules: "none",
5653 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005654
5655 prebuilt_apis {
5656 name: "sdk",
5657 api_dirs: ["100"],
5658 }
Paul Duffin9b879592020-05-26 13:21:35 +01005659 `, withFiles(filesForSdkLibrary))
5660
5661 // java_sdk_library installs both impl jar and permission XML
5662 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5663 "javalib/bar.jar",
5664 "javalib/foo.jar",
5665 "etc/permissions/foo.xml",
5666 })
5667
5668 // The bar library should depend on the implementation jar.
5669 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5670 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5671 t.Errorf("expected %q, found %#q", expected, actual)
5672 }
5673}
5674
5675func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
5676 ctx, _ := testApex(t, `
5677 apex {
5678 name: "myapex",
5679 key: "myapex.key",
5680 java_libs: ["foo"],
5681 }
5682
5683 apex_key {
5684 name: "myapex.key",
5685 public_key: "testkey.avbpubkey",
5686 private_key: "testkey.pem",
5687 }
5688
5689 java_sdk_library {
5690 name: "foo",
5691 srcs: ["a.java"],
5692 api_packages: ["foo"],
5693 apex_available: ["myapex"],
5694 sdk_version: "none",
5695 system_modules: "none",
5696 }
5697
5698 java_library {
5699 name: "bar",
5700 srcs: ["a.java"],
5701 libs: ["foo"],
5702 sdk_version: "none",
5703 system_modules: "none",
5704 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005705
5706 prebuilt_apis {
5707 name: "sdk",
5708 api_dirs: ["100"],
5709 }
Paul Duffin9b879592020-05-26 13:21:35 +01005710 `, withFiles(filesForSdkLibrary))
5711
5712 // java_sdk_library installs both impl jar and permission XML
5713 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5714 "javalib/foo.jar",
5715 "etc/permissions/foo.xml",
5716 })
5717
5718 // The bar library should depend on the stubs jar.
5719 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5720 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5721 t.Errorf("expected %q, found %#q", expected, actual)
5722 }
5723}
5724
Paul Duffineedc5d52020-06-12 17:46:39 +01005725func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Anton Hanssondff2c782020-12-21 17:10:01 +00005726 ctx, _ := testApex(t, `
5727 prebuilt_apis {
5728 name: "sdk",
5729 api_dirs: ["100"],
5730 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005731 withFiles(map[string][]byte{
5732 "apex/a.java": nil,
5733 "apex/apex_manifest.json": nil,
5734 "apex/Android.bp": []byte(`
5735 package {
5736 default_visibility: ["//visibility:private"],
5737 }
5738
5739 apex {
5740 name: "myapex",
5741 key: "myapex.key",
5742 java_libs: ["foo", "bar"],
5743 }
5744
5745 apex_key {
5746 name: "myapex.key",
5747 public_key: "testkey.avbpubkey",
5748 private_key: "testkey.pem",
5749 }
5750
5751 java_library {
5752 name: "bar",
5753 srcs: ["a.java"],
5754 libs: ["foo"],
5755 apex_available: ["myapex"],
5756 sdk_version: "none",
5757 system_modules: "none",
5758 }
5759`),
5760 "source/a.java": nil,
5761 "source/api/current.txt": nil,
5762 "source/api/removed.txt": nil,
5763 "source/Android.bp": []byte(`
5764 package {
5765 default_visibility: ["//visibility:private"],
5766 }
5767
5768 java_sdk_library {
5769 name: "foo",
5770 visibility: ["//apex"],
5771 srcs: ["a.java"],
5772 api_packages: ["foo"],
5773 apex_available: ["myapex"],
5774 sdk_version: "none",
5775 system_modules: "none",
5776 public: {
5777 enabled: true,
5778 },
5779 }
5780`),
5781 "prebuilt/a.jar": nil,
5782 "prebuilt/Android.bp": []byte(`
5783 package {
5784 default_visibility: ["//visibility:private"],
5785 }
5786
5787 java_sdk_library_import {
5788 name: "foo",
5789 visibility: ["//apex", "//source"],
5790 apex_available: ["myapex"],
5791 prefer: true,
5792 public: {
5793 jars: ["a.jar"],
5794 },
5795 }
5796`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005797 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005798 )
5799
5800 // java_sdk_library installs both impl jar and permission XML
5801 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5802 "javalib/bar.jar",
5803 "javalib/foo.jar",
5804 "etc/permissions/foo.xml",
5805 })
5806
5807 // The bar library should depend on the implementation jar.
5808 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5809 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5810 t.Errorf("expected %q, found %#q", expected, actual)
5811 }
5812}
5813
5814func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5815 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5816 apex {
5817 name: "myapex",
5818 key: "myapex.key",
5819 java_libs: ["foo"],
5820 }
5821
5822 apex_key {
5823 name: "myapex.key",
5824 public_key: "testkey.avbpubkey",
5825 private_key: "testkey.pem",
5826 }
5827
5828 java_sdk_library_import {
5829 name: "foo",
5830 apex_available: ["myapex"],
5831 prefer: true,
5832 public: {
5833 jars: ["a.jar"],
5834 },
5835 }
5836
5837 `, withFiles(filesForSdkLibrary))
5838}
5839
atrost6e126252020-01-27 17:01:16 +00005840func TestCompatConfig(t *testing.T) {
5841 ctx, _ := testApex(t, `
5842 apex {
5843 name: "myapex",
5844 key: "myapex.key",
5845 prebuilts: ["myjar-platform-compat-config"],
5846 java_libs: ["myjar"],
5847 }
5848
5849 apex_key {
5850 name: "myapex.key",
5851 public_key: "testkey.avbpubkey",
5852 private_key: "testkey.pem",
5853 }
5854
5855 platform_compat_config {
5856 name: "myjar-platform-compat-config",
5857 src: ":myjar",
5858 }
5859
5860 java_library {
5861 name: "myjar",
5862 srcs: ["foo/bar/MyClass.java"],
5863 sdk_version: "none",
5864 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005865 apex_available: [ "myapex" ],
5866 }
5867 `)
5868 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5869 "etc/compatconfig/myjar-platform-compat-config.xml",
5870 "javalib/myjar.jar",
5871 })
5872}
5873
Jiyong Park479321d2019-12-16 11:47:12 +09005874func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5875 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5876 apex {
5877 name: "myapex",
5878 key: "myapex.key",
5879 java_libs: ["myjar"],
5880 }
5881
5882 apex_key {
5883 name: "myapex.key",
5884 public_key: "testkey.avbpubkey",
5885 private_key: "testkey.pem",
5886 }
5887
5888 java_library {
5889 name: "myjar",
5890 srcs: ["foo/bar/MyClass.java"],
5891 sdk_version: "none",
5892 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005893 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005894 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005895 }
5896 `)
5897}
5898
Jiyong Park7afd1072019-12-30 16:56:33 +09005899func TestCarryRequiredModuleNames(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07005900 ctx, _ := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005901 apex {
5902 name: "myapex",
5903 key: "myapex.key",
5904 native_shared_libs: ["mylib"],
5905 }
5906
5907 apex_key {
5908 name: "myapex.key",
5909 public_key: "testkey.avbpubkey",
5910 private_key: "testkey.pem",
5911 }
5912
5913 cc_library {
5914 name: "mylib",
5915 srcs: ["mylib.cpp"],
5916 system_shared_libs: [],
5917 stl: "none",
5918 required: ["a", "b"],
5919 host_required: ["c", "d"],
5920 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005921 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005922 }
5923 `)
5924
5925 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005926 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09005927 name := apexBundle.BaseModuleName()
5928 prefix := "TARGET_"
5929 var builder strings.Builder
5930 data.Custom(&builder, name, prefix, "", data)
5931 androidMk := builder.String()
5932 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
5933 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
5934 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
5935}
5936
Jiyong Park7cd10e32020-01-14 09:22:18 +09005937func TestSymlinksFromApexToSystem(t *testing.T) {
5938 bp := `
5939 apex {
5940 name: "myapex",
5941 key: "myapex.key",
5942 native_shared_libs: ["mylib"],
5943 java_libs: ["myjar"],
5944 }
5945
Jiyong Park9d677202020-02-19 16:29:35 +09005946 apex {
5947 name: "myapex.updatable",
5948 key: "myapex.key",
5949 native_shared_libs: ["mylib"],
5950 java_libs: ["myjar"],
5951 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09005952 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09005953 }
5954
Jiyong Park7cd10e32020-01-14 09:22:18 +09005955 apex_key {
5956 name: "myapex.key",
5957 public_key: "testkey.avbpubkey",
5958 private_key: "testkey.pem",
5959 }
5960
5961 cc_library {
5962 name: "mylib",
5963 srcs: ["mylib.cpp"],
5964 shared_libs: ["myotherlib"],
5965 system_shared_libs: [],
5966 stl: "none",
5967 apex_available: [
5968 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005969 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005970 "//apex_available:platform",
5971 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005972 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005973 }
5974
5975 cc_library {
5976 name: "myotherlib",
5977 srcs: ["mylib.cpp"],
5978 system_shared_libs: [],
5979 stl: "none",
5980 apex_available: [
5981 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005982 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005983 "//apex_available:platform",
5984 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005985 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005986 }
5987
5988 java_library {
5989 name: "myjar",
5990 srcs: ["foo/bar/MyClass.java"],
5991 sdk_version: "none",
5992 system_modules: "none",
5993 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09005994 apex_available: [
5995 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005996 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005997 "//apex_available:platform",
5998 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005999 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006000 }
6001
6002 java_library {
6003 name: "myotherjar",
6004 srcs: ["foo/bar/MyClass.java"],
6005 sdk_version: "none",
6006 system_modules: "none",
6007 apex_available: [
6008 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006009 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006010 "//apex_available:platform",
6011 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006012 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006013 }
6014 `
6015
6016 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6017 for _, f := range files {
6018 if f.path == file {
6019 if f.isLink {
6020 t.Errorf("%q is not a real file", file)
6021 }
6022 return
6023 }
6024 }
6025 t.Errorf("%q is not found", file)
6026 }
6027
6028 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6029 for _, f := range files {
6030 if f.path == file {
6031 if !f.isLink {
6032 t.Errorf("%q is not a symlink", file)
6033 }
6034 return
6035 }
6036 }
6037 t.Errorf("%q is not found", file)
6038 }
6039
Jiyong Park9d677202020-02-19 16:29:35 +09006040 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6041 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09006042 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006043 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006044 ensureRealfileExists(t, files, "javalib/myjar.jar")
6045 ensureRealfileExists(t, files, "lib64/mylib.so")
6046 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6047
Jiyong Park9d677202020-02-19 16:29:35 +09006048 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6049 ensureRealfileExists(t, files, "javalib/myjar.jar")
6050 ensureRealfileExists(t, files, "lib64/mylib.so")
6051 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6052
6053 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09006054 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006055 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006056 ensureRealfileExists(t, files, "javalib/myjar.jar")
6057 ensureRealfileExists(t, files, "lib64/mylib.so")
6058 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006059
6060 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6061 ensureRealfileExists(t, files, "javalib/myjar.jar")
6062 ensureRealfileExists(t, files, "lib64/mylib.so")
6063 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006064}
6065
Yo Chiange8128052020-07-23 20:09:18 +08006066func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07006067 ctx, _ := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006068 apex {
6069 name: "myapex",
6070 key: "myapex.key",
6071 native_shared_libs: ["mylib"],
6072 }
6073
6074 apex_key {
6075 name: "myapex.key",
6076 public_key: "testkey.avbpubkey",
6077 private_key: "testkey.pem",
6078 }
6079
6080 cc_library_shared {
6081 name: "mylib",
6082 srcs: ["mylib.cpp"],
6083 shared_libs: ["myotherlib"],
6084 system_shared_libs: [],
6085 stl: "none",
6086 apex_available: [
6087 "myapex",
6088 "//apex_available:platform",
6089 ],
6090 }
6091
6092 cc_prebuilt_library_shared {
6093 name: "myotherlib",
6094 srcs: ["prebuilt.so"],
6095 system_shared_libs: [],
6096 stl: "none",
6097 apex_available: [
6098 "myapex",
6099 "//apex_available:platform",
6100 ],
6101 }
6102 `)
6103
6104 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006105 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006106 var builder strings.Builder
6107 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6108 androidMk := builder.String()
6109 // `myotherlib` is added to `myapex` as symlink
6110 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6111 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6112 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6113 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006114 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += mylib.myapex:64 myotherlib:64 apex_manifest.pb.myapex apex_pubkey.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08006115}
6116
Jooyung Han643adc42020-02-27 13:50:06 +09006117func TestApexWithJniLibs(t *testing.T) {
6118 ctx, _ := testApex(t, `
6119 apex {
6120 name: "myapex",
6121 key: "myapex.key",
6122 jni_libs: ["mylib"],
6123 }
6124
6125 apex_key {
6126 name: "myapex.key",
6127 public_key: "testkey.avbpubkey",
6128 private_key: "testkey.pem",
6129 }
6130
6131 cc_library {
6132 name: "mylib",
6133 srcs: ["mylib.cpp"],
6134 shared_libs: ["mylib2"],
6135 system_shared_libs: [],
6136 stl: "none",
6137 apex_available: [ "myapex" ],
6138 }
6139
6140 cc_library {
6141 name: "mylib2",
6142 srcs: ["mylib.cpp"],
6143 system_shared_libs: [],
6144 stl: "none",
6145 apex_available: [ "myapex" ],
6146 }
6147 `)
6148
6149 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6150 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6151 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6152 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6153 "lib64/mylib.so",
6154 "lib64/mylib2.so",
6155 })
6156}
6157
Jooyung Han49f67012020-04-17 13:43:10 +09006158func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
6159 ctx, _ := testApex(t, `
6160 apex {
6161 name: "myapex",
6162 key: "myapex.key",
6163 }
6164 apex_key {
6165 name: "myapex.key",
6166 public_key: "testkey.avbpubkey",
6167 private_key: "testkey.pem",
6168 }
6169 `, func(fs map[string][]byte, config android.Config) {
6170 delete(config.Targets, android.Android)
6171 config.AndroidCommonTarget = android.Target{}
6172 })
6173
6174 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6175 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6176 }
6177}
6178
Jiyong Parkbd159612020-02-28 15:22:21 +09006179func TestAppBundle(t *testing.T) {
6180 ctx, _ := testApex(t, `
6181 apex {
6182 name: "myapex",
6183 key: "myapex.key",
6184 apps: ["AppFoo"],
6185 }
6186
6187 apex_key {
6188 name: "myapex.key",
6189 public_key: "testkey.avbpubkey",
6190 private_key: "testkey.pem",
6191 }
6192
6193 android_app {
6194 name: "AppFoo",
6195 srcs: ["foo/bar/MyClass.java"],
6196 sdk_version: "none",
6197 system_modules: "none",
6198 apex_available: [ "myapex" ],
6199 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006200 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006201
Colin Crosscf371cc2020-11-13 11:48:42 -08006202 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006203 content := bundleConfigRule.Args["content"]
6204
6205 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006206 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 +09006207}
6208
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006209func TestAppSetBundle(t *testing.T) {
6210 ctx, _ := testApex(t, `
6211 apex {
6212 name: "myapex",
6213 key: "myapex.key",
6214 apps: ["AppSet"],
6215 }
6216
6217 apex_key {
6218 name: "myapex.key",
6219 public_key: "testkey.avbpubkey",
6220 private_key: "testkey.pem",
6221 }
6222
6223 android_app_set {
6224 name: "AppSet",
6225 set: "AppSet.apks",
6226 }`)
6227 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006228 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006229 content := bundleConfigRule.Args["content"]
6230 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6231 s := mod.Rule("apexRule").Args["copy_commands"]
6232 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6233 if len(copyCmds) != 3 {
6234 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6235 }
6236 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6237 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6238 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6239}
6240
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006241func TestAppSetBundlePrebuilt(t *testing.T) {
6242 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
6243 bp := `
6244 apex_set {
6245 name: "myapex",
6246 filename: "foo_v2.apex",
6247 sanitized: {
6248 none: { set: "myapex.apks", },
6249 hwaddress: { set: "myapex.hwasan.apks", },
6250 },
6251 }`
6252 fs["Android.bp"] = []byte(bp)
6253
6254 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
6255 })
6256
6257 m := ctx.ModuleForTests("myapex", "android_common")
6258 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6259
6260 actual := extractedApex.Inputs
6261 if len(actual) != 1 {
6262 t.Errorf("expected a single input")
6263 }
6264
6265 expected := "myapex.hwasan.apks"
6266 if actual[0].String() != expected {
6267 t.Errorf("expected %s, got %s", expected, actual[0].String())
6268 }
6269}
6270
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006271func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006272 t.Helper()
6273
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006274 bp := `
6275 java_library {
6276 name: "some-updatable-apex-lib",
6277 srcs: ["a.java"],
6278 sdk_version: "current",
6279 apex_available: [
6280 "some-updatable-apex",
6281 ],
6282 }
6283
6284 java_library {
6285 name: "some-non-updatable-apex-lib",
6286 srcs: ["a.java"],
6287 apex_available: [
6288 "some-non-updatable-apex",
6289 ],
6290 }
6291
6292 java_library {
6293 name: "some-platform-lib",
6294 srcs: ["a.java"],
6295 sdk_version: "current",
6296 installable: true,
6297 }
6298
6299 java_library {
6300 name: "some-art-lib",
6301 srcs: ["a.java"],
6302 sdk_version: "current",
6303 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006304 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006305 ],
6306 hostdex: true,
6307 }
6308
6309 apex {
6310 name: "some-updatable-apex",
6311 key: "some-updatable-apex.key",
6312 java_libs: ["some-updatable-apex-lib"],
6313 updatable: true,
6314 min_sdk_version: "current",
6315 }
6316
6317 apex {
6318 name: "some-non-updatable-apex",
6319 key: "some-non-updatable-apex.key",
6320 java_libs: ["some-non-updatable-apex-lib"],
6321 }
6322
6323 apex_key {
6324 name: "some-updatable-apex.key",
6325 }
6326
6327 apex_key {
6328 name: "some-non-updatable-apex.key",
6329 }
6330
6331 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006332 name: "com.android.art.debug",
6333 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006334 java_libs: ["some-art-lib"],
6335 updatable: true,
6336 min_sdk_version: "current",
6337 }
6338
6339 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006340 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006341 }
6342
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006343 filegroup {
6344 name: "some-updatable-apex-file_contexts",
6345 srcs: [
6346 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6347 ],
6348 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006349
6350 filegroup {
6351 name: "some-non-updatable-apex-file_contexts",
6352 srcs: [
6353 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6354 ],
6355 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006356 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006357
6358 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6359}
6360
Paul Duffin064b70c2020-11-02 17:32:38 +00006361func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006362 t.Helper()
6363
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006364 bp += cc.GatherRequiredDepsForTest(android.Android)
6365 bp += java.GatherRequiredDepsForTest()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006366
6367 fs := map[string][]byte{
6368 "a.java": nil,
6369 "a.jar": nil,
6370 "build/make/target/product/security": nil,
6371 "apex_manifest.json": nil,
6372 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006373 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006374 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6375 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6376 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006377 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006378 }
6379 cc.GatherRequiredFilesForTest(fs)
6380
Paul Duffin39853512021-02-26 11:09:39 +00006381 for k, v := range filesForSdkLibrary {
6382 fs[k] = v
6383 }
Colin Crossae8600b2020-10-29 17:09:13 -07006384 config := android.TestArchConfig(buildDir, nil, bp, fs)
6385
6386 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006387 ctx.RegisterModuleType("apex", BundleFactory)
6388 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00006389 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006390 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01006391 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin37856732021-02-26 14:24:15 +00006392 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffin021f4e52020-07-30 16:04:17 +01006393 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006394 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006395 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf38931c2021-02-05 16:58:28 +00006396 java.RegisterHiddenApiSingletonComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006397 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6398 ctx.PreDepsMutators(RegisterPreDepsMutators)
6399 ctx.PostDepsMutators(RegisterPostDepsMutators)
6400
Colin Crossae8600b2020-10-29 17:09:13 -07006401 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006402
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006403 pathCtx := android.PathContextForTesting(config)
6404 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6405 transformDexpreoptConfig(dexpreoptConfig)
6406 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6407
Paul Duffinf38931c2021-02-05 16:58:28 +00006408 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
Paul Duffin4fd997b2021-02-03 20:06:33 +00006409 // product variables.
Paul Duffinf38931c2021-02-05 16:58:28 +00006410 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6411 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6412
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006413 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6414 android.FailIfErrored(t, errs)
6415
6416 _, errs = ctx.PrepareBuildActions(config)
6417 if errmsg == "" {
6418 android.FailIfErrored(t, errs)
6419 } else if len(errs) > 0 {
6420 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006421 } else {
6422 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6423 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006424
6425 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006426}
6427
Jooyung Han548640b2020-04-27 12:10:30 +09006428func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6429 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6430 apex {
6431 name: "myapex",
6432 key: "myapex.key",
6433 updatable: true,
6434 }
6435
6436 apex_key {
6437 name: "myapex.key",
6438 public_key: "testkey.avbpubkey",
6439 private_key: "testkey.pem",
6440 }
6441 `)
6442}
6443
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006444func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006445 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006446 var transform func(*dexpreopt.GlobalConfig)
6447
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006448 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6449 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006450 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006451 }
6452 testNoUpdatableJarsInBootImage(t, "", transform)
6453 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006454
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006455 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006456 err = `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006457 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006458 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006459 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006460 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006461 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006462
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006463 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 -07006464 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 +01006465 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006466 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006467 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006468 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006469 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006470
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006471 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 -07006472 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006473 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006474 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006475 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006476 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006477 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006478
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006479 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 -07006480 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 +01006481 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006482 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006483 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006484 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006485 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006486
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006487 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6488 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006489 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006490 }
6491 testNoUpdatableJarsInBootImage(t, "", transform)
6492 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006493
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006494 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006495 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006496 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006497 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006498 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006499 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006500 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006501
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006502 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006503 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006504 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006505 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006506 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006507 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006508 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006509
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006510 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006511 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006512 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006513 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006514 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006515 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006516 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006517
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006518 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6519 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006520 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006521 }
6522 testNoUpdatableJarsInBootImage(t, "", transform)
6523 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006524
6525}
6526
6527func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6528 transform := func(config *dexpreopt.GlobalConfig) {
6529 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6530 }
6531 t.Run("prebuilt no source", func(t *testing.T) {
6532 testDexpreoptWithApexes(t, `
6533 prebuilt_apex {
6534 name: "myapex" ,
6535 arch: {
6536 arm64: {
6537 src: "myapex-arm64.apex",
6538 },
6539 arm: {
6540 src: "myapex-arm.apex",
6541 },
6542 },
6543 exported_java_libs: ["libfoo"],
6544 }
6545
6546 java_import {
6547 name: "libfoo",
6548 jars: ["libfoo.jar"],
6549 }
6550`, "", transform)
6551 })
6552
6553 t.Run("prebuilt no source", func(t *testing.T) {
6554 testDexpreoptWithApexes(t, `
6555 prebuilt_apex {
6556 name: "myapex" ,
6557 arch: {
6558 arm64: {
6559 src: "myapex-arm64.apex",
6560 },
6561 arm: {
6562 src: "myapex-arm.apex",
6563 },
6564 },
6565 exported_java_libs: ["libfoo"],
6566 }
6567
6568 java_import {
6569 name: "libfoo",
6570 jars: ["libfoo.jar"],
6571 }
6572`, "", transform)
6573 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006574}
6575
Andrei Onea115e7e72020-06-05 21:14:03 +01006576func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6577 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006578 bp += `
6579 apex_key {
6580 name: "myapex.key",
6581 public_key: "testkey.avbpubkey",
6582 private_key: "testkey.pem",
6583 }`
6584 fs := map[string][]byte{
6585 "lib1/src/A.java": nil,
6586 "lib2/src/B.java": nil,
6587 "system/sepolicy/apex/myapex-file_contexts": nil,
6588 }
6589
Colin Crossae8600b2020-10-29 17:09:13 -07006590 config := android.TestArchConfig(buildDir, nil, bp, fs)
6591 android.SetTestNeverallowRules(config, rules)
6592 updatableBootJars := make([]string, 0, len(apexBootJars))
6593 for _, apexBootJar := range apexBootJars {
6594 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6595 }
6596 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6597
6598 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006599 ctx.RegisterModuleType("apex", BundleFactory)
6600 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6601 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6602 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006603 java.RegisterRequiredBuildComponentsForTest(ctx)
Andrei Onea115e7e72020-06-05 21:14:03 +01006604 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6605 ctx.PreDepsMutators(RegisterPreDepsMutators)
6606 ctx.PostDepsMutators(RegisterPostDepsMutators)
6607 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6608
Colin Crossae8600b2020-10-29 17:09:13 -07006609 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006610
6611 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6612 android.FailIfErrored(t, errs)
6613
6614 _, errs = ctx.PrepareBuildActions(config)
6615 if errmsg == "" {
6616 android.FailIfErrored(t, errs)
6617 } else if len(errs) > 0 {
6618 android.FailIfNoMatchingErrors(t, errmsg, errs)
6619 return
6620 } else {
6621 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6622 }
6623}
6624
6625func TestApexPermittedPackagesRules(t *testing.T) {
6626 testcases := []struct {
6627 name string
6628 expectedError string
6629 bp string
6630 bootJars []string
6631 modulesPackages map[string][]string
6632 }{
6633
6634 {
6635 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6636 expectedError: "",
6637 bp: `
6638 java_library {
6639 name: "bcp_lib1",
6640 srcs: ["lib1/src/*.java"],
6641 permitted_packages: ["foo.bar"],
6642 apex_available: ["myapex"],
6643 sdk_version: "none",
6644 system_modules: "none",
6645 }
6646 java_library {
6647 name: "nonbcp_lib2",
6648 srcs: ["lib2/src/*.java"],
6649 apex_available: ["myapex"],
6650 permitted_packages: ["a.b"],
6651 sdk_version: "none",
6652 system_modules: "none",
6653 }
6654 apex {
6655 name: "myapex",
6656 key: "myapex.key",
6657 java_libs: ["bcp_lib1", "nonbcp_lib2"],
6658 }`,
6659 bootJars: []string{"bcp_lib1"},
6660 modulesPackages: map[string][]string{
6661 "myapex": []string{
6662 "foo.bar",
6663 },
6664 },
6665 },
6666 {
6667 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6668 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.`,
6669 bp: `
6670 java_library {
6671 name: "bcp_lib1",
6672 srcs: ["lib1/src/*.java"],
6673 apex_available: ["myapex"],
6674 permitted_packages: ["foo.bar"],
6675 sdk_version: "none",
6676 system_modules: "none",
6677 }
6678 java_library {
6679 name: "bcp_lib2",
6680 srcs: ["lib2/src/*.java"],
6681 apex_available: ["myapex"],
6682 permitted_packages: ["foo.bar", "bar.baz"],
6683 sdk_version: "none",
6684 system_modules: "none",
6685 }
6686 apex {
6687 name: "myapex",
6688 key: "myapex.key",
6689 java_libs: ["bcp_lib1", "bcp_lib2"],
6690 }
6691 `,
6692 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6693 modulesPackages: map[string][]string{
6694 "myapex": []string{
6695 "foo.bar",
6696 },
6697 },
6698 },
6699 }
6700 for _, tc := range testcases {
6701 t.Run(tc.name, func(t *testing.T) {
6702 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6703 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6704 })
6705 }
6706}
6707
Jiyong Park62304bb2020-04-13 16:19:48 +09006708func TestTestFor(t *testing.T) {
6709 ctx, _ := testApex(t, `
6710 apex {
6711 name: "myapex",
6712 key: "myapex.key",
6713 native_shared_libs: ["mylib", "myprivlib"],
6714 }
6715
6716 apex_key {
6717 name: "myapex.key",
6718 public_key: "testkey.avbpubkey",
6719 private_key: "testkey.pem",
6720 }
6721
6722 cc_library {
6723 name: "mylib",
6724 srcs: ["mylib.cpp"],
6725 system_shared_libs: [],
6726 stl: "none",
6727 stubs: {
6728 versions: ["1"],
6729 },
6730 apex_available: ["myapex"],
6731 }
6732
6733 cc_library {
6734 name: "myprivlib",
6735 srcs: ["mylib.cpp"],
6736 system_shared_libs: [],
6737 stl: "none",
6738 apex_available: ["myapex"],
6739 }
6740
6741
6742 cc_test {
6743 name: "mytest",
6744 gtest: false,
6745 srcs: ["mylib.cpp"],
6746 system_shared_libs: [],
6747 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006748 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006749 test_for: ["myapex"]
6750 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006751
6752 cc_library {
6753 name: "mytestlib",
6754 srcs: ["mylib.cpp"],
6755 system_shared_libs: [],
6756 shared_libs: ["mylib", "myprivlib"],
6757 stl: "none",
6758 test_for: ["myapex"],
6759 }
6760
6761 cc_benchmark {
6762 name: "mybench",
6763 srcs: ["mylib.cpp"],
6764 system_shared_libs: [],
6765 shared_libs: ["mylib", "myprivlib"],
6766 stl: "none",
6767 test_for: ["myapex"],
6768 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006769 `)
6770
6771 // the test 'mytest' is a test for the apex, therefore is linked to the
6772 // actual implementation of mylib instead of its stub.
6773 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6774 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6775 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park46a512f2020-12-04 18:02:13 +09006776
6777 // The same should be true for cc_library
6778 ldFlags = ctx.ModuleForTests("mytestlib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
6779 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6780 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
6781
6782 // ... and for cc_benchmark
6783 ldFlags = ctx.ModuleForTests("mybench", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6784 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6785 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006786}
6787
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006788// TODO(jungjw): Move this to proptools
6789func intPtr(i int) *int {
6790 return &i
6791}
6792
6793func TestApexSet(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07006794 ctx, _ := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006795 apex_set {
6796 name: "myapex",
6797 set: "myapex.apks",
6798 filename: "foo_v2.apex",
6799 overrides: ["foo"],
6800 }
6801 `, func(fs map[string][]byte, config android.Config) {
6802 config.TestProductVariables.Platform_sdk_version = intPtr(30)
Jaewoong Jung829b7132020-06-10 12:23:32 -07006803 config.Targets[android.Android] = []android.Target{
6804 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6805 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6806 }
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006807 })
6808
6809 m := ctx.ModuleForTests("myapex", "android_common")
6810
6811 // Check extract_apks tool parameters.
6812 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6813 actual := extractedApex.Args["abis"]
6814 expected := "ARMEABI_V7A,ARM64_V8A"
6815 if actual != expected {
6816 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6817 }
6818 actual = extractedApex.Args["sdk-version"]
6819 expected = "30"
6820 if actual != expected {
6821 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6822 }
6823
6824 a := m.Module().(*ApexSet)
6825 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07006826 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006827 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6828 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6829 }
6830}
6831
Jiyong Park7d95a512020-05-10 15:16:24 +09006832func TestNoStaticLinkingToStubsLib(t *testing.T) {
6833 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6834 apex {
6835 name: "myapex",
6836 key: "myapex.key",
6837 native_shared_libs: ["mylib"],
6838 }
6839
6840 apex_key {
6841 name: "myapex.key",
6842 public_key: "testkey.avbpubkey",
6843 private_key: "testkey.pem",
6844 }
6845
6846 cc_library {
6847 name: "mylib",
6848 srcs: ["mylib.cpp"],
6849 static_libs: ["otherlib"],
6850 system_shared_libs: [],
6851 stl: "none",
6852 apex_available: [ "myapex" ],
6853 }
6854
6855 cc_library {
6856 name: "otherlib",
6857 srcs: ["mylib.cpp"],
6858 system_shared_libs: [],
6859 stl: "none",
6860 stubs: {
6861 versions: ["1", "2", "3"],
6862 },
6863 apex_available: [ "myapex" ],
6864 }
6865 `)
6866}
6867
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006868func TestApexKeysTxt(t *testing.T) {
6869 ctx, _ := testApex(t, `
6870 apex {
6871 name: "myapex",
6872 key: "myapex.key",
6873 }
6874
6875 apex_key {
6876 name: "myapex.key",
6877 public_key: "testkey.avbpubkey",
6878 private_key: "testkey.pem",
6879 }
6880
6881 prebuilt_apex {
6882 name: "myapex",
6883 prefer: true,
6884 arch: {
6885 arm64: {
6886 src: "myapex-arm64.apex",
6887 },
6888 arm: {
6889 src: "myapex-arm.apex",
6890 },
6891 },
6892 }
6893
6894 apex_set {
6895 name: "myapex_set",
6896 set: "myapex.apks",
6897 filename: "myapex_set.apex",
6898 overrides: ["myapex"],
6899 }
6900 `)
6901
6902 apexKeysText := ctx.SingletonForTests("apex_keys_text")
6903 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
6904 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 +09006905 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 +09006906}
6907
Jooyung Han938b5932020-06-20 12:47:47 +09006908func TestAllowedFiles(t *testing.T) {
6909 ctx, _ := testApex(t, `
6910 apex {
6911 name: "myapex",
6912 key: "myapex.key",
6913 apps: ["app"],
6914 allowed_files: "allowed.txt",
6915 }
6916
6917 apex_key {
6918 name: "myapex.key",
6919 public_key: "testkey.avbpubkey",
6920 private_key: "testkey.pem",
6921 }
6922
6923 android_app {
6924 name: "app",
6925 srcs: ["foo/bar/MyClass.java"],
6926 package_name: "foo",
6927 sdk_version: "none",
6928 system_modules: "none",
6929 apex_available: [ "myapex" ],
6930 }
6931 `, withFiles(map[string][]byte{
6932 "sub/Android.bp": []byte(`
6933 override_apex {
6934 name: "override_myapex",
6935 base: "myapex",
6936 apps: ["override_app"],
6937 allowed_files: ":allowed",
6938 }
6939 // Overridable "path" property should be referenced indirectly
6940 filegroup {
6941 name: "allowed",
6942 srcs: ["allowed.txt"],
6943 }
6944 override_android_app {
6945 name: "override_app",
6946 base: "app",
6947 package_name: "bar",
6948 }
6949 `),
6950 }))
6951
6952 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
6953 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
6954 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
6955 }
6956
6957 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
6958 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
6959 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
6960 }
6961}
6962
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006963func TestNonPreferredPrebuiltDependency(t *testing.T) {
6964 _, _ = testApex(t, `
6965 apex {
6966 name: "myapex",
6967 key: "myapex.key",
6968 native_shared_libs: ["mylib"],
6969 }
6970
6971 apex_key {
6972 name: "myapex.key",
6973 public_key: "testkey.avbpubkey",
6974 private_key: "testkey.pem",
6975 }
6976
6977 cc_library {
6978 name: "mylib",
6979 srcs: ["mylib.cpp"],
6980 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07006981 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006982 },
6983 apex_available: ["myapex"],
6984 }
6985
6986 cc_prebuilt_library_shared {
6987 name: "mylib",
6988 prefer: false,
6989 srcs: ["prebuilt.so"],
6990 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07006991 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006992 },
6993 apex_available: ["myapex"],
6994 }
6995 `)
6996}
6997
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00006998func TestCompressedApex(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07006999 ctx, _ := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007000 apex {
7001 name: "myapex",
7002 key: "myapex.key",
7003 compressible: true,
7004 }
7005 apex_key {
7006 name: "myapex.key",
7007 public_key: "testkey.avbpubkey",
7008 private_key: "testkey.pem",
7009 }
7010 `, func(fs map[string][]byte, config android.Config) {
7011 config.TestProductVariables.CompressedApex = proptools.BoolPtr(true)
7012 })
7013
7014 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7015 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7016
7017 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7018 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7019
7020 // Make sure output of bundle is .capex
7021 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7022 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7023
7024 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007025 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007026 var builder strings.Builder
7027 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7028 androidMk := builder.String()
7029 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7030}
7031
Martin Stjernholm2856c662020-12-02 15:03:42 +00007032func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07007033 ctx, _ := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007034 apex {
7035 name: "myapex",
7036 key: "myapex.key",
7037 native_shared_libs: ["mylib"],
7038 }
7039
7040 apex_key {
7041 name: "myapex.key",
7042 public_key: "testkey.avbpubkey",
7043 private_key: "testkey.pem",
7044 }
7045
7046 cc_library {
7047 name: "mylib",
7048 srcs: ["mylib.cpp"],
7049 apex_available: ["myapex"],
7050 shared_libs: ["otherlib"],
7051 system_shared_libs: [],
7052 }
7053
7054 cc_library {
7055 name: "otherlib",
7056 srcs: ["mylib.cpp"],
7057 stubs: {
7058 versions: ["current"],
7059 },
7060 }
7061
7062 cc_prebuilt_library_shared {
7063 name: "otherlib",
7064 prefer: true,
7065 srcs: ["prebuilt.so"],
7066 stubs: {
7067 versions: ["current"],
7068 },
7069 }
7070 `)
7071
7072 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007073 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007074 var builder strings.Builder
7075 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7076 androidMk := builder.String()
7077
7078 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7079 // a thing there.
7080 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7081}
7082
Jiyong Parke3867542020-12-03 17:28:25 +09007083func TestExcludeDependency(t *testing.T) {
7084 ctx, _ := testApex(t, `
7085 apex {
7086 name: "myapex",
7087 key: "myapex.key",
7088 native_shared_libs: ["mylib"],
7089 }
7090
7091 apex_key {
7092 name: "myapex.key",
7093 public_key: "testkey.avbpubkey",
7094 private_key: "testkey.pem",
7095 }
7096
7097 cc_library {
7098 name: "mylib",
7099 srcs: ["mylib.cpp"],
7100 system_shared_libs: [],
7101 stl: "none",
7102 apex_available: ["myapex"],
7103 shared_libs: ["mylib2"],
7104 target: {
7105 apex: {
7106 exclude_shared_libs: ["mylib2"],
7107 },
7108 },
7109 }
7110
7111 cc_library {
7112 name: "mylib2",
7113 srcs: ["mylib.cpp"],
7114 system_shared_libs: [],
7115 stl: "none",
7116 }
7117 `)
7118
7119 // Check if mylib is linked to mylib2 for the non-apex target
7120 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7121 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7122
7123 // Make sure that the link doesn't occur for the apex target
7124 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7125 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7126
7127 // It shouldn't appear in the copy cmd as well.
7128 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7129 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7130}
7131
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007132func TestPrebuiltStubLibDep(t *testing.T) {
7133 bpBase := `
7134 apex {
7135 name: "myapex",
7136 key: "myapex.key",
7137 native_shared_libs: ["mylib"],
7138 }
7139 apex_key {
7140 name: "myapex.key",
7141 public_key: "testkey.avbpubkey",
7142 private_key: "testkey.pem",
7143 }
7144 cc_library {
7145 name: "mylib",
7146 srcs: ["mylib.cpp"],
7147 apex_available: ["myapex"],
7148 shared_libs: ["stublib"],
7149 system_shared_libs: [],
7150 }
7151 apex {
7152 name: "otherapex",
7153 enabled: %s,
7154 key: "myapex.key",
7155 native_shared_libs: ["stublib"],
7156 }
7157 `
7158
7159 stublibSourceBp := `
7160 cc_library {
7161 name: "stublib",
7162 srcs: ["mylib.cpp"],
7163 apex_available: ["otherapex"],
7164 system_shared_libs: [],
7165 stl: "none",
7166 stubs: {
7167 versions: ["1"],
7168 },
7169 }
7170 `
7171
7172 stublibPrebuiltBp := `
7173 cc_prebuilt_library_shared {
7174 name: "stublib",
7175 srcs: ["prebuilt.so"],
7176 apex_available: ["otherapex"],
7177 stubs: {
7178 versions: ["1"],
7179 },
7180 %s
7181 }
7182 `
7183
7184 tests := []struct {
7185 name string
7186 stublibBp string
7187 usePrebuilt bool
7188 modNames []string // Modules to collect AndroidMkEntries for
7189 otherApexEnabled []string
7190 }{
7191 {
7192 name: "only_source",
7193 stublibBp: stublibSourceBp,
7194 usePrebuilt: false,
7195 modNames: []string{"stublib"},
7196 otherApexEnabled: []string{"true", "false"},
7197 },
7198 {
7199 name: "source_preferred",
7200 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7201 usePrebuilt: false,
7202 modNames: []string{"stublib", "prebuilt_stublib"},
7203 otherApexEnabled: []string{"true", "false"},
7204 },
7205 {
7206 name: "prebuilt_preferred",
7207 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7208 usePrebuilt: true,
7209 modNames: []string{"stublib", "prebuilt_stublib"},
7210 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7211 },
7212 {
7213 name: "only_prebuilt",
7214 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7215 usePrebuilt: true,
7216 modNames: []string{"stublib"},
7217 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7218 },
7219 }
7220
7221 for _, test := range tests {
7222 t.Run(test.name, func(t *testing.T) {
7223 for _, otherApexEnabled := range test.otherApexEnabled {
7224 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07007225 ctx, _ := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007226
7227 type modAndMkEntries struct {
7228 mod *cc.Module
7229 mkEntries android.AndroidMkEntries
7230 }
7231 entries := []*modAndMkEntries{}
7232
7233 // Gather shared lib modules that are installable
7234 for _, modName := range test.modNames {
7235 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7236 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7237 continue
7238 }
7239 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007240 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007241 continue
7242 }
Colin Crossaa255532020-07-03 13:18:24 -07007243 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007244 if ent.Disabled {
7245 continue
7246 }
7247 entries = append(entries, &modAndMkEntries{
7248 mod: mod,
7249 mkEntries: ent,
7250 })
7251 }
7252 }
7253 }
7254
7255 var entry *modAndMkEntries = nil
7256 for _, ent := range entries {
7257 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7258 if entry != nil {
7259 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7260 } else {
7261 entry = ent
7262 }
7263 }
7264 }
7265
7266 if entry == nil {
7267 t.Errorf("AndroidMk entry for \"stublib\" missing")
7268 } else {
7269 isPrebuilt := entry.mod.Prebuilt() != nil
7270 if isPrebuilt != test.usePrebuilt {
7271 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7272 }
7273 if !entry.mod.IsStubs() {
7274 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7275 }
7276 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7277 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7278 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007279 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
7280 expected := "-D__STUBLIB_API__=1"
7281 if !android.InList(expected, cflags) {
7282 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7283 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007284 }
7285 })
7286 }
7287 })
7288 }
7289}
7290
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007291func TestMain(m *testing.M) {
7292 run := func() int {
7293 setUp()
7294 defer tearDown()
7295
7296 return m.Run()
7297 }
7298
7299 os.Exit(run())
7300}