blob: 05ad79b63a2eeeb24e55960a6f9db4d57819578a [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"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070022 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010023 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090024 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
28 "github.com/google/blueprint/proptools"
29
30 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080031 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090032 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000033 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070034 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090035 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090036 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070037 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090038)
39
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070040var buildDir string
41
Jooyung Hand3639552019-08-09 12:57:43 +090042// names returns name list from white space separated string
43func names(s string) (ns []string) {
44 for _, n := range strings.Split(s, " ") {
45 if len(n) > 0 {
46 ns = append(ns, n)
47 }
48 }
49 return
50}
51
Jooyung Han344d5432019-08-23 11:17:39 +090052func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
53 t.Helper()
54 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090055 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
56 if len(errs) > 0 {
57 android.FailIfNoMatchingErrors(t, pattern, errs)
58 return
59 }
60 _, errs = ctx.PrepareBuildActions(config)
61 if len(errs) > 0 {
62 android.FailIfNoMatchingErrors(t, pattern, errs)
63 return
64 }
65
66 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
67}
68
Jooyung Han344d5432019-08-23 11:17:39 +090069func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
70 t.Helper()
71 ctx, config := testApexContext(t, bp, handlers...)
Paul Duffineedc5d52020-06-12 17:46:39 +010072 _, errs := ctx.ParseBlueprintsFiles(".")
Jooyung Han5c998b92019-06-27 11:30:33 +090073 android.FailIfErrored(t, errs)
74 _, errs = ctx.PrepareBuildActions(config)
75 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070076 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090077}
78
Jooyung Han344d5432019-08-23 11:17:39 +090079type testCustomizer func(fs map[string][]byte, config android.Config)
80
81func withFiles(files map[string][]byte) testCustomizer {
82 return func(fs map[string][]byte, config android.Config) {
83 for k, v := range files {
84 fs[k] = v
85 }
86 }
87}
88
89func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
90 return func(fs map[string][]byte, config android.Config) {
91 for k, v := range targets {
92 config.Targets[k] = v
93 }
94 }
95}
96
Jooyung Han35155c42020-02-06 17:33:20 +090097// withNativeBridgeTargets sets configuration with targets including:
98// - X86_64 (primary)
99// - X86 (secondary)
100// - Arm64 on X86_64 (native bridge)
101// - Arm on X86 (native bridge)
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700102func withNativeBridgeEnabled(_ map[string][]byte, config android.Config) {
Jooyung Han35155c42020-02-06 17:33:20 +0900103 config.Targets[android.Android] = []android.Target{
104 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
105 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
106 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
107 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
108 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
109 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
110 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
111 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
112 }
113}
114
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900115func withManifestPackageNameOverrides(specs []string) testCustomizer {
116 return func(fs map[string][]byte, config android.Config) {
117 config.TestProductVariables.ManifestPackageNameOverrides = specs
118 }
119}
120
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700121func withBinder32bit(_ map[string][]byte, config android.Config) {
Jooyung Han31c470b2019-10-18 16:26:59 +0900122 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
123}
124
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700125func withUnbundledBuild(_ map[string][]byte, config android.Config) {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900126 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
127}
128
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700129func testApexContext(_ *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900130 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900131 filegroup {
132 name: "myapex-file_contexts",
133 srcs: [
134 "system/sepolicy/apex/myapex-file_contexts",
135 ],
136 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900137 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800138
Colin Crossf9aabd72020-02-15 11:29:50 -0800139 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
140
Jiyong Park99644e92020-11-17 22:21:02 +0900141 bp = bp + rust.GatherRequiredDepsForTest()
142
Dario Frenicde2a032019-10-27 00:29:22 +0100143 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900144
Jooyung Han344d5432019-08-23 11:17:39 +0900145 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900146 "a.java": nil,
147 "PrebuiltAppFoo.apk": nil,
148 "PrebuiltAppFooPriv.apk": nil,
149 "build/make/target/product/security": nil,
150 "apex_manifest.json": nil,
151 "AndroidManifest.xml": nil,
152 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900153 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900154 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900155 "system/sepolicy/apex/otherapex-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900156 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800157 "mylib.cpp": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800158 "mytest.cpp": nil,
159 "mytest1.cpp": nil,
160 "mytest2.cpp": nil,
161 "mytest3.cpp": nil,
162 "myprebuilt": nil,
163 "my_include": nil,
164 "foo/bar/MyClass.java": nil,
165 "prebuilt.jar": nil,
Paul Duffindddd5462020-04-07 15:25:44 +0100166 "prebuilt.so": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800167 "vendor/foo/devkeys/test.x509.pem": nil,
168 "vendor/foo/devkeys/test.pk8": nil,
169 "testkey.x509.pem": nil,
170 "testkey.pk8": nil,
171 "testkey.override.x509.pem": nil,
172 "testkey.override.pk8": nil,
173 "vendor/foo/devkeys/testkey.avbpubkey": nil,
174 "vendor/foo/devkeys/testkey.pem": nil,
175 "NOTICE": nil,
176 "custom_notice": nil,
Jiyong Park9918e1a2020-03-17 19:16:40 +0900177 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800178 "testkey2.avbpubkey": nil,
179 "testkey2.pem": nil,
180 "myapex-arm64.apex": nil,
181 "myapex-arm.apex": nil,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700182 "myapex.apks": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800183 "frameworks/base/api/current.txt": nil,
184 "framework/aidl/a.aidl": nil,
185 "build/make/core/proguard.flags": nil,
186 "build/make/core/proguard_basic_keeps.flags": nil,
187 "dummy.txt": nil,
Liz Kammer1c14a212020-05-12 15:26:55 -0700188 "baz": nil,
189 "bar/baz": nil,
Liz Kammer5bd365f2020-05-27 15:15:11 -0700190 "testdata/baz": nil,
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700191 "AppSet.apks": nil,
Jiyong Park99644e92020-11-17 22:21:02 +0900192 "foo.rs": nil,
Paul Duffin064b70c2020-11-02 17:32:38 +0000193 "libfoo.jar": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900194 }
195
Colin Crossf9aabd72020-02-15 11:29:50 -0800196 cc.GatherRequiredFilesForTest(fs)
197
Jooyung Han344d5432019-08-23 11:17:39 +0900198 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800199 // The fs now needs to be populated before creating the config, call handlers twice
200 // for now, once to get any fs changes, and later after the config was created to
201 // set product variables or targets.
202 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
203 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900204 }
205
Colin Cross98be1bb2019-12-13 20:41:13 -0800206 config := android.TestArchConfig(buildDir, nil, bp, fs)
207 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
208 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
209 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
210 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
211 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Dan Albert4f378d72020-07-23 17:32:15 -0700212 config.TestProductVariables.Platform_version_active_codenames = []string{"Q"}
Colin Cross98be1bb2019-12-13 20:41:13 -0800213 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
214
215 for _, handler := range handlers {
216 // The fs now needs to be populated before creating the config, call handlers twice
217 // for now, earlier to get any fs changes, and now after the config was created to
218 // set product variables or targets.
219 tempFS := map[string][]byte{}
220 handler(tempFS, config)
221 }
222
Colin Crossae8600b2020-10-29 17:09:13 -0700223 ctx := android.NewTestArchContext(config)
Paul Duffineedc5d52020-06-12 17:46:39 +0100224
225 // from android package
226 android.RegisterPackageBuildComponents(ctx)
227 ctx.PreArchMutators(android.RegisterVisibilityRuleChecker)
228
Colin Cross98be1bb2019-12-13 20:41:13 -0800229 ctx.RegisterModuleType("apex", BundleFactory)
230 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
231 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
232 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
233 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
234 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
235 ctx.RegisterModuleType("override_apex", overrideApexFactory)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700236 ctx.RegisterModuleType("apex_set", apexSetFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800237
Jooyung Hana57af4a2020-01-23 05:36:59 +0000238 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +0100239 ctx.PreArchMutators(android.RegisterComponentsMutator)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000240 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
241
Paul Duffin021f4e52020-07-30 16:04:17 +0100242 android.RegisterPrebuiltMutators(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +0100243
Paul Duffin021f4e52020-07-30 16:04:17 +0100244 // Register these after the prebuilt mutators have been registered to match what
245 // happens at runtime.
Paul Duffineedc5d52020-06-12 17:46:39 +0100246 ctx.PreArchMutators(android.RegisterVisibilityRuleGatherer)
247 ctx.PostDepsMutators(android.RegisterVisibilityRuleEnforcer)
248
Paul Duffin021f4e52020-07-30 16:04:17 +0100249 cc.RegisterRequiredBuildComponentsForTest(ctx)
Jiyong Park99644e92020-11-17 22:21:02 +0900250 rust.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000251 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffin021f4e52020-07-30 16:04:17 +0100252
Colin Cross98be1bb2019-12-13 20:41:13 -0800253 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800254 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
Colin Crosse4e44bc2020-12-28 13:50:21 -0800255 cc.RegisterVndkLibraryTxtTypes(ctx)
Jooyung Han0703fd82020-08-26 22:11:53 +0900256 prebuilt_etc.RegisterPrebuiltEtcBuildComponents(ctx)
atrost6e126252020-01-27 17:01:16 +0000257 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700258 ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800259 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900260 ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
markchien2f59ec92020-09-02 16:23:38 +0800261 ctx.RegisterModuleType("bpf", bpf.BpfFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800262
Colin Cross98be1bb2019-12-13 20:41:13 -0800263 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800264 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800265
Colin Crossae8600b2020-10-29 17:09:13 -0700266 ctx.Register()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900267
Jooyung Han5c998b92019-06-27 11:30:33 +0900268 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900269}
270
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700271func setUp() {
272 var err error
273 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700275 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900276 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900277}
278
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700279func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700280 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281}
282
Jooyung Han643adc42020-02-27 13:50:06 +0900283// ensure that 'result' equals 'expected'
284func ensureEquals(t *testing.T, result string, expected string) {
285 t.Helper()
286 if result != expected {
287 t.Errorf("%q != %q", expected, result)
288 }
289}
290
Jiyong Park25fc6a92018-11-18 18:02:45 +0900291// ensure that 'result' contains 'expected'
292func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900293 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900294 if !strings.Contains(result, expected) {
295 t.Errorf("%q is not found in %q", expected, result)
296 }
297}
298
Liz Kammer5bd365f2020-05-27 15:15:11 -0700299// ensure that 'result' contains 'expected' exactly one time
300func ensureContainsOnce(t *testing.T, result string, expected string) {
301 t.Helper()
302 count := strings.Count(result, expected)
303 if count != 1 {
304 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
305 }
306}
307
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308// ensures that 'result' does not contain 'notExpected'
309func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900310 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900311 if strings.Contains(result, notExpected) {
312 t.Errorf("%q is found in %q", notExpected, result)
313 }
314}
315
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700316func ensureMatches(t *testing.T, result string, expectedRex string) {
317 ok, err := regexp.MatchString(expectedRex, result)
318 if err != nil {
319 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
320 return
321 }
322 if !ok {
323 t.Errorf("%s does not match regular expession %s", result, expectedRex)
324 }
325}
326
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900328 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900329 if !android.InList(expected, result) {
330 t.Errorf("%q is not found in %v", expected, result)
331 }
332}
333
334func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900335 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900336 if android.InList(notExpected, result) {
337 t.Errorf("%q is found in %v", notExpected, result)
338 }
339}
340
Jooyung Hane1633032019-08-01 17:41:43 +0900341func ensureListEmpty(t *testing.T, result []string) {
342 t.Helper()
343 if len(result) > 0 {
344 t.Errorf("%q is expected to be empty", result)
345 }
346}
347
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000348func ensureListNotEmpty(t *testing.T, result []string) {
349 t.Helper()
350 if len(result) == 0 {
351 t.Errorf("%q is expected to be not empty", result)
352 }
353}
354
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355// Minimal test
356func TestBasicApex(t *testing.T) {
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900357 ctx, config := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900358 apex_defaults {
359 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900360 manifest: ":myapex.manifest",
361 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900363 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900364 native_shared_libs: [
365 "mylib",
366 "libfoo.ffi",
367 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900368 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800369 multilib: {
370 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900371 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800372 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900373 },
Jiyong Park77acec62020-06-01 21:39:15 +0900374 java_libs: [
375 "myjar",
376 "myjar_dex",
377 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900378 }
379
Jiyong Park30ca9372019-02-07 16:27:23 +0900380 apex {
381 name: "myapex",
382 defaults: ["myapex-defaults"],
383 }
384
Jiyong Park25fc6a92018-11-18 18:02:45 +0900385 apex_key {
386 name: "myapex.key",
387 public_key: "testkey.avbpubkey",
388 private_key: "testkey.pem",
389 }
390
Jiyong Park809bb722019-02-13 21:33:49 +0900391 filegroup {
392 name: "myapex.manifest",
393 srcs: ["apex_manifest.json"],
394 }
395
396 filegroup {
397 name: "myapex.androidmanifest",
398 srcs: ["AndroidManifest.xml"],
399 }
400
Jiyong Park25fc6a92018-11-18 18:02:45 +0900401 cc_library {
402 name: "mylib",
403 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900404 shared_libs: [
405 "mylib2",
406 "libbar.ffi",
407 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900408 system_shared_libs: [],
409 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000410 // TODO: remove //apex_available:platform
411 apex_available: [
412 "//apex_available:platform",
413 "myapex",
414 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900415 }
416
Alex Light3d673592019-01-18 14:37:31 -0800417 cc_binary {
418 name: "foo",
419 srcs: ["mylib.cpp"],
420 compile_multilib: "both",
421 multilib: {
422 lib32: {
423 suffix: "32",
424 },
425 lib64: {
426 suffix: "64",
427 },
428 },
429 symlinks: ["foo_link_"],
430 symlink_preferred_arch: true,
431 system_shared_libs: [],
432 static_executable: true,
433 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700434 apex_available: [ "myapex", "com.android.gki.*" ],
435 }
436
Jiyong Park99644e92020-11-17 22:21:02 +0900437 rust_binary {
438 name: "foo.rust",
439 srcs: ["foo.rs"],
440 rlibs: ["libfoo.rlib.rust"],
441 dylibs: ["libfoo.dylib.rust"],
442 apex_available: ["myapex"],
443 }
444
445 rust_library_rlib {
446 name: "libfoo.rlib.rust",
447 srcs: ["foo.rs"],
448 crate_name: "foo",
449 apex_available: ["myapex"],
450 }
451
452 rust_library_dylib {
453 name: "libfoo.dylib.rust",
454 srcs: ["foo.rs"],
455 crate_name: "foo",
456 apex_available: ["myapex"],
457 }
458
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900459 rust_ffi_shared {
460 name: "libfoo.ffi",
461 srcs: ["foo.rs"],
462 crate_name: "foo",
463 apex_available: ["myapex"],
464 }
465
466 rust_ffi_shared {
467 name: "libbar.ffi",
468 srcs: ["foo.rs"],
469 crate_name: "bar",
470 apex_available: ["myapex"],
471 }
472
Yifan Hongd22a84a2020-07-28 17:37:46 -0700473 apex {
474 name: "com.android.gki.fake",
475 binaries: ["foo"],
476 key: "myapex.key",
477 file_contexts: ":myapex-file_contexts",
Alex Light3d673592019-01-18 14:37:31 -0800478 }
479
Paul Duffindddd5462020-04-07 15:25:44 +0100480 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900481 name: "mylib2",
482 srcs: ["mylib.cpp"],
483 system_shared_libs: [],
484 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900485 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900486 static_libs: ["libstatic"],
487 // TODO: remove //apex_available:platform
488 apex_available: [
489 "//apex_available:platform",
490 "myapex",
491 ],
492 }
493
Paul Duffindddd5462020-04-07 15:25:44 +0100494 cc_prebuilt_library_shared {
495 name: "mylib2",
496 srcs: ["prebuilt.so"],
497 // TODO: remove //apex_available:platform
498 apex_available: [
499 "//apex_available:platform",
500 "myapex",
501 ],
502 }
503
Jiyong Park9918e1a2020-03-17 19:16:40 +0900504 cc_library_static {
505 name: "libstatic",
506 srcs: ["mylib.cpp"],
507 system_shared_libs: [],
508 stl: "none",
509 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000510 // TODO: remove //apex_available:platform
511 apex_available: [
512 "//apex_available:platform",
513 "myapex",
514 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900515 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900516
517 java_library {
518 name: "myjar",
519 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900520 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900521 sdk_version: "none",
522 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900523 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900524 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000525 // TODO: remove //apex_available:platform
526 apex_available: [
527 "//apex_available:platform",
528 "myapex",
529 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900530 }
531
Jiyong Park77acec62020-06-01 21:39:15 +0900532 dex_import {
533 name: "myjar_dex",
534 jars: ["prebuilt.jar"],
535 apex_available: [
536 "//apex_available:platform",
537 "myapex",
538 ],
539 }
540
Jiyong Park7f7766d2019-07-25 22:02:35 +0900541 java_library {
542 name: "myotherjar",
543 srcs: ["foo/bar/MyClass.java"],
544 sdk_version: "none",
545 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900546 // TODO: remove //apex_available:platform
547 apex_available: [
548 "//apex_available:platform",
549 "myapex",
550 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900551 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900552
553 java_library {
554 name: "mysharedjar",
555 srcs: ["foo/bar/MyClass.java"],
556 sdk_version: "none",
557 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900558 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900559 `)
560
Sundong Ahnabb64432019-10-22 13:58:29 +0900561 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900562
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900563 // Make sure that Android.mk is created
564 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
565 data := android.AndroidMkDataForTest(t, config, "", ab)
566 var builder strings.Builder
567 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
568
569 androidMk := builder.String()
570 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
571 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
572
Jiyong Park42cca6c2019-04-01 11:15:50 +0900573 optFlags := apexRule.Args["opt_flags"]
574 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700575 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900576 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900577
Jiyong Park25fc6a92018-11-18 18:02:45 +0900578 copyCmds := apexRule.Args["copy_commands"]
579
580 // Ensure that main rule creates an output
581 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
582
583 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700584 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
585 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
586 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900587 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900588 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900589
590 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700591 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
592 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900593 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
594 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900595 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900596
597 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800598 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
599 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900600 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900601 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900602 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900603 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
604 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900605 // .. but not for java libs
606 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900607 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800608
Colin Cross7113d202019-11-20 16:39:12 -0800609 // Ensure that the platform variant ends with _shared or _common
610 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
611 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900612 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
613 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900614 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
615
616 // Ensure that dynamic dependency to java libs are not included
617 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800618
619 // Ensure that all symlinks are present.
620 found_foo_link_64 := false
621 found_foo := false
622 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900623 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800624 if strings.HasSuffix(cmd, "bin/foo") {
625 found_foo = true
626 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
627 found_foo_link_64 = true
628 }
629 }
630 }
631 good := found_foo && found_foo_link_64
632 if !good {
633 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
634 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900635
Sundong Ahnabb64432019-10-22 13:58:29 +0900636 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700637 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900638 if len(noticeInputs) != 3 {
639 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900640 }
641 ensureListContains(t, noticeInputs, "NOTICE")
642 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900643 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900644
Artur Satayeva8bd1132020-04-27 18:07:06 +0100645 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100646 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
647 ensureListContains(t, fullDepsInfo, " mylib(minSdkVersion:(no version)) <- myapex")
648 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
649 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
650 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100651
652 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100653 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
654 ensureListContains(t, flatDepsInfo, "mylib(minSdkVersion:(no version))")
655 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
656 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
657 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800658}
659
Jooyung Hanf21c7972019-12-16 22:32:06 +0900660func TestDefaults(t *testing.T) {
661 ctx, _ := testApex(t, `
662 apex_defaults {
663 name: "myapex-defaults",
664 key: "myapex.key",
665 prebuilts: ["myetc"],
666 native_shared_libs: ["mylib"],
667 java_libs: ["myjar"],
668 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900669 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800670 bpfs: ["bpf"],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900671 }
672
673 prebuilt_etc {
674 name: "myetc",
675 src: "myprebuilt",
676 }
677
678 apex {
679 name: "myapex",
680 defaults: ["myapex-defaults"],
681 }
682
683 apex_key {
684 name: "myapex.key",
685 public_key: "testkey.avbpubkey",
686 private_key: "testkey.pem",
687 }
688
689 cc_library {
690 name: "mylib",
691 system_shared_libs: [],
692 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000693 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900694 }
695
696 java_library {
697 name: "myjar",
698 srcs: ["foo/bar/MyClass.java"],
699 sdk_version: "none",
700 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000701 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900702 }
703
704 android_app {
705 name: "AppFoo",
706 srcs: ["foo/bar/MyClass.java"],
707 sdk_version: "none",
708 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000709 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900710 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900711
712 runtime_resource_overlay {
713 name: "rro",
714 theme: "blue",
715 }
716
markchien2f59ec92020-09-02 16:23:38 +0800717 bpf {
718 name: "bpf",
719 srcs: ["bpf.c", "bpf2.c"],
720 }
721
Jooyung Hanf21c7972019-12-16 22:32:06 +0900722 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000723 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900724 "etc/myetc",
725 "javalib/myjar.jar",
726 "lib64/mylib.so",
727 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900728 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800729 "etc/bpf/bpf.o",
730 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900731 })
732}
733
Jooyung Han01a3ee22019-11-02 02:52:25 +0900734func TestApexManifest(t *testing.T) {
735 ctx, _ := testApex(t, `
736 apex {
737 name: "myapex",
738 key: "myapex.key",
739 }
740
741 apex_key {
742 name: "myapex.key",
743 public_key: "testkey.avbpubkey",
744 private_key: "testkey.pem",
745 }
746 `)
747
748 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900749 args := module.Rule("apexRule").Args
750 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
751 t.Error("manifest should be apex_manifest.pb, but " + manifest)
752 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900753}
754
Alex Light5098a612018-11-29 17:12:15 -0800755func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700756 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800757 apex {
758 name: "myapex",
759 key: "myapex.key",
760 payload_type: "zip",
761 native_shared_libs: ["mylib"],
762 }
763
764 apex_key {
765 name: "myapex.key",
766 public_key: "testkey.avbpubkey",
767 private_key: "testkey.pem",
768 }
769
770 cc_library {
771 name: "mylib",
772 srcs: ["mylib.cpp"],
773 shared_libs: ["mylib2"],
774 system_shared_libs: [],
775 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000776 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800777 }
778
779 cc_library {
780 name: "mylib2",
781 srcs: ["mylib.cpp"],
782 system_shared_libs: [],
783 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000784 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800785 }
786 `)
787
Sundong Ahnabb64432019-10-22 13:58:29 +0900788 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800789 copyCmds := zipApexRule.Args["copy_commands"]
790
791 // Ensure that main rule creates an output
792 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
793
794 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700795 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800796
797 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700798 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800799
800 // Ensure that both direct and indirect deps are copied into apex
801 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
802 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900803}
804
805func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700806 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900807 apex {
808 name: "myapex",
809 key: "myapex.key",
810 native_shared_libs: ["mylib", "mylib3"],
811 }
812
813 apex_key {
814 name: "myapex.key",
815 public_key: "testkey.avbpubkey",
816 private_key: "testkey.pem",
817 }
818
819 cc_library {
820 name: "mylib",
821 srcs: ["mylib.cpp"],
822 shared_libs: ["mylib2", "mylib3"],
823 system_shared_libs: [],
824 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000825 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900826 }
827
828 cc_library {
829 name: "mylib2",
830 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900831 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900832 system_shared_libs: [],
833 stl: "none",
834 stubs: {
835 versions: ["1", "2", "3"],
836 },
837 }
838
839 cc_library {
840 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900841 srcs: ["mylib.cpp"],
842 shared_libs: ["mylib4"],
843 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900844 stl: "none",
845 stubs: {
846 versions: ["10", "11", "12"],
847 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000848 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900849 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900850
851 cc_library {
852 name: "mylib4",
853 srcs: ["mylib.cpp"],
854 system_shared_libs: [],
855 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000856 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900857 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900858 `)
859
Sundong Ahnabb64432019-10-22 13:58:29 +0900860 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900861 copyCmds := apexRule.Args["copy_commands"]
862
863 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800864 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900865
866 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800867 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900868
869 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800870 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900871
Colin Crossaede88c2020-08-11 12:17:01 -0700872 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900873
874 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900875 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900876 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900877 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900878
879 // 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 -0700880 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900881 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700882 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900883
884 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900885 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900886 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900887
888 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700889 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900890
Jooyung Hana57af4a2020-01-23 05:36:59 +0000891 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900892 "lib64/mylib.so",
893 "lib64/mylib3.so",
894 "lib64/mylib4.so",
895 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900896}
897
Colin Cross7812fd32020-09-25 12:35:10 -0700898func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
899 t.Parallel()
900 ctx, _ := testApex(t, `
901 apex {
902 name: "myapex",
903 key: "myapex.key",
904 native_shared_libs: ["mylib", "mylib3"],
905 min_sdk_version: "29",
906 }
907
908 apex_key {
909 name: "myapex.key",
910 public_key: "testkey.avbpubkey",
911 private_key: "testkey.pem",
912 }
913
914 cc_library {
915 name: "mylib",
916 srcs: ["mylib.cpp"],
917 shared_libs: ["mylib2", "mylib3"],
918 system_shared_libs: [],
919 stl: "none",
920 apex_available: [ "myapex" ],
921 min_sdk_version: "28",
922 }
923
924 cc_library {
925 name: "mylib2",
926 srcs: ["mylib.cpp"],
927 cflags: ["-include mylib.h"],
928 system_shared_libs: [],
929 stl: "none",
930 stubs: {
931 versions: ["28", "29", "30", "current"],
932 },
933 min_sdk_version: "28",
934 }
935
936 cc_library {
937 name: "mylib3",
938 srcs: ["mylib.cpp"],
939 shared_libs: ["mylib4"],
940 system_shared_libs: [],
941 stl: "none",
942 stubs: {
943 versions: ["28", "29", "30", "current"],
944 },
945 apex_available: [ "myapex" ],
946 min_sdk_version: "28",
947 }
948
949 cc_library {
950 name: "mylib4",
951 srcs: ["mylib.cpp"],
952 system_shared_libs: [],
953 stl: "none",
954 apex_available: [ "myapex" ],
955 min_sdk_version: "28",
956 }
957 `)
958
959 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
960 copyCmds := apexRule.Args["copy_commands"]
961
962 // Ensure that direct non-stubs dep is always included
963 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
964
965 // Ensure that indirect stubs dep is not included
966 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
967
968 // Ensure that direct stubs dep is included
969 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
970
971 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
972
973 // Ensure that mylib is linking with the version 29 stubs for mylib2
974 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_29/mylib2.so")
975 // ... and not linking to the non-stub (impl) variant of mylib2
976 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
977
978 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
979 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
980 // .. and not linking to the stubs variant of mylib3
981 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
982
983 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700984 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700985 ensureNotContains(t, mylib2Cflags, "-include ")
986
987 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700988 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700989
990 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
991 "lib64/mylib.so",
992 "lib64/mylib3.so",
993 "lib64/mylib4.so",
994 })
995}
996
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900997func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
998 t.Parallel()
999 // myapex (Z)
1000 // mylib -----------------.
1001 // |
1002 // otherapex (29) |
1003 // libstub's versions: 29 Z current
1004 // |
1005 // <platform> |
1006 // libplatform ----------------'
1007 ctx, _ := testApex(t, `
1008 apex {
1009 name: "myapex",
1010 key: "myapex.key",
1011 native_shared_libs: ["mylib"],
1012 min_sdk_version: "Z", // non-final
1013 }
1014
1015 cc_library {
1016 name: "mylib",
1017 srcs: ["mylib.cpp"],
1018 shared_libs: ["libstub"],
1019 apex_available: ["myapex"],
1020 min_sdk_version: "Z",
1021 }
1022
1023 apex_key {
1024 name: "myapex.key",
1025 public_key: "testkey.avbpubkey",
1026 private_key: "testkey.pem",
1027 }
1028
1029 apex {
1030 name: "otherapex",
1031 key: "myapex.key",
1032 native_shared_libs: ["libstub"],
1033 min_sdk_version: "29",
1034 }
1035
1036 cc_library {
1037 name: "libstub",
1038 srcs: ["mylib.cpp"],
1039 stubs: {
1040 versions: ["29", "Z", "current"],
1041 },
1042 apex_available: ["otherapex"],
1043 min_sdk_version: "29",
1044 }
1045
1046 // platform module depending on libstub from otherapex should use the latest stub("current")
1047 cc_library {
1048 name: "libplatform",
1049 srcs: ["mylib.cpp"],
1050 shared_libs: ["libstub"],
1051 }
1052 `, func(fs map[string][]byte, config android.Config) {
1053 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Z")
1054 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
1055 config.TestProductVariables.Platform_version_active_codenames = []string{"Z"}
1056 })
1057
1058 // Ensure that mylib from myapex is built against "min_sdk_version" stub ("Z"), which is non-final
1059 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1060 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=9000 ")
1061 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1062 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_Z/libstub.so ")
1063
1064 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1065 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1066 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1067 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1068 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1069}
1070
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001071func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001072 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001073 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001074 name: "myapex2",
1075 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001076 native_shared_libs: ["mylib"],
1077 }
1078
1079 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001080 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001081 public_key: "testkey.avbpubkey",
1082 private_key: "testkey.pem",
1083 }
1084
1085 cc_library {
1086 name: "mylib",
1087 srcs: ["mylib.cpp"],
1088 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001089 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001090 system_shared_libs: [],
1091 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001092 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001093 }
1094
1095 cc_library {
1096 name: "libfoo",
1097 srcs: ["mylib.cpp"],
1098 shared_libs: ["libbar"],
1099 system_shared_libs: [],
1100 stl: "none",
1101 stubs: {
1102 versions: ["10", "20", "30"],
1103 },
1104 }
1105
1106 cc_library {
1107 name: "libbar",
1108 srcs: ["mylib.cpp"],
1109 system_shared_libs: [],
1110 stl: "none",
1111 }
1112
Jiyong Park678c8812020-02-07 17:25:49 +09001113 cc_library_static {
1114 name: "libbaz",
1115 srcs: ["mylib.cpp"],
1116 system_shared_libs: [],
1117 stl: "none",
1118 apex_available: [ "myapex2" ],
1119 }
1120
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001121 `)
1122
Jiyong Park83dc74b2020-01-14 18:38:44 +09001123 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001124 copyCmds := apexRule.Args["copy_commands"]
1125
1126 // Ensure that direct non-stubs dep is always included
1127 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1128
1129 // Ensure that indirect stubs dep is not included
1130 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1131
1132 // Ensure that dependency of stubs is not included
1133 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1134
Colin Crossaede88c2020-08-11 12:17:01 -07001135 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001136
1137 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001138 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001139 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001140 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001141
Jiyong Park3ff16992019-12-27 14:11:47 +09001142 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001143
1144 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1145 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001146
Artur Satayeva8bd1132020-04-27 18:07:06 +01001147 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001148 ensureListContains(t, fullDepsInfo, " mylib(minSdkVersion:(no version)) <- myapex2")
1149 ensureListContains(t, fullDepsInfo, " libbaz(minSdkVersion:(no version)) <- mylib")
1150 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001151
Artur Satayeva8bd1132020-04-27 18:07:06 +01001152 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001153 ensureListContains(t, flatDepsInfo, "mylib(minSdkVersion:(no version))")
1154 ensureListContains(t, flatDepsInfo, "libbaz(minSdkVersion:(no version))")
1155 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001156}
1157
Jooyung Hand3639552019-08-09 12:57:43 +09001158func TestApexWithRuntimeLibsDependency(t *testing.T) {
1159 /*
1160 myapex
1161 |
1162 v (runtime_libs)
1163 mylib ------+------> libfoo [provides stub]
1164 |
1165 `------> libbar
1166 */
1167 ctx, _ := testApex(t, `
1168 apex {
1169 name: "myapex",
1170 key: "myapex.key",
1171 native_shared_libs: ["mylib"],
1172 }
1173
1174 apex_key {
1175 name: "myapex.key",
1176 public_key: "testkey.avbpubkey",
1177 private_key: "testkey.pem",
1178 }
1179
1180 cc_library {
1181 name: "mylib",
1182 srcs: ["mylib.cpp"],
1183 runtime_libs: ["libfoo", "libbar"],
1184 system_shared_libs: [],
1185 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001186 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001187 }
1188
1189 cc_library {
1190 name: "libfoo",
1191 srcs: ["mylib.cpp"],
1192 system_shared_libs: [],
1193 stl: "none",
1194 stubs: {
1195 versions: ["10", "20", "30"],
1196 },
1197 }
1198
1199 cc_library {
1200 name: "libbar",
1201 srcs: ["mylib.cpp"],
1202 system_shared_libs: [],
1203 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001204 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001205 }
1206
1207 `)
1208
Sundong Ahnabb64432019-10-22 13:58:29 +09001209 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001210 copyCmds := apexRule.Args["copy_commands"]
1211
1212 // Ensure that direct non-stubs dep is always included
1213 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1214
1215 // Ensure that indirect stubs dep is not included
1216 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1217
1218 // Ensure that runtime_libs dep in included
1219 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1220
Sundong Ahnabb64432019-10-22 13:58:29 +09001221 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001222 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1223 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001224
1225}
1226
Jooyung Han8ce8db92020-05-15 19:05:05 +09001227func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
1228 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
1229 bp := `
1230 apex {
1231 name: "com.android.runtime",
1232 key: "com.android.runtime.key",
1233 native_shared_libs: ["libc"],
1234 }
1235
1236 apex_key {
1237 name: "com.android.runtime.key",
1238 public_key: "testkey.avbpubkey",
1239 private_key: "testkey.pem",
1240 }
1241
1242 cc_library {
1243 name: "libc",
1244 no_libcrt: true,
1245 nocrt: true,
1246 stl: "none",
1247 system_shared_libs: [],
1248 stubs: { versions: ["1"] },
1249 apex_available: ["com.android.runtime"],
1250
1251 sanitize: {
1252 hwaddress: true,
1253 }
1254 }
1255
1256 cc_prebuilt_library_shared {
1257 name: "libclang_rt.hwasan-aarch64-android",
1258 no_libcrt: true,
1259 nocrt: true,
1260 stl: "none",
1261 system_shared_libs: [],
1262 srcs: [""],
1263 stubs: { versions: ["1"] },
1264
1265 sanitize: {
1266 never: true,
1267 },
1268 }
1269 `
1270 // override bp to use hard-coded names: com.android.runtime and libc
1271 fs["Android.bp"] = []byte(bp)
1272 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
1273 })
1274
1275 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1276 "lib64/bionic/libc.so",
1277 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1278 })
1279
1280 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1281
1282 installed := hwasan.Description("install libclang_rt.hwasan")
1283 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1284
1285 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1286 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1287 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1288}
1289
1290func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
1291 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
1292 bp := `
1293 apex {
1294 name: "com.android.runtime",
1295 key: "com.android.runtime.key",
1296 native_shared_libs: ["libc"],
1297 }
1298
1299 apex_key {
1300 name: "com.android.runtime.key",
1301 public_key: "testkey.avbpubkey",
1302 private_key: "testkey.pem",
1303 }
1304
1305 cc_library {
1306 name: "libc",
1307 no_libcrt: true,
1308 nocrt: true,
1309 stl: "none",
1310 system_shared_libs: [],
1311 stubs: { versions: ["1"] },
1312 apex_available: ["com.android.runtime"],
1313 }
1314
1315 cc_prebuilt_library_shared {
1316 name: "libclang_rt.hwasan-aarch64-android",
1317 no_libcrt: true,
1318 nocrt: true,
1319 stl: "none",
1320 system_shared_libs: [],
1321 srcs: [""],
1322 stubs: { versions: ["1"] },
1323
1324 sanitize: {
1325 never: true,
1326 },
1327 }
1328 `
1329 // override bp to use hard-coded names: com.android.runtime and libc
1330 fs["Android.bp"] = []byte(bp)
1331 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
1332
1333 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1334 })
1335
1336 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1337 "lib64/bionic/libc.so",
1338 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1339 })
1340
1341 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1342
1343 installed := hwasan.Description("install libclang_rt.hwasan")
1344 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1345
1346 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1347 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1348 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1349}
1350
Jooyung Han61b66e92020-03-21 14:21:46 +00001351func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1352 testcases := []struct {
1353 name string
1354 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001355 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001356 shouldLink string
1357 shouldNotLink []string
1358 }{
1359 {
Jooyung Han75568392020-03-20 04:29:24 +09001360 name: "should link to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001361 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001362 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001363 shouldLink: "30",
1364 shouldNotLink: []string{"29"},
1365 },
1366 {
1367 name: "should link to llndk#29",
Jooyung Han749dc692020-04-15 11:03:39 +09001368 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001369 apexVariant: "apex29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001370 shouldLink: "29",
1371 shouldNotLink: []string{"30"},
1372 },
1373 }
1374 for _, tc := range testcases {
1375 t.Run(tc.name, func(t *testing.T) {
1376 ctx, _ := testApex(t, `
1377 apex {
1378 name: "myapex",
1379 key: "myapex.key",
1380 use_vendor: true,
1381 native_shared_libs: ["mylib"],
Jooyung Han749dc692020-04-15 11:03:39 +09001382 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001383 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001384
Jooyung Han61b66e92020-03-21 14:21:46 +00001385 apex_key {
1386 name: "myapex.key",
1387 public_key: "testkey.avbpubkey",
1388 private_key: "testkey.pem",
1389 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001390
Jooyung Han61b66e92020-03-21 14:21:46 +00001391 cc_library {
1392 name: "mylib",
1393 srcs: ["mylib.cpp"],
1394 vendor_available: true,
1395 shared_libs: ["libbar"],
1396 system_shared_libs: [],
1397 stl: "none",
1398 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001399 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001400 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001401
Jooyung Han61b66e92020-03-21 14:21:46 +00001402 cc_library {
1403 name: "libbar",
1404 srcs: ["mylib.cpp"],
1405 system_shared_libs: [],
1406 stl: "none",
1407 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001408 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001409 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001410
Jooyung Han61b66e92020-03-21 14:21:46 +00001411 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001412 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001413 symbol_file: "",
1414 }
1415 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07001416 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Han61b66e92020-03-21 14:21:46 +00001417 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +09001418
Jooyung Han61b66e92020-03-21 14:21:46 +00001419 // Ensure that LLNDK dep is not included
1420 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1421 "lib64/mylib.so",
1422 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001423
Jooyung Han61b66e92020-03-21 14:21:46 +00001424 // Ensure that LLNDK dep is required
1425 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1426 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1427 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001428
Colin Crossaede88c2020-08-11 12:17:01 -07001429 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001430 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001431 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001432 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001433 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001434
Colin Crossaede88c2020-08-11 12:17:01 -07001435 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001436 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1437 })
1438 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001439}
1440
Jiyong Park25fc6a92018-11-18 18:02:45 +09001441func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001442 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001443 apex {
1444 name: "myapex",
1445 key: "myapex.key",
1446 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
1447 }
1448
1449 apex_key {
1450 name: "myapex.key",
1451 public_key: "testkey.avbpubkey",
1452 private_key: "testkey.pem",
1453 }
1454
1455 cc_library {
1456 name: "mylib",
1457 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001458 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001459 shared_libs: ["libdl#27"],
1460 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001461 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001462 }
1463
1464 cc_library_shared {
1465 name: "mylib_shared",
1466 srcs: ["mylib.cpp"],
1467 shared_libs: ["libdl#27"],
1468 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001469 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001470 }
1471
1472 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001473 name: "libBootstrap",
1474 srcs: ["mylib.cpp"],
1475 stl: "none",
1476 bootstrap: true,
1477 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001478 `)
1479
Sundong Ahnabb64432019-10-22 13:58:29 +09001480 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001481 copyCmds := apexRule.Args["copy_commands"]
1482
1483 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001484 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001485 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1486 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001487
1488 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001489 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001490
Colin Crossaede88c2020-08-11 12:17:01 -07001491 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1492 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1493 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001494
1495 // For dependency to libc
1496 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001497 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001498 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001499 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001500 // ... Cflags from stub is correctly exported to mylib
1501 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1502 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1503
1504 // For dependency to libm
1505 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001506 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001507 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001508 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001509 // ... and is not compiling with the stub
1510 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1511 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1512
1513 // For dependency to libdl
1514 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001515 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001516 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001517 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1518 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001519 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001520 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001521 // ... Cflags from stub is correctly exported to mylib
1522 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1523 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001524
1525 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001526 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1527 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1528 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1529 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001530}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001531
Jooyung Han749dc692020-04-15 11:03:39 +09001532func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001533 // there are three links between liba --> libz
Jooyung Han749dc692020-04-15 11:03:39 +09001534 // 1) myapex -> libx -> liba -> libz : this should be #29 link, but fallback to #28
1535 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001536 // 3) (platform) -> liba -> libz : this should be non-stub link
1537 ctx, _ := testApex(t, `
1538 apex {
1539 name: "myapex",
1540 key: "myapex.key",
1541 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001542 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001543 }
1544
1545 apex {
1546 name: "otherapex",
1547 key: "myapex.key",
1548 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001549 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001550 }
1551
1552 apex_key {
1553 name: "myapex.key",
1554 public_key: "testkey.avbpubkey",
1555 private_key: "testkey.pem",
1556 }
1557
1558 cc_library {
1559 name: "libx",
1560 shared_libs: ["liba"],
1561 system_shared_libs: [],
1562 stl: "none",
1563 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001564 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001565 }
1566
1567 cc_library {
1568 name: "liby",
1569 shared_libs: ["liba"],
1570 system_shared_libs: [],
1571 stl: "none",
1572 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001573 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001574 }
1575
1576 cc_library {
1577 name: "liba",
1578 shared_libs: ["libz"],
1579 system_shared_libs: [],
1580 stl: "none",
1581 apex_available: [
1582 "//apex_available:anyapex",
1583 "//apex_available:platform",
1584 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001585 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001586 }
1587
1588 cc_library {
1589 name: "libz",
1590 system_shared_libs: [],
1591 stl: "none",
1592 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001593 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001594 },
1595 }
Jooyung Han749dc692020-04-15 11:03:39 +09001596 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001597
1598 expectLink := func(from, from_variant, to, to_variant string) {
1599 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1600 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1601 }
1602 expectNoLink := func(from, from_variant, to, to_variant string) {
1603 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1604 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1605 }
1606 // platform liba is linked to non-stub version
1607 expectLink("liba", "shared", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001608 // liba in myapex is linked to #28
Colin Crossaede88c2020-08-11 12:17:01 -07001609 expectLink("liba", "shared_apex29", "libz", "shared_28")
1610 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
1611 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001612 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001613 expectLink("liba", "shared_apex30", "libz", "shared_30")
1614 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1615 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001616}
1617
Jooyung Hanaed150d2020-04-02 01:41:41 +09001618func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
1619 ctx, _ := testApex(t, `
1620 apex {
1621 name: "myapex",
1622 key: "myapex.key",
1623 native_shared_libs: ["libx"],
1624 min_sdk_version: "R",
1625 }
1626
1627 apex_key {
1628 name: "myapex.key",
1629 public_key: "testkey.avbpubkey",
1630 private_key: "testkey.pem",
1631 }
1632
1633 cc_library {
1634 name: "libx",
1635 shared_libs: ["libz"],
1636 system_shared_libs: [],
1637 stl: "none",
1638 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001639 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001640 }
1641
1642 cc_library {
1643 name: "libz",
1644 system_shared_libs: [],
1645 stl: "none",
1646 stubs: {
1647 versions: ["29", "R"],
1648 },
1649 }
1650 `, func(fs map[string][]byte, config android.Config) {
1651 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
1652 })
1653
1654 expectLink := func(from, from_variant, to, to_variant string) {
1655 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1656 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1657 }
1658 expectNoLink := func(from, from_variant, to, to_variant string) {
1659 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1660 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1661 }
Dan Albertc8060532020-07-22 22:32:17 -07001662 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001663 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1664 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001665}
1666
Jooyung Han749dc692020-04-15 11:03:39 +09001667func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001668 ctx, _ := testApex(t, `
1669 apex {
1670 name: "myapex",
1671 key: "myapex.key",
1672 native_shared_libs: ["libx"],
1673 }
1674
1675 apex_key {
1676 name: "myapex.key",
1677 public_key: "testkey.avbpubkey",
1678 private_key: "testkey.pem",
1679 }
1680
1681 cc_library {
1682 name: "libx",
1683 shared_libs: ["libz"],
1684 system_shared_libs: [],
1685 stl: "none",
1686 apex_available: [ "myapex" ],
1687 }
1688
1689 cc_library {
1690 name: "libz",
1691 system_shared_libs: [],
1692 stl: "none",
1693 stubs: {
1694 versions: ["1", "2"],
1695 },
1696 }
1697 `)
1698
1699 expectLink := func(from, from_variant, to, to_variant string) {
1700 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1701 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1702 }
1703 expectNoLink := func(from, from_variant, to, to_variant string) {
1704 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1705 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1706 }
Colin Crossaede88c2020-08-11 12:17:01 -07001707 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1708 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1709 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001710}
1711
1712func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1713 ctx, _ := testApex(t, `
1714 apex {
1715 name: "myapex",
1716 key: "myapex.key",
1717 native_shared_libs: ["libx"],
1718 }
1719
1720 apex_key {
1721 name: "myapex.key",
1722 public_key: "testkey.avbpubkey",
1723 private_key: "testkey.pem",
1724 }
1725
1726 cc_library {
1727 name: "libx",
1728 system_shared_libs: [],
1729 stl: "none",
1730 apex_available: [ "myapex" ],
1731 stubs: {
1732 versions: ["1", "2"],
1733 },
1734 }
1735
1736 cc_library {
1737 name: "libz",
1738 shared_libs: ["libx"],
1739 system_shared_libs: [],
1740 stl: "none",
1741 }
1742 `)
1743
1744 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001745 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001746 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1747 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1748 }
1749 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001750 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001751 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1752 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1753 }
1754 expectLink("libz", "shared", "libx", "shared_2")
1755 expectNoLink("libz", "shared", "libz", "shared_1")
1756 expectNoLink("libz", "shared", "libz", "shared")
1757}
1758
Jooyung Han75568392020-03-20 04:29:24 +09001759func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001760 ctx, _ := testApex(t, `
1761 apex {
1762 name: "myapex",
1763 key: "myapex.key",
1764 native_shared_libs: ["libx"],
1765 min_sdk_version: "29",
1766 }
1767
1768 apex_key {
1769 name: "myapex.key",
1770 public_key: "testkey.avbpubkey",
1771 private_key: "testkey.pem",
1772 }
1773
1774 cc_library {
1775 name: "libx",
1776 shared_libs: ["libbar"],
1777 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001778 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001779 }
1780
1781 cc_library {
1782 name: "libbar",
1783 stubs: {
1784 versions: ["29", "30"],
1785 },
1786 }
Jooyung Han75568392020-03-20 04:29:24 +09001787 `, func(fs map[string][]byte, config android.Config) {
1788 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1789 })
Jooyung Han03b51852020-02-26 22:45:42 +09001790 expectLink := func(from, from_variant, to, to_variant string) {
1791 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1792 libFlags := ld.Args["libFlags"]
1793 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1794 }
Colin Crossaede88c2020-08-11 12:17:01 -07001795 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001796}
1797
Jooyung Han75568392020-03-20 04:29:24 +09001798func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001799 ctx, _ := testApex(t, `
1800 apex {
1801 name: "myapex",
1802 key: "myapex.key",
1803 native_shared_libs: ["libx"],
1804 min_sdk_version: "29",
1805 }
1806
1807 apex_key {
1808 name: "myapex.key",
1809 public_key: "testkey.avbpubkey",
1810 private_key: "testkey.pem",
1811 }
1812
1813 cc_library {
1814 name: "libx",
1815 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001816 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001817 }
Jooyung Han75568392020-03-20 04:29:24 +09001818 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001819
1820 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001821 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001822 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001823 // note that platform variant is not.
1824 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001825 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001826}
1827
Jooyung Han749dc692020-04-15 11:03:39 +09001828func TestApexMinSdkVersion_ErrorIfIncompatibleStubs(t *testing.T) {
Jooyung Han75568392020-03-20 04:29:24 +09001829 testApexError(t, `"libz" .*: not found a version\(<=29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001830 apex {
1831 name: "myapex",
1832 key: "myapex.key",
1833 native_shared_libs: ["libx"],
1834 min_sdk_version: "29",
1835 }
1836
1837 apex_key {
1838 name: "myapex.key",
1839 public_key: "testkey.avbpubkey",
1840 private_key: "testkey.pem",
1841 }
1842
1843 cc_library {
1844 name: "libx",
1845 shared_libs: ["libz"],
1846 system_shared_libs: [],
1847 stl: "none",
1848 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001849 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001850 }
1851
1852 cc_library {
1853 name: "libz",
1854 system_shared_libs: [],
1855 stl: "none",
1856 stubs: {
1857 versions: ["30"],
1858 },
1859 }
Jooyung Han75568392020-03-20 04:29:24 +09001860 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001861}
Jooyung Han03b51852020-02-26 22:45:42 +09001862
Jooyung Han749dc692020-04-15 11:03:39 +09001863func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1864 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001865 apex {
1866 name: "myapex",
1867 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001868 native_shared_libs: ["mylib"],
1869 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001870 }
1871
1872 apex_key {
1873 name: "myapex.key",
1874 public_key: "testkey.avbpubkey",
1875 private_key: "testkey.pem",
1876 }
Jooyung Han749dc692020-04-15 11:03:39 +09001877
1878 cc_library {
1879 name: "mylib",
1880 srcs: ["mylib.cpp"],
1881 system_shared_libs: [],
1882 stl: "none",
1883 apex_available: [
1884 "myapex",
1885 ],
1886 min_sdk_version: "30",
1887 }
1888 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001889
1890 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1891 apex {
1892 name: "myapex",
1893 key: "myapex.key",
1894 native_shared_libs: ["libfoo.ffi"],
1895 min_sdk_version: "29",
1896 }
1897
1898 apex_key {
1899 name: "myapex.key",
1900 public_key: "testkey.avbpubkey",
1901 private_key: "testkey.pem",
1902 }
1903
1904 rust_ffi_shared {
1905 name: "libfoo.ffi",
1906 srcs: ["foo.rs"],
1907 crate_name: "foo",
1908 apex_available: [
1909 "myapex",
1910 ],
1911 min_sdk_version: "30",
1912 }
1913 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001914}
1915
1916func TestApexMinSdkVersion_Okay(t *testing.T) {
1917 testApex(t, `
1918 apex {
1919 name: "myapex",
1920 key: "myapex.key",
1921 native_shared_libs: ["libfoo"],
1922 java_libs: ["libbar"],
1923 min_sdk_version: "29",
1924 }
1925
1926 apex_key {
1927 name: "myapex.key",
1928 public_key: "testkey.avbpubkey",
1929 private_key: "testkey.pem",
1930 }
1931
1932 cc_library {
1933 name: "libfoo",
1934 srcs: ["mylib.cpp"],
1935 shared_libs: ["libfoo_dep"],
1936 apex_available: ["myapex"],
1937 min_sdk_version: "29",
1938 }
1939
1940 cc_library {
1941 name: "libfoo_dep",
1942 srcs: ["mylib.cpp"],
1943 apex_available: ["myapex"],
1944 min_sdk_version: "29",
1945 }
1946
1947 java_library {
1948 name: "libbar",
1949 sdk_version: "current",
1950 srcs: ["a.java"],
1951 static_libs: ["libbar_dep"],
1952 apex_available: ["myapex"],
1953 min_sdk_version: "29",
1954 }
1955
1956 java_library {
1957 name: "libbar_dep",
1958 sdk_version: "current",
1959 srcs: ["a.java"],
1960 apex_available: ["myapex"],
1961 min_sdk_version: "29",
1962 }
Jooyung Han03b51852020-02-26 22:45:42 +09001963 `)
1964}
1965
Artur Satayev8cf899a2020-04-15 17:29:42 +01001966func TestJavaStableSdkVersion(t *testing.T) {
1967 testCases := []struct {
1968 name string
1969 expectedError string
1970 bp string
1971 }{
1972 {
1973 name: "Non-updatable apex with non-stable dep",
1974 bp: `
1975 apex {
1976 name: "myapex",
1977 java_libs: ["myjar"],
1978 key: "myapex.key",
1979 }
1980 apex_key {
1981 name: "myapex.key",
1982 public_key: "testkey.avbpubkey",
1983 private_key: "testkey.pem",
1984 }
1985 java_library {
1986 name: "myjar",
1987 srcs: ["foo/bar/MyClass.java"],
1988 sdk_version: "core_platform",
1989 apex_available: ["myapex"],
1990 }
1991 `,
1992 },
1993 {
1994 name: "Updatable apex with stable dep",
1995 bp: `
1996 apex {
1997 name: "myapex",
1998 java_libs: ["myjar"],
1999 key: "myapex.key",
2000 updatable: true,
2001 min_sdk_version: "29",
2002 }
2003 apex_key {
2004 name: "myapex.key",
2005 public_key: "testkey.avbpubkey",
2006 private_key: "testkey.pem",
2007 }
2008 java_library {
2009 name: "myjar",
2010 srcs: ["foo/bar/MyClass.java"],
2011 sdk_version: "current",
2012 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002013 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002014 }
2015 `,
2016 },
2017 {
2018 name: "Updatable apex with non-stable dep",
2019 expectedError: "cannot depend on \"myjar\"",
2020 bp: `
2021 apex {
2022 name: "myapex",
2023 java_libs: ["myjar"],
2024 key: "myapex.key",
2025 updatable: true,
2026 }
2027 apex_key {
2028 name: "myapex.key",
2029 public_key: "testkey.avbpubkey",
2030 private_key: "testkey.pem",
2031 }
2032 java_library {
2033 name: "myjar",
2034 srcs: ["foo/bar/MyClass.java"],
2035 sdk_version: "core_platform",
2036 apex_available: ["myapex"],
2037 }
2038 `,
2039 },
2040 {
2041 name: "Updatable apex with non-stable transitive dep",
2042 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against non-public Android API.",
2043 bp: `
2044 apex {
2045 name: "myapex",
2046 java_libs: ["myjar"],
2047 key: "myapex.key",
2048 updatable: true,
2049 }
2050 apex_key {
2051 name: "myapex.key",
2052 public_key: "testkey.avbpubkey",
2053 private_key: "testkey.pem",
2054 }
2055 java_library {
2056 name: "myjar",
2057 srcs: ["foo/bar/MyClass.java"],
2058 sdk_version: "current",
2059 apex_available: ["myapex"],
2060 static_libs: ["transitive-jar"],
2061 }
2062 java_library {
2063 name: "transitive-jar",
2064 srcs: ["foo/bar/MyClass.java"],
2065 sdk_version: "core_platform",
2066 apex_available: ["myapex"],
2067 }
2068 `,
2069 },
2070 }
2071
2072 for _, test := range testCases {
2073 t.Run(test.name, func(t *testing.T) {
2074 if test.expectedError == "" {
2075 testApex(t, test.bp)
2076 } else {
2077 testApexError(t, test.expectedError, test.bp)
2078 }
2079 })
2080 }
2081}
2082
Jooyung Han749dc692020-04-15 11:03:39 +09002083func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2084 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2085 apex {
2086 name: "myapex",
2087 key: "myapex.key",
2088 native_shared_libs: ["mylib"],
2089 min_sdk_version: "29",
2090 }
2091
2092 apex_key {
2093 name: "myapex.key",
2094 public_key: "testkey.avbpubkey",
2095 private_key: "testkey.pem",
2096 }
2097
2098 cc_library {
2099 name: "mylib",
2100 srcs: ["mylib.cpp"],
2101 shared_libs: ["mylib2"],
2102 system_shared_libs: [],
2103 stl: "none",
2104 apex_available: [
2105 "myapex",
2106 ],
2107 min_sdk_version: "29",
2108 }
2109
2110 // indirect part of the apex
2111 cc_library {
2112 name: "mylib2",
2113 srcs: ["mylib.cpp"],
2114 system_shared_libs: [],
2115 stl: "none",
2116 apex_available: [
2117 "myapex",
2118 ],
2119 min_sdk_version: "30",
2120 }
2121 `)
2122}
2123
2124func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2125 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2126 apex {
2127 name: "myapex",
2128 key: "myapex.key",
2129 apps: ["AppFoo"],
2130 min_sdk_version: "29",
2131 }
2132
2133 apex_key {
2134 name: "myapex.key",
2135 public_key: "testkey.avbpubkey",
2136 private_key: "testkey.pem",
2137 }
2138
2139 android_app {
2140 name: "AppFoo",
2141 srcs: ["foo/bar/MyClass.java"],
2142 sdk_version: "current",
2143 min_sdk_version: "29",
2144 system_modules: "none",
2145 stl: "none",
2146 static_libs: ["bar"],
2147 apex_available: [ "myapex" ],
2148 }
2149
2150 java_library {
2151 name: "bar",
2152 sdk_version: "current",
2153 srcs: ["a.java"],
2154 apex_available: [ "myapex" ],
2155 }
2156 `)
2157}
2158
2159func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
2160 ctx, _ := testApex(t, `
2161 apex {
2162 name: "myapex",
2163 key: "myapex.key",
2164 native_shared_libs: ["mylib"],
2165 min_sdk_version: "29",
2166 }
2167
2168 apex_key {
2169 name: "myapex.key",
2170 public_key: "testkey.avbpubkey",
2171 private_key: "testkey.pem",
2172 }
2173
2174 // mylib in myapex will link to mylib2#29
2175 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2176 cc_library {
2177 name: "mylib",
2178 srcs: ["mylib.cpp"],
2179 shared_libs: ["mylib2"],
2180 system_shared_libs: [],
2181 stl: "none",
2182 apex_available: ["myapex", "otherapex"],
2183 min_sdk_version: "29",
2184 }
2185
2186 cc_library {
2187 name: "mylib2",
2188 srcs: ["mylib.cpp"],
2189 system_shared_libs: [],
2190 stl: "none",
2191 apex_available: ["otherapex"],
2192 stubs: { versions: ["29", "30"] },
2193 min_sdk_version: "30",
2194 }
2195
2196 apex {
2197 name: "otherapex",
2198 key: "myapex.key",
2199 native_shared_libs: ["mylib", "mylib2"],
2200 min_sdk_version: "30",
2201 }
2202 `)
2203 expectLink := func(from, from_variant, to, to_variant string) {
2204 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2205 libFlags := ld.Args["libFlags"]
2206 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2207 }
Colin Crossaede88c2020-08-11 12:17:01 -07002208 expectLink("mylib", "shared_apex29", "mylib2", "shared_29")
2209 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002210}
2211
Jooyung Haned124c32021-01-26 11:43:46 +09002212func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
2213 withSAsActiveCodeNames := func(fs map[string][]byte, config android.Config) {
2214 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("S")
2215 config.TestProductVariables.Platform_version_active_codenames = []string{"S"}
2216 }
2217 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2218 apex {
2219 name: "myapex",
2220 key: "myapex.key",
2221 native_shared_libs: ["libfoo"],
2222 min_sdk_version: "S",
2223 }
2224 apex_key {
2225 name: "myapex.key",
2226 public_key: "testkey.avbpubkey",
2227 private_key: "testkey.pem",
2228 }
2229 cc_library {
2230 name: "libfoo",
2231 shared_libs: ["libbar"],
2232 apex_available: ["myapex"],
2233 min_sdk_version: "29",
2234 }
2235 cc_library {
2236 name: "libbar",
2237 apex_available: ["myapex"],
2238 }
2239 `, withSAsActiveCodeNames)
2240}
2241
2242func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
2243 withSAsActiveCodeNames := func(fs map[string][]byte, config android.Config) {
2244 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("S")
2245 config.TestProductVariables.Platform_version_active_codenames = []string{"S", "T"}
2246 }
2247 ctx, _ := testApex(t, `
2248 apex {
2249 name: "myapex",
2250 key: "myapex.key",
2251 native_shared_libs: ["libfoo"],
2252 min_sdk_version: "S",
2253 }
2254 apex_key {
2255 name: "myapex.key",
2256 public_key: "testkey.avbpubkey",
2257 private_key: "testkey.pem",
2258 }
2259 cc_library {
2260 name: "libfoo",
2261 shared_libs: ["libbar"],
2262 apex_available: ["myapex"],
2263 min_sdk_version: "S",
2264 }
2265 cc_library {
2266 name: "libbar",
2267 stubs: {
2268 symbol_file: "libbar.map.txt",
2269 versions: ["30", "S", "T"],
2270 },
2271 }
2272 `, withSAsActiveCodeNames)
2273
2274 // ensure libfoo is linked with "S" version of libbar stub
2275 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2276 libFlags := libfoo.Rule("ld").Args["libFlags"]
2277 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_S/libbar.so")
2278}
2279
Jiyong Park7c2ee712018-12-07 00:42:25 +09002280func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002281 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002282 apex {
2283 name: "myapex",
2284 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002285 native_shared_libs: ["mylib"],
2286 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002287 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002288 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09002289 }
2290
2291 apex_key {
2292 name: "myapex.key",
2293 public_key: "testkey.avbpubkey",
2294 private_key: "testkey.pem",
2295 }
2296
2297 prebuilt_etc {
2298 name: "myetc",
2299 src: "myprebuilt",
2300 sub_dir: "foo/bar",
2301 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002302
2303 cc_library {
2304 name: "mylib",
2305 srcs: ["mylib.cpp"],
2306 relative_install_path: "foo/bar",
2307 system_shared_libs: [],
2308 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002309 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002310 }
2311
2312 cc_binary {
2313 name: "mybin",
2314 srcs: ["mylib.cpp"],
2315 relative_install_path: "foo/bar",
2316 system_shared_libs: [],
2317 static_executable: true,
2318 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002319 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002320 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002321 `)
2322
Sundong Ahnabb64432019-10-22 13:58:29 +09002323 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002324 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2325
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002326 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002327 ensureListContains(t, dirs, "etc")
2328 ensureListContains(t, dirs, "etc/foo")
2329 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002330 ensureListContains(t, dirs, "lib64")
2331 ensureListContains(t, dirs, "lib64/foo")
2332 ensureListContains(t, dirs, "lib64/foo/bar")
2333 ensureListContains(t, dirs, "lib")
2334 ensureListContains(t, dirs, "lib/foo")
2335 ensureListContains(t, dirs, "lib/foo/bar")
2336
Jiyong Parkbd13e442019-03-15 18:10:35 +09002337 ensureListContains(t, dirs, "bin")
2338 ensureListContains(t, dirs, "bin/foo")
2339 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002340}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002341
Jooyung Han35155c42020-02-06 17:33:20 +09002342func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
2343 ctx, _ := testApex(t, `
2344 apex {
2345 name: "myapex",
2346 key: "myapex.key",
2347 multilib: {
2348 both: {
2349 native_shared_libs: ["mylib"],
2350 binaries: ["mybin"],
2351 },
2352 },
2353 compile_multilib: "both",
2354 native_bridge_supported: true,
2355 }
2356
2357 apex_key {
2358 name: "myapex.key",
2359 public_key: "testkey.avbpubkey",
2360 private_key: "testkey.pem",
2361 }
2362
2363 cc_library {
2364 name: "mylib",
2365 relative_install_path: "foo/bar",
2366 system_shared_libs: [],
2367 stl: "none",
2368 apex_available: [ "myapex" ],
2369 native_bridge_supported: true,
2370 }
2371
2372 cc_binary {
2373 name: "mybin",
2374 relative_install_path: "foo/bar",
2375 system_shared_libs: [],
2376 static_executable: true,
2377 stl: "none",
2378 apex_available: [ "myapex" ],
2379 native_bridge_supported: true,
2380 compile_multilib: "both", // default is "first" for binary
2381 multilib: {
2382 lib64: {
2383 suffix: "64",
2384 },
2385 },
2386 }
2387 `, withNativeBridgeEnabled)
2388 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2389 "bin/foo/bar/mybin",
2390 "bin/foo/bar/mybin64",
2391 "bin/arm/foo/bar/mybin",
2392 "bin/arm64/foo/bar/mybin64",
2393 "lib/foo/bar/mylib.so",
2394 "lib/arm/foo/bar/mylib.so",
2395 "lib64/foo/bar/mylib.so",
2396 "lib64/arm64/foo/bar/mylib.so",
2397 })
2398}
2399
Jiyong Parkda6eb592018-12-19 17:12:36 +09002400func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002401 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002402 apex {
2403 name: "myapex",
2404 key: "myapex.key",
2405 native_shared_libs: ["mylib"],
2406 use_vendor: true,
2407 }
2408
2409 apex_key {
2410 name: "myapex.key",
2411 public_key: "testkey.avbpubkey",
2412 private_key: "testkey.pem",
2413 }
2414
2415 cc_library {
2416 name: "mylib",
2417 srcs: ["mylib.cpp"],
2418 shared_libs: ["mylib2"],
2419 system_shared_libs: [],
2420 vendor_available: true,
2421 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002422 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002423 }
2424
2425 cc_library {
2426 name: "mylib2",
2427 srcs: ["mylib.cpp"],
2428 system_shared_libs: [],
2429 vendor_available: true,
2430 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002431 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002432 }
Jooyung Handc782442019-11-01 03:14:38 +09002433 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002434 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002435 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09002436
2437 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002438 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002439 for _, implicit := range i.Implicits {
2440 inputsList = append(inputsList, implicit.String())
2441 }
2442 }
2443 inputsString := strings.Join(inputsList, " ")
2444
2445 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002446 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2447 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002448
2449 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002450 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2451 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002452}
Jiyong Park16e91a02018-12-20 18:18:08 +09002453
Jooyung Han85d61762020-06-24 23:50:26 +09002454func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002455 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2456 apex {
2457 name: "myapex",
2458 key: "myapex.key",
2459 use_vendor: true,
2460 }
2461 apex_key {
2462 name: "myapex.key",
2463 public_key: "testkey.avbpubkey",
2464 private_key: "testkey.pem",
2465 }
2466 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002467 setUseVendorAllowListForTest(config, []string{""})
Jooyung Handc782442019-11-01 03:14:38 +09002468 })
Colin Cross440e0d02020-06-11 11:32:11 -07002469 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002470 testApex(t, `
2471 apex {
2472 name: "myapex",
2473 key: "myapex.key",
2474 use_vendor: true,
2475 }
2476 apex_key {
2477 name: "myapex.key",
2478 public_key: "testkey.avbpubkey",
2479 private_key: "testkey.pem",
2480 }
2481 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002482 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002483 })
2484}
2485
Jooyung Han5c998b92019-06-27 11:30:33 +09002486func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2487 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2488 apex {
2489 name: "myapex",
2490 key: "myapex.key",
2491 native_shared_libs: ["mylib"],
2492 use_vendor: true,
2493 }
2494
2495 apex_key {
2496 name: "myapex.key",
2497 public_key: "testkey.avbpubkey",
2498 private_key: "testkey.pem",
2499 }
2500
2501 cc_library {
2502 name: "mylib",
2503 srcs: ["mylib.cpp"],
2504 system_shared_libs: [],
2505 stl: "none",
2506 }
2507 `)
2508}
2509
Jooyung Han85d61762020-06-24 23:50:26 +09002510func TestVendorApex(t *testing.T) {
2511 ctx, config := testApex(t, `
2512 apex {
2513 name: "myapex",
2514 key: "myapex.key",
2515 binaries: ["mybin"],
2516 vendor: true,
2517 }
2518 apex_key {
2519 name: "myapex.key",
2520 public_key: "testkey.avbpubkey",
2521 private_key: "testkey.pem",
2522 }
2523 cc_binary {
2524 name: "mybin",
2525 vendor: true,
2526 shared_libs: ["libfoo"],
2527 }
2528 cc_library {
2529 name: "libfoo",
2530 proprietary: true,
2531 }
2532 `)
2533
2534 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2535 "bin/mybin",
2536 "lib64/libfoo.so",
2537 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2538 "lib64/libc++.so",
2539 })
2540
2541 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2542 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2543 name := apexBundle.BaseModuleName()
2544 prefix := "TARGET_"
2545 var builder strings.Builder
2546 data.Custom(&builder, name, prefix, "", data)
2547 androidMk := builder.String()
2548 ensureContains(t, androidMk, `LOCAL_MODULE_PATH := /tmp/target/product/test_device/vendor/apex`)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002549
2550 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2551 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2552 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002553}
2554
Jooyung Handf78e212020-07-22 15:54:47 +09002555func TestVendorApex_use_vndk_as_stable(t *testing.T) {
2556 ctx, _ := testApex(t, `
2557 apex {
2558 name: "myapex",
2559 key: "myapex.key",
2560 binaries: ["mybin"],
2561 vendor: true,
2562 use_vndk_as_stable: true,
2563 }
2564 apex_key {
2565 name: "myapex.key",
2566 public_key: "testkey.avbpubkey",
2567 private_key: "testkey.pem",
2568 }
2569 cc_binary {
2570 name: "mybin",
2571 vendor: true,
2572 shared_libs: ["libvndk", "libvendor"],
2573 }
2574 cc_library {
2575 name: "libvndk",
2576 vndk: {
2577 enabled: true,
2578 },
2579 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002580 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002581 }
2582 cc_library {
2583 name: "libvendor",
2584 vendor: true,
2585 }
2586 `)
2587
2588 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2589
Colin Crossaede88c2020-08-11 12:17:01 -07002590 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002591 libs := names(ldRule.Args["libFlags"])
2592 // VNDK libs(libvndk/libc++) as they are
2593 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2594 ensureListContains(t, libs, buildDir+"/.intermediates/libc++/"+vendorVariant+"_shared/libc++.so")
2595 // non-stable Vendor libs as APEX variants
Colin Crossaede88c2020-08-11 12:17:01 -07002596 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002597
2598 // VNDK libs are not included when use_vndk_as_stable: true
2599 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2600 "bin/mybin",
2601 "lib64/libvendor.so",
2602 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002603
2604 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2605 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2606 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002607}
2608
Jooyung Han8e5685d2020-09-21 11:02:57 +09002609func TestApex_withPrebuiltFirmware(t *testing.T) {
2610 testCases := []struct {
2611 name string
2612 additionalProp string
2613 }{
2614 {"system apex with prebuilt_firmware", ""},
2615 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2616 }
2617 for _, tc := range testCases {
2618 t.Run(tc.name, func(t *testing.T) {
2619 ctx, _ := testApex(t, `
2620 apex {
2621 name: "myapex",
2622 key: "myapex.key",
2623 prebuilts: ["myfirmware"],
2624 `+tc.additionalProp+`
2625 }
2626 apex_key {
2627 name: "myapex.key",
2628 public_key: "testkey.avbpubkey",
2629 private_key: "testkey.pem",
2630 }
2631 prebuilt_firmware {
2632 name: "myfirmware",
2633 src: "myfirmware.bin",
2634 filename_from_src: true,
2635 `+tc.additionalProp+`
2636 }
2637 `)
2638 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2639 "etc/firmware/myfirmware.bin",
2640 })
2641 })
2642 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002643}
2644
Jooyung Hanefb184e2020-06-25 17:14:25 +09002645func TestAndroidMk_UseVendorRequired(t *testing.T) {
2646 ctx, config := testApex(t, `
2647 apex {
2648 name: "myapex",
2649 key: "myapex.key",
2650 use_vendor: true,
2651 native_shared_libs: ["mylib"],
2652 }
2653
2654 apex_key {
2655 name: "myapex.key",
2656 public_key: "testkey.avbpubkey",
2657 private_key: "testkey.pem",
2658 }
2659
2660 cc_library {
2661 name: "mylib",
2662 vendor_available: true,
2663 apex_available: ["myapex"],
2664 }
2665 `, func(fs map[string][]byte, config android.Config) {
2666 setUseVendorAllowListForTest(config, []string{"myapex"})
2667 })
2668
2669 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2670 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2671 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 libm libdl\n")
2677}
2678
2679func TestAndroidMk_VendorApexRequired(t *testing.T) {
2680 ctx, config := testApex(t, `
2681 apex {
2682 name: "myapex",
2683 key: "myapex.key",
2684 vendor: true,
2685 native_shared_libs: ["mylib"],
2686 }
2687
2688 apex_key {
2689 name: "myapex.key",
2690 public_key: "testkey.avbpubkey",
2691 private_key: "testkey.pem",
2692 }
2693
2694 cc_library {
2695 name: "mylib",
2696 vendor_available: true,
2697 }
2698 `)
2699
2700 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2701 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2702 name := apexBundle.BaseModuleName()
2703 prefix := "TARGET_"
2704 var builder strings.Builder
2705 data.Custom(&builder, name, prefix, "", data)
2706 androidMk := builder.String()
2707 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2708}
2709
Jooyung Han2ed99d02020-06-24 23:26:26 +09002710func TestAndroidMkWritesCommonProperties(t *testing.T) {
2711 ctx, config := testApex(t, `
2712 apex {
2713 name: "myapex",
2714 key: "myapex.key",
2715 vintf_fragments: ["fragment.xml"],
2716 init_rc: ["init.rc"],
2717 }
2718 apex_key {
2719 name: "myapex.key",
2720 public_key: "testkey.avbpubkey",
2721 private_key: "testkey.pem",
2722 }
2723 cc_binary {
2724 name: "mybin",
2725 }
2726 `)
2727
2728 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2729 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2730 name := apexBundle.BaseModuleName()
2731 prefix := "TARGET_"
2732 var builder strings.Builder
2733 data.Custom(&builder, name, prefix, "", data)
2734 androidMk := builder.String()
2735 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2736 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2737}
2738
Jiyong Park16e91a02018-12-20 18:18:08 +09002739func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002740 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002741 apex {
2742 name: "myapex",
2743 key: "myapex.key",
2744 native_shared_libs: ["mylib"],
2745 }
2746
2747 apex_key {
2748 name: "myapex.key",
2749 public_key: "testkey.avbpubkey",
2750 private_key: "testkey.pem",
2751 }
2752
2753 cc_library {
2754 name: "mylib",
2755 srcs: ["mylib.cpp"],
2756 system_shared_libs: [],
2757 stl: "none",
2758 stubs: {
2759 versions: ["1", "2", "3"],
2760 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002761 apex_available: [
2762 "//apex_available:platform",
2763 "myapex",
2764 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002765 }
2766
2767 cc_binary {
2768 name: "not_in_apex",
2769 srcs: ["mylib.cpp"],
2770 static_libs: ["mylib"],
2771 static_executable: true,
2772 system_shared_libs: [],
2773 stl: "none",
2774 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002775 `)
2776
Colin Cross7113d202019-11-20 16:39:12 -08002777 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002778
2779 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002780 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002781}
Jiyong Park9335a262018-12-24 11:31:58 +09002782
2783func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002784 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002785 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002786 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002787 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002788 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002789 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002790 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09002791 }
2792
2793 cc_library {
2794 name: "mylib",
2795 srcs: ["mylib.cpp"],
2796 system_shared_libs: [],
2797 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002798 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002799 }
2800
2801 apex_key {
2802 name: "myapex.key",
2803 public_key: "testkey.avbpubkey",
2804 private_key: "testkey.pem",
2805 }
2806
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002807 android_app_certificate {
2808 name: "myapex.certificate",
2809 certificate: "testkey",
2810 }
2811
2812 android_app_certificate {
2813 name: "myapex.certificate.override",
2814 certificate: "testkey.override",
2815 }
2816
Jiyong Park9335a262018-12-24 11:31:58 +09002817 `)
2818
2819 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002820 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002821
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002822 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2823 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002824 "vendor/foo/devkeys/testkey.avbpubkey")
2825 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002826 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2827 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002828 "vendor/foo/devkeys/testkey.pem")
2829 }
2830
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002831 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002832 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002833 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002834 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002835 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002836 }
2837}
Jiyong Park58e364a2019-01-19 19:24:06 +09002838
Jooyung Hanf121a652019-12-17 14:30:11 +09002839func TestCertificate(t *testing.T) {
2840 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
2841 ctx, _ := testApex(t, `
2842 apex {
2843 name: "myapex",
2844 key: "myapex.key",
2845 }
2846 apex_key {
2847 name: "myapex.key",
2848 public_key: "testkey.avbpubkey",
2849 private_key: "testkey.pem",
2850 }`)
2851 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2852 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2853 if actual := rule.Args["certificates"]; actual != expected {
2854 t.Errorf("certificates should be %q, not %q", expected, actual)
2855 }
2856 })
2857 t.Run("override when unspecified", func(t *testing.T) {
2858 ctx, _ := testApex(t, `
2859 apex {
2860 name: "myapex_keytest",
2861 key: "myapex.key",
2862 file_contexts: ":myapex-file_contexts",
2863 }
2864 apex_key {
2865 name: "myapex.key",
2866 public_key: "testkey.avbpubkey",
2867 private_key: "testkey.pem",
2868 }
2869 android_app_certificate {
2870 name: "myapex.certificate.override",
2871 certificate: "testkey.override",
2872 }`)
2873 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2874 expected := "testkey.override.x509.pem testkey.override.pk8"
2875 if actual := rule.Args["certificates"]; actual != expected {
2876 t.Errorf("certificates should be %q, not %q", expected, actual)
2877 }
2878 })
2879 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
2880 ctx, _ := testApex(t, `
2881 apex {
2882 name: "myapex",
2883 key: "myapex.key",
2884 certificate: ":myapex.certificate",
2885 }
2886 apex_key {
2887 name: "myapex.key",
2888 public_key: "testkey.avbpubkey",
2889 private_key: "testkey.pem",
2890 }
2891 android_app_certificate {
2892 name: "myapex.certificate",
2893 certificate: "testkey",
2894 }`)
2895 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2896 expected := "testkey.x509.pem testkey.pk8"
2897 if actual := rule.Args["certificates"]; actual != expected {
2898 t.Errorf("certificates should be %q, not %q", expected, actual)
2899 }
2900 })
2901 t.Run("override when specifiec as <:module>", func(t *testing.T) {
2902 ctx, _ := testApex(t, `
2903 apex {
2904 name: "myapex_keytest",
2905 key: "myapex.key",
2906 file_contexts: ":myapex-file_contexts",
2907 certificate: ":myapex.certificate",
2908 }
2909 apex_key {
2910 name: "myapex.key",
2911 public_key: "testkey.avbpubkey",
2912 private_key: "testkey.pem",
2913 }
2914 android_app_certificate {
2915 name: "myapex.certificate.override",
2916 certificate: "testkey.override",
2917 }`)
2918 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2919 expected := "testkey.override.x509.pem testkey.override.pk8"
2920 if actual := rule.Args["certificates"]; actual != expected {
2921 t.Errorf("certificates should be %q, not %q", expected, actual)
2922 }
2923 })
2924 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
2925 ctx, _ := testApex(t, `
2926 apex {
2927 name: "myapex",
2928 key: "myapex.key",
2929 certificate: "testkey",
2930 }
2931 apex_key {
2932 name: "myapex.key",
2933 public_key: "testkey.avbpubkey",
2934 private_key: "testkey.pem",
2935 }`)
2936 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2937 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2938 if actual := rule.Args["certificates"]; actual != expected {
2939 t.Errorf("certificates should be %q, not %q", expected, actual)
2940 }
2941 })
2942 t.Run("override when specified as <name>", func(t *testing.T) {
2943 ctx, _ := testApex(t, `
2944 apex {
2945 name: "myapex_keytest",
2946 key: "myapex.key",
2947 file_contexts: ":myapex-file_contexts",
2948 certificate: "testkey",
2949 }
2950 apex_key {
2951 name: "myapex.key",
2952 public_key: "testkey.avbpubkey",
2953 private_key: "testkey.pem",
2954 }
2955 android_app_certificate {
2956 name: "myapex.certificate.override",
2957 certificate: "testkey.override",
2958 }`)
2959 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2960 expected := "testkey.override.x509.pem testkey.override.pk8"
2961 if actual := rule.Args["certificates"]; actual != expected {
2962 t.Errorf("certificates should be %q, not %q", expected, actual)
2963 }
2964 })
2965}
2966
Jiyong Park58e364a2019-01-19 19:24:06 +09002967func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002968 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002969 apex {
2970 name: "myapex",
2971 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002972 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09002973 }
2974
2975 apex {
2976 name: "otherapex",
2977 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002978 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002979 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002980 }
2981
2982 apex_key {
2983 name: "myapex.key",
2984 public_key: "testkey.avbpubkey",
2985 private_key: "testkey.pem",
2986 }
2987
2988 cc_library {
2989 name: "mylib",
2990 srcs: ["mylib.cpp"],
2991 system_shared_libs: [],
2992 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002993 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002994 "myapex",
2995 "otherapex",
2996 ],
Jooyung Han24282772020-03-21 23:20:55 +09002997 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002998 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002999 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003000 cc_library {
3001 name: "mylib2",
3002 srcs: ["mylib.cpp"],
3003 system_shared_libs: [],
3004 stl: "none",
3005 apex_available: [
3006 "myapex",
3007 "otherapex",
3008 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003009 static_libs: ["mylib3"],
3010 recovery_available: true,
3011 min_sdk_version: "29",
3012 }
3013 cc_library {
3014 name: "mylib3",
3015 srcs: ["mylib.cpp"],
3016 system_shared_libs: [],
3017 stl: "none",
3018 apex_available: [
3019 "myapex",
3020 "otherapex",
3021 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09003022 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07003023 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003024 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003025 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003026 `)
3027
Jooyung Hanc87a0592020-03-02 17:44:33 +09003028 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003029 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003030 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003031 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003032
Jooyung Hanccce2f22020-03-07 03:45:53 +09003033 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003034 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003035 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003036 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003037 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003038
Jooyung Hanccce2f22020-03-07 03:45:53 +09003039 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003040 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003041 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003042 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003043 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003044
Colin Crossaede88c2020-08-11 12:17:01 -07003045 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3046 // each variant defines additional macros to distinguish which apex variant it is built for
3047
3048 // non-APEX variant does not have __ANDROID_APEX__ defined
3049 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3050 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3051
3052 // APEX variant has __ANDROID_APEX__ defined
3053 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3054 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3055 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3056 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3057
3058 // APEX variant has __ANDROID_APEX__ defined
3059 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3060 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3061 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3062 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3063
Dan Albertb19953d2020-11-17 15:29:36 -08003064 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003065 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3066 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003067 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003068
3069 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3070 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003071
3072 // non-APEX variant does not have __ANDROID_APEX__ defined
3073 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3074 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3075
3076 // APEX variant has __ANDROID_APEX__ defined
3077 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003078 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003079 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003080 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003081
Jooyung Hanc87a0592020-03-02 17:44:33 +09003082 // APEX variant has __ANDROID_APEX__ defined
3083 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003084 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003085 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003086 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003087
Dan Albertb19953d2020-11-17 15:29:36 -08003088 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003089 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003090 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003091 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003092}
Jiyong Park7e636d02019-01-28 16:16:54 +09003093
3094func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003095 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003096 apex {
3097 name: "myapex",
3098 key: "myapex.key",
3099 native_shared_libs: ["mylib"],
3100 }
3101
3102 apex_key {
3103 name: "myapex.key",
3104 public_key: "testkey.avbpubkey",
3105 private_key: "testkey.pem",
3106 }
3107
3108 cc_library_headers {
3109 name: "mylib_headers",
3110 export_include_dirs: ["my_include"],
3111 system_shared_libs: [],
3112 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003113 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003114 }
3115
3116 cc_library {
3117 name: "mylib",
3118 srcs: ["mylib.cpp"],
3119 system_shared_libs: [],
3120 stl: "none",
3121 header_libs: ["mylib_headers"],
3122 export_header_lib_headers: ["mylib_headers"],
3123 stubs: {
3124 versions: ["1", "2", "3"],
3125 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003126 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003127 }
3128
3129 cc_library {
3130 name: "otherlib",
3131 srcs: ["mylib.cpp"],
3132 system_shared_libs: [],
3133 stl: "none",
3134 shared_libs: ["mylib"],
3135 }
3136 `)
3137
Colin Cross7113d202019-11-20 16:39:12 -08003138 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003139
3140 // Ensure that the include path of the header lib is exported to 'otherlib'
3141 ensureContains(t, cFlags, "-Imy_include")
3142}
Alex Light9670d332019-01-29 18:07:33 -08003143
Jiyong Park7cd10e32020-01-14 09:22:18 +09003144type fileInApex struct {
3145 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003146 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003147 isLink bool
3148}
3149
Jooyung Hana57af4a2020-01-23 05:36:59 +00003150func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003151 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003152 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003153 copyCmds := apexRule.Args["copy_commands"]
3154 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003155 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003156 for _, cmd := range strings.Split(copyCmds, "&&") {
3157 cmd = strings.TrimSpace(cmd)
3158 if cmd == "" {
3159 continue
3160 }
3161 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003162 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003163 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003164 switch terms[0] {
3165 case "mkdir":
3166 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003167 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003168 t.Fatal("copyCmds contains invalid cp command", cmd)
3169 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003170 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003171 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003172 isLink = false
3173 case "ln":
3174 if len(terms) != 3 && len(terms) != 4 {
3175 // ln LINK TARGET or ln -s LINK TARGET
3176 t.Fatal("copyCmds contains invalid ln command", cmd)
3177 }
3178 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003179 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003180 isLink = true
3181 default:
3182 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3183 }
3184 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003185 index := strings.Index(dst, imageApexDir)
3186 if index == -1 {
3187 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3188 }
3189 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003190 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003191 }
3192 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003193 return ret
3194}
3195
Jooyung Hana57af4a2020-01-23 05:36:59 +00003196func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3197 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003198 var failed bool
3199 var surplus []string
3200 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003201 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003202 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003203 for _, expected := range files {
3204 if matched, _ := path.Match(expected, file.path); matched {
3205 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003206 mactchFound = true
3207 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003208 }
3209 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003210 if !mactchFound {
3211 surplus = append(surplus, file.path)
3212 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003213 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003214
Jooyung Han31c470b2019-10-18 16:26:59 +09003215 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003216 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003217 t.Log("surplus files", surplus)
3218 failed = true
3219 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003220
3221 if len(files) > len(filesMatched) {
3222 var missing []string
3223 for _, expected := range files {
3224 if !filesMatched[expected] {
3225 missing = append(missing, expected)
3226 }
3227 }
3228 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003229 t.Log("missing files", missing)
3230 failed = true
3231 }
3232 if failed {
3233 t.Fail()
3234 }
3235}
3236
Jooyung Han344d5432019-08-23 11:17:39 +09003237func TestVndkApexCurrent(t *testing.T) {
3238 ctx, _ := testApex(t, `
3239 apex_vndk {
3240 name: "myapex",
3241 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003242 }
3243
3244 apex_key {
3245 name: "myapex.key",
3246 public_key: "testkey.avbpubkey",
3247 private_key: "testkey.pem",
3248 }
3249
3250 cc_library {
3251 name: "libvndk",
3252 srcs: ["mylib.cpp"],
3253 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003254 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003255 vndk: {
3256 enabled: true,
3257 },
3258 system_shared_libs: [],
3259 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003260 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003261 }
3262
3263 cc_library {
3264 name: "libvndksp",
3265 srcs: ["mylib.cpp"],
3266 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003267 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003268 vndk: {
3269 enabled: true,
3270 support_system_process: true,
3271 },
3272 system_shared_libs: [],
3273 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003274 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003275 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003276 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003277
Jooyung Hana57af4a2020-01-23 05:36:59 +00003278 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003279 "lib/libvndk.so",
3280 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003281 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003282 "lib64/libvndk.so",
3283 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003284 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003285 "etc/llndk.libraries.VER.txt",
3286 "etc/vndkcore.libraries.VER.txt",
3287 "etc/vndksp.libraries.VER.txt",
3288 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003289 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003290 })
Jooyung Han344d5432019-08-23 11:17:39 +09003291}
3292
3293func TestVndkApexWithPrebuilt(t *testing.T) {
3294 ctx, _ := testApex(t, `
3295 apex_vndk {
3296 name: "myapex",
3297 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003298 }
3299
3300 apex_key {
3301 name: "myapex.key",
3302 public_key: "testkey.avbpubkey",
3303 private_key: "testkey.pem",
3304 }
3305
3306 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003307 name: "libvndk",
3308 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003309 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003310 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003311 vndk: {
3312 enabled: true,
3313 },
3314 system_shared_libs: [],
3315 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003316 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003317 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003318
3319 cc_prebuilt_library_shared {
3320 name: "libvndk.arm",
3321 srcs: ["libvndk.arm.so"],
3322 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003323 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003324 vndk: {
3325 enabled: true,
3326 },
3327 enabled: false,
3328 arch: {
3329 arm: {
3330 enabled: true,
3331 },
3332 },
3333 system_shared_libs: [],
3334 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003335 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003336 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003337 `+vndkLibrariesTxtFiles("current"),
3338 withFiles(map[string][]byte{
3339 "libvndk.so": nil,
3340 "libvndk.arm.so": nil,
3341 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003342
Jooyung Hana57af4a2020-01-23 05:36:59 +00003343 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003344 "lib/libvndk.so",
3345 "lib/libvndk.arm.so",
3346 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003347 "lib/libc++.so",
3348 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003349 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003350 })
Jooyung Han344d5432019-08-23 11:17:39 +09003351}
3352
Jooyung Han39edb6c2019-11-06 16:53:07 +09003353func vndkLibrariesTxtFiles(vers ...string) (result string) {
3354 for _, v := range vers {
3355 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003356 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003357 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003358 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003359 name: "` + txt + `.libraries.txt",
3360 }
3361 `
3362 }
3363 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003364 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003365 result += `
3366 prebuilt_etc {
3367 name: "` + txt + `.libraries.` + v + `.txt",
3368 src: "dummy.txt",
3369 }
3370 `
3371 }
3372 }
3373 }
3374 return
3375}
3376
Jooyung Han344d5432019-08-23 11:17:39 +09003377func TestVndkApexVersion(t *testing.T) {
3378 ctx, _ := testApex(t, `
3379 apex_vndk {
3380 name: "myapex_v27",
3381 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003382 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003383 vndk_version: "27",
3384 }
3385
3386 apex_key {
3387 name: "myapex.key",
3388 public_key: "testkey.avbpubkey",
3389 private_key: "testkey.pem",
3390 }
3391
Jooyung Han31c470b2019-10-18 16:26:59 +09003392 vndk_prebuilt_shared {
3393 name: "libvndk27",
3394 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003395 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003396 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003397 vndk: {
3398 enabled: true,
3399 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003400 target_arch: "arm64",
3401 arch: {
3402 arm: {
3403 srcs: ["libvndk27_arm.so"],
3404 },
3405 arm64: {
3406 srcs: ["libvndk27_arm64.so"],
3407 },
3408 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003409 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003410 }
3411
3412 vndk_prebuilt_shared {
3413 name: "libvndk27",
3414 version: "27",
3415 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003416 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003417 vndk: {
3418 enabled: true,
3419 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003420 target_arch: "x86_64",
3421 arch: {
3422 x86: {
3423 srcs: ["libvndk27_x86.so"],
3424 },
3425 x86_64: {
3426 srcs: ["libvndk27_x86_64.so"],
3427 },
3428 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003429 }
3430 `+vndkLibrariesTxtFiles("27"),
3431 withFiles(map[string][]byte{
3432 "libvndk27_arm.so": nil,
3433 "libvndk27_arm64.so": nil,
3434 "libvndk27_x86.so": nil,
3435 "libvndk27_x86_64.so": nil,
3436 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003437
Jooyung Hana57af4a2020-01-23 05:36:59 +00003438 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003439 "lib/libvndk27_arm.so",
3440 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003441 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003442 })
Jooyung Han344d5432019-08-23 11:17:39 +09003443}
3444
3445func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
3446 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
3447 apex_vndk {
3448 name: "myapex_v27",
3449 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003450 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003451 vndk_version: "27",
3452 }
3453 apex_vndk {
3454 name: "myapex_v27_other",
3455 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003456 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003457 vndk_version: "27",
3458 }
3459
3460 apex_key {
3461 name: "myapex.key",
3462 public_key: "testkey.avbpubkey",
3463 private_key: "testkey.pem",
3464 }
3465
3466 cc_library {
3467 name: "libvndk",
3468 srcs: ["mylib.cpp"],
3469 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003470 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003471 vndk: {
3472 enabled: true,
3473 },
3474 system_shared_libs: [],
3475 stl: "none",
3476 }
3477
3478 vndk_prebuilt_shared {
3479 name: "libvndk",
3480 version: "27",
3481 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003482 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003483 vndk: {
3484 enabled: true,
3485 },
3486 srcs: ["libvndk.so"],
3487 }
3488 `, withFiles(map[string][]byte{
3489 "libvndk.so": nil,
3490 }))
3491}
3492
Jooyung Han90eee022019-10-01 20:02:42 +09003493func TestVndkApexNameRule(t *testing.T) {
3494 ctx, _ := testApex(t, `
3495 apex_vndk {
3496 name: "myapex",
3497 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003498 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003499 }
3500 apex_vndk {
3501 name: "myapex_v28",
3502 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003503 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003504 vndk_version: "28",
3505 }
3506 apex_key {
3507 name: "myapex.key",
3508 public_key: "testkey.avbpubkey",
3509 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003510 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003511
3512 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003513 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003514 actual := proptools.String(bundle.properties.Apex_name)
3515 if !reflect.DeepEqual(actual, expected) {
3516 t.Errorf("Got '%v', expected '%v'", actual, expected)
3517 }
3518 }
3519
3520 assertApexName("com.android.vndk.vVER", "myapex")
3521 assertApexName("com.android.vndk.v28", "myapex_v28")
3522}
3523
Jooyung Han344d5432019-08-23 11:17:39 +09003524func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
3525 ctx, _ := testApex(t, `
3526 apex_vndk {
3527 name: "myapex",
3528 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003529 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003530 }
3531
3532 apex_key {
3533 name: "myapex.key",
3534 public_key: "testkey.avbpubkey",
3535 private_key: "testkey.pem",
3536 }
3537
3538 cc_library {
3539 name: "libvndk",
3540 srcs: ["mylib.cpp"],
3541 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003542 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003543 native_bridge_supported: true,
3544 host_supported: true,
3545 vndk: {
3546 enabled: true,
3547 },
3548 system_shared_libs: [],
3549 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003550 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003551 }
Jooyung Han35155c42020-02-06 17:33:20 +09003552 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003553
Jooyung Hana57af4a2020-01-23 05:36:59 +00003554 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003555 "lib/libvndk.so",
3556 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003557 "lib/libc++.so",
3558 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003559 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003560 })
Jooyung Han344d5432019-08-23 11:17:39 +09003561}
3562
3563func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
3564 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
3565 apex_vndk {
3566 name: "myapex",
3567 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003568 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003569 native_bridge_supported: true,
3570 }
3571
3572 apex_key {
3573 name: "myapex.key",
3574 public_key: "testkey.avbpubkey",
3575 private_key: "testkey.pem",
3576 }
3577
3578 cc_library {
3579 name: "libvndk",
3580 srcs: ["mylib.cpp"],
3581 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003582 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003583 native_bridge_supported: true,
3584 host_supported: true,
3585 vndk: {
3586 enabled: true,
3587 },
3588 system_shared_libs: [],
3589 stl: "none",
3590 }
3591 `)
3592}
3593
Jooyung Han31c470b2019-10-18 16:26:59 +09003594func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003595 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003596 apex_vndk {
3597 name: "myapex_v27",
3598 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003599 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003600 vndk_version: "27",
3601 }
3602
3603 apex_key {
3604 name: "myapex.key",
3605 public_key: "testkey.avbpubkey",
3606 private_key: "testkey.pem",
3607 }
3608
3609 vndk_prebuilt_shared {
3610 name: "libvndk27",
3611 version: "27",
3612 target_arch: "arm",
3613 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003614 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003615 vndk: {
3616 enabled: true,
3617 },
3618 arch: {
3619 arm: {
3620 srcs: ["libvndk27.so"],
3621 }
3622 },
3623 }
3624
3625 vndk_prebuilt_shared {
3626 name: "libvndk27",
3627 version: "27",
3628 target_arch: "arm",
3629 binder32bit: true,
3630 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003631 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003632 vndk: {
3633 enabled: true,
3634 },
3635 arch: {
3636 arm: {
3637 srcs: ["libvndk27binder32.so"],
3638 }
3639 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003640 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003641 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003642 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003643 withFiles(map[string][]byte{
3644 "libvndk27.so": nil,
3645 "libvndk27binder32.so": nil,
3646 }),
3647 withBinder32bit,
3648 withTargets(map[android.OsType][]android.Target{
3649 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003650 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3651 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003652 },
3653 }),
3654 )
3655
Jooyung Hana57af4a2020-01-23 05:36:59 +00003656 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003657 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003658 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003659 })
3660}
3661
Jooyung Han45a96772020-06-15 14:59:42 +09003662func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
3663 ctx, _ := testApex(t, `
3664 apex_vndk {
3665 name: "myapex",
3666 key: "myapex.key",
3667 file_contexts: ":myapex-file_contexts",
3668 }
3669
3670 apex_key {
3671 name: "myapex.key",
3672 public_key: "testkey.avbpubkey",
3673 private_key: "testkey.pem",
3674 }
3675
3676 cc_library {
3677 name: "libz",
3678 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003679 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003680 vndk: {
3681 enabled: true,
3682 },
3683 stubs: {
3684 symbol_file: "libz.map.txt",
3685 versions: ["30"],
3686 }
3687 }
3688 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3689 "libz.map.txt": nil,
3690 }))
3691
3692 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_image").Rule("apexManifestRule")
3693 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3694 ensureListEmpty(t, provideNativeLibs)
3695}
3696
Jooyung Hane1633032019-08-01 17:41:43 +09003697func TestDependenciesInApexManifest(t *testing.T) {
3698 ctx, _ := testApex(t, `
3699 apex {
3700 name: "myapex_nodep",
3701 key: "myapex.key",
3702 native_shared_libs: ["lib_nodep"],
3703 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003704 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003705 }
3706
3707 apex {
3708 name: "myapex_dep",
3709 key: "myapex.key",
3710 native_shared_libs: ["lib_dep"],
3711 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003712 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003713 }
3714
3715 apex {
3716 name: "myapex_provider",
3717 key: "myapex.key",
3718 native_shared_libs: ["libfoo"],
3719 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003720 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003721 }
3722
3723 apex {
3724 name: "myapex_selfcontained",
3725 key: "myapex.key",
3726 native_shared_libs: ["lib_dep", "libfoo"],
3727 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003728 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003729 }
3730
3731 apex_key {
3732 name: "myapex.key",
3733 public_key: "testkey.avbpubkey",
3734 private_key: "testkey.pem",
3735 }
3736
3737 cc_library {
3738 name: "lib_nodep",
3739 srcs: ["mylib.cpp"],
3740 system_shared_libs: [],
3741 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003742 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003743 }
3744
3745 cc_library {
3746 name: "lib_dep",
3747 srcs: ["mylib.cpp"],
3748 shared_libs: ["libfoo"],
3749 system_shared_libs: [],
3750 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003751 apex_available: [
3752 "myapex_dep",
3753 "myapex_provider",
3754 "myapex_selfcontained",
3755 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003756 }
3757
3758 cc_library {
3759 name: "libfoo",
3760 srcs: ["mytest.cpp"],
3761 stubs: {
3762 versions: ["1"],
3763 },
3764 system_shared_libs: [],
3765 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003766 apex_available: [
3767 "myapex_provider",
3768 "myapex_selfcontained",
3769 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003770 }
3771 `)
3772
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003773 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003774 var provideNativeLibs, requireNativeLibs []string
3775
Sundong Ahnabb64432019-10-22 13:58:29 +09003776 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003777 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3778 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003779 ensureListEmpty(t, provideNativeLibs)
3780 ensureListEmpty(t, requireNativeLibs)
3781
Sundong Ahnabb64432019-10-22 13:58:29 +09003782 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003783 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3784 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003785 ensureListEmpty(t, provideNativeLibs)
3786 ensureListContains(t, requireNativeLibs, "libfoo.so")
3787
Sundong Ahnabb64432019-10-22 13:58:29 +09003788 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003789 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3790 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003791 ensureListContains(t, provideNativeLibs, "libfoo.so")
3792 ensureListEmpty(t, requireNativeLibs)
3793
Sundong Ahnabb64432019-10-22 13:58:29 +09003794 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003795 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3796 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003797 ensureListContains(t, provideNativeLibs, "libfoo.so")
3798 ensureListEmpty(t, requireNativeLibs)
3799}
3800
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003801func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09003802 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003803 apex {
3804 name: "myapex",
3805 key: "myapex.key",
3806 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003807 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003808 }
3809
3810 apex_key {
3811 name: "myapex.key",
3812 public_key: "testkey.avbpubkey",
3813 private_key: "testkey.pem",
3814 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003815
3816 cc_library {
3817 name: "mylib",
3818 srcs: ["mylib.cpp"],
3819 system_shared_libs: [],
3820 stl: "none",
3821 apex_available: [
3822 "//apex_available:platform",
3823 "myapex",
3824 ],
3825 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003826 `)
3827
Sundong Ahnabb64432019-10-22 13:58:29 +09003828 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003829 apexManifestRule := module.Rule("apexManifestRule")
3830 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3831 apexRule := module.Rule("apexRule")
3832 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003833
3834 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3835 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3836 name := apexBundle.BaseModuleName()
3837 prefix := "TARGET_"
3838 var builder strings.Builder
3839 data.Custom(&builder, name, prefix, "", data)
3840 androidMk := builder.String()
3841 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3842 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003843}
3844
Alex Light0851b882019-02-07 13:20:53 -08003845func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003846 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003847 apex {
3848 name: "myapex",
3849 key: "myapex.key",
3850 native_shared_libs: ["mylib_common"],
3851 }
3852
3853 apex_key {
3854 name: "myapex.key",
3855 public_key: "testkey.avbpubkey",
3856 private_key: "testkey.pem",
3857 }
3858
3859 cc_library {
3860 name: "mylib_common",
3861 srcs: ["mylib.cpp"],
3862 system_shared_libs: [],
3863 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003864 apex_available: [
3865 "//apex_available:platform",
3866 "myapex",
3867 ],
Alex Light0851b882019-02-07 13:20:53 -08003868 }
3869 `)
3870
Sundong Ahnabb64432019-10-22 13:58:29 +09003871 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003872 apexRule := module.Rule("apexRule")
3873 copyCmds := apexRule.Args["copy_commands"]
3874
3875 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3876 t.Log("Apex was a test apex!")
3877 t.Fail()
3878 }
3879 // Ensure that main rule creates an output
3880 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3881
3882 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003883 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003884
3885 // Ensure that both direct and indirect deps are copied into apex
3886 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3887
Colin Cross7113d202019-11-20 16:39:12 -08003888 // Ensure that the platform variant ends with _shared
3889 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003890
Colin Cross56a83212020-09-15 18:30:11 -07003891 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003892 t.Log("Found mylib_common not in any apex!")
3893 t.Fail()
3894 }
3895}
3896
3897func TestTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003898 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003899 apex_test {
3900 name: "myapex",
3901 key: "myapex.key",
3902 native_shared_libs: ["mylib_common_test"],
3903 }
3904
3905 apex_key {
3906 name: "myapex.key",
3907 public_key: "testkey.avbpubkey",
3908 private_key: "testkey.pem",
3909 }
3910
3911 cc_library {
3912 name: "mylib_common_test",
3913 srcs: ["mylib.cpp"],
3914 system_shared_libs: [],
3915 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003916 // TODO: remove //apex_available:platform
3917 apex_available: [
3918 "//apex_available:platform",
3919 "myapex",
3920 ],
Alex Light0851b882019-02-07 13:20:53 -08003921 }
3922 `)
3923
Sundong Ahnabb64432019-10-22 13:58:29 +09003924 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003925 apexRule := module.Rule("apexRule")
3926 copyCmds := apexRule.Args["copy_commands"]
3927
3928 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3929 t.Log("Apex was not a test apex!")
3930 t.Fail()
3931 }
3932 // Ensure that main rule creates an output
3933 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3934
3935 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003936 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003937
3938 // Ensure that both direct and indirect deps are copied into apex
3939 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3940
Colin Cross7113d202019-11-20 16:39:12 -08003941 // Ensure that the platform variant ends with _shared
3942 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003943}
3944
Alex Light9670d332019-01-29 18:07:33 -08003945func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003946 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003947 apex {
3948 name: "myapex",
3949 key: "myapex.key",
3950 multilib: {
3951 first: {
3952 native_shared_libs: ["mylib_common"],
3953 }
3954 },
3955 target: {
3956 android: {
3957 multilib: {
3958 first: {
3959 native_shared_libs: ["mylib"],
3960 }
3961 }
3962 },
3963 host: {
3964 multilib: {
3965 first: {
3966 native_shared_libs: ["mylib2"],
3967 }
3968 }
3969 }
3970 }
3971 }
3972
3973 apex_key {
3974 name: "myapex.key",
3975 public_key: "testkey.avbpubkey",
3976 private_key: "testkey.pem",
3977 }
3978
3979 cc_library {
3980 name: "mylib",
3981 srcs: ["mylib.cpp"],
3982 system_shared_libs: [],
3983 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003984 // TODO: remove //apex_available:platform
3985 apex_available: [
3986 "//apex_available:platform",
3987 "myapex",
3988 ],
Alex Light9670d332019-01-29 18:07:33 -08003989 }
3990
3991 cc_library {
3992 name: "mylib_common",
3993 srcs: ["mylib.cpp"],
3994 system_shared_libs: [],
3995 stl: "none",
3996 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003997 // TODO: remove //apex_available:platform
3998 apex_available: [
3999 "//apex_available:platform",
4000 "myapex",
4001 ],
Alex Light9670d332019-01-29 18:07:33 -08004002 }
4003
4004 cc_library {
4005 name: "mylib2",
4006 srcs: ["mylib.cpp"],
4007 system_shared_libs: [],
4008 stl: "none",
4009 compile_multilib: "first",
4010 }
4011 `)
4012
Sundong Ahnabb64432019-10-22 13:58:29 +09004013 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004014 copyCmds := apexRule.Args["copy_commands"]
4015
4016 // Ensure that main rule creates an output
4017 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4018
4019 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004020 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4021 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4022 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004023
4024 // Ensure that both direct and indirect deps are copied into apex
4025 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4026 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4027 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4028
Colin Cross7113d202019-11-20 16:39:12 -08004029 // Ensure that the platform variant ends with _shared
4030 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4031 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4032 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004033}
Jiyong Park04480cf2019-02-06 00:16:29 +09004034
Jiyong Park59140302020-12-14 18:44:04 +09004035func TestApexWithArch(t *testing.T) {
4036 ctx, _ := testApex(t, `
4037 apex {
4038 name: "myapex",
4039 key: "myapex.key",
4040 arch: {
4041 arm64: {
4042 native_shared_libs: ["mylib.arm64"],
4043 },
4044 x86_64: {
4045 native_shared_libs: ["mylib.x64"],
4046 },
4047 }
4048 }
4049
4050 apex_key {
4051 name: "myapex.key",
4052 public_key: "testkey.avbpubkey",
4053 private_key: "testkey.pem",
4054 }
4055
4056 cc_library {
4057 name: "mylib.arm64",
4058 srcs: ["mylib.cpp"],
4059 system_shared_libs: [],
4060 stl: "none",
4061 // TODO: remove //apex_available:platform
4062 apex_available: [
4063 "//apex_available:platform",
4064 "myapex",
4065 ],
4066 }
4067
4068 cc_library {
4069 name: "mylib.x64",
4070 srcs: ["mylib.cpp"],
4071 system_shared_libs: [],
4072 stl: "none",
4073 // TODO: remove //apex_available:platform
4074 apex_available: [
4075 "//apex_available:platform",
4076 "myapex",
4077 ],
4078 }
4079 `)
4080
4081 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4082 copyCmds := apexRule.Args["copy_commands"]
4083
4084 // Ensure that apex variant is created for the direct dep
4085 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4086 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4087
4088 // Ensure that both direct and indirect deps are copied into apex
4089 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4090 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4091}
4092
Jiyong Park04480cf2019-02-06 00:16:29 +09004093func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004094 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004095 apex {
4096 name: "myapex",
4097 key: "myapex.key",
4098 binaries: ["myscript"],
4099 }
4100
4101 apex_key {
4102 name: "myapex.key",
4103 public_key: "testkey.avbpubkey",
4104 private_key: "testkey.pem",
4105 }
4106
4107 sh_binary {
4108 name: "myscript",
4109 src: "mylib.cpp",
4110 filename: "myscript.sh",
4111 sub_dir: "script",
4112 }
4113 `)
4114
Sundong Ahnabb64432019-10-22 13:58:29 +09004115 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004116 copyCmds := apexRule.Args["copy_commands"]
4117
4118 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4119}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004120
Jooyung Han91df2082019-11-20 01:49:42 +09004121func TestApexInVariousPartition(t *testing.T) {
4122 testcases := []struct {
4123 propName, parition, flattenedPartition string
4124 }{
4125 {"", "system", "system_ext"},
4126 {"product_specific: true", "product", "product"},
4127 {"soc_specific: true", "vendor", "vendor"},
4128 {"proprietary: true", "vendor", "vendor"},
4129 {"vendor: true", "vendor", "vendor"},
4130 {"system_ext_specific: true", "system_ext", "system_ext"},
4131 }
4132 for _, tc := range testcases {
4133 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
4134 ctx, _ := testApex(t, `
4135 apex {
4136 name: "myapex",
4137 key: "myapex.key",
4138 `+tc.propName+`
4139 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004140
Jooyung Han91df2082019-11-20 01:49:42 +09004141 apex_key {
4142 name: "myapex.key",
4143 public_key: "testkey.avbpubkey",
4144 private_key: "testkey.pem",
4145 }
4146 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004147
Jooyung Han91df2082019-11-20 01:49:42 +09004148 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4149 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
4150 actual := apex.installDir.String()
4151 if actual != expected {
4152 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4153 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004154
Jooyung Han91df2082019-11-20 01:49:42 +09004155 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4156 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
4157 actual = flattened.installDir.String()
4158 if actual != expected {
4159 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4160 }
4161 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004162 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004163}
Jiyong Park67882562019-03-21 01:11:21 +09004164
Jooyung Han580eb4f2020-06-24 19:33:06 +09004165func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004166 ctx, _ := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004167 apex {
4168 name: "myapex",
4169 key: "myapex.key",
4170 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004171
Jooyung Han580eb4f2020-06-24 19:33:06 +09004172 apex_key {
4173 name: "myapex.key",
4174 public_key: "testkey.avbpubkey",
4175 private_key: "testkey.pem",
4176 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004177 `)
4178 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004179 rule := module.Output("file_contexts")
4180 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4181}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004182
Jooyung Han580eb4f2020-06-24 19:33:06 +09004183func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004184 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004185 apex {
4186 name: "myapex",
4187 key: "myapex.key",
4188 file_contexts: "my_own_file_contexts",
4189 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004190
Jooyung Han580eb4f2020-06-24 19:33:06 +09004191 apex_key {
4192 name: "myapex.key",
4193 public_key: "testkey.avbpubkey",
4194 private_key: "testkey.pem",
4195 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004196 `, withFiles(map[string][]byte{
4197 "my_own_file_contexts": nil,
4198 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004199}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004200
Jooyung Han580eb4f2020-06-24 19:33:06 +09004201func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004202 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004203 apex {
4204 name: "myapex",
4205 key: "myapex.key",
4206 product_specific: true,
4207 file_contexts: "product_specific_file_contexts",
4208 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004209
Jooyung Han580eb4f2020-06-24 19:33:06 +09004210 apex_key {
4211 name: "myapex.key",
4212 public_key: "testkey.avbpubkey",
4213 private_key: "testkey.pem",
4214 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004215 `)
4216
Jooyung Han580eb4f2020-06-24 19:33:06 +09004217 ctx, _ := testApex(t, `
4218 apex {
4219 name: "myapex",
4220 key: "myapex.key",
4221 product_specific: true,
4222 file_contexts: "product_specific_file_contexts",
4223 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004224
Jooyung Han580eb4f2020-06-24 19:33:06 +09004225 apex_key {
4226 name: "myapex.key",
4227 public_key: "testkey.avbpubkey",
4228 private_key: "testkey.pem",
4229 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004230 `, withFiles(map[string][]byte{
4231 "product_specific_file_contexts": nil,
4232 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004233 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4234 rule := module.Output("file_contexts")
4235 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4236}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004237
Jooyung Han580eb4f2020-06-24 19:33:06 +09004238func TestFileContexts_SetViaFileGroup(t *testing.T) {
4239 ctx, _ := testApex(t, `
4240 apex {
4241 name: "myapex",
4242 key: "myapex.key",
4243 product_specific: true,
4244 file_contexts: ":my-file-contexts",
4245 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004246
Jooyung Han580eb4f2020-06-24 19:33:06 +09004247 apex_key {
4248 name: "myapex.key",
4249 public_key: "testkey.avbpubkey",
4250 private_key: "testkey.pem",
4251 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004252
Jooyung Han580eb4f2020-06-24 19:33:06 +09004253 filegroup {
4254 name: "my-file-contexts",
4255 srcs: ["product_specific_file_contexts"],
4256 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004257 `, withFiles(map[string][]byte{
4258 "product_specific_file_contexts": nil,
4259 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004260 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4261 rule := module.Output("file_contexts")
4262 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004263}
4264
Jiyong Park67882562019-03-21 01:11:21 +09004265func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004266 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004267 apex_key {
4268 name: "myapex.key",
4269 public_key: ":my.avbpubkey",
4270 private_key: ":my.pem",
4271 product_specific: true,
4272 }
4273
4274 filegroup {
4275 name: "my.avbpubkey",
4276 srcs: ["testkey2.avbpubkey"],
4277 }
4278
4279 filegroup {
4280 name: "my.pem",
4281 srcs: ["testkey2.pem"],
4282 }
4283 `)
4284
4285 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4286 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004287 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004288 if actual_pubkey != expected_pubkey {
4289 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4290 }
4291 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004292 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004293 if actual_privkey != expected_privkey {
4294 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4295 }
4296}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004297
4298func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004299 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004300 prebuilt_apex {
4301 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004302 arch: {
4303 arm64: {
4304 src: "myapex-arm64.apex",
4305 },
4306 arm: {
4307 src: "myapex-arm.apex",
4308 },
4309 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004310 }
4311 `)
4312
4313 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4314
Jiyong Parkc95714e2019-03-29 14:23:10 +09004315 expectedInput := "myapex-arm64.apex"
4316 if prebuilt.inputApex.String() != expectedInput {
4317 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4318 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004319}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004320
4321func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004322 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004323 prebuilt_apex {
4324 name: "myapex",
4325 src: "myapex-arm.apex",
4326 filename: "notmyapex.apex",
4327 }
4328 `)
4329
4330 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4331
4332 expected := "notmyapex.apex"
4333 if p.installFilename != expected {
4334 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4335 }
4336}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004337
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004338func TestPrebuiltOverrides(t *testing.T) {
4339 ctx, config := testApex(t, `
4340 prebuilt_apex {
4341 name: "myapex.prebuilt",
4342 src: "myapex-arm.apex",
4343 overrides: [
4344 "myapex",
4345 ],
4346 }
4347 `)
4348
4349 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4350
4351 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09004352 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004353 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004354 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004355 }
4356}
4357
Paul Duffin092153d2021-01-26 11:42:39 +00004358// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4359// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004360func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4361 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004362 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004363 }
4364
Paul Duffin89886cb2021-02-05 16:44:03 +00004365 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004366 // Make sure the import has been given the correct path to the dex jar.
4367 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.Dependency)
4368 dexJarBuildPath := p.DexJarBuildPath()
4369 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
4370 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4371 }
4372 }
4373
Paul Duffin89886cb2021-02-05 16:44:03 +00004374 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004375 // Make sure that an apex variant is not created for the source module.
4376 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests("libfoo"); !reflect.DeepEqual(expected, actual) {
4377 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4378 }
4379 }
4380
4381 t.Run("prebuilt only", func(t *testing.T) {
4382 bp := `
4383 prebuilt_apex {
4384 name: "myapex",
4385 arch: {
4386 arm64: {
4387 src: "myapex-arm64.apex",
4388 },
4389 arm: {
4390 src: "myapex-arm.apex",
4391 },
4392 },
4393 exported_java_libs: ["libfoo"],
4394 }
4395
4396 java_import {
4397 name: "libfoo",
4398 jars: ["libfoo.jar"],
4399 }
4400 `
4401
4402 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4403 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4404
Paul Duffin89886cb2021-02-05 16:44:03 +00004405 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00004406 })
4407
4408 t.Run("prebuilt with source preferred", func(t *testing.T) {
4409
4410 bp := `
4411 prebuilt_apex {
4412 name: "myapex",
4413 arch: {
4414 arm64: {
4415 src: "myapex-arm64.apex",
4416 },
4417 arm: {
4418 src: "myapex-arm.apex",
4419 },
4420 },
4421 exported_java_libs: ["libfoo"],
4422 }
4423
4424 java_import {
4425 name: "libfoo",
4426 jars: ["libfoo.jar"],
4427 }
4428
4429 java_library {
4430 name: "libfoo",
4431 }
4432 `
4433
4434 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4435 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4436
Paul Duffin89886cb2021-02-05 16:44:03 +00004437 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
4438 ensureNoSourceVariant(t, ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +00004439 })
4440
4441 t.Run("prebuilt preferred with source", func(t *testing.T) {
4442 bp := `
4443 prebuilt_apex {
4444 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004445 arch: {
4446 arm64: {
4447 src: "myapex-arm64.apex",
4448 },
4449 arm: {
4450 src: "myapex-arm.apex",
4451 },
4452 },
4453 exported_java_libs: ["libfoo"],
4454 }
4455
4456 java_import {
4457 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004458 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004459 jars: ["libfoo.jar"],
4460 }
4461
4462 java_library {
4463 name: "libfoo",
4464 }
4465 `
4466
4467 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4468 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4469
Paul Duffin89886cb2021-02-05 16:44:03 +00004470 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
4471 ensureNoSourceVariant(t, ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +00004472 })
4473}
4474
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004475func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4476 transform := func(config *dexpreopt.GlobalConfig) {
4477 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
4478 }
4479
Paul Duffin89886cb2021-02-05 16:44:03 +00004480 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, bootDexJarPath string) {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004481 s := ctx.SingletonForTests("dex_bootjars")
4482 foundLibfooJar := false
4483 for _, output := range s.AllOutputs() {
4484 if strings.HasSuffix(output, "/libfoo.jar") {
4485 foundLibfooJar = true
4486 buildRule := s.Output(output)
4487 actual := android.NormalizePathForTesting(buildRule.Input)
4488 if actual != bootDexJarPath {
4489 t.Errorf("Incorrect boot dex jar path '%s', expected '%s'", actual, bootDexJarPath)
4490 }
4491 }
4492 }
4493 if !foundLibfooJar {
4494 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs")
4495 }
4496 }
4497
4498 t.Run("prebuilt only", func(t *testing.T) {
4499 bp := `
4500 prebuilt_apex {
4501 name: "myapex",
4502 arch: {
4503 arm64: {
4504 src: "myapex-arm64.apex",
4505 },
4506 arm: {
4507 src: "myapex-arm.apex",
4508 },
4509 },
4510 exported_java_libs: ["libfoo"],
4511 }
4512
4513 java_import {
4514 name: "libfoo",
4515 jars: ["libfoo.jar"],
4516 apex_available: ["myapex"],
4517 }
4518 `
4519
4520 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin89886cb2021-02-05 16:44:03 +00004521 checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004522 })
4523
4524 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4525 bp := `
4526 prebuilt_apex {
4527 name: "myapex",
4528 arch: {
4529 arm64: {
4530 src: "myapex-arm64.apex",
4531 },
4532 arm: {
4533 src: "myapex-arm.apex",
4534 },
4535 },
4536 exported_java_libs: ["libfoo"],
4537 }
4538
4539 java_import {
4540 name: "libfoo",
4541 jars: ["libfoo.jar"],
4542 apex_available: ["myapex"],
4543 }
4544
4545 java_library {
4546 name: "libfoo",
4547 srcs: ["foo/bar/MyClass.java"],
4548 apex_available: ["myapex"],
4549 }
4550 `
4551
4552 // In this test the source (java_library) libfoo is active since the
4553 // prebuilt (java_import) defaults to prefer:false. However the
4554 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4555 // find the dex boot jar in it. We either need to disable the source libfoo
4556 // or make the prebuilt libfoo preferred.
4557 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4558 })
4559
4560 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4561 bp := `
4562 prebuilt_apex {
4563 name: "myapex",
4564 arch: {
4565 arm64: {
4566 src: "myapex-arm64.apex",
4567 },
4568 arm: {
4569 src: "myapex-arm.apex",
4570 },
4571 },
4572 exported_java_libs: ["libfoo"],
4573 }
4574
4575 java_import {
4576 name: "libfoo",
4577 prefer: true,
4578 jars: ["libfoo.jar"],
4579 apex_available: ["myapex"],
4580 }
4581
4582 java_library {
4583 name: "libfoo",
4584 srcs: ["foo/bar/MyClass.java"],
4585 apex_available: ["myapex"],
4586 }
4587 `
4588
4589 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin89886cb2021-02-05 16:44:03 +00004590 checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004591 })
4592
4593 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4594 bp := `
4595 apex {
4596 name: "myapex",
4597 key: "myapex.key",
4598 java_libs: ["libfoo"],
4599 }
4600
4601 apex_key {
4602 name: "myapex.key",
4603 public_key: "testkey.avbpubkey",
4604 private_key: "testkey.pem",
4605 }
4606
4607 prebuilt_apex {
4608 name: "myapex",
4609 arch: {
4610 arm64: {
4611 src: "myapex-arm64.apex",
4612 },
4613 arm: {
4614 src: "myapex-arm.apex",
4615 },
4616 },
4617 exported_java_libs: ["libfoo"],
4618 }
4619
4620 java_import {
4621 name: "libfoo",
4622 jars: ["libfoo.jar"],
4623 apex_available: ["myapex"],
4624 }
4625
4626 java_library {
4627 name: "libfoo",
4628 srcs: ["foo/bar/MyClass.java"],
4629 apex_available: ["myapex"],
4630 }
4631 `
4632
4633 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffinf38931c2021-02-05 16:58:28 +00004634 checkBootDexJarPath(t, ctx, ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004635 })
4636
4637 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4638 bp := `
4639 apex {
4640 name: "myapex",
4641 enabled: false,
4642 key: "myapex.key",
4643 java_libs: ["libfoo"],
4644 }
4645
4646 apex_key {
4647 name: "myapex.key",
4648 public_key: "testkey.avbpubkey",
4649 private_key: "testkey.pem",
4650 }
4651
4652 prebuilt_apex {
4653 name: "myapex",
4654 arch: {
4655 arm64: {
4656 src: "myapex-arm64.apex",
4657 },
4658 arm: {
4659 src: "myapex-arm.apex",
4660 },
4661 },
4662 exported_java_libs: ["libfoo"],
4663 }
4664
4665 java_import {
4666 name: "libfoo",
4667 prefer: true,
4668 jars: ["libfoo.jar"],
4669 apex_available: ["myapex"],
4670 }
4671
4672 java_library {
4673 name: "libfoo",
4674 srcs: ["foo/bar/MyClass.java"],
4675 apex_available: ["myapex"],
4676 }
4677 `
4678
4679 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin89886cb2021-02-05 16:44:03 +00004680 checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004681 })
4682}
4683
Roland Levillain630846d2019-06-26 12:48:34 +01004684func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01004685 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004686 apex_test {
4687 name: "myapex",
4688 key: "myapex.key",
4689 tests: [
4690 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004691 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004692 ],
4693 }
4694
4695 apex_key {
4696 name: "myapex.key",
4697 public_key: "testkey.avbpubkey",
4698 private_key: "testkey.pem",
4699 }
4700
Liz Kammer1c14a212020-05-12 15:26:55 -07004701 filegroup {
4702 name: "fg",
4703 srcs: [
4704 "baz",
4705 "bar/baz"
4706 ],
4707 }
4708
Roland Levillain630846d2019-06-26 12:48:34 +01004709 cc_test {
4710 name: "mytest",
4711 gtest: false,
4712 srcs: ["mytest.cpp"],
4713 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004714 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004715 system_shared_libs: [],
4716 static_executable: true,
4717 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004718 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004719 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004720
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004721 cc_library {
4722 name: "mylib",
4723 srcs: ["mylib.cpp"],
4724 system_shared_libs: [],
4725 stl: "none",
4726 }
4727
Liz Kammer5bd365f2020-05-27 15:15:11 -07004728 filegroup {
4729 name: "fg2",
4730 srcs: [
4731 "testdata/baz"
4732 ],
4733 }
4734
Roland Levillain9b5fde92019-06-28 15:41:19 +01004735 cc_test {
4736 name: "mytests",
4737 gtest: false,
4738 srcs: [
4739 "mytest1.cpp",
4740 "mytest2.cpp",
4741 "mytest3.cpp",
4742 ],
4743 test_per_src: true,
4744 relative_install_path: "test",
4745 system_shared_libs: [],
4746 static_executable: true,
4747 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004748 data: [
4749 ":fg",
4750 ":fg2",
4751 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004752 }
Roland Levillain630846d2019-06-26 12:48:34 +01004753 `)
4754
Sundong Ahnabb64432019-10-22 13:58:29 +09004755 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004756 copyCmds := apexRule.Args["copy_commands"]
4757
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004758 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004759 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004760 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004761
Liz Kammer1c14a212020-05-12 15:26:55 -07004762 //Ensure that test data are copied into apex.
4763 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4764 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4765
Roland Levillain9b5fde92019-06-28 15:41:19 +01004766 // Ensure that test deps built with `test_per_src` are copied into apex.
4767 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4768 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4769 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004770
4771 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004772 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4773 data := android.AndroidMkDataForTest(t, config, "", bundle)
4774 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004775 prefix := "TARGET_"
4776 var builder strings.Builder
4777 data.Custom(&builder, name, prefix, "", data)
4778 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004779 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4780 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4781 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4782 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004783 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004784 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004785 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004786
4787 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4788 data = android.AndroidMkDataForTest(t, config, "", flatBundle)
4789 data.Custom(&builder, name, prefix, "", data)
4790 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004791 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4792 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004793}
4794
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004795func TestInstallExtraFlattenedApexes(t *testing.T) {
4796 ctx, config := testApex(t, `
4797 apex {
4798 name: "myapex",
4799 key: "myapex.key",
4800 }
4801 apex_key {
4802 name: "myapex.key",
4803 public_key: "testkey.avbpubkey",
4804 private_key: "testkey.pem",
4805 }
4806 `, func(fs map[string][]byte, config android.Config) {
4807 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4808 })
4809 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004810 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004811 mk := android.AndroidMkDataForTest(t, config, "", ab)
4812 var builder strings.Builder
4813 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4814 androidMk := builder.String()
4815 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4816}
4817
Jooyung Hand48f3c32019-08-23 11:18:57 +09004818func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4819 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4820 apex {
4821 name: "myapex",
4822 key: "myapex.key",
4823 native_shared_libs: ["libfoo"],
4824 }
4825
4826 apex_key {
4827 name: "myapex.key",
4828 public_key: "testkey.avbpubkey",
4829 private_key: "testkey.pem",
4830 }
4831
4832 cc_library {
4833 name: "libfoo",
4834 stl: "none",
4835 system_shared_libs: [],
4836 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004837 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004838 }
4839 `)
4840 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4841 apex {
4842 name: "myapex",
4843 key: "myapex.key",
4844 java_libs: ["myjar"],
4845 }
4846
4847 apex_key {
4848 name: "myapex.key",
4849 public_key: "testkey.avbpubkey",
4850 private_key: "testkey.pem",
4851 }
4852
4853 java_library {
4854 name: "myjar",
4855 srcs: ["foo/bar/MyClass.java"],
4856 sdk_version: "none",
4857 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004858 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004859 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004860 }
4861 `)
4862}
4863
Bill Peckhama41a6962021-01-11 10:58:54 -08004864func TestApexWithJavaImport(t *testing.T) {
4865 ctx, _ := testApex(t, `
4866 apex {
4867 name: "myapex",
4868 key: "myapex.key",
4869 java_libs: ["myjavaimport"],
4870 }
4871
4872 apex_key {
4873 name: "myapex.key",
4874 public_key: "testkey.avbpubkey",
4875 private_key: "testkey.pem",
4876 }
4877
4878 java_import {
4879 name: "myjavaimport",
4880 apex_available: ["myapex"],
4881 jars: ["my.jar"],
4882 compile_dex: true,
4883 }
4884 `)
4885
4886 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4887 apexRule := module.Rule("apexRule")
4888 copyCmds := apexRule.Args["copy_commands"]
4889 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
4890}
4891
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004892func TestApexWithApps(t *testing.T) {
4893 ctx, _ := testApex(t, `
4894 apex {
4895 name: "myapex",
4896 key: "myapex.key",
4897 apps: [
4898 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09004899 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004900 ],
4901 }
4902
4903 apex_key {
4904 name: "myapex.key",
4905 public_key: "testkey.avbpubkey",
4906 private_key: "testkey.pem",
4907 }
4908
4909 android_app {
4910 name: "AppFoo",
4911 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004912 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004913 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09004914 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08004915 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004916 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004917 }
Jiyong Parkf7487312019-10-17 12:54:30 +09004918
4919 android_app {
4920 name: "AppFooPriv",
4921 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004922 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09004923 system_modules: "none",
4924 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08004925 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004926 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09004927 }
Jiyong Park8be103b2019-11-08 15:53:48 +09004928
4929 cc_library_shared {
4930 name: "libjni",
4931 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004932 shared_libs: ["libfoo"],
4933 stl: "none",
4934 system_shared_libs: [],
4935 apex_available: [ "myapex" ],
4936 sdk_version: "current",
4937 }
4938
4939 cc_library_shared {
4940 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09004941 stl: "none",
4942 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09004943 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08004944 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09004945 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004946 `)
4947
Sundong Ahnabb64432019-10-22 13:58:29 +09004948 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004949 apexRule := module.Rule("apexRule")
4950 copyCmds := apexRule.Args["copy_commands"]
4951
4952 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09004953 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004954
Colin Crossaede88c2020-08-11 12:17:01 -07004955 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004956 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09004957 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004958 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004959 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004960 // JNI libraries including transitive deps are
4961 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07004962 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004963 // ... embedded inside APK (jnilibs.zip)
4964 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
4965 // ... and not directly inside the APEX
4966 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
4967 }
Dario Frenicde2a032019-10-27 00:29:22 +01004968}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004969
Dario Frenicde2a032019-10-27 00:29:22 +01004970func TestApexWithAppImports(t *testing.T) {
4971 ctx, _ := testApex(t, `
4972 apex {
4973 name: "myapex",
4974 key: "myapex.key",
4975 apps: [
4976 "AppFooPrebuilt",
4977 "AppFooPrivPrebuilt",
4978 ],
4979 }
4980
4981 apex_key {
4982 name: "myapex.key",
4983 public_key: "testkey.avbpubkey",
4984 private_key: "testkey.pem",
4985 }
4986
4987 android_app_import {
4988 name: "AppFooPrebuilt",
4989 apk: "PrebuiltAppFoo.apk",
4990 presigned: true,
4991 dex_preopt: {
4992 enabled: false,
4993 },
Jiyong Park592a6a42020-04-21 22:34:28 +09004994 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01004995 }
4996
4997 android_app_import {
4998 name: "AppFooPrivPrebuilt",
4999 apk: "PrebuiltAppFooPriv.apk",
5000 privileged: true,
5001 presigned: true,
5002 dex_preopt: {
5003 enabled: false,
5004 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005005 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005006 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005007 }
5008 `)
5009
Sundong Ahnabb64432019-10-22 13:58:29 +09005010 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005011 apexRule := module.Rule("apexRule")
5012 copyCmds := apexRule.Args["copy_commands"]
5013
5014 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005015 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5016}
5017
5018func TestApexWithAppImportsPrefer(t *testing.T) {
5019 ctx, _ := testApex(t, `
5020 apex {
5021 name: "myapex",
5022 key: "myapex.key",
5023 apps: [
5024 "AppFoo",
5025 ],
5026 }
5027
5028 apex_key {
5029 name: "myapex.key",
5030 public_key: "testkey.avbpubkey",
5031 private_key: "testkey.pem",
5032 }
5033
5034 android_app {
5035 name: "AppFoo",
5036 srcs: ["foo/bar/MyClass.java"],
5037 sdk_version: "none",
5038 system_modules: "none",
5039 apex_available: [ "myapex" ],
5040 }
5041
5042 android_app_import {
5043 name: "AppFoo",
5044 apk: "AppFooPrebuilt.apk",
5045 filename: "AppFooPrebuilt.apk",
5046 presigned: true,
5047 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005048 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005049 }
5050 `, withFiles(map[string][]byte{
5051 "AppFooPrebuilt.apk": nil,
5052 }))
5053
5054 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5055 "app/AppFoo/AppFooPrebuilt.apk",
5056 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005057}
5058
Dario Freni6f3937c2019-12-20 22:58:03 +00005059func TestApexWithTestHelperApp(t *testing.T) {
5060 ctx, _ := testApex(t, `
5061 apex {
5062 name: "myapex",
5063 key: "myapex.key",
5064 apps: [
5065 "TesterHelpAppFoo",
5066 ],
5067 }
5068
5069 apex_key {
5070 name: "myapex.key",
5071 public_key: "testkey.avbpubkey",
5072 private_key: "testkey.pem",
5073 }
5074
5075 android_test_helper_app {
5076 name: "TesterHelpAppFoo",
5077 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005078 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005079 }
5080
5081 `)
5082
5083 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5084 apexRule := module.Rule("apexRule")
5085 copyCmds := apexRule.Args["copy_commands"]
5086
5087 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5088}
5089
Jooyung Han18020ea2019-11-13 10:50:48 +09005090func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5091 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005092 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005093 apex {
5094 name: "myapex",
5095 key: "myapex.key",
5096 native_shared_libs: ["libfoo"],
5097 }
5098
5099 apex_key {
5100 name: "myapex.key",
5101 public_key: "testkey.avbpubkey",
5102 private_key: "testkey.pem",
5103 }
5104
5105 apex {
5106 name: "otherapex",
5107 key: "myapex.key",
5108 native_shared_libs: ["libfoo"],
5109 }
5110
5111 cc_defaults {
5112 name: "libfoo-defaults",
5113 apex_available: ["otherapex"],
5114 }
5115
5116 cc_library {
5117 name: "libfoo",
5118 defaults: ["libfoo-defaults"],
5119 stl: "none",
5120 system_shared_libs: [],
5121 }`)
5122}
5123
Paul Duffine52e66f2020-03-30 17:54:29 +01005124func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005125 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005126 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005127 apex {
5128 name: "myapex",
5129 key: "myapex.key",
5130 native_shared_libs: ["libfoo"],
5131 }
5132
5133 apex_key {
5134 name: "myapex.key",
5135 public_key: "testkey.avbpubkey",
5136 private_key: "testkey.pem",
5137 }
5138
5139 apex {
5140 name: "otherapex",
5141 key: "otherapex.key",
5142 native_shared_libs: ["libfoo"],
5143 }
5144
5145 apex_key {
5146 name: "otherapex.key",
5147 public_key: "testkey.avbpubkey",
5148 private_key: "testkey.pem",
5149 }
5150
5151 cc_library {
5152 name: "libfoo",
5153 stl: "none",
5154 system_shared_libs: [],
5155 apex_available: ["otherapex"],
5156 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005157}
Jiyong Park127b40b2019-09-30 16:04:35 +09005158
Paul Duffine52e66f2020-03-30 17:54:29 +01005159func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005160 // libbbaz is an indirect dep
Steven Moreland6e36cd62020-10-22 01:08:35 +00005161 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'. Dependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005162.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005163.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005164.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005165.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005166.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005167.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005168 apex {
5169 name: "myapex",
5170 key: "myapex.key",
5171 native_shared_libs: ["libfoo"],
5172 }
5173
5174 apex_key {
5175 name: "myapex.key",
5176 public_key: "testkey.avbpubkey",
5177 private_key: "testkey.pem",
5178 }
5179
Jiyong Park127b40b2019-09-30 16:04:35 +09005180 cc_library {
5181 name: "libfoo",
5182 stl: "none",
5183 shared_libs: ["libbar"],
5184 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005185 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005186 }
5187
5188 cc_library {
5189 name: "libbar",
5190 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005191 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005192 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005193 apex_available: ["myapex"],
5194 }
5195
5196 cc_library {
5197 name: "libbaz",
5198 stl: "none",
5199 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005200 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005201}
Jiyong Park127b40b2019-09-30 16:04:35 +09005202
Paul Duffine52e66f2020-03-30 17:54:29 +01005203func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005204 testApexError(t, "\"otherapex\" is not a valid module name", `
5205 apex {
5206 name: "myapex",
5207 key: "myapex.key",
5208 native_shared_libs: ["libfoo"],
5209 }
5210
5211 apex_key {
5212 name: "myapex.key",
5213 public_key: "testkey.avbpubkey",
5214 private_key: "testkey.pem",
5215 }
5216
5217 cc_library {
5218 name: "libfoo",
5219 stl: "none",
5220 system_shared_libs: [],
5221 apex_available: ["otherapex"],
5222 }`)
5223
Paul Duffine52e66f2020-03-30 17:54:29 +01005224 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005225 apex {
5226 name: "myapex",
5227 key: "myapex.key",
5228 native_shared_libs: ["libfoo", "libbar"],
5229 }
5230
5231 apex_key {
5232 name: "myapex.key",
5233 public_key: "testkey.avbpubkey",
5234 private_key: "testkey.pem",
5235 }
5236
5237 cc_library {
5238 name: "libfoo",
5239 stl: "none",
5240 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005241 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005242 apex_available: ["myapex"],
5243 }
5244
5245 cc_library {
5246 name: "libbar",
5247 stl: "none",
5248 system_shared_libs: [],
5249 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005250 }
5251
5252 cc_library {
5253 name: "libbaz",
5254 stl: "none",
5255 system_shared_libs: [],
5256 stubs: {
5257 versions: ["10", "20", "30"],
5258 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005259 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005260}
Jiyong Park127b40b2019-09-30 16:04:35 +09005261
Jiyong Park89e850a2020-04-07 16:37:39 +09005262func TestApexAvailable_CheckForPlatform(t *testing.T) {
Paul Duffine52e66f2020-03-30 17:54:29 +01005263 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005264 apex {
5265 name: "myapex",
5266 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005267 native_shared_libs: ["libbar", "libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005268 }
5269
5270 apex_key {
5271 name: "myapex.key",
5272 public_key: "testkey.avbpubkey",
5273 private_key: "testkey.pem",
5274 }
5275
5276 cc_library {
5277 name: "libfoo",
5278 stl: "none",
5279 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005280 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005281 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005282 }
5283
5284 cc_library {
5285 name: "libfoo2",
5286 stl: "none",
5287 system_shared_libs: [],
5288 shared_libs: ["libbaz"],
5289 apex_available: ["//apex_available:platform"],
5290 }
5291
5292 cc_library {
5293 name: "libbar",
5294 stl: "none",
5295 system_shared_libs: [],
5296 apex_available: ["myapex"],
5297 }
5298
5299 cc_library {
5300 name: "libbaz",
5301 stl: "none",
5302 system_shared_libs: [],
5303 apex_available: ["myapex"],
5304 stubs: {
5305 versions: ["1"],
5306 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005307 }`)
5308
Jiyong Park89e850a2020-04-07 16:37:39 +09005309 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5310 // because it depends on libbar which isn't available to platform
5311 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5312 if libfoo.NotAvailableForPlatform() != true {
5313 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5314 }
5315
5316 // libfoo2 however can be available to platform because it depends on libbaz which provides
5317 // stubs
5318 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5319 if libfoo2.NotAvailableForPlatform() == true {
5320 t.Errorf("%q should be available to platform", libfoo2.String())
5321 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005322}
Jiyong Parka90ca002019-10-07 15:47:24 +09005323
Paul Duffine52e66f2020-03-30 17:54:29 +01005324func TestApexAvailable_CreatedForApex(t *testing.T) {
Jiyong Park89e850a2020-04-07 16:37:39 +09005325 ctx, _ := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005326 apex {
5327 name: "myapex",
5328 key: "myapex.key",
5329 native_shared_libs: ["libfoo"],
5330 }
5331
5332 apex_key {
5333 name: "myapex.key",
5334 public_key: "testkey.avbpubkey",
5335 private_key: "testkey.pem",
5336 }
5337
5338 cc_library {
5339 name: "libfoo",
5340 stl: "none",
5341 system_shared_libs: [],
5342 apex_available: ["myapex"],
5343 static: {
5344 apex_available: ["//apex_available:platform"],
5345 },
5346 }`)
5347
Jiyong Park89e850a2020-04-07 16:37:39 +09005348 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5349 if libfooShared.NotAvailableForPlatform() != true {
5350 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5351 }
5352 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5353 if libfooStatic.NotAvailableForPlatform() != false {
5354 t.Errorf("%q should be available to platform", libfooStatic.String())
5355 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005356}
5357
Jiyong Park5d790c32019-11-15 18:40:32 +09005358func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005359 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005360 apex {
5361 name: "myapex",
5362 key: "myapex.key",
5363 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005364 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09005365 }
5366
5367 override_apex {
5368 name: "override_myapex",
5369 base: "myapex",
5370 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005371 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005372 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005373 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005374 }
5375
5376 apex_key {
5377 name: "myapex.key",
5378 public_key: "testkey.avbpubkey",
5379 private_key: "testkey.pem",
5380 }
5381
5382 android_app {
5383 name: "app",
5384 srcs: ["foo/bar/MyClass.java"],
5385 package_name: "foo",
5386 sdk_version: "none",
5387 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005388 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005389 }
5390
5391 override_android_app {
5392 name: "override_app",
5393 base: "app",
5394 package_name: "bar",
5395 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005396 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005397
Jiyong Park317645e2019-12-05 13:20:58 +09005398 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5399 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5400 if originalVariant.GetOverriddenBy() != "" {
5401 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5402 }
5403 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5404 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5405 }
5406
Jiyong Park5d790c32019-11-15 18:40:32 +09005407 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5408 apexRule := module.Rule("apexRule")
5409 copyCmds := apexRule.Args["copy_commands"]
5410
5411 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005412 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005413
5414 apexBundle := module.Module().(*apexBundle)
5415 name := apexBundle.Name()
5416 if name != "override_myapex" {
5417 t.Errorf("name should be \"override_myapex\", but was %q", name)
5418 }
5419
Baligh Uddin004d7172020-02-19 21:29:28 -08005420 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5421 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5422 }
5423
Jiyong Park20bacab2020-03-03 11:45:41 +09005424 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005425 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005426
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005427 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
5428 var builder strings.Builder
5429 data.Custom(&builder, name, "TARGET_", "", data)
5430 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005431 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005432 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5433 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005434 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005435 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005436 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005437 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5438 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005439}
5440
Jooyung Han214bf372019-11-12 13:03:50 +09005441func TestLegacyAndroid10Support(t *testing.T) {
5442 ctx, _ := testApex(t, `
5443 apex {
5444 name: "myapex",
5445 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005446 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005447 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005448 }
5449
5450 apex_key {
5451 name: "myapex.key",
5452 public_key: "testkey.avbpubkey",
5453 private_key: "testkey.pem",
5454 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005455
5456 cc_library {
5457 name: "mylib",
5458 srcs: ["mylib.cpp"],
5459 stl: "libc++",
5460 system_shared_libs: [],
5461 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005462 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005463 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005464 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005465
5466 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5467 args := module.Rule("apexRule").Args
5468 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005469 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005470
5471 // The copies of the libraries in the apex should have one more dependency than
5472 // the ones outside the apex, namely the unwinder. Ideally we should check
5473 // the dependency names directly here but for some reason the names are blank in
5474 // this test.
5475 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005476 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005477 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5478 if len(apexImplicits) != len(nonApexImplicits)+1 {
5479 t.Errorf("%q missing unwinder dep", lib)
5480 }
5481 }
Jooyung Han214bf372019-11-12 13:03:50 +09005482}
5483
Paul Duffin9b879592020-05-26 13:21:35 +01005484var filesForSdkLibrary = map[string][]byte{
5485 "api/current.txt": nil,
5486 "api/removed.txt": nil,
5487 "api/system-current.txt": nil,
5488 "api/system-removed.txt": nil,
5489 "api/test-current.txt": nil,
5490 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005491
Anton Hanssondff2c782020-12-21 17:10:01 +00005492 "100/public/api/foo.txt": nil,
5493 "100/public/api/foo-removed.txt": nil,
5494 "100/system/api/foo.txt": nil,
5495 "100/system/api/foo-removed.txt": nil,
5496
Paul Duffineedc5d52020-06-12 17:46:39 +01005497 // For java_sdk_library_import
5498 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005499}
5500
Jooyung Han58f26ab2019-12-18 15:34:32 +09005501func TestJavaSDKLibrary(t *testing.T) {
5502 ctx, _ := testApex(t, `
5503 apex {
5504 name: "myapex",
5505 key: "myapex.key",
5506 java_libs: ["foo"],
5507 }
5508
5509 apex_key {
5510 name: "myapex.key",
5511 public_key: "testkey.avbpubkey",
5512 private_key: "testkey.pem",
5513 }
5514
5515 java_sdk_library {
5516 name: "foo",
5517 srcs: ["a.java"],
5518 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005519 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005520 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005521
5522 prebuilt_apis {
5523 name: "sdk",
5524 api_dirs: ["100"],
5525 }
Paul Duffin9b879592020-05-26 13:21:35 +01005526 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005527
5528 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005529 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005530 "javalib/foo.jar",
5531 "etc/permissions/foo.xml",
5532 })
5533 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005534 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5535 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005536}
5537
Paul Duffin9b879592020-05-26 13:21:35 +01005538func TestJavaSDKLibrary_WithinApex(t *testing.T) {
5539 ctx, _ := testApex(t, `
5540 apex {
5541 name: "myapex",
5542 key: "myapex.key",
5543 java_libs: ["foo", "bar"],
5544 }
5545
5546 apex_key {
5547 name: "myapex.key",
5548 public_key: "testkey.avbpubkey",
5549 private_key: "testkey.pem",
5550 }
5551
5552 java_sdk_library {
5553 name: "foo",
5554 srcs: ["a.java"],
5555 api_packages: ["foo"],
5556 apex_available: ["myapex"],
5557 sdk_version: "none",
5558 system_modules: "none",
5559 }
5560
5561 java_library {
5562 name: "bar",
5563 srcs: ["a.java"],
5564 libs: ["foo"],
5565 apex_available: ["myapex"],
5566 sdk_version: "none",
5567 system_modules: "none",
5568 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005569
5570 prebuilt_apis {
5571 name: "sdk",
5572 api_dirs: ["100"],
5573 }
Paul Duffin9b879592020-05-26 13:21:35 +01005574 `, withFiles(filesForSdkLibrary))
5575
5576 // java_sdk_library installs both impl jar and permission XML
5577 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5578 "javalib/bar.jar",
5579 "javalib/foo.jar",
5580 "etc/permissions/foo.xml",
5581 })
5582
5583 // The bar library should depend on the implementation jar.
5584 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5585 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5586 t.Errorf("expected %q, found %#q", expected, actual)
5587 }
5588}
5589
5590func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
5591 ctx, _ := testApex(t, `
5592 apex {
5593 name: "myapex",
5594 key: "myapex.key",
5595 java_libs: ["foo"],
5596 }
5597
5598 apex_key {
5599 name: "myapex.key",
5600 public_key: "testkey.avbpubkey",
5601 private_key: "testkey.pem",
5602 }
5603
5604 java_sdk_library {
5605 name: "foo",
5606 srcs: ["a.java"],
5607 api_packages: ["foo"],
5608 apex_available: ["myapex"],
5609 sdk_version: "none",
5610 system_modules: "none",
5611 }
5612
5613 java_library {
5614 name: "bar",
5615 srcs: ["a.java"],
5616 libs: ["foo"],
5617 sdk_version: "none",
5618 system_modules: "none",
5619 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005620
5621 prebuilt_apis {
5622 name: "sdk",
5623 api_dirs: ["100"],
5624 }
Paul Duffin9b879592020-05-26 13:21:35 +01005625 `, withFiles(filesForSdkLibrary))
5626
5627 // java_sdk_library installs both impl jar and permission XML
5628 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5629 "javalib/foo.jar",
5630 "etc/permissions/foo.xml",
5631 })
5632
5633 // The bar library should depend on the stubs jar.
5634 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5635 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5636 t.Errorf("expected %q, found %#q", expected, actual)
5637 }
5638}
5639
Paul Duffineedc5d52020-06-12 17:46:39 +01005640func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Anton Hanssondff2c782020-12-21 17:10:01 +00005641 ctx, _ := testApex(t, `
5642 prebuilt_apis {
5643 name: "sdk",
5644 api_dirs: ["100"],
5645 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005646 withFiles(map[string][]byte{
5647 "apex/a.java": nil,
5648 "apex/apex_manifest.json": nil,
5649 "apex/Android.bp": []byte(`
5650 package {
5651 default_visibility: ["//visibility:private"],
5652 }
5653
5654 apex {
5655 name: "myapex",
5656 key: "myapex.key",
5657 java_libs: ["foo", "bar"],
5658 }
5659
5660 apex_key {
5661 name: "myapex.key",
5662 public_key: "testkey.avbpubkey",
5663 private_key: "testkey.pem",
5664 }
5665
5666 java_library {
5667 name: "bar",
5668 srcs: ["a.java"],
5669 libs: ["foo"],
5670 apex_available: ["myapex"],
5671 sdk_version: "none",
5672 system_modules: "none",
5673 }
5674`),
5675 "source/a.java": nil,
5676 "source/api/current.txt": nil,
5677 "source/api/removed.txt": nil,
5678 "source/Android.bp": []byte(`
5679 package {
5680 default_visibility: ["//visibility:private"],
5681 }
5682
5683 java_sdk_library {
5684 name: "foo",
5685 visibility: ["//apex"],
5686 srcs: ["a.java"],
5687 api_packages: ["foo"],
5688 apex_available: ["myapex"],
5689 sdk_version: "none",
5690 system_modules: "none",
5691 public: {
5692 enabled: true,
5693 },
5694 }
5695`),
5696 "prebuilt/a.jar": nil,
5697 "prebuilt/Android.bp": []byte(`
5698 package {
5699 default_visibility: ["//visibility:private"],
5700 }
5701
5702 java_sdk_library_import {
5703 name: "foo",
5704 visibility: ["//apex", "//source"],
5705 apex_available: ["myapex"],
5706 prefer: true,
5707 public: {
5708 jars: ["a.jar"],
5709 },
5710 }
5711`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005712 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005713 )
5714
5715 // java_sdk_library installs both impl jar and permission XML
5716 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5717 "javalib/bar.jar",
5718 "javalib/foo.jar",
5719 "etc/permissions/foo.xml",
5720 })
5721
5722 // The bar library should depend on the implementation jar.
5723 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5724 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5725 t.Errorf("expected %q, found %#q", expected, actual)
5726 }
5727}
5728
5729func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5730 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5731 apex {
5732 name: "myapex",
5733 key: "myapex.key",
5734 java_libs: ["foo"],
5735 }
5736
5737 apex_key {
5738 name: "myapex.key",
5739 public_key: "testkey.avbpubkey",
5740 private_key: "testkey.pem",
5741 }
5742
5743 java_sdk_library_import {
5744 name: "foo",
5745 apex_available: ["myapex"],
5746 prefer: true,
5747 public: {
5748 jars: ["a.jar"],
5749 },
5750 }
5751
5752 `, withFiles(filesForSdkLibrary))
5753}
5754
atrost6e126252020-01-27 17:01:16 +00005755func TestCompatConfig(t *testing.T) {
5756 ctx, _ := testApex(t, `
5757 apex {
5758 name: "myapex",
5759 key: "myapex.key",
5760 prebuilts: ["myjar-platform-compat-config"],
5761 java_libs: ["myjar"],
5762 }
5763
5764 apex_key {
5765 name: "myapex.key",
5766 public_key: "testkey.avbpubkey",
5767 private_key: "testkey.pem",
5768 }
5769
5770 platform_compat_config {
5771 name: "myjar-platform-compat-config",
5772 src: ":myjar",
5773 }
5774
5775 java_library {
5776 name: "myjar",
5777 srcs: ["foo/bar/MyClass.java"],
5778 sdk_version: "none",
5779 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005780 apex_available: [ "myapex" ],
5781 }
5782 `)
5783 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5784 "etc/compatconfig/myjar-platform-compat-config.xml",
5785 "javalib/myjar.jar",
5786 })
5787}
5788
Jiyong Park479321d2019-12-16 11:47:12 +09005789func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5790 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5791 apex {
5792 name: "myapex",
5793 key: "myapex.key",
5794 java_libs: ["myjar"],
5795 }
5796
5797 apex_key {
5798 name: "myapex.key",
5799 public_key: "testkey.avbpubkey",
5800 private_key: "testkey.pem",
5801 }
5802
5803 java_library {
5804 name: "myjar",
5805 srcs: ["foo/bar/MyClass.java"],
5806 sdk_version: "none",
5807 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005808 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005809 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005810 }
5811 `)
5812}
5813
Jiyong Park7afd1072019-12-30 16:56:33 +09005814func TestCarryRequiredModuleNames(t *testing.T) {
5815 ctx, config := testApex(t, `
5816 apex {
5817 name: "myapex",
5818 key: "myapex.key",
5819 native_shared_libs: ["mylib"],
5820 }
5821
5822 apex_key {
5823 name: "myapex.key",
5824 public_key: "testkey.avbpubkey",
5825 private_key: "testkey.pem",
5826 }
5827
5828 cc_library {
5829 name: "mylib",
5830 srcs: ["mylib.cpp"],
5831 system_shared_libs: [],
5832 stl: "none",
5833 required: ["a", "b"],
5834 host_required: ["c", "d"],
5835 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005836 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005837 }
5838 `)
5839
5840 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
5841 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
5842 name := apexBundle.BaseModuleName()
5843 prefix := "TARGET_"
5844 var builder strings.Builder
5845 data.Custom(&builder, name, prefix, "", data)
5846 androidMk := builder.String()
5847 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
5848 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
5849 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
5850}
5851
Jiyong Park7cd10e32020-01-14 09:22:18 +09005852func TestSymlinksFromApexToSystem(t *testing.T) {
5853 bp := `
5854 apex {
5855 name: "myapex",
5856 key: "myapex.key",
5857 native_shared_libs: ["mylib"],
5858 java_libs: ["myjar"],
5859 }
5860
Jiyong Park9d677202020-02-19 16:29:35 +09005861 apex {
5862 name: "myapex.updatable",
5863 key: "myapex.key",
5864 native_shared_libs: ["mylib"],
5865 java_libs: ["myjar"],
5866 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09005867 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09005868 }
5869
Jiyong Park7cd10e32020-01-14 09:22:18 +09005870 apex_key {
5871 name: "myapex.key",
5872 public_key: "testkey.avbpubkey",
5873 private_key: "testkey.pem",
5874 }
5875
5876 cc_library {
5877 name: "mylib",
5878 srcs: ["mylib.cpp"],
5879 shared_libs: ["myotherlib"],
5880 system_shared_libs: [],
5881 stl: "none",
5882 apex_available: [
5883 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005884 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005885 "//apex_available:platform",
5886 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005887 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005888 }
5889
5890 cc_library {
5891 name: "myotherlib",
5892 srcs: ["mylib.cpp"],
5893 system_shared_libs: [],
5894 stl: "none",
5895 apex_available: [
5896 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005897 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005898 "//apex_available:platform",
5899 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005900 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005901 }
5902
5903 java_library {
5904 name: "myjar",
5905 srcs: ["foo/bar/MyClass.java"],
5906 sdk_version: "none",
5907 system_modules: "none",
5908 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09005909 apex_available: [
5910 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005911 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005912 "//apex_available:platform",
5913 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005914 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005915 }
5916
5917 java_library {
5918 name: "myotherjar",
5919 srcs: ["foo/bar/MyClass.java"],
5920 sdk_version: "none",
5921 system_modules: "none",
5922 apex_available: [
5923 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005924 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005925 "//apex_available:platform",
5926 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005927 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005928 }
5929 `
5930
5931 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
5932 for _, f := range files {
5933 if f.path == file {
5934 if f.isLink {
5935 t.Errorf("%q is not a real file", file)
5936 }
5937 return
5938 }
5939 }
5940 t.Errorf("%q is not found", file)
5941 }
5942
5943 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
5944 for _, f := range files {
5945 if f.path == file {
5946 if !f.isLink {
5947 t.Errorf("%q is not a symlink", file)
5948 }
5949 return
5950 }
5951 }
5952 t.Errorf("%q is not found", file)
5953 }
5954
Jiyong Park9d677202020-02-19 16:29:35 +09005955 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
5956 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09005957 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005958 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005959 ensureRealfileExists(t, files, "javalib/myjar.jar")
5960 ensureRealfileExists(t, files, "lib64/mylib.so")
5961 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5962
Jiyong Park9d677202020-02-19 16:29:35 +09005963 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5964 ensureRealfileExists(t, files, "javalib/myjar.jar")
5965 ensureRealfileExists(t, files, "lib64/mylib.so")
5966 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5967
5968 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09005969 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005970 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005971 ensureRealfileExists(t, files, "javalib/myjar.jar")
5972 ensureRealfileExists(t, files, "lib64/mylib.so")
5973 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09005974
5975 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5976 ensureRealfileExists(t, files, "javalib/myjar.jar")
5977 ensureRealfileExists(t, files, "lib64/mylib.so")
5978 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09005979}
5980
Yo Chiange8128052020-07-23 20:09:18 +08005981func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
5982 ctx, config := testApex(t, `
5983 apex {
5984 name: "myapex",
5985 key: "myapex.key",
5986 native_shared_libs: ["mylib"],
5987 }
5988
5989 apex_key {
5990 name: "myapex.key",
5991 public_key: "testkey.avbpubkey",
5992 private_key: "testkey.pem",
5993 }
5994
5995 cc_library_shared {
5996 name: "mylib",
5997 srcs: ["mylib.cpp"],
5998 shared_libs: ["myotherlib"],
5999 system_shared_libs: [],
6000 stl: "none",
6001 apex_available: [
6002 "myapex",
6003 "//apex_available:platform",
6004 ],
6005 }
6006
6007 cc_prebuilt_library_shared {
6008 name: "myotherlib",
6009 srcs: ["prebuilt.so"],
6010 system_shared_libs: [],
6011 stl: "none",
6012 apex_available: [
6013 "myapex",
6014 "//apex_available:platform",
6015 ],
6016 }
6017 `)
6018
6019 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
6020 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
6021 var builder strings.Builder
6022 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6023 androidMk := builder.String()
6024 // `myotherlib` is added to `myapex` as symlink
6025 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6026 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6027 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6028 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006029 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 +08006030}
6031
Jooyung Han643adc42020-02-27 13:50:06 +09006032func TestApexWithJniLibs(t *testing.T) {
6033 ctx, _ := testApex(t, `
6034 apex {
6035 name: "myapex",
6036 key: "myapex.key",
6037 jni_libs: ["mylib"],
6038 }
6039
6040 apex_key {
6041 name: "myapex.key",
6042 public_key: "testkey.avbpubkey",
6043 private_key: "testkey.pem",
6044 }
6045
6046 cc_library {
6047 name: "mylib",
6048 srcs: ["mylib.cpp"],
6049 shared_libs: ["mylib2"],
6050 system_shared_libs: [],
6051 stl: "none",
6052 apex_available: [ "myapex" ],
6053 }
6054
6055 cc_library {
6056 name: "mylib2",
6057 srcs: ["mylib.cpp"],
6058 system_shared_libs: [],
6059 stl: "none",
6060 apex_available: [ "myapex" ],
6061 }
6062 `)
6063
6064 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6065 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6066 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6067 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6068 "lib64/mylib.so",
6069 "lib64/mylib2.so",
6070 })
6071}
6072
Jooyung Han49f67012020-04-17 13:43:10 +09006073func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
6074 ctx, _ := testApex(t, `
6075 apex {
6076 name: "myapex",
6077 key: "myapex.key",
6078 }
6079 apex_key {
6080 name: "myapex.key",
6081 public_key: "testkey.avbpubkey",
6082 private_key: "testkey.pem",
6083 }
6084 `, func(fs map[string][]byte, config android.Config) {
6085 delete(config.Targets, android.Android)
6086 config.AndroidCommonTarget = android.Target{}
6087 })
6088
6089 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6090 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6091 }
6092}
6093
Jiyong Parkbd159612020-02-28 15:22:21 +09006094func TestAppBundle(t *testing.T) {
6095 ctx, _ := testApex(t, `
6096 apex {
6097 name: "myapex",
6098 key: "myapex.key",
6099 apps: ["AppFoo"],
6100 }
6101
6102 apex_key {
6103 name: "myapex.key",
6104 public_key: "testkey.avbpubkey",
6105 private_key: "testkey.pem",
6106 }
6107
6108 android_app {
6109 name: "AppFoo",
6110 srcs: ["foo/bar/MyClass.java"],
6111 sdk_version: "none",
6112 system_modules: "none",
6113 apex_available: [ "myapex" ],
6114 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006115 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006116
Colin Crosscf371cc2020-11-13 11:48:42 -08006117 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006118 content := bundleConfigRule.Args["content"]
6119
6120 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006121 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 +09006122}
6123
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006124func TestAppSetBundle(t *testing.T) {
6125 ctx, _ := testApex(t, `
6126 apex {
6127 name: "myapex",
6128 key: "myapex.key",
6129 apps: ["AppSet"],
6130 }
6131
6132 apex_key {
6133 name: "myapex.key",
6134 public_key: "testkey.avbpubkey",
6135 private_key: "testkey.pem",
6136 }
6137
6138 android_app_set {
6139 name: "AppSet",
6140 set: "AppSet.apks",
6141 }`)
6142 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006143 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006144 content := bundleConfigRule.Args["content"]
6145 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6146 s := mod.Rule("apexRule").Args["copy_commands"]
6147 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6148 if len(copyCmds) != 3 {
6149 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6150 }
6151 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6152 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6153 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6154}
6155
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006156func TestAppSetBundlePrebuilt(t *testing.T) {
6157 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
6158 bp := `
6159 apex_set {
6160 name: "myapex",
6161 filename: "foo_v2.apex",
6162 sanitized: {
6163 none: { set: "myapex.apks", },
6164 hwaddress: { set: "myapex.hwasan.apks", },
6165 },
6166 }`
6167 fs["Android.bp"] = []byte(bp)
6168
6169 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
6170 })
6171
6172 m := ctx.ModuleForTests("myapex", "android_common")
6173 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6174
6175 actual := extractedApex.Inputs
6176 if len(actual) != 1 {
6177 t.Errorf("expected a single input")
6178 }
6179
6180 expected := "myapex.hwasan.apks"
6181 if actual[0].String() != expected {
6182 t.Errorf("expected %s, got %s", expected, actual[0].String())
6183 }
6184}
6185
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006186func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006187 t.Helper()
6188
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006189 bp := `
6190 java_library {
6191 name: "some-updatable-apex-lib",
6192 srcs: ["a.java"],
6193 sdk_version: "current",
6194 apex_available: [
6195 "some-updatable-apex",
6196 ],
6197 }
6198
6199 java_library {
6200 name: "some-non-updatable-apex-lib",
6201 srcs: ["a.java"],
6202 apex_available: [
6203 "some-non-updatable-apex",
6204 ],
6205 }
6206
6207 java_library {
6208 name: "some-platform-lib",
6209 srcs: ["a.java"],
6210 sdk_version: "current",
6211 installable: true,
6212 }
6213
6214 java_library {
6215 name: "some-art-lib",
6216 srcs: ["a.java"],
6217 sdk_version: "current",
6218 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006219 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006220 ],
6221 hostdex: true,
6222 }
6223
6224 apex {
6225 name: "some-updatable-apex",
6226 key: "some-updatable-apex.key",
6227 java_libs: ["some-updatable-apex-lib"],
6228 updatable: true,
6229 min_sdk_version: "current",
6230 }
6231
6232 apex {
6233 name: "some-non-updatable-apex",
6234 key: "some-non-updatable-apex.key",
6235 java_libs: ["some-non-updatable-apex-lib"],
6236 }
6237
6238 apex_key {
6239 name: "some-updatable-apex.key",
6240 }
6241
6242 apex_key {
6243 name: "some-non-updatable-apex.key",
6244 }
6245
6246 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006247 name: "com.android.art.debug",
6248 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006249 java_libs: ["some-art-lib"],
6250 updatable: true,
6251 min_sdk_version: "current",
6252 }
6253
6254 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006255 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006256 }
6257
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006258 filegroup {
6259 name: "some-updatable-apex-file_contexts",
6260 srcs: [
6261 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6262 ],
6263 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006264
6265 filegroup {
6266 name: "some-non-updatable-apex-file_contexts",
6267 srcs: [
6268 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6269 ],
6270 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006271 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006272
6273 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6274}
6275
Paul Duffin064b70c2020-11-02 17:32:38 +00006276func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006277 t.Helper()
6278
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006279 bp += cc.GatherRequiredDepsForTest(android.Android)
6280 bp += java.GatherRequiredDepsForTest()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006281
6282 fs := map[string][]byte{
6283 "a.java": nil,
6284 "a.jar": nil,
6285 "build/make/target/product/security": nil,
6286 "apex_manifest.json": nil,
6287 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006288 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006289 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6290 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6291 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006292 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006293 }
6294 cc.GatherRequiredFilesForTest(fs)
6295
Colin Crossae8600b2020-10-29 17:09:13 -07006296 config := android.TestArchConfig(buildDir, nil, bp, fs)
6297
6298 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006299 ctx.RegisterModuleType("apex", BundleFactory)
6300 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00006301 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006302 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01006303 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin021f4e52020-07-30 16:04:17 +01006304 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006305 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006306 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf38931c2021-02-05 16:58:28 +00006307 java.RegisterHiddenApiSingletonComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006308 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6309 ctx.PreDepsMutators(RegisterPreDepsMutators)
6310 ctx.PostDepsMutators(RegisterPostDepsMutators)
6311
Colin Crossae8600b2020-10-29 17:09:13 -07006312 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006313
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006314 pathCtx := android.PathContextForTesting(config)
6315 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6316 transformDexpreoptConfig(dexpreoptConfig)
6317 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6318
Paul Duffinf38931c2021-02-05 16:58:28 +00006319 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
6320 // product variables that are used by hiddenapi.
6321 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6322 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6323
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006324 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6325 android.FailIfErrored(t, errs)
6326
6327 _, errs = ctx.PrepareBuildActions(config)
6328 if errmsg == "" {
6329 android.FailIfErrored(t, errs)
6330 } else if len(errs) > 0 {
6331 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006332 } else {
6333 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6334 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006335
6336 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006337}
6338
Jooyung Han548640b2020-04-27 12:10:30 +09006339func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6340 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6341 apex {
6342 name: "myapex",
6343 key: "myapex.key",
6344 updatable: true,
6345 }
6346
6347 apex_key {
6348 name: "myapex.key",
6349 public_key: "testkey.avbpubkey",
6350 private_key: "testkey.pem",
6351 }
6352 `)
6353}
6354
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006355func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006356 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006357 var transform func(*dexpreopt.GlobalConfig)
6358
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006359 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6360 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006361 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006362 }
6363 testNoUpdatableJarsInBootImage(t, "", transform)
6364 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006365
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006366 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006367 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 +01006368 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006369 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006370 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006371 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006372 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006373
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006374 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 -07006375 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 +01006376 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006377 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006378 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006379 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006380 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006381
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006382 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 -07006383 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006384 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006385 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006386 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006387 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006388 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006389
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006390 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 -07006391 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 +01006392 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006393 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006394 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006395 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006396 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006397
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006398 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6399 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006400 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006401 }
6402 testNoUpdatableJarsInBootImage(t, "", transform)
6403 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006404
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006405 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006406 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006407 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006408 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006409 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006410 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006411 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006412
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006413 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006414 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006415 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006416 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006417 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006418 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006419 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006420
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006421 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006422 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006423 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006424 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006425 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006426 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006427 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006428
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006429 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6430 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006431 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006432 }
6433 testNoUpdatableJarsInBootImage(t, "", transform)
6434 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006435
6436}
6437
6438func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6439 transform := func(config *dexpreopt.GlobalConfig) {
6440 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6441 }
6442 t.Run("prebuilt no source", func(t *testing.T) {
6443 testDexpreoptWithApexes(t, `
6444 prebuilt_apex {
6445 name: "myapex" ,
6446 arch: {
6447 arm64: {
6448 src: "myapex-arm64.apex",
6449 },
6450 arm: {
6451 src: "myapex-arm.apex",
6452 },
6453 },
6454 exported_java_libs: ["libfoo"],
6455 }
6456
6457 java_import {
6458 name: "libfoo",
6459 jars: ["libfoo.jar"],
6460 }
6461`, "", transform)
6462 })
6463
6464 t.Run("prebuilt no source", func(t *testing.T) {
6465 testDexpreoptWithApexes(t, `
6466 prebuilt_apex {
6467 name: "myapex" ,
6468 arch: {
6469 arm64: {
6470 src: "myapex-arm64.apex",
6471 },
6472 arm: {
6473 src: "myapex-arm.apex",
6474 },
6475 },
6476 exported_java_libs: ["libfoo"],
6477 }
6478
6479 java_import {
6480 name: "libfoo",
6481 jars: ["libfoo.jar"],
6482 }
6483`, "", transform)
6484 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006485}
6486
Andrei Onea115e7e72020-06-05 21:14:03 +01006487func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6488 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006489 bp += `
6490 apex_key {
6491 name: "myapex.key",
6492 public_key: "testkey.avbpubkey",
6493 private_key: "testkey.pem",
6494 }`
6495 fs := map[string][]byte{
6496 "lib1/src/A.java": nil,
6497 "lib2/src/B.java": nil,
6498 "system/sepolicy/apex/myapex-file_contexts": nil,
6499 }
6500
Colin Crossae8600b2020-10-29 17:09:13 -07006501 config := android.TestArchConfig(buildDir, nil, bp, fs)
6502 android.SetTestNeverallowRules(config, rules)
6503 updatableBootJars := make([]string, 0, len(apexBootJars))
6504 for _, apexBootJar := range apexBootJars {
6505 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6506 }
6507 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6508
6509 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006510 ctx.RegisterModuleType("apex", BundleFactory)
6511 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6512 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6513 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006514 java.RegisterRequiredBuildComponentsForTest(ctx)
Andrei Onea115e7e72020-06-05 21:14:03 +01006515 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6516 ctx.PreDepsMutators(RegisterPreDepsMutators)
6517 ctx.PostDepsMutators(RegisterPostDepsMutators)
6518 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6519
Colin Crossae8600b2020-10-29 17:09:13 -07006520 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006521
6522 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6523 android.FailIfErrored(t, errs)
6524
6525 _, errs = ctx.PrepareBuildActions(config)
6526 if errmsg == "" {
6527 android.FailIfErrored(t, errs)
6528 } else if len(errs) > 0 {
6529 android.FailIfNoMatchingErrors(t, errmsg, errs)
6530 return
6531 } else {
6532 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6533 }
6534}
6535
6536func TestApexPermittedPackagesRules(t *testing.T) {
6537 testcases := []struct {
6538 name string
6539 expectedError string
6540 bp string
6541 bootJars []string
6542 modulesPackages map[string][]string
6543 }{
6544
6545 {
6546 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6547 expectedError: "",
6548 bp: `
6549 java_library {
6550 name: "bcp_lib1",
6551 srcs: ["lib1/src/*.java"],
6552 permitted_packages: ["foo.bar"],
6553 apex_available: ["myapex"],
6554 sdk_version: "none",
6555 system_modules: "none",
6556 }
6557 java_library {
6558 name: "nonbcp_lib2",
6559 srcs: ["lib2/src/*.java"],
6560 apex_available: ["myapex"],
6561 permitted_packages: ["a.b"],
6562 sdk_version: "none",
6563 system_modules: "none",
6564 }
6565 apex {
6566 name: "myapex",
6567 key: "myapex.key",
6568 java_libs: ["bcp_lib1", "nonbcp_lib2"],
6569 }`,
6570 bootJars: []string{"bcp_lib1"},
6571 modulesPackages: map[string][]string{
6572 "myapex": []string{
6573 "foo.bar",
6574 },
6575 },
6576 },
6577 {
6578 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6579 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.`,
6580 bp: `
6581 java_library {
6582 name: "bcp_lib1",
6583 srcs: ["lib1/src/*.java"],
6584 apex_available: ["myapex"],
6585 permitted_packages: ["foo.bar"],
6586 sdk_version: "none",
6587 system_modules: "none",
6588 }
6589 java_library {
6590 name: "bcp_lib2",
6591 srcs: ["lib2/src/*.java"],
6592 apex_available: ["myapex"],
6593 permitted_packages: ["foo.bar", "bar.baz"],
6594 sdk_version: "none",
6595 system_modules: "none",
6596 }
6597 apex {
6598 name: "myapex",
6599 key: "myapex.key",
6600 java_libs: ["bcp_lib1", "bcp_lib2"],
6601 }
6602 `,
6603 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6604 modulesPackages: map[string][]string{
6605 "myapex": []string{
6606 "foo.bar",
6607 },
6608 },
6609 },
6610 }
6611 for _, tc := range testcases {
6612 t.Run(tc.name, func(t *testing.T) {
6613 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6614 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6615 })
6616 }
6617}
6618
Jiyong Park62304bb2020-04-13 16:19:48 +09006619func TestTestFor(t *testing.T) {
6620 ctx, _ := testApex(t, `
6621 apex {
6622 name: "myapex",
6623 key: "myapex.key",
6624 native_shared_libs: ["mylib", "myprivlib"],
6625 }
6626
6627 apex_key {
6628 name: "myapex.key",
6629 public_key: "testkey.avbpubkey",
6630 private_key: "testkey.pem",
6631 }
6632
6633 cc_library {
6634 name: "mylib",
6635 srcs: ["mylib.cpp"],
6636 system_shared_libs: [],
6637 stl: "none",
6638 stubs: {
6639 versions: ["1"],
6640 },
6641 apex_available: ["myapex"],
6642 }
6643
6644 cc_library {
6645 name: "myprivlib",
6646 srcs: ["mylib.cpp"],
6647 system_shared_libs: [],
6648 stl: "none",
6649 apex_available: ["myapex"],
6650 }
6651
6652
6653 cc_test {
6654 name: "mytest",
6655 gtest: false,
6656 srcs: ["mylib.cpp"],
6657 system_shared_libs: [],
6658 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006659 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006660 test_for: ["myapex"]
6661 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006662
6663 cc_library {
6664 name: "mytestlib",
6665 srcs: ["mylib.cpp"],
6666 system_shared_libs: [],
6667 shared_libs: ["mylib", "myprivlib"],
6668 stl: "none",
6669 test_for: ["myapex"],
6670 }
6671
6672 cc_benchmark {
6673 name: "mybench",
6674 srcs: ["mylib.cpp"],
6675 system_shared_libs: [],
6676 shared_libs: ["mylib", "myprivlib"],
6677 stl: "none",
6678 test_for: ["myapex"],
6679 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006680 `)
6681
6682 // the test 'mytest' is a test for the apex, therefore is linked to the
6683 // actual implementation of mylib instead of its stub.
6684 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6685 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6686 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park46a512f2020-12-04 18:02:13 +09006687
6688 // The same should be true for cc_library
6689 ldFlags = ctx.ModuleForTests("mytestlib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
6690 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6691 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
6692
6693 // ... and for cc_benchmark
6694 ldFlags = ctx.ModuleForTests("mybench", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6695 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6696 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006697}
6698
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006699// TODO(jungjw): Move this to proptools
6700func intPtr(i int) *int {
6701 return &i
6702}
6703
6704func TestApexSet(t *testing.T) {
6705 ctx, config := testApex(t, `
6706 apex_set {
6707 name: "myapex",
6708 set: "myapex.apks",
6709 filename: "foo_v2.apex",
6710 overrides: ["foo"],
6711 }
6712 `, func(fs map[string][]byte, config android.Config) {
6713 config.TestProductVariables.Platform_sdk_version = intPtr(30)
Jaewoong Jung829b7132020-06-10 12:23:32 -07006714 config.Targets[android.Android] = []android.Target{
6715 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6716 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6717 }
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006718 })
6719
6720 m := ctx.ModuleForTests("myapex", "android_common")
6721
6722 // Check extract_apks tool parameters.
6723 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6724 actual := extractedApex.Args["abis"]
6725 expected := "ARMEABI_V7A,ARM64_V8A"
6726 if actual != expected {
6727 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6728 }
6729 actual = extractedApex.Args["sdk-version"]
6730 expected = "30"
6731 if actual != expected {
6732 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6733 }
6734
6735 a := m.Module().(*ApexSet)
6736 expectedOverrides := []string{"foo"}
6737 actualOverrides := android.AndroidMkEntriesForTest(t, config, "", a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
6738 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6739 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6740 }
6741}
6742
Jiyong Park7d95a512020-05-10 15:16:24 +09006743func TestNoStaticLinkingToStubsLib(t *testing.T) {
6744 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6745 apex {
6746 name: "myapex",
6747 key: "myapex.key",
6748 native_shared_libs: ["mylib"],
6749 }
6750
6751 apex_key {
6752 name: "myapex.key",
6753 public_key: "testkey.avbpubkey",
6754 private_key: "testkey.pem",
6755 }
6756
6757 cc_library {
6758 name: "mylib",
6759 srcs: ["mylib.cpp"],
6760 static_libs: ["otherlib"],
6761 system_shared_libs: [],
6762 stl: "none",
6763 apex_available: [ "myapex" ],
6764 }
6765
6766 cc_library {
6767 name: "otherlib",
6768 srcs: ["mylib.cpp"],
6769 system_shared_libs: [],
6770 stl: "none",
6771 stubs: {
6772 versions: ["1", "2", "3"],
6773 },
6774 apex_available: [ "myapex" ],
6775 }
6776 `)
6777}
6778
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006779func TestApexKeysTxt(t *testing.T) {
6780 ctx, _ := testApex(t, `
6781 apex {
6782 name: "myapex",
6783 key: "myapex.key",
6784 }
6785
6786 apex_key {
6787 name: "myapex.key",
6788 public_key: "testkey.avbpubkey",
6789 private_key: "testkey.pem",
6790 }
6791
6792 prebuilt_apex {
6793 name: "myapex",
6794 prefer: true,
6795 arch: {
6796 arm64: {
6797 src: "myapex-arm64.apex",
6798 },
6799 arm: {
6800 src: "myapex-arm.apex",
6801 },
6802 },
6803 }
6804
6805 apex_set {
6806 name: "myapex_set",
6807 set: "myapex.apks",
6808 filename: "myapex_set.apex",
6809 overrides: ["myapex"],
6810 }
6811 `)
6812
6813 apexKeysText := ctx.SingletonForTests("apex_keys_text")
6814 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
6815 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 +09006816 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 +09006817}
6818
Jooyung Han938b5932020-06-20 12:47:47 +09006819func TestAllowedFiles(t *testing.T) {
6820 ctx, _ := testApex(t, `
6821 apex {
6822 name: "myapex",
6823 key: "myapex.key",
6824 apps: ["app"],
6825 allowed_files: "allowed.txt",
6826 }
6827
6828 apex_key {
6829 name: "myapex.key",
6830 public_key: "testkey.avbpubkey",
6831 private_key: "testkey.pem",
6832 }
6833
6834 android_app {
6835 name: "app",
6836 srcs: ["foo/bar/MyClass.java"],
6837 package_name: "foo",
6838 sdk_version: "none",
6839 system_modules: "none",
6840 apex_available: [ "myapex" ],
6841 }
6842 `, withFiles(map[string][]byte{
6843 "sub/Android.bp": []byte(`
6844 override_apex {
6845 name: "override_myapex",
6846 base: "myapex",
6847 apps: ["override_app"],
6848 allowed_files: ":allowed",
6849 }
6850 // Overridable "path" property should be referenced indirectly
6851 filegroup {
6852 name: "allowed",
6853 srcs: ["allowed.txt"],
6854 }
6855 override_android_app {
6856 name: "override_app",
6857 base: "app",
6858 package_name: "bar",
6859 }
6860 `),
6861 }))
6862
6863 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
6864 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
6865 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
6866 }
6867
6868 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
6869 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
6870 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
6871 }
6872}
6873
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006874func TestNonPreferredPrebuiltDependency(t *testing.T) {
6875 _, _ = testApex(t, `
6876 apex {
6877 name: "myapex",
6878 key: "myapex.key",
6879 native_shared_libs: ["mylib"],
6880 }
6881
6882 apex_key {
6883 name: "myapex.key",
6884 public_key: "testkey.avbpubkey",
6885 private_key: "testkey.pem",
6886 }
6887
6888 cc_library {
6889 name: "mylib",
6890 srcs: ["mylib.cpp"],
6891 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07006892 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006893 },
6894 apex_available: ["myapex"],
6895 }
6896
6897 cc_prebuilt_library_shared {
6898 name: "mylib",
6899 prefer: false,
6900 srcs: ["prebuilt.so"],
6901 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07006902 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006903 },
6904 apex_available: ["myapex"],
6905 }
6906 `)
6907}
6908
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00006909func TestCompressedApex(t *testing.T) {
6910 ctx, config := testApex(t, `
6911 apex {
6912 name: "myapex",
6913 key: "myapex.key",
6914 compressible: true,
6915 }
6916 apex_key {
6917 name: "myapex.key",
6918 public_key: "testkey.avbpubkey",
6919 private_key: "testkey.pem",
6920 }
6921 `, func(fs map[string][]byte, config android.Config) {
6922 config.TestProductVariables.CompressedApex = proptools.BoolPtr(true)
6923 })
6924
6925 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
6926 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
6927
6928 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
6929 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
6930
6931 // Make sure output of bundle is .capex
6932 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
6933 ensureContains(t, ab.outputFile.String(), "myapex.capex")
6934
6935 // Verify android.mk rules
6936 data := android.AndroidMkDataForTest(t, config, "", ab)
6937 var builder strings.Builder
6938 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
6939 androidMk := builder.String()
6940 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
6941}
6942
Martin Stjernholm2856c662020-12-02 15:03:42 +00006943func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
6944 ctx, config := testApex(t, `
6945 apex {
6946 name: "myapex",
6947 key: "myapex.key",
6948 native_shared_libs: ["mylib"],
6949 }
6950
6951 apex_key {
6952 name: "myapex.key",
6953 public_key: "testkey.avbpubkey",
6954 private_key: "testkey.pem",
6955 }
6956
6957 cc_library {
6958 name: "mylib",
6959 srcs: ["mylib.cpp"],
6960 apex_available: ["myapex"],
6961 shared_libs: ["otherlib"],
6962 system_shared_libs: [],
6963 }
6964
6965 cc_library {
6966 name: "otherlib",
6967 srcs: ["mylib.cpp"],
6968 stubs: {
6969 versions: ["current"],
6970 },
6971 }
6972
6973 cc_prebuilt_library_shared {
6974 name: "otherlib",
6975 prefer: true,
6976 srcs: ["prebuilt.so"],
6977 stubs: {
6978 versions: ["current"],
6979 },
6980 }
6981 `)
6982
6983 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
6984 data := android.AndroidMkDataForTest(t, config, "", ab)
6985 var builder strings.Builder
6986 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
6987 androidMk := builder.String()
6988
6989 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
6990 // a thing there.
6991 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
6992}
6993
Jiyong Parke3867542020-12-03 17:28:25 +09006994func TestExcludeDependency(t *testing.T) {
6995 ctx, _ := testApex(t, `
6996 apex {
6997 name: "myapex",
6998 key: "myapex.key",
6999 native_shared_libs: ["mylib"],
7000 }
7001
7002 apex_key {
7003 name: "myapex.key",
7004 public_key: "testkey.avbpubkey",
7005 private_key: "testkey.pem",
7006 }
7007
7008 cc_library {
7009 name: "mylib",
7010 srcs: ["mylib.cpp"],
7011 system_shared_libs: [],
7012 stl: "none",
7013 apex_available: ["myapex"],
7014 shared_libs: ["mylib2"],
7015 target: {
7016 apex: {
7017 exclude_shared_libs: ["mylib2"],
7018 },
7019 },
7020 }
7021
7022 cc_library {
7023 name: "mylib2",
7024 srcs: ["mylib.cpp"],
7025 system_shared_libs: [],
7026 stl: "none",
7027 }
7028 `)
7029
7030 // Check if mylib is linked to mylib2 for the non-apex target
7031 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7032 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7033
7034 // Make sure that the link doesn't occur for the apex target
7035 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7036 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7037
7038 // It shouldn't appear in the copy cmd as well.
7039 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7040 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7041}
7042
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007043func TestPrebuiltStubLibDep(t *testing.T) {
7044 bpBase := `
7045 apex {
7046 name: "myapex",
7047 key: "myapex.key",
7048 native_shared_libs: ["mylib"],
7049 }
7050 apex_key {
7051 name: "myapex.key",
7052 public_key: "testkey.avbpubkey",
7053 private_key: "testkey.pem",
7054 }
7055 cc_library {
7056 name: "mylib",
7057 srcs: ["mylib.cpp"],
7058 apex_available: ["myapex"],
7059 shared_libs: ["stublib"],
7060 system_shared_libs: [],
7061 }
7062 apex {
7063 name: "otherapex",
7064 enabled: %s,
7065 key: "myapex.key",
7066 native_shared_libs: ["stublib"],
7067 }
7068 `
7069
7070 stublibSourceBp := `
7071 cc_library {
7072 name: "stublib",
7073 srcs: ["mylib.cpp"],
7074 apex_available: ["otherapex"],
7075 system_shared_libs: [],
7076 stl: "none",
7077 stubs: {
7078 versions: ["1"],
7079 },
7080 }
7081 `
7082
7083 stublibPrebuiltBp := `
7084 cc_prebuilt_library_shared {
7085 name: "stublib",
7086 srcs: ["prebuilt.so"],
7087 apex_available: ["otherapex"],
7088 stubs: {
7089 versions: ["1"],
7090 },
7091 %s
7092 }
7093 `
7094
7095 tests := []struct {
7096 name string
7097 stublibBp string
7098 usePrebuilt bool
7099 modNames []string // Modules to collect AndroidMkEntries for
7100 otherApexEnabled []string
7101 }{
7102 {
7103 name: "only_source",
7104 stublibBp: stublibSourceBp,
7105 usePrebuilt: false,
7106 modNames: []string{"stublib"},
7107 otherApexEnabled: []string{"true", "false"},
7108 },
7109 {
7110 name: "source_preferred",
7111 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7112 usePrebuilt: false,
7113 modNames: []string{"stublib", "prebuilt_stublib"},
7114 otherApexEnabled: []string{"true", "false"},
7115 },
7116 {
7117 name: "prebuilt_preferred",
7118 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7119 usePrebuilt: true,
7120 modNames: []string{"stublib", "prebuilt_stublib"},
7121 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7122 },
7123 {
7124 name: "only_prebuilt",
7125 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7126 usePrebuilt: true,
7127 modNames: []string{"stublib"},
7128 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7129 },
7130 }
7131
7132 for _, test := range tests {
7133 t.Run(test.name, func(t *testing.T) {
7134 for _, otherApexEnabled := range test.otherApexEnabled {
7135 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
7136 ctx, config := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
7137
7138 type modAndMkEntries struct {
7139 mod *cc.Module
7140 mkEntries android.AndroidMkEntries
7141 }
7142 entries := []*modAndMkEntries{}
7143
7144 // Gather shared lib modules that are installable
7145 for _, modName := range test.modNames {
7146 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7147 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7148 continue
7149 }
7150 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007151 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007152 continue
7153 }
7154 for _, ent := range android.AndroidMkEntriesForTest(t, config, "", mod) {
7155 if ent.Disabled {
7156 continue
7157 }
7158 entries = append(entries, &modAndMkEntries{
7159 mod: mod,
7160 mkEntries: ent,
7161 })
7162 }
7163 }
7164 }
7165
7166 var entry *modAndMkEntries = nil
7167 for _, ent := range entries {
7168 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7169 if entry != nil {
7170 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7171 } else {
7172 entry = ent
7173 }
7174 }
7175 }
7176
7177 if entry == nil {
7178 t.Errorf("AndroidMk entry for \"stublib\" missing")
7179 } else {
7180 isPrebuilt := entry.mod.Prebuilt() != nil
7181 if isPrebuilt != test.usePrebuilt {
7182 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7183 }
7184 if !entry.mod.IsStubs() {
7185 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7186 }
7187 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7188 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7189 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007190 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
7191 expected := "-D__STUBLIB_API__=1"
7192 if !android.InList(expected, cflags) {
7193 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7194 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007195 }
7196 })
7197 }
7198 })
7199 }
7200}
7201
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007202func TestMain(m *testing.M) {
7203 run := func() int {
7204 setUp()
7205 defer tearDown()
7206
7207 return m.Run()
7208 }
7209
7210 os.Exit(run())
7211}