blob: 12201a341e6ba82711bb52ffbfe074df6399cfc5 [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
Paul Duffin4fd997b2021-02-03 20:06:33 +00004498 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
4499 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4500 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4501 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4502 }
4503
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004504 t.Run("prebuilt only", func(t *testing.T) {
4505 bp := `
4506 prebuilt_apex {
4507 name: "myapex",
4508 arch: {
4509 arm64: {
4510 src: "myapex-arm64.apex",
4511 },
4512 arm: {
4513 src: "myapex-arm.apex",
4514 },
4515 },
4516 exported_java_libs: ["libfoo"],
4517 }
4518
4519 java_import {
4520 name: "libfoo",
4521 jars: ["libfoo.jar"],
4522 apex_available: ["myapex"],
4523 }
4524 `
4525
4526 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin89886cb2021-02-05 16:44:03 +00004527 checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004528
4529 // Make sure that the dex file from the prebuilt_apex does NOT contribute to the hiddenapi index
4530 // file.
4531 checkHiddenAPIIndexInputs(t, ctx, ``)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004532 })
4533
4534 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4535 bp := `
4536 prebuilt_apex {
4537 name: "myapex",
4538 arch: {
4539 arm64: {
4540 src: "myapex-arm64.apex",
4541 },
4542 arm: {
4543 src: "myapex-arm.apex",
4544 },
4545 },
4546 exported_java_libs: ["libfoo"],
4547 }
4548
4549 java_import {
4550 name: "libfoo",
4551 jars: ["libfoo.jar"],
4552 apex_available: ["myapex"],
4553 }
4554
4555 java_library {
4556 name: "libfoo",
4557 srcs: ["foo/bar/MyClass.java"],
4558 apex_available: ["myapex"],
4559 }
4560 `
4561
4562 // In this test the source (java_library) libfoo is active since the
4563 // prebuilt (java_import) defaults to prefer:false. However the
4564 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4565 // find the dex boot jar in it. We either need to disable the source libfoo
4566 // or make the prebuilt libfoo preferred.
4567 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4568 })
4569
4570 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4571 bp := `
4572 prebuilt_apex {
4573 name: "myapex",
4574 arch: {
4575 arm64: {
4576 src: "myapex-arm64.apex",
4577 },
4578 arm: {
4579 src: "myapex-arm.apex",
4580 },
4581 },
4582 exported_java_libs: ["libfoo"],
4583 }
4584
4585 java_import {
4586 name: "libfoo",
4587 prefer: true,
4588 jars: ["libfoo.jar"],
4589 apex_available: ["myapex"],
4590 }
4591
4592 java_library {
4593 name: "libfoo",
4594 srcs: ["foo/bar/MyClass.java"],
4595 apex_available: ["myapex"],
4596 }
4597 `
4598
4599 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin89886cb2021-02-05 16:44:03 +00004600 checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004601
4602 // Make sure that the dex file from the prebuilt_apex does NOT contribute to the hiddenapi index
4603 // file.
4604 checkHiddenAPIIndexInputs(t, ctx, ``)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004605 })
4606
4607 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4608 bp := `
4609 apex {
4610 name: "myapex",
4611 key: "myapex.key",
4612 java_libs: ["libfoo"],
4613 }
4614
4615 apex_key {
4616 name: "myapex.key",
4617 public_key: "testkey.avbpubkey",
4618 private_key: "testkey.pem",
4619 }
4620
4621 prebuilt_apex {
4622 name: "myapex",
4623 arch: {
4624 arm64: {
4625 src: "myapex-arm64.apex",
4626 },
4627 arm: {
4628 src: "myapex-arm.apex",
4629 },
4630 },
4631 exported_java_libs: ["libfoo"],
4632 }
4633
4634 java_import {
4635 name: "libfoo",
4636 jars: ["libfoo.jar"],
4637 apex_available: ["myapex"],
4638 }
4639
4640 java_library {
4641 name: "libfoo",
4642 srcs: ["foo/bar/MyClass.java"],
4643 apex_available: ["myapex"],
4644 }
4645 `
4646
4647 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffinf38931c2021-02-05 16:58:28 +00004648 checkBootDexJarPath(t, ctx, ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004649
4650 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4651 checkHiddenAPIIndexInputs(t, ctx, `
4652.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4653`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004654 })
4655
4656 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4657 bp := `
4658 apex {
4659 name: "myapex",
4660 enabled: false,
4661 key: "myapex.key",
4662 java_libs: ["libfoo"],
4663 }
4664
4665 apex_key {
4666 name: "myapex.key",
4667 public_key: "testkey.avbpubkey",
4668 private_key: "testkey.pem",
4669 }
4670
4671 prebuilt_apex {
4672 name: "myapex",
4673 arch: {
4674 arm64: {
4675 src: "myapex-arm64.apex",
4676 },
4677 arm: {
4678 src: "myapex-arm.apex",
4679 },
4680 },
4681 exported_java_libs: ["libfoo"],
4682 }
4683
4684 java_import {
4685 name: "libfoo",
4686 prefer: true,
4687 jars: ["libfoo.jar"],
4688 apex_available: ["myapex"],
4689 }
4690
4691 java_library {
4692 name: "libfoo",
4693 srcs: ["foo/bar/MyClass.java"],
4694 apex_available: ["myapex"],
4695 }
4696 `
4697
4698 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin89886cb2021-02-05 16:44:03 +00004699 checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004700
4701 // Make sure that the dex file from the prebuilt_apex does NOT contribute to the hiddenapi index
4702 // file.
4703 checkHiddenAPIIndexInputs(t, ctx, ``)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004704 })
4705}
4706
Roland Levillain630846d2019-06-26 12:48:34 +01004707func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01004708 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004709 apex_test {
4710 name: "myapex",
4711 key: "myapex.key",
4712 tests: [
4713 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004714 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004715 ],
4716 }
4717
4718 apex_key {
4719 name: "myapex.key",
4720 public_key: "testkey.avbpubkey",
4721 private_key: "testkey.pem",
4722 }
4723
Liz Kammer1c14a212020-05-12 15:26:55 -07004724 filegroup {
4725 name: "fg",
4726 srcs: [
4727 "baz",
4728 "bar/baz"
4729 ],
4730 }
4731
Roland Levillain630846d2019-06-26 12:48:34 +01004732 cc_test {
4733 name: "mytest",
4734 gtest: false,
4735 srcs: ["mytest.cpp"],
4736 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004737 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004738 system_shared_libs: [],
4739 static_executable: true,
4740 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004741 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004742 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004743
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004744 cc_library {
4745 name: "mylib",
4746 srcs: ["mylib.cpp"],
4747 system_shared_libs: [],
4748 stl: "none",
4749 }
4750
Liz Kammer5bd365f2020-05-27 15:15:11 -07004751 filegroup {
4752 name: "fg2",
4753 srcs: [
4754 "testdata/baz"
4755 ],
4756 }
4757
Roland Levillain9b5fde92019-06-28 15:41:19 +01004758 cc_test {
4759 name: "mytests",
4760 gtest: false,
4761 srcs: [
4762 "mytest1.cpp",
4763 "mytest2.cpp",
4764 "mytest3.cpp",
4765 ],
4766 test_per_src: true,
4767 relative_install_path: "test",
4768 system_shared_libs: [],
4769 static_executable: true,
4770 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004771 data: [
4772 ":fg",
4773 ":fg2",
4774 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004775 }
Roland Levillain630846d2019-06-26 12:48:34 +01004776 `)
4777
Sundong Ahnabb64432019-10-22 13:58:29 +09004778 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004779 copyCmds := apexRule.Args["copy_commands"]
4780
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004781 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004782 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004783 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004784
Liz Kammer1c14a212020-05-12 15:26:55 -07004785 //Ensure that test data are copied into apex.
4786 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4787 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4788
Roland Levillain9b5fde92019-06-28 15:41:19 +01004789 // Ensure that test deps built with `test_per_src` are copied into apex.
4790 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4791 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4792 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004793
4794 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004795 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4796 data := android.AndroidMkDataForTest(t, config, "", bundle)
4797 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004798 prefix := "TARGET_"
4799 var builder strings.Builder
4800 data.Custom(&builder, name, prefix, "", data)
4801 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004802 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4803 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4804 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4805 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004806 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004807 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004808 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004809
4810 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4811 data = android.AndroidMkDataForTest(t, config, "", flatBundle)
4812 data.Custom(&builder, name, prefix, "", data)
4813 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004814 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4815 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004816}
4817
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004818func TestInstallExtraFlattenedApexes(t *testing.T) {
4819 ctx, config := testApex(t, `
4820 apex {
4821 name: "myapex",
4822 key: "myapex.key",
4823 }
4824 apex_key {
4825 name: "myapex.key",
4826 public_key: "testkey.avbpubkey",
4827 private_key: "testkey.pem",
4828 }
4829 `, func(fs map[string][]byte, config android.Config) {
4830 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4831 })
4832 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004833 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004834 mk := android.AndroidMkDataForTest(t, config, "", ab)
4835 var builder strings.Builder
4836 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4837 androidMk := builder.String()
4838 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4839}
4840
Jooyung Hand48f3c32019-08-23 11:18:57 +09004841func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4842 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4843 apex {
4844 name: "myapex",
4845 key: "myapex.key",
4846 native_shared_libs: ["libfoo"],
4847 }
4848
4849 apex_key {
4850 name: "myapex.key",
4851 public_key: "testkey.avbpubkey",
4852 private_key: "testkey.pem",
4853 }
4854
4855 cc_library {
4856 name: "libfoo",
4857 stl: "none",
4858 system_shared_libs: [],
4859 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004860 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004861 }
4862 `)
4863 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4864 apex {
4865 name: "myapex",
4866 key: "myapex.key",
4867 java_libs: ["myjar"],
4868 }
4869
4870 apex_key {
4871 name: "myapex.key",
4872 public_key: "testkey.avbpubkey",
4873 private_key: "testkey.pem",
4874 }
4875
4876 java_library {
4877 name: "myjar",
4878 srcs: ["foo/bar/MyClass.java"],
4879 sdk_version: "none",
4880 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004881 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004882 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004883 }
4884 `)
4885}
4886
Bill Peckhama41a6962021-01-11 10:58:54 -08004887func TestApexWithJavaImport(t *testing.T) {
4888 ctx, _ := testApex(t, `
4889 apex {
4890 name: "myapex",
4891 key: "myapex.key",
4892 java_libs: ["myjavaimport"],
4893 }
4894
4895 apex_key {
4896 name: "myapex.key",
4897 public_key: "testkey.avbpubkey",
4898 private_key: "testkey.pem",
4899 }
4900
4901 java_import {
4902 name: "myjavaimport",
4903 apex_available: ["myapex"],
4904 jars: ["my.jar"],
4905 compile_dex: true,
4906 }
4907 `)
4908
4909 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4910 apexRule := module.Rule("apexRule")
4911 copyCmds := apexRule.Args["copy_commands"]
4912 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
4913}
4914
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004915func TestApexWithApps(t *testing.T) {
4916 ctx, _ := testApex(t, `
4917 apex {
4918 name: "myapex",
4919 key: "myapex.key",
4920 apps: [
4921 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09004922 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004923 ],
4924 }
4925
4926 apex_key {
4927 name: "myapex.key",
4928 public_key: "testkey.avbpubkey",
4929 private_key: "testkey.pem",
4930 }
4931
4932 android_app {
4933 name: "AppFoo",
4934 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004935 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004936 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09004937 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08004938 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004939 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004940 }
Jiyong Parkf7487312019-10-17 12:54:30 +09004941
4942 android_app {
4943 name: "AppFooPriv",
4944 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004945 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09004946 system_modules: "none",
4947 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08004948 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004949 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09004950 }
Jiyong Park8be103b2019-11-08 15:53:48 +09004951
4952 cc_library_shared {
4953 name: "libjni",
4954 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004955 shared_libs: ["libfoo"],
4956 stl: "none",
4957 system_shared_libs: [],
4958 apex_available: [ "myapex" ],
4959 sdk_version: "current",
4960 }
4961
4962 cc_library_shared {
4963 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09004964 stl: "none",
4965 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09004966 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08004967 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09004968 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004969 `)
4970
Sundong Ahnabb64432019-10-22 13:58:29 +09004971 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004972 apexRule := module.Rule("apexRule")
4973 copyCmds := apexRule.Args["copy_commands"]
4974
4975 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09004976 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004977
Colin Crossaede88c2020-08-11 12:17:01 -07004978 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004979 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09004980 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004981 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004982 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004983 // JNI libraries including transitive deps are
4984 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07004985 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004986 // ... embedded inside APK (jnilibs.zip)
4987 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
4988 // ... and not directly inside the APEX
4989 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
4990 }
Dario Frenicde2a032019-10-27 00:29:22 +01004991}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004992
Dario Frenicde2a032019-10-27 00:29:22 +01004993func TestApexWithAppImports(t *testing.T) {
4994 ctx, _ := testApex(t, `
4995 apex {
4996 name: "myapex",
4997 key: "myapex.key",
4998 apps: [
4999 "AppFooPrebuilt",
5000 "AppFooPrivPrebuilt",
5001 ],
5002 }
5003
5004 apex_key {
5005 name: "myapex.key",
5006 public_key: "testkey.avbpubkey",
5007 private_key: "testkey.pem",
5008 }
5009
5010 android_app_import {
5011 name: "AppFooPrebuilt",
5012 apk: "PrebuiltAppFoo.apk",
5013 presigned: true,
5014 dex_preopt: {
5015 enabled: false,
5016 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005017 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005018 }
5019
5020 android_app_import {
5021 name: "AppFooPrivPrebuilt",
5022 apk: "PrebuiltAppFooPriv.apk",
5023 privileged: true,
5024 presigned: true,
5025 dex_preopt: {
5026 enabled: false,
5027 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005028 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005029 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005030 }
5031 `)
5032
Sundong Ahnabb64432019-10-22 13:58:29 +09005033 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005034 apexRule := module.Rule("apexRule")
5035 copyCmds := apexRule.Args["copy_commands"]
5036
5037 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005038 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5039}
5040
5041func TestApexWithAppImportsPrefer(t *testing.T) {
5042 ctx, _ := testApex(t, `
5043 apex {
5044 name: "myapex",
5045 key: "myapex.key",
5046 apps: [
5047 "AppFoo",
5048 ],
5049 }
5050
5051 apex_key {
5052 name: "myapex.key",
5053 public_key: "testkey.avbpubkey",
5054 private_key: "testkey.pem",
5055 }
5056
5057 android_app {
5058 name: "AppFoo",
5059 srcs: ["foo/bar/MyClass.java"],
5060 sdk_version: "none",
5061 system_modules: "none",
5062 apex_available: [ "myapex" ],
5063 }
5064
5065 android_app_import {
5066 name: "AppFoo",
5067 apk: "AppFooPrebuilt.apk",
5068 filename: "AppFooPrebuilt.apk",
5069 presigned: true,
5070 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005071 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005072 }
5073 `, withFiles(map[string][]byte{
5074 "AppFooPrebuilt.apk": nil,
5075 }))
5076
5077 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5078 "app/AppFoo/AppFooPrebuilt.apk",
5079 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005080}
5081
Dario Freni6f3937c2019-12-20 22:58:03 +00005082func TestApexWithTestHelperApp(t *testing.T) {
5083 ctx, _ := testApex(t, `
5084 apex {
5085 name: "myapex",
5086 key: "myapex.key",
5087 apps: [
5088 "TesterHelpAppFoo",
5089 ],
5090 }
5091
5092 apex_key {
5093 name: "myapex.key",
5094 public_key: "testkey.avbpubkey",
5095 private_key: "testkey.pem",
5096 }
5097
5098 android_test_helper_app {
5099 name: "TesterHelpAppFoo",
5100 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005101 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005102 }
5103
5104 `)
5105
5106 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5107 apexRule := module.Rule("apexRule")
5108 copyCmds := apexRule.Args["copy_commands"]
5109
5110 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5111}
5112
Jooyung Han18020ea2019-11-13 10:50:48 +09005113func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5114 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005115 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005116 apex {
5117 name: "myapex",
5118 key: "myapex.key",
5119 native_shared_libs: ["libfoo"],
5120 }
5121
5122 apex_key {
5123 name: "myapex.key",
5124 public_key: "testkey.avbpubkey",
5125 private_key: "testkey.pem",
5126 }
5127
5128 apex {
5129 name: "otherapex",
5130 key: "myapex.key",
5131 native_shared_libs: ["libfoo"],
5132 }
5133
5134 cc_defaults {
5135 name: "libfoo-defaults",
5136 apex_available: ["otherapex"],
5137 }
5138
5139 cc_library {
5140 name: "libfoo",
5141 defaults: ["libfoo-defaults"],
5142 stl: "none",
5143 system_shared_libs: [],
5144 }`)
5145}
5146
Paul Duffine52e66f2020-03-30 17:54:29 +01005147func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005148 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005149 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005150 apex {
5151 name: "myapex",
5152 key: "myapex.key",
5153 native_shared_libs: ["libfoo"],
5154 }
5155
5156 apex_key {
5157 name: "myapex.key",
5158 public_key: "testkey.avbpubkey",
5159 private_key: "testkey.pem",
5160 }
5161
5162 apex {
5163 name: "otherapex",
5164 key: "otherapex.key",
5165 native_shared_libs: ["libfoo"],
5166 }
5167
5168 apex_key {
5169 name: "otherapex.key",
5170 public_key: "testkey.avbpubkey",
5171 private_key: "testkey.pem",
5172 }
5173
5174 cc_library {
5175 name: "libfoo",
5176 stl: "none",
5177 system_shared_libs: [],
5178 apex_available: ["otherapex"],
5179 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005180}
Jiyong Park127b40b2019-09-30 16:04:35 +09005181
Paul Duffine52e66f2020-03-30 17:54:29 +01005182func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005183 // libbbaz is an indirect dep
Steven Moreland6e36cd62020-10-22 01:08:35 +00005184 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'. Dependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005185.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005186.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005187.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005188.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005189.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005190.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005191 apex {
5192 name: "myapex",
5193 key: "myapex.key",
5194 native_shared_libs: ["libfoo"],
5195 }
5196
5197 apex_key {
5198 name: "myapex.key",
5199 public_key: "testkey.avbpubkey",
5200 private_key: "testkey.pem",
5201 }
5202
Jiyong Park127b40b2019-09-30 16:04:35 +09005203 cc_library {
5204 name: "libfoo",
5205 stl: "none",
5206 shared_libs: ["libbar"],
5207 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005208 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005209 }
5210
5211 cc_library {
5212 name: "libbar",
5213 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005214 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005215 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005216 apex_available: ["myapex"],
5217 }
5218
5219 cc_library {
5220 name: "libbaz",
5221 stl: "none",
5222 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005223 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005224}
Jiyong Park127b40b2019-09-30 16:04:35 +09005225
Paul Duffine52e66f2020-03-30 17:54:29 +01005226func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005227 testApexError(t, "\"otherapex\" is not a valid module name", `
5228 apex {
5229 name: "myapex",
5230 key: "myapex.key",
5231 native_shared_libs: ["libfoo"],
5232 }
5233
5234 apex_key {
5235 name: "myapex.key",
5236 public_key: "testkey.avbpubkey",
5237 private_key: "testkey.pem",
5238 }
5239
5240 cc_library {
5241 name: "libfoo",
5242 stl: "none",
5243 system_shared_libs: [],
5244 apex_available: ["otherapex"],
5245 }`)
5246
Paul Duffine52e66f2020-03-30 17:54:29 +01005247 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005248 apex {
5249 name: "myapex",
5250 key: "myapex.key",
5251 native_shared_libs: ["libfoo", "libbar"],
5252 }
5253
5254 apex_key {
5255 name: "myapex.key",
5256 public_key: "testkey.avbpubkey",
5257 private_key: "testkey.pem",
5258 }
5259
5260 cc_library {
5261 name: "libfoo",
5262 stl: "none",
5263 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005264 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005265 apex_available: ["myapex"],
5266 }
5267
5268 cc_library {
5269 name: "libbar",
5270 stl: "none",
5271 system_shared_libs: [],
5272 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005273 }
5274
5275 cc_library {
5276 name: "libbaz",
5277 stl: "none",
5278 system_shared_libs: [],
5279 stubs: {
5280 versions: ["10", "20", "30"],
5281 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005282 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005283}
Jiyong Park127b40b2019-09-30 16:04:35 +09005284
Jiyong Park89e850a2020-04-07 16:37:39 +09005285func TestApexAvailable_CheckForPlatform(t *testing.T) {
Paul Duffine52e66f2020-03-30 17:54:29 +01005286 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005287 apex {
5288 name: "myapex",
5289 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005290 native_shared_libs: ["libbar", "libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005291 }
5292
5293 apex_key {
5294 name: "myapex.key",
5295 public_key: "testkey.avbpubkey",
5296 private_key: "testkey.pem",
5297 }
5298
5299 cc_library {
5300 name: "libfoo",
5301 stl: "none",
5302 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005303 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005304 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005305 }
5306
5307 cc_library {
5308 name: "libfoo2",
5309 stl: "none",
5310 system_shared_libs: [],
5311 shared_libs: ["libbaz"],
5312 apex_available: ["//apex_available:platform"],
5313 }
5314
5315 cc_library {
5316 name: "libbar",
5317 stl: "none",
5318 system_shared_libs: [],
5319 apex_available: ["myapex"],
5320 }
5321
5322 cc_library {
5323 name: "libbaz",
5324 stl: "none",
5325 system_shared_libs: [],
5326 apex_available: ["myapex"],
5327 stubs: {
5328 versions: ["1"],
5329 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005330 }`)
5331
Jiyong Park89e850a2020-04-07 16:37:39 +09005332 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5333 // because it depends on libbar which isn't available to platform
5334 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5335 if libfoo.NotAvailableForPlatform() != true {
5336 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5337 }
5338
5339 // libfoo2 however can be available to platform because it depends on libbaz which provides
5340 // stubs
5341 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5342 if libfoo2.NotAvailableForPlatform() == true {
5343 t.Errorf("%q should be available to platform", libfoo2.String())
5344 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005345}
Jiyong Parka90ca002019-10-07 15:47:24 +09005346
Paul Duffine52e66f2020-03-30 17:54:29 +01005347func TestApexAvailable_CreatedForApex(t *testing.T) {
Jiyong Park89e850a2020-04-07 16:37:39 +09005348 ctx, _ := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005349 apex {
5350 name: "myapex",
5351 key: "myapex.key",
5352 native_shared_libs: ["libfoo"],
5353 }
5354
5355 apex_key {
5356 name: "myapex.key",
5357 public_key: "testkey.avbpubkey",
5358 private_key: "testkey.pem",
5359 }
5360
5361 cc_library {
5362 name: "libfoo",
5363 stl: "none",
5364 system_shared_libs: [],
5365 apex_available: ["myapex"],
5366 static: {
5367 apex_available: ["//apex_available:platform"],
5368 },
5369 }`)
5370
Jiyong Park89e850a2020-04-07 16:37:39 +09005371 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5372 if libfooShared.NotAvailableForPlatform() != true {
5373 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5374 }
5375 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5376 if libfooStatic.NotAvailableForPlatform() != false {
5377 t.Errorf("%q should be available to platform", libfooStatic.String())
5378 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005379}
5380
Jiyong Park5d790c32019-11-15 18:40:32 +09005381func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005382 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005383 apex {
5384 name: "myapex",
5385 key: "myapex.key",
5386 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005387 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09005388 }
5389
5390 override_apex {
5391 name: "override_myapex",
5392 base: "myapex",
5393 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005394 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005395 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005396 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005397 }
5398
5399 apex_key {
5400 name: "myapex.key",
5401 public_key: "testkey.avbpubkey",
5402 private_key: "testkey.pem",
5403 }
5404
5405 android_app {
5406 name: "app",
5407 srcs: ["foo/bar/MyClass.java"],
5408 package_name: "foo",
5409 sdk_version: "none",
5410 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005411 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005412 }
5413
5414 override_android_app {
5415 name: "override_app",
5416 base: "app",
5417 package_name: "bar",
5418 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005419 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005420
Jiyong Park317645e2019-12-05 13:20:58 +09005421 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5422 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5423 if originalVariant.GetOverriddenBy() != "" {
5424 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5425 }
5426 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5427 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5428 }
5429
Jiyong Park5d790c32019-11-15 18:40:32 +09005430 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5431 apexRule := module.Rule("apexRule")
5432 copyCmds := apexRule.Args["copy_commands"]
5433
5434 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005435 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005436
5437 apexBundle := module.Module().(*apexBundle)
5438 name := apexBundle.Name()
5439 if name != "override_myapex" {
5440 t.Errorf("name should be \"override_myapex\", but was %q", name)
5441 }
5442
Baligh Uddin004d7172020-02-19 21:29:28 -08005443 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5444 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5445 }
5446
Jiyong Park20bacab2020-03-03 11:45:41 +09005447 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005448 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005449
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005450 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
5451 var builder strings.Builder
5452 data.Custom(&builder, name, "TARGET_", "", data)
5453 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005454 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005455 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5456 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005457 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005458 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005459 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005460 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5461 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005462}
5463
Jooyung Han214bf372019-11-12 13:03:50 +09005464func TestLegacyAndroid10Support(t *testing.T) {
5465 ctx, _ := testApex(t, `
5466 apex {
5467 name: "myapex",
5468 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005469 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005470 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005471 }
5472
5473 apex_key {
5474 name: "myapex.key",
5475 public_key: "testkey.avbpubkey",
5476 private_key: "testkey.pem",
5477 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005478
5479 cc_library {
5480 name: "mylib",
5481 srcs: ["mylib.cpp"],
5482 stl: "libc++",
5483 system_shared_libs: [],
5484 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005485 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005486 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005487 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005488
5489 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5490 args := module.Rule("apexRule").Args
5491 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005492 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005493
5494 // The copies of the libraries in the apex should have one more dependency than
5495 // the ones outside the apex, namely the unwinder. Ideally we should check
5496 // the dependency names directly here but for some reason the names are blank in
5497 // this test.
5498 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005499 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005500 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5501 if len(apexImplicits) != len(nonApexImplicits)+1 {
5502 t.Errorf("%q missing unwinder dep", lib)
5503 }
5504 }
Jooyung Han214bf372019-11-12 13:03:50 +09005505}
5506
Paul Duffin9b879592020-05-26 13:21:35 +01005507var filesForSdkLibrary = map[string][]byte{
5508 "api/current.txt": nil,
5509 "api/removed.txt": nil,
5510 "api/system-current.txt": nil,
5511 "api/system-removed.txt": nil,
5512 "api/test-current.txt": nil,
5513 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005514
Anton Hanssondff2c782020-12-21 17:10:01 +00005515 "100/public/api/foo.txt": nil,
5516 "100/public/api/foo-removed.txt": nil,
5517 "100/system/api/foo.txt": nil,
5518 "100/system/api/foo-removed.txt": nil,
5519
Paul Duffineedc5d52020-06-12 17:46:39 +01005520 // For java_sdk_library_import
5521 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005522}
5523
Jooyung Han58f26ab2019-12-18 15:34:32 +09005524func TestJavaSDKLibrary(t *testing.T) {
5525 ctx, _ := testApex(t, `
5526 apex {
5527 name: "myapex",
5528 key: "myapex.key",
5529 java_libs: ["foo"],
5530 }
5531
5532 apex_key {
5533 name: "myapex.key",
5534 public_key: "testkey.avbpubkey",
5535 private_key: "testkey.pem",
5536 }
5537
5538 java_sdk_library {
5539 name: "foo",
5540 srcs: ["a.java"],
5541 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005542 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005543 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005544
5545 prebuilt_apis {
5546 name: "sdk",
5547 api_dirs: ["100"],
5548 }
Paul Duffin9b879592020-05-26 13:21:35 +01005549 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005550
5551 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005552 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005553 "javalib/foo.jar",
5554 "etc/permissions/foo.xml",
5555 })
5556 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005557 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5558 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005559}
5560
Paul Duffin9b879592020-05-26 13:21:35 +01005561func TestJavaSDKLibrary_WithinApex(t *testing.T) {
5562 ctx, _ := testApex(t, `
5563 apex {
5564 name: "myapex",
5565 key: "myapex.key",
5566 java_libs: ["foo", "bar"],
5567 }
5568
5569 apex_key {
5570 name: "myapex.key",
5571 public_key: "testkey.avbpubkey",
5572 private_key: "testkey.pem",
5573 }
5574
5575 java_sdk_library {
5576 name: "foo",
5577 srcs: ["a.java"],
5578 api_packages: ["foo"],
5579 apex_available: ["myapex"],
5580 sdk_version: "none",
5581 system_modules: "none",
5582 }
5583
5584 java_library {
5585 name: "bar",
5586 srcs: ["a.java"],
5587 libs: ["foo"],
5588 apex_available: ["myapex"],
5589 sdk_version: "none",
5590 system_modules: "none",
5591 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005592
5593 prebuilt_apis {
5594 name: "sdk",
5595 api_dirs: ["100"],
5596 }
Paul Duffin9b879592020-05-26 13:21:35 +01005597 `, withFiles(filesForSdkLibrary))
5598
5599 // java_sdk_library installs both impl jar and permission XML
5600 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5601 "javalib/bar.jar",
5602 "javalib/foo.jar",
5603 "etc/permissions/foo.xml",
5604 })
5605
5606 // The bar library should depend on the implementation jar.
5607 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5608 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5609 t.Errorf("expected %q, found %#q", expected, actual)
5610 }
5611}
5612
5613func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
5614 ctx, _ := testApex(t, `
5615 apex {
5616 name: "myapex",
5617 key: "myapex.key",
5618 java_libs: ["foo"],
5619 }
5620
5621 apex_key {
5622 name: "myapex.key",
5623 public_key: "testkey.avbpubkey",
5624 private_key: "testkey.pem",
5625 }
5626
5627 java_sdk_library {
5628 name: "foo",
5629 srcs: ["a.java"],
5630 api_packages: ["foo"],
5631 apex_available: ["myapex"],
5632 sdk_version: "none",
5633 system_modules: "none",
5634 }
5635
5636 java_library {
5637 name: "bar",
5638 srcs: ["a.java"],
5639 libs: ["foo"],
5640 sdk_version: "none",
5641 system_modules: "none",
5642 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005643
5644 prebuilt_apis {
5645 name: "sdk",
5646 api_dirs: ["100"],
5647 }
Paul Duffin9b879592020-05-26 13:21:35 +01005648 `, withFiles(filesForSdkLibrary))
5649
5650 // java_sdk_library installs both impl jar and permission XML
5651 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5652 "javalib/foo.jar",
5653 "etc/permissions/foo.xml",
5654 })
5655
5656 // The bar library should depend on the stubs jar.
5657 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5658 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5659 t.Errorf("expected %q, found %#q", expected, actual)
5660 }
5661}
5662
Paul Duffineedc5d52020-06-12 17:46:39 +01005663func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Anton Hanssondff2c782020-12-21 17:10:01 +00005664 ctx, _ := testApex(t, `
5665 prebuilt_apis {
5666 name: "sdk",
5667 api_dirs: ["100"],
5668 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005669 withFiles(map[string][]byte{
5670 "apex/a.java": nil,
5671 "apex/apex_manifest.json": nil,
5672 "apex/Android.bp": []byte(`
5673 package {
5674 default_visibility: ["//visibility:private"],
5675 }
5676
5677 apex {
5678 name: "myapex",
5679 key: "myapex.key",
5680 java_libs: ["foo", "bar"],
5681 }
5682
5683 apex_key {
5684 name: "myapex.key",
5685 public_key: "testkey.avbpubkey",
5686 private_key: "testkey.pem",
5687 }
5688
5689 java_library {
5690 name: "bar",
5691 srcs: ["a.java"],
5692 libs: ["foo"],
5693 apex_available: ["myapex"],
5694 sdk_version: "none",
5695 system_modules: "none",
5696 }
5697`),
5698 "source/a.java": nil,
5699 "source/api/current.txt": nil,
5700 "source/api/removed.txt": nil,
5701 "source/Android.bp": []byte(`
5702 package {
5703 default_visibility: ["//visibility:private"],
5704 }
5705
5706 java_sdk_library {
5707 name: "foo",
5708 visibility: ["//apex"],
5709 srcs: ["a.java"],
5710 api_packages: ["foo"],
5711 apex_available: ["myapex"],
5712 sdk_version: "none",
5713 system_modules: "none",
5714 public: {
5715 enabled: true,
5716 },
5717 }
5718`),
5719 "prebuilt/a.jar": nil,
5720 "prebuilt/Android.bp": []byte(`
5721 package {
5722 default_visibility: ["//visibility:private"],
5723 }
5724
5725 java_sdk_library_import {
5726 name: "foo",
5727 visibility: ["//apex", "//source"],
5728 apex_available: ["myapex"],
5729 prefer: true,
5730 public: {
5731 jars: ["a.jar"],
5732 },
5733 }
5734`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005735 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005736 )
5737
5738 // java_sdk_library installs both impl jar and permission XML
5739 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5740 "javalib/bar.jar",
5741 "javalib/foo.jar",
5742 "etc/permissions/foo.xml",
5743 })
5744
5745 // The bar library should depend on the implementation jar.
5746 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5747 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5748 t.Errorf("expected %q, found %#q", expected, actual)
5749 }
5750}
5751
5752func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5753 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5754 apex {
5755 name: "myapex",
5756 key: "myapex.key",
5757 java_libs: ["foo"],
5758 }
5759
5760 apex_key {
5761 name: "myapex.key",
5762 public_key: "testkey.avbpubkey",
5763 private_key: "testkey.pem",
5764 }
5765
5766 java_sdk_library_import {
5767 name: "foo",
5768 apex_available: ["myapex"],
5769 prefer: true,
5770 public: {
5771 jars: ["a.jar"],
5772 },
5773 }
5774
5775 `, withFiles(filesForSdkLibrary))
5776}
5777
atrost6e126252020-01-27 17:01:16 +00005778func TestCompatConfig(t *testing.T) {
5779 ctx, _ := testApex(t, `
5780 apex {
5781 name: "myapex",
5782 key: "myapex.key",
5783 prebuilts: ["myjar-platform-compat-config"],
5784 java_libs: ["myjar"],
5785 }
5786
5787 apex_key {
5788 name: "myapex.key",
5789 public_key: "testkey.avbpubkey",
5790 private_key: "testkey.pem",
5791 }
5792
5793 platform_compat_config {
5794 name: "myjar-platform-compat-config",
5795 src: ":myjar",
5796 }
5797
5798 java_library {
5799 name: "myjar",
5800 srcs: ["foo/bar/MyClass.java"],
5801 sdk_version: "none",
5802 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005803 apex_available: [ "myapex" ],
5804 }
5805 `)
5806 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5807 "etc/compatconfig/myjar-platform-compat-config.xml",
5808 "javalib/myjar.jar",
5809 })
5810}
5811
Jiyong Park479321d2019-12-16 11:47:12 +09005812func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5813 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5814 apex {
5815 name: "myapex",
5816 key: "myapex.key",
5817 java_libs: ["myjar"],
5818 }
5819
5820 apex_key {
5821 name: "myapex.key",
5822 public_key: "testkey.avbpubkey",
5823 private_key: "testkey.pem",
5824 }
5825
5826 java_library {
5827 name: "myjar",
5828 srcs: ["foo/bar/MyClass.java"],
5829 sdk_version: "none",
5830 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005831 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005832 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005833 }
5834 `)
5835}
5836
Jiyong Park7afd1072019-12-30 16:56:33 +09005837func TestCarryRequiredModuleNames(t *testing.T) {
5838 ctx, config := testApex(t, `
5839 apex {
5840 name: "myapex",
5841 key: "myapex.key",
5842 native_shared_libs: ["mylib"],
5843 }
5844
5845 apex_key {
5846 name: "myapex.key",
5847 public_key: "testkey.avbpubkey",
5848 private_key: "testkey.pem",
5849 }
5850
5851 cc_library {
5852 name: "mylib",
5853 srcs: ["mylib.cpp"],
5854 system_shared_libs: [],
5855 stl: "none",
5856 required: ["a", "b"],
5857 host_required: ["c", "d"],
5858 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005859 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005860 }
5861 `)
5862
5863 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
5864 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
5865 name := apexBundle.BaseModuleName()
5866 prefix := "TARGET_"
5867 var builder strings.Builder
5868 data.Custom(&builder, name, prefix, "", data)
5869 androidMk := builder.String()
5870 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
5871 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
5872 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
5873}
5874
Jiyong Park7cd10e32020-01-14 09:22:18 +09005875func TestSymlinksFromApexToSystem(t *testing.T) {
5876 bp := `
5877 apex {
5878 name: "myapex",
5879 key: "myapex.key",
5880 native_shared_libs: ["mylib"],
5881 java_libs: ["myjar"],
5882 }
5883
Jiyong Park9d677202020-02-19 16:29:35 +09005884 apex {
5885 name: "myapex.updatable",
5886 key: "myapex.key",
5887 native_shared_libs: ["mylib"],
5888 java_libs: ["myjar"],
5889 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09005890 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09005891 }
5892
Jiyong Park7cd10e32020-01-14 09:22:18 +09005893 apex_key {
5894 name: "myapex.key",
5895 public_key: "testkey.avbpubkey",
5896 private_key: "testkey.pem",
5897 }
5898
5899 cc_library {
5900 name: "mylib",
5901 srcs: ["mylib.cpp"],
5902 shared_libs: ["myotherlib"],
5903 system_shared_libs: [],
5904 stl: "none",
5905 apex_available: [
5906 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005907 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005908 "//apex_available:platform",
5909 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005910 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005911 }
5912
5913 cc_library {
5914 name: "myotherlib",
5915 srcs: ["mylib.cpp"],
5916 system_shared_libs: [],
5917 stl: "none",
5918 apex_available: [
5919 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005920 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005921 "//apex_available:platform",
5922 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005923 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005924 }
5925
5926 java_library {
5927 name: "myjar",
5928 srcs: ["foo/bar/MyClass.java"],
5929 sdk_version: "none",
5930 system_modules: "none",
5931 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09005932 apex_available: [
5933 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005934 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005935 "//apex_available:platform",
5936 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005937 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005938 }
5939
5940 java_library {
5941 name: "myotherjar",
5942 srcs: ["foo/bar/MyClass.java"],
5943 sdk_version: "none",
5944 system_modules: "none",
5945 apex_available: [
5946 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005947 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005948 "//apex_available:platform",
5949 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005950 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005951 }
5952 `
5953
5954 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
5955 for _, f := range files {
5956 if f.path == file {
5957 if f.isLink {
5958 t.Errorf("%q is not a real file", file)
5959 }
5960 return
5961 }
5962 }
5963 t.Errorf("%q is not found", file)
5964 }
5965
5966 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
5967 for _, f := range files {
5968 if f.path == file {
5969 if !f.isLink {
5970 t.Errorf("%q is not a symlink", file)
5971 }
5972 return
5973 }
5974 }
5975 t.Errorf("%q is not found", file)
5976 }
5977
Jiyong Park9d677202020-02-19 16:29:35 +09005978 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
5979 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09005980 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005981 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005982 ensureRealfileExists(t, files, "javalib/myjar.jar")
5983 ensureRealfileExists(t, files, "lib64/mylib.so")
5984 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5985
Jiyong Park9d677202020-02-19 16:29:35 +09005986 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5987 ensureRealfileExists(t, files, "javalib/myjar.jar")
5988 ensureRealfileExists(t, files, "lib64/mylib.so")
5989 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5990
5991 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09005992 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005993 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005994 ensureRealfileExists(t, files, "javalib/myjar.jar")
5995 ensureRealfileExists(t, files, "lib64/mylib.so")
5996 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09005997
5998 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5999 ensureRealfileExists(t, files, "javalib/myjar.jar")
6000 ensureRealfileExists(t, files, "lib64/mylib.so")
6001 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006002}
6003
Yo Chiange8128052020-07-23 20:09:18 +08006004func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
6005 ctx, config := testApex(t, `
6006 apex {
6007 name: "myapex",
6008 key: "myapex.key",
6009 native_shared_libs: ["mylib"],
6010 }
6011
6012 apex_key {
6013 name: "myapex.key",
6014 public_key: "testkey.avbpubkey",
6015 private_key: "testkey.pem",
6016 }
6017
6018 cc_library_shared {
6019 name: "mylib",
6020 srcs: ["mylib.cpp"],
6021 shared_libs: ["myotherlib"],
6022 system_shared_libs: [],
6023 stl: "none",
6024 apex_available: [
6025 "myapex",
6026 "//apex_available:platform",
6027 ],
6028 }
6029
6030 cc_prebuilt_library_shared {
6031 name: "myotherlib",
6032 srcs: ["prebuilt.so"],
6033 system_shared_libs: [],
6034 stl: "none",
6035 apex_available: [
6036 "myapex",
6037 "//apex_available:platform",
6038 ],
6039 }
6040 `)
6041
6042 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
6043 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
6044 var builder strings.Builder
6045 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6046 androidMk := builder.String()
6047 // `myotherlib` is added to `myapex` as symlink
6048 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6049 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6050 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6051 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006052 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 +08006053}
6054
Jooyung Han643adc42020-02-27 13:50:06 +09006055func TestApexWithJniLibs(t *testing.T) {
6056 ctx, _ := testApex(t, `
6057 apex {
6058 name: "myapex",
6059 key: "myapex.key",
6060 jni_libs: ["mylib"],
6061 }
6062
6063 apex_key {
6064 name: "myapex.key",
6065 public_key: "testkey.avbpubkey",
6066 private_key: "testkey.pem",
6067 }
6068
6069 cc_library {
6070 name: "mylib",
6071 srcs: ["mylib.cpp"],
6072 shared_libs: ["mylib2"],
6073 system_shared_libs: [],
6074 stl: "none",
6075 apex_available: [ "myapex" ],
6076 }
6077
6078 cc_library {
6079 name: "mylib2",
6080 srcs: ["mylib.cpp"],
6081 system_shared_libs: [],
6082 stl: "none",
6083 apex_available: [ "myapex" ],
6084 }
6085 `)
6086
6087 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6088 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6089 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6090 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6091 "lib64/mylib.so",
6092 "lib64/mylib2.so",
6093 })
6094}
6095
Jooyung Han49f67012020-04-17 13:43:10 +09006096func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
6097 ctx, _ := testApex(t, `
6098 apex {
6099 name: "myapex",
6100 key: "myapex.key",
6101 }
6102 apex_key {
6103 name: "myapex.key",
6104 public_key: "testkey.avbpubkey",
6105 private_key: "testkey.pem",
6106 }
6107 `, func(fs map[string][]byte, config android.Config) {
6108 delete(config.Targets, android.Android)
6109 config.AndroidCommonTarget = android.Target{}
6110 })
6111
6112 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6113 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6114 }
6115}
6116
Jiyong Parkbd159612020-02-28 15:22:21 +09006117func TestAppBundle(t *testing.T) {
6118 ctx, _ := testApex(t, `
6119 apex {
6120 name: "myapex",
6121 key: "myapex.key",
6122 apps: ["AppFoo"],
6123 }
6124
6125 apex_key {
6126 name: "myapex.key",
6127 public_key: "testkey.avbpubkey",
6128 private_key: "testkey.pem",
6129 }
6130
6131 android_app {
6132 name: "AppFoo",
6133 srcs: ["foo/bar/MyClass.java"],
6134 sdk_version: "none",
6135 system_modules: "none",
6136 apex_available: [ "myapex" ],
6137 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006138 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006139
Colin Crosscf371cc2020-11-13 11:48:42 -08006140 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006141 content := bundleConfigRule.Args["content"]
6142
6143 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006144 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 +09006145}
6146
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006147func TestAppSetBundle(t *testing.T) {
6148 ctx, _ := testApex(t, `
6149 apex {
6150 name: "myapex",
6151 key: "myapex.key",
6152 apps: ["AppSet"],
6153 }
6154
6155 apex_key {
6156 name: "myapex.key",
6157 public_key: "testkey.avbpubkey",
6158 private_key: "testkey.pem",
6159 }
6160
6161 android_app_set {
6162 name: "AppSet",
6163 set: "AppSet.apks",
6164 }`)
6165 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006166 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006167 content := bundleConfigRule.Args["content"]
6168 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6169 s := mod.Rule("apexRule").Args["copy_commands"]
6170 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6171 if len(copyCmds) != 3 {
6172 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6173 }
6174 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6175 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6176 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6177}
6178
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006179func TestAppSetBundlePrebuilt(t *testing.T) {
6180 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
6181 bp := `
6182 apex_set {
6183 name: "myapex",
6184 filename: "foo_v2.apex",
6185 sanitized: {
6186 none: { set: "myapex.apks", },
6187 hwaddress: { set: "myapex.hwasan.apks", },
6188 },
6189 }`
6190 fs["Android.bp"] = []byte(bp)
6191
6192 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
6193 })
6194
6195 m := ctx.ModuleForTests("myapex", "android_common")
6196 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6197
6198 actual := extractedApex.Inputs
6199 if len(actual) != 1 {
6200 t.Errorf("expected a single input")
6201 }
6202
6203 expected := "myapex.hwasan.apks"
6204 if actual[0].String() != expected {
6205 t.Errorf("expected %s, got %s", expected, actual[0].String())
6206 }
6207}
6208
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006209func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006210 t.Helper()
6211
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006212 bp := `
6213 java_library {
6214 name: "some-updatable-apex-lib",
6215 srcs: ["a.java"],
6216 sdk_version: "current",
6217 apex_available: [
6218 "some-updatable-apex",
6219 ],
6220 }
6221
6222 java_library {
6223 name: "some-non-updatable-apex-lib",
6224 srcs: ["a.java"],
6225 apex_available: [
6226 "some-non-updatable-apex",
6227 ],
6228 }
6229
6230 java_library {
6231 name: "some-platform-lib",
6232 srcs: ["a.java"],
6233 sdk_version: "current",
6234 installable: true,
6235 }
6236
6237 java_library {
6238 name: "some-art-lib",
6239 srcs: ["a.java"],
6240 sdk_version: "current",
6241 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006242 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006243 ],
6244 hostdex: true,
6245 }
6246
6247 apex {
6248 name: "some-updatable-apex",
6249 key: "some-updatable-apex.key",
6250 java_libs: ["some-updatable-apex-lib"],
6251 updatable: true,
6252 min_sdk_version: "current",
6253 }
6254
6255 apex {
6256 name: "some-non-updatable-apex",
6257 key: "some-non-updatable-apex.key",
6258 java_libs: ["some-non-updatable-apex-lib"],
6259 }
6260
6261 apex_key {
6262 name: "some-updatable-apex.key",
6263 }
6264
6265 apex_key {
6266 name: "some-non-updatable-apex.key",
6267 }
6268
6269 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006270 name: "com.android.art.debug",
6271 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006272 java_libs: ["some-art-lib"],
6273 updatable: true,
6274 min_sdk_version: "current",
6275 }
6276
6277 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006278 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006279 }
6280
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006281 filegroup {
6282 name: "some-updatable-apex-file_contexts",
6283 srcs: [
6284 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6285 ],
6286 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006287
6288 filegroup {
6289 name: "some-non-updatable-apex-file_contexts",
6290 srcs: [
6291 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6292 ],
6293 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006294 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006295
6296 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6297}
6298
Paul Duffin064b70c2020-11-02 17:32:38 +00006299func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006300 t.Helper()
6301
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006302 bp += cc.GatherRequiredDepsForTest(android.Android)
6303 bp += java.GatherRequiredDepsForTest()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006304
6305 fs := map[string][]byte{
6306 "a.java": nil,
6307 "a.jar": nil,
6308 "build/make/target/product/security": nil,
6309 "apex_manifest.json": nil,
6310 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006311 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006312 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6313 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6314 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006315 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006316 }
6317 cc.GatherRequiredFilesForTest(fs)
6318
Colin Crossae8600b2020-10-29 17:09:13 -07006319 config := android.TestArchConfig(buildDir, nil, bp, fs)
6320
6321 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006322 ctx.RegisterModuleType("apex", BundleFactory)
6323 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00006324 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006325 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01006326 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin021f4e52020-07-30 16:04:17 +01006327 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006328 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006329 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf38931c2021-02-05 16:58:28 +00006330 java.RegisterHiddenApiSingletonComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006331 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6332 ctx.PreDepsMutators(RegisterPreDepsMutators)
6333 ctx.PostDepsMutators(RegisterPostDepsMutators)
6334
Colin Crossae8600b2020-10-29 17:09:13 -07006335 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006336
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006337 pathCtx := android.PathContextForTesting(config)
6338 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6339 transformDexpreoptConfig(dexpreoptConfig)
6340 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6341
Paul Duffinf38931c2021-02-05 16:58:28 +00006342 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
Paul Duffin4fd997b2021-02-03 20:06:33 +00006343 // product variables.
Paul Duffinf38931c2021-02-05 16:58:28 +00006344 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6345 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6346
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006347 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6348 android.FailIfErrored(t, errs)
6349
6350 _, errs = ctx.PrepareBuildActions(config)
6351 if errmsg == "" {
6352 android.FailIfErrored(t, errs)
6353 } else if len(errs) > 0 {
6354 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006355 } else {
6356 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6357 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006358
6359 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006360}
6361
Jooyung Han548640b2020-04-27 12:10:30 +09006362func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6363 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6364 apex {
6365 name: "myapex",
6366 key: "myapex.key",
6367 updatable: true,
6368 }
6369
6370 apex_key {
6371 name: "myapex.key",
6372 public_key: "testkey.avbpubkey",
6373 private_key: "testkey.pem",
6374 }
6375 `)
6376}
6377
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006378func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006379 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006380 var transform func(*dexpreopt.GlobalConfig)
6381
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006382 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6383 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006384 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006385 }
6386 testNoUpdatableJarsInBootImage(t, "", transform)
6387 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006388
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006389 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006390 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 +01006391 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006392 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006393 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006394 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006395 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006396
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006397 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 -07006398 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 +01006399 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006400 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006401 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006402 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006403 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006404
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006405 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 -07006406 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
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{"some-non-updatable-apex:some-non-updatable-apex-lib"})
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 Trafimovich7c140d82020-04-22 18:05:58 +01006412
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006413 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 -07006414 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 +01006415 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006416 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
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("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6422 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006423 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006424 }
6425 testNoUpdatableJarsInBootImage(t, "", transform)
6426 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006427
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006428 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006429 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006430 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006431 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006432 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006433 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006434 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006435
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006436 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006437 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006438 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006439 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006440 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006441 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006442 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006443
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006444 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006445 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006446 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006447 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006448 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006449 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006450 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006451
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006452 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6453 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006454 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006455 }
6456 testNoUpdatableJarsInBootImage(t, "", transform)
6457 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006458
6459}
6460
6461func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6462 transform := func(config *dexpreopt.GlobalConfig) {
6463 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6464 }
6465 t.Run("prebuilt no source", func(t *testing.T) {
6466 testDexpreoptWithApexes(t, `
6467 prebuilt_apex {
6468 name: "myapex" ,
6469 arch: {
6470 arm64: {
6471 src: "myapex-arm64.apex",
6472 },
6473 arm: {
6474 src: "myapex-arm.apex",
6475 },
6476 },
6477 exported_java_libs: ["libfoo"],
6478 }
6479
6480 java_import {
6481 name: "libfoo",
6482 jars: ["libfoo.jar"],
6483 }
6484`, "", transform)
6485 })
6486
6487 t.Run("prebuilt no source", func(t *testing.T) {
6488 testDexpreoptWithApexes(t, `
6489 prebuilt_apex {
6490 name: "myapex" ,
6491 arch: {
6492 arm64: {
6493 src: "myapex-arm64.apex",
6494 },
6495 arm: {
6496 src: "myapex-arm.apex",
6497 },
6498 },
6499 exported_java_libs: ["libfoo"],
6500 }
6501
6502 java_import {
6503 name: "libfoo",
6504 jars: ["libfoo.jar"],
6505 }
6506`, "", transform)
6507 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006508}
6509
Andrei Onea115e7e72020-06-05 21:14:03 +01006510func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6511 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006512 bp += `
6513 apex_key {
6514 name: "myapex.key",
6515 public_key: "testkey.avbpubkey",
6516 private_key: "testkey.pem",
6517 }`
6518 fs := map[string][]byte{
6519 "lib1/src/A.java": nil,
6520 "lib2/src/B.java": nil,
6521 "system/sepolicy/apex/myapex-file_contexts": nil,
6522 }
6523
Colin Crossae8600b2020-10-29 17:09:13 -07006524 config := android.TestArchConfig(buildDir, nil, bp, fs)
6525 android.SetTestNeverallowRules(config, rules)
6526 updatableBootJars := make([]string, 0, len(apexBootJars))
6527 for _, apexBootJar := range apexBootJars {
6528 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6529 }
6530 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6531
6532 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006533 ctx.RegisterModuleType("apex", BundleFactory)
6534 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6535 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6536 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006537 java.RegisterRequiredBuildComponentsForTest(ctx)
Andrei Onea115e7e72020-06-05 21:14:03 +01006538 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6539 ctx.PreDepsMutators(RegisterPreDepsMutators)
6540 ctx.PostDepsMutators(RegisterPostDepsMutators)
6541 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6542
Colin Crossae8600b2020-10-29 17:09:13 -07006543 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006544
6545 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6546 android.FailIfErrored(t, errs)
6547
6548 _, errs = ctx.PrepareBuildActions(config)
6549 if errmsg == "" {
6550 android.FailIfErrored(t, errs)
6551 } else if len(errs) > 0 {
6552 android.FailIfNoMatchingErrors(t, errmsg, errs)
6553 return
6554 } else {
6555 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6556 }
6557}
6558
6559func TestApexPermittedPackagesRules(t *testing.T) {
6560 testcases := []struct {
6561 name string
6562 expectedError string
6563 bp string
6564 bootJars []string
6565 modulesPackages map[string][]string
6566 }{
6567
6568 {
6569 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6570 expectedError: "",
6571 bp: `
6572 java_library {
6573 name: "bcp_lib1",
6574 srcs: ["lib1/src/*.java"],
6575 permitted_packages: ["foo.bar"],
6576 apex_available: ["myapex"],
6577 sdk_version: "none",
6578 system_modules: "none",
6579 }
6580 java_library {
6581 name: "nonbcp_lib2",
6582 srcs: ["lib2/src/*.java"],
6583 apex_available: ["myapex"],
6584 permitted_packages: ["a.b"],
6585 sdk_version: "none",
6586 system_modules: "none",
6587 }
6588 apex {
6589 name: "myapex",
6590 key: "myapex.key",
6591 java_libs: ["bcp_lib1", "nonbcp_lib2"],
6592 }`,
6593 bootJars: []string{"bcp_lib1"},
6594 modulesPackages: map[string][]string{
6595 "myapex": []string{
6596 "foo.bar",
6597 },
6598 },
6599 },
6600 {
6601 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6602 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.`,
6603 bp: `
6604 java_library {
6605 name: "bcp_lib1",
6606 srcs: ["lib1/src/*.java"],
6607 apex_available: ["myapex"],
6608 permitted_packages: ["foo.bar"],
6609 sdk_version: "none",
6610 system_modules: "none",
6611 }
6612 java_library {
6613 name: "bcp_lib2",
6614 srcs: ["lib2/src/*.java"],
6615 apex_available: ["myapex"],
6616 permitted_packages: ["foo.bar", "bar.baz"],
6617 sdk_version: "none",
6618 system_modules: "none",
6619 }
6620 apex {
6621 name: "myapex",
6622 key: "myapex.key",
6623 java_libs: ["bcp_lib1", "bcp_lib2"],
6624 }
6625 `,
6626 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6627 modulesPackages: map[string][]string{
6628 "myapex": []string{
6629 "foo.bar",
6630 },
6631 },
6632 },
6633 }
6634 for _, tc := range testcases {
6635 t.Run(tc.name, func(t *testing.T) {
6636 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6637 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6638 })
6639 }
6640}
6641
Jiyong Park62304bb2020-04-13 16:19:48 +09006642func TestTestFor(t *testing.T) {
6643 ctx, _ := testApex(t, `
6644 apex {
6645 name: "myapex",
6646 key: "myapex.key",
6647 native_shared_libs: ["mylib", "myprivlib"],
6648 }
6649
6650 apex_key {
6651 name: "myapex.key",
6652 public_key: "testkey.avbpubkey",
6653 private_key: "testkey.pem",
6654 }
6655
6656 cc_library {
6657 name: "mylib",
6658 srcs: ["mylib.cpp"],
6659 system_shared_libs: [],
6660 stl: "none",
6661 stubs: {
6662 versions: ["1"],
6663 },
6664 apex_available: ["myapex"],
6665 }
6666
6667 cc_library {
6668 name: "myprivlib",
6669 srcs: ["mylib.cpp"],
6670 system_shared_libs: [],
6671 stl: "none",
6672 apex_available: ["myapex"],
6673 }
6674
6675
6676 cc_test {
6677 name: "mytest",
6678 gtest: false,
6679 srcs: ["mylib.cpp"],
6680 system_shared_libs: [],
6681 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006682 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006683 test_for: ["myapex"]
6684 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006685
6686 cc_library {
6687 name: "mytestlib",
6688 srcs: ["mylib.cpp"],
6689 system_shared_libs: [],
6690 shared_libs: ["mylib", "myprivlib"],
6691 stl: "none",
6692 test_for: ["myapex"],
6693 }
6694
6695 cc_benchmark {
6696 name: "mybench",
6697 srcs: ["mylib.cpp"],
6698 system_shared_libs: [],
6699 shared_libs: ["mylib", "myprivlib"],
6700 stl: "none",
6701 test_for: ["myapex"],
6702 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006703 `)
6704
6705 // the test 'mytest' is a test for the apex, therefore is linked to the
6706 // actual implementation of mylib instead of its stub.
6707 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6708 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6709 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park46a512f2020-12-04 18:02:13 +09006710
6711 // The same should be true for cc_library
6712 ldFlags = ctx.ModuleForTests("mytestlib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
6713 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6714 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
6715
6716 // ... and for cc_benchmark
6717 ldFlags = ctx.ModuleForTests("mybench", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6718 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6719 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006720}
6721
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006722// TODO(jungjw): Move this to proptools
6723func intPtr(i int) *int {
6724 return &i
6725}
6726
6727func TestApexSet(t *testing.T) {
6728 ctx, config := testApex(t, `
6729 apex_set {
6730 name: "myapex",
6731 set: "myapex.apks",
6732 filename: "foo_v2.apex",
6733 overrides: ["foo"],
6734 }
6735 `, func(fs map[string][]byte, config android.Config) {
6736 config.TestProductVariables.Platform_sdk_version = intPtr(30)
Jaewoong Jung829b7132020-06-10 12:23:32 -07006737 config.Targets[android.Android] = []android.Target{
6738 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6739 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6740 }
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006741 })
6742
6743 m := ctx.ModuleForTests("myapex", "android_common")
6744
6745 // Check extract_apks tool parameters.
6746 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6747 actual := extractedApex.Args["abis"]
6748 expected := "ARMEABI_V7A,ARM64_V8A"
6749 if actual != expected {
6750 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6751 }
6752 actual = extractedApex.Args["sdk-version"]
6753 expected = "30"
6754 if actual != expected {
6755 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6756 }
6757
6758 a := m.Module().(*ApexSet)
6759 expectedOverrides := []string{"foo"}
6760 actualOverrides := android.AndroidMkEntriesForTest(t, config, "", a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
6761 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6762 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6763 }
6764}
6765
Jiyong Park7d95a512020-05-10 15:16:24 +09006766func TestNoStaticLinkingToStubsLib(t *testing.T) {
6767 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6768 apex {
6769 name: "myapex",
6770 key: "myapex.key",
6771 native_shared_libs: ["mylib"],
6772 }
6773
6774 apex_key {
6775 name: "myapex.key",
6776 public_key: "testkey.avbpubkey",
6777 private_key: "testkey.pem",
6778 }
6779
6780 cc_library {
6781 name: "mylib",
6782 srcs: ["mylib.cpp"],
6783 static_libs: ["otherlib"],
6784 system_shared_libs: [],
6785 stl: "none",
6786 apex_available: [ "myapex" ],
6787 }
6788
6789 cc_library {
6790 name: "otherlib",
6791 srcs: ["mylib.cpp"],
6792 system_shared_libs: [],
6793 stl: "none",
6794 stubs: {
6795 versions: ["1", "2", "3"],
6796 },
6797 apex_available: [ "myapex" ],
6798 }
6799 `)
6800}
6801
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006802func TestApexKeysTxt(t *testing.T) {
6803 ctx, _ := testApex(t, `
6804 apex {
6805 name: "myapex",
6806 key: "myapex.key",
6807 }
6808
6809 apex_key {
6810 name: "myapex.key",
6811 public_key: "testkey.avbpubkey",
6812 private_key: "testkey.pem",
6813 }
6814
6815 prebuilt_apex {
6816 name: "myapex",
6817 prefer: true,
6818 arch: {
6819 arm64: {
6820 src: "myapex-arm64.apex",
6821 },
6822 arm: {
6823 src: "myapex-arm.apex",
6824 },
6825 },
6826 }
6827
6828 apex_set {
6829 name: "myapex_set",
6830 set: "myapex.apks",
6831 filename: "myapex_set.apex",
6832 overrides: ["myapex"],
6833 }
6834 `)
6835
6836 apexKeysText := ctx.SingletonForTests("apex_keys_text")
6837 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
6838 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 +09006839 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 +09006840}
6841
Jooyung Han938b5932020-06-20 12:47:47 +09006842func TestAllowedFiles(t *testing.T) {
6843 ctx, _ := testApex(t, `
6844 apex {
6845 name: "myapex",
6846 key: "myapex.key",
6847 apps: ["app"],
6848 allowed_files: "allowed.txt",
6849 }
6850
6851 apex_key {
6852 name: "myapex.key",
6853 public_key: "testkey.avbpubkey",
6854 private_key: "testkey.pem",
6855 }
6856
6857 android_app {
6858 name: "app",
6859 srcs: ["foo/bar/MyClass.java"],
6860 package_name: "foo",
6861 sdk_version: "none",
6862 system_modules: "none",
6863 apex_available: [ "myapex" ],
6864 }
6865 `, withFiles(map[string][]byte{
6866 "sub/Android.bp": []byte(`
6867 override_apex {
6868 name: "override_myapex",
6869 base: "myapex",
6870 apps: ["override_app"],
6871 allowed_files: ":allowed",
6872 }
6873 // Overridable "path" property should be referenced indirectly
6874 filegroup {
6875 name: "allowed",
6876 srcs: ["allowed.txt"],
6877 }
6878 override_android_app {
6879 name: "override_app",
6880 base: "app",
6881 package_name: "bar",
6882 }
6883 `),
6884 }))
6885
6886 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
6887 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
6888 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
6889 }
6890
6891 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
6892 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
6893 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
6894 }
6895}
6896
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006897func TestNonPreferredPrebuiltDependency(t *testing.T) {
6898 _, _ = testApex(t, `
6899 apex {
6900 name: "myapex",
6901 key: "myapex.key",
6902 native_shared_libs: ["mylib"],
6903 }
6904
6905 apex_key {
6906 name: "myapex.key",
6907 public_key: "testkey.avbpubkey",
6908 private_key: "testkey.pem",
6909 }
6910
6911 cc_library {
6912 name: "mylib",
6913 srcs: ["mylib.cpp"],
6914 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07006915 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006916 },
6917 apex_available: ["myapex"],
6918 }
6919
6920 cc_prebuilt_library_shared {
6921 name: "mylib",
6922 prefer: false,
6923 srcs: ["prebuilt.so"],
6924 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07006925 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01006926 },
6927 apex_available: ["myapex"],
6928 }
6929 `)
6930}
6931
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00006932func TestCompressedApex(t *testing.T) {
6933 ctx, config := testApex(t, `
6934 apex {
6935 name: "myapex",
6936 key: "myapex.key",
6937 compressible: true,
6938 }
6939 apex_key {
6940 name: "myapex.key",
6941 public_key: "testkey.avbpubkey",
6942 private_key: "testkey.pem",
6943 }
6944 `, func(fs map[string][]byte, config android.Config) {
6945 config.TestProductVariables.CompressedApex = proptools.BoolPtr(true)
6946 })
6947
6948 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
6949 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
6950
6951 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
6952 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
6953
6954 // Make sure output of bundle is .capex
6955 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
6956 ensureContains(t, ab.outputFile.String(), "myapex.capex")
6957
6958 // Verify android.mk rules
6959 data := android.AndroidMkDataForTest(t, config, "", ab)
6960 var builder strings.Builder
6961 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
6962 androidMk := builder.String()
6963 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
6964}
6965
Martin Stjernholm2856c662020-12-02 15:03:42 +00006966func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
6967 ctx, config := testApex(t, `
6968 apex {
6969 name: "myapex",
6970 key: "myapex.key",
6971 native_shared_libs: ["mylib"],
6972 }
6973
6974 apex_key {
6975 name: "myapex.key",
6976 public_key: "testkey.avbpubkey",
6977 private_key: "testkey.pem",
6978 }
6979
6980 cc_library {
6981 name: "mylib",
6982 srcs: ["mylib.cpp"],
6983 apex_available: ["myapex"],
6984 shared_libs: ["otherlib"],
6985 system_shared_libs: [],
6986 }
6987
6988 cc_library {
6989 name: "otherlib",
6990 srcs: ["mylib.cpp"],
6991 stubs: {
6992 versions: ["current"],
6993 },
6994 }
6995
6996 cc_prebuilt_library_shared {
6997 name: "otherlib",
6998 prefer: true,
6999 srcs: ["prebuilt.so"],
7000 stubs: {
7001 versions: ["current"],
7002 },
7003 }
7004 `)
7005
7006 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7007 data := android.AndroidMkDataForTest(t, config, "", ab)
7008 var builder strings.Builder
7009 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7010 androidMk := builder.String()
7011
7012 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7013 // a thing there.
7014 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7015}
7016
Jiyong Parke3867542020-12-03 17:28:25 +09007017func TestExcludeDependency(t *testing.T) {
7018 ctx, _ := testApex(t, `
7019 apex {
7020 name: "myapex",
7021 key: "myapex.key",
7022 native_shared_libs: ["mylib"],
7023 }
7024
7025 apex_key {
7026 name: "myapex.key",
7027 public_key: "testkey.avbpubkey",
7028 private_key: "testkey.pem",
7029 }
7030
7031 cc_library {
7032 name: "mylib",
7033 srcs: ["mylib.cpp"],
7034 system_shared_libs: [],
7035 stl: "none",
7036 apex_available: ["myapex"],
7037 shared_libs: ["mylib2"],
7038 target: {
7039 apex: {
7040 exclude_shared_libs: ["mylib2"],
7041 },
7042 },
7043 }
7044
7045 cc_library {
7046 name: "mylib2",
7047 srcs: ["mylib.cpp"],
7048 system_shared_libs: [],
7049 stl: "none",
7050 }
7051 `)
7052
7053 // Check if mylib is linked to mylib2 for the non-apex target
7054 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7055 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7056
7057 // Make sure that the link doesn't occur for the apex target
7058 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7059 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7060
7061 // It shouldn't appear in the copy cmd as well.
7062 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7063 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7064}
7065
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007066func TestPrebuiltStubLibDep(t *testing.T) {
7067 bpBase := `
7068 apex {
7069 name: "myapex",
7070 key: "myapex.key",
7071 native_shared_libs: ["mylib"],
7072 }
7073 apex_key {
7074 name: "myapex.key",
7075 public_key: "testkey.avbpubkey",
7076 private_key: "testkey.pem",
7077 }
7078 cc_library {
7079 name: "mylib",
7080 srcs: ["mylib.cpp"],
7081 apex_available: ["myapex"],
7082 shared_libs: ["stublib"],
7083 system_shared_libs: [],
7084 }
7085 apex {
7086 name: "otherapex",
7087 enabled: %s,
7088 key: "myapex.key",
7089 native_shared_libs: ["stublib"],
7090 }
7091 `
7092
7093 stublibSourceBp := `
7094 cc_library {
7095 name: "stublib",
7096 srcs: ["mylib.cpp"],
7097 apex_available: ["otherapex"],
7098 system_shared_libs: [],
7099 stl: "none",
7100 stubs: {
7101 versions: ["1"],
7102 },
7103 }
7104 `
7105
7106 stublibPrebuiltBp := `
7107 cc_prebuilt_library_shared {
7108 name: "stublib",
7109 srcs: ["prebuilt.so"],
7110 apex_available: ["otherapex"],
7111 stubs: {
7112 versions: ["1"],
7113 },
7114 %s
7115 }
7116 `
7117
7118 tests := []struct {
7119 name string
7120 stublibBp string
7121 usePrebuilt bool
7122 modNames []string // Modules to collect AndroidMkEntries for
7123 otherApexEnabled []string
7124 }{
7125 {
7126 name: "only_source",
7127 stublibBp: stublibSourceBp,
7128 usePrebuilt: false,
7129 modNames: []string{"stublib"},
7130 otherApexEnabled: []string{"true", "false"},
7131 },
7132 {
7133 name: "source_preferred",
7134 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7135 usePrebuilt: false,
7136 modNames: []string{"stublib", "prebuilt_stublib"},
7137 otherApexEnabled: []string{"true", "false"},
7138 },
7139 {
7140 name: "prebuilt_preferred",
7141 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7142 usePrebuilt: true,
7143 modNames: []string{"stublib", "prebuilt_stublib"},
7144 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7145 },
7146 {
7147 name: "only_prebuilt",
7148 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7149 usePrebuilt: true,
7150 modNames: []string{"stublib"},
7151 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7152 },
7153 }
7154
7155 for _, test := range tests {
7156 t.Run(test.name, func(t *testing.T) {
7157 for _, otherApexEnabled := range test.otherApexEnabled {
7158 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
7159 ctx, config := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
7160
7161 type modAndMkEntries struct {
7162 mod *cc.Module
7163 mkEntries android.AndroidMkEntries
7164 }
7165 entries := []*modAndMkEntries{}
7166
7167 // Gather shared lib modules that are installable
7168 for _, modName := range test.modNames {
7169 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7170 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7171 continue
7172 }
7173 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007174 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007175 continue
7176 }
7177 for _, ent := range android.AndroidMkEntriesForTest(t, config, "", mod) {
7178 if ent.Disabled {
7179 continue
7180 }
7181 entries = append(entries, &modAndMkEntries{
7182 mod: mod,
7183 mkEntries: ent,
7184 })
7185 }
7186 }
7187 }
7188
7189 var entry *modAndMkEntries = nil
7190 for _, ent := range entries {
7191 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7192 if entry != nil {
7193 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7194 } else {
7195 entry = ent
7196 }
7197 }
7198 }
7199
7200 if entry == nil {
7201 t.Errorf("AndroidMk entry for \"stublib\" missing")
7202 } else {
7203 isPrebuilt := entry.mod.Prebuilt() != nil
7204 if isPrebuilt != test.usePrebuilt {
7205 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7206 }
7207 if !entry.mod.IsStubs() {
7208 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7209 }
7210 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7211 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7212 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007213 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
7214 expected := "-D__STUBLIB_API__=1"
7215 if !android.InList(expected, cflags) {
7216 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7217 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007218 }
7219 })
7220 }
7221 })
7222 }
7223}
7224
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007225func TestMain(m *testing.M) {
7226 run := func() int {
7227 setUp()
7228 defer tearDown()
7229
7230 return m.Run()
7231 }
7232
7233 os.Exit(run())
7234}