blob: 9662a7f78afa4a79c7c1b8b150f2ecdf896ecb40 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090024 "strings"
25 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090026
27 "github.com/google/blueprint/proptools"
28
29 "android/soong/android"
30 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000031 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070032 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090033 "android/soong/java"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070034 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090035)
36
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070037var buildDir string
38
Jooyung Hand3639552019-08-09 12:57:43 +090039// names returns name list from white space separated string
40func names(s string) (ns []string) {
41 for _, n := range strings.Split(s, " ") {
42 if len(n) > 0 {
43 ns = append(ns, n)
44 }
45 }
46 return
47}
48
Jooyung Han344d5432019-08-23 11:17:39 +090049func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
50 t.Helper()
51 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090052 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
53 if len(errs) > 0 {
54 android.FailIfNoMatchingErrors(t, pattern, errs)
55 return
56 }
57 _, errs = ctx.PrepareBuildActions(config)
58 if len(errs) > 0 {
59 android.FailIfNoMatchingErrors(t, pattern, errs)
60 return
61 }
62
63 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
64}
65
Jooyung Han344d5432019-08-23 11:17:39 +090066func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
67 t.Helper()
68 ctx, config := testApexContext(t, bp, handlers...)
Paul Duffineedc5d52020-06-12 17:46:39 +010069 _, errs := ctx.ParseBlueprintsFiles(".")
Jooyung Han5c998b92019-06-27 11:30:33 +090070 android.FailIfErrored(t, errs)
71 _, errs = ctx.PrepareBuildActions(config)
72 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070073 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090074}
75
Jooyung Han344d5432019-08-23 11:17:39 +090076type testCustomizer func(fs map[string][]byte, config android.Config)
77
78func withFiles(files map[string][]byte) testCustomizer {
79 return func(fs map[string][]byte, config android.Config) {
80 for k, v := range files {
81 fs[k] = v
82 }
83 }
84}
85
86func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
87 return func(fs map[string][]byte, config android.Config) {
88 for k, v := range targets {
89 config.Targets[k] = v
90 }
91 }
92}
93
Jooyung Han35155c42020-02-06 17:33:20 +090094// withNativeBridgeTargets sets configuration with targets including:
95// - X86_64 (primary)
96// - X86 (secondary)
97// - Arm64 on X86_64 (native bridge)
98// - Arm on X86 (native bridge)
Sasha Smundak18d98bc2020-05-27 16:36:07 -070099func withNativeBridgeEnabled(_ map[string][]byte, config android.Config) {
Jooyung Han35155c42020-02-06 17:33:20 +0900100 config.Targets[android.Android] = []android.Target{
101 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
102 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
103 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
104 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
105 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
106 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
107 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
108 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
109 }
110}
111
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900112func withManifestPackageNameOverrides(specs []string) testCustomizer {
113 return func(fs map[string][]byte, config android.Config) {
114 config.TestProductVariables.ManifestPackageNameOverrides = specs
115 }
116}
117
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700118func withBinder32bit(_ map[string][]byte, config android.Config) {
Jooyung Han31c470b2019-10-18 16:26:59 +0900119 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
120}
121
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700122func withUnbundledBuild(_ map[string][]byte, config android.Config) {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900123 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
124}
125
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700126func testApexContext(_ *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +0900127 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900128
129 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900130 filegroup {
131 name: "myapex-file_contexts",
132 srcs: [
133 "system/sepolicy/apex/myapex-file_contexts",
134 ],
135 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900136 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800137
Colin Crossf9aabd72020-02-15 11:29:50 -0800138 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
139
Dario Frenicde2a032019-10-27 00:29:22 +0100140 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900141
Jooyung Han344d5432019-08-23 11:17:39 +0900142 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900143 "a.java": nil,
144 "PrebuiltAppFoo.apk": nil,
145 "PrebuiltAppFooPriv.apk": nil,
146 "build/make/target/product/security": nil,
147 "apex_manifest.json": nil,
148 "AndroidManifest.xml": nil,
149 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900150 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900151 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900152 "system/sepolicy/apex/otherapex-file_contexts": nil,
153 "system/sepolicy/apex/commonapex-file_contexts": nil,
154 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800155 "mylib.cpp": nil,
156 "mylib_common.cpp": nil,
157 "mytest.cpp": nil,
158 "mytest1.cpp": nil,
159 "mytest2.cpp": nil,
160 "mytest3.cpp": nil,
161 "myprebuilt": nil,
162 "my_include": nil,
163 "foo/bar/MyClass.java": nil,
164 "prebuilt.jar": nil,
Paul Duffindddd5462020-04-07 15:25:44 +0100165 "prebuilt.so": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800166 "vendor/foo/devkeys/test.x509.pem": nil,
167 "vendor/foo/devkeys/test.pk8": nil,
168 "testkey.x509.pem": nil,
169 "testkey.pk8": nil,
170 "testkey.override.x509.pem": nil,
171 "testkey.override.pk8": nil,
172 "vendor/foo/devkeys/testkey.avbpubkey": nil,
173 "vendor/foo/devkeys/testkey.pem": nil,
174 "NOTICE": nil,
175 "custom_notice": nil,
Jiyong Park9918e1a2020-03-17 19:16:40 +0900176 "custom_notice_for_static_lib": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800177 "testkey2.avbpubkey": nil,
178 "testkey2.pem": nil,
179 "myapex-arm64.apex": nil,
180 "myapex-arm.apex": nil,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700181 "myapex.apks": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800182 "frameworks/base/api/current.txt": nil,
183 "framework/aidl/a.aidl": nil,
184 "build/make/core/proguard.flags": nil,
185 "build/make/core/proguard_basic_keeps.flags": nil,
186 "dummy.txt": nil,
Liz Kammer1c14a212020-05-12 15:26:55 -0700187 "baz": nil,
188 "bar/baz": nil,
Liz Kammer5bd365f2020-05-27 15:15:11 -0700189 "testdata/baz": nil,
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700190 "AppSet.apks": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900191 }
192
Colin Crossf9aabd72020-02-15 11:29:50 -0800193 cc.GatherRequiredFilesForTest(fs)
194
Jooyung Han344d5432019-08-23 11:17:39 +0900195 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800196 // The fs now needs to be populated before creating the config, call handlers twice
197 // for now, once to get any fs changes, and later after the config was created to
198 // set product variables or targets.
199 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
200 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900201 }
202
Colin Cross98be1bb2019-12-13 20:41:13 -0800203 config := android.TestArchConfig(buildDir, nil, bp, fs)
204 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
205 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
206 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
207 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
208 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jooyung Han749dc692020-04-15 11:03:39 +0900209 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
Colin Cross98be1bb2019-12-13 20:41:13 -0800210 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
211
212 for _, handler := range handlers {
213 // The fs now needs to be populated before creating the config, call handlers twice
214 // for now, earlier to get any fs changes, and now after the config was created to
215 // set product variables or targets.
216 tempFS := map[string][]byte{}
217 handler(tempFS, config)
218 }
219
220 ctx := android.NewTestArchContext()
Paul Duffineedc5d52020-06-12 17:46:39 +0100221
222 // from android package
223 android.RegisterPackageBuildComponents(ctx)
224 ctx.PreArchMutators(android.RegisterVisibilityRuleChecker)
225
Colin Cross98be1bb2019-12-13 20:41:13 -0800226 ctx.RegisterModuleType("apex", BundleFactory)
227 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
228 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
229 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
230 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
231 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
232 ctx.RegisterModuleType("override_apex", overrideApexFactory)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700233 ctx.RegisterModuleType("apex_set", apexSetFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800234
Jooyung Hana57af4a2020-01-23 05:36:59 +0000235 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +0100236 ctx.PreArchMutators(android.RegisterComponentsMutator)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000237 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
238
Paul Duffin77980a82019-12-19 16:01:36 +0000239 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +0100240
241 // Register this after the prebuilt mutators have been registered (in
242 // cc.RegisterRequiredBuildComponentsForTest) to match what happens at runtime.
243 ctx.PreArchMutators(android.RegisterVisibilityRuleGatherer)
244 ctx.PostDepsMutators(android.RegisterVisibilityRuleEnforcer)
245
Colin Cross98be1bb2019-12-13 20:41:13 -0800246 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800247 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
248 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700249 ctx.RegisterModuleType("prebuilt_etc", prebuilt_etc.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000250 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700251 ctx.RegisterModuleType("sh_binary", sh.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800252 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000253 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000254 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000255 java.RegisterAppBuildComponents(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +0100256 java.RegisterSdkLibraryBuildComponents(ctx)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900257 ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800258
Colin Cross98be1bb2019-12-13 20:41:13 -0800259 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800260 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800261
262 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900263
Jooyung Han5c998b92019-06-27 11:30:33 +0900264 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900265}
266
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700267func setUp() {
268 var err error
269 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900270 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700271 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900272 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273}
274
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700275func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700276 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900277}
278
Jooyung Han643adc42020-02-27 13:50:06 +0900279// ensure that 'result' equals 'expected'
280func ensureEquals(t *testing.T, result string, expected string) {
281 t.Helper()
282 if result != expected {
283 t.Errorf("%q != %q", expected, result)
284 }
285}
286
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287// ensure that 'result' contains 'expected'
288func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900289 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900290 if !strings.Contains(result, expected) {
291 t.Errorf("%q is not found in %q", expected, result)
292 }
293}
294
Liz Kammer5bd365f2020-05-27 15:15:11 -0700295// ensure that 'result' contains 'expected' exactly one time
296func ensureContainsOnce(t *testing.T, result string, expected string) {
297 t.Helper()
298 count := strings.Count(result, expected)
299 if count != 1 {
300 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
301 }
302}
303
Jiyong Park25fc6a92018-11-18 18:02:45 +0900304// ensures that 'result' does not contain 'notExpected'
305func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900306 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900307 if strings.Contains(result, notExpected) {
308 t.Errorf("%q is found in %q", notExpected, result)
309 }
310}
311
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700312func ensureMatches(t *testing.T, result string, expectedRex string) {
313 ok, err := regexp.MatchString(expectedRex, result)
314 if err != nil {
315 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
316 return
317 }
318 if !ok {
319 t.Errorf("%s does not match regular expession %s", result, expectedRex)
320 }
321}
322
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900324 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900325 if !android.InList(expected, result) {
326 t.Errorf("%q is not found in %v", expected, result)
327 }
328}
329
330func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900331 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900332 if android.InList(notExpected, result) {
333 t.Errorf("%q is found in %v", notExpected, result)
334 }
335}
336
Jooyung Hane1633032019-08-01 17:41:43 +0900337func ensureListEmpty(t *testing.T, result []string) {
338 t.Helper()
339 if len(result) > 0 {
340 t.Errorf("%q is expected to be empty", result)
341 }
342}
343
Jiyong Park25fc6a92018-11-18 18:02:45 +0900344// Minimal test
345func TestBasicApex(t *testing.T) {
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900346 ctx, config := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900347 apex_defaults {
348 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900349 manifest: ":myapex.manifest",
350 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900351 key: "myapex.key",
352 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800353 multilib: {
354 both: {
355 binaries: ["foo",],
356 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900357 },
Jiyong Park77acec62020-06-01 21:39:15 +0900358 java_libs: [
359 "myjar",
360 "myjar_dex",
361 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362 }
363
Jiyong Park30ca9372019-02-07 16:27:23 +0900364 apex {
365 name: "myapex",
366 defaults: ["myapex-defaults"],
367 }
368
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 apex_key {
370 name: "myapex.key",
371 public_key: "testkey.avbpubkey",
372 private_key: "testkey.pem",
373 }
374
Jiyong Park809bb722019-02-13 21:33:49 +0900375 filegroup {
376 name: "myapex.manifest",
377 srcs: ["apex_manifest.json"],
378 }
379
380 filegroup {
381 name: "myapex.androidmanifest",
382 srcs: ["AndroidManifest.xml"],
383 }
384
Jiyong Park25fc6a92018-11-18 18:02:45 +0900385 cc_library {
386 name: "mylib",
387 srcs: ["mylib.cpp"],
388 shared_libs: ["mylib2"],
389 system_shared_libs: [],
390 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000391 // TODO: remove //apex_available:platform
392 apex_available: [
393 "//apex_available:platform",
394 "myapex",
395 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900396 }
397
Alex Light3d673592019-01-18 14:37:31 -0800398 cc_binary {
399 name: "foo",
400 srcs: ["mylib.cpp"],
401 compile_multilib: "both",
402 multilib: {
403 lib32: {
404 suffix: "32",
405 },
406 lib64: {
407 suffix: "64",
408 },
409 },
410 symlinks: ["foo_link_"],
411 symlink_preferred_arch: true,
412 system_shared_libs: [],
413 static_executable: true,
414 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000415 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800416 }
417
Paul Duffindddd5462020-04-07 15:25:44 +0100418 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900419 name: "mylib2",
420 srcs: ["mylib.cpp"],
421 system_shared_libs: [],
422 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900423 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900424 static_libs: ["libstatic"],
425 // TODO: remove //apex_available:platform
426 apex_available: [
427 "//apex_available:platform",
428 "myapex",
429 ],
430 }
431
Paul Duffindddd5462020-04-07 15:25:44 +0100432 cc_prebuilt_library_shared {
433 name: "mylib2",
434 srcs: ["prebuilt.so"],
435 // TODO: remove //apex_available:platform
436 apex_available: [
437 "//apex_available:platform",
438 "myapex",
439 ],
440 }
441
Jiyong Park9918e1a2020-03-17 19:16:40 +0900442 cc_library_static {
443 name: "libstatic",
444 srcs: ["mylib.cpp"],
445 system_shared_libs: [],
446 stl: "none",
447 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000448 // TODO: remove //apex_available:platform
449 apex_available: [
450 "//apex_available:platform",
451 "myapex",
452 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900453 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900454
455 java_library {
456 name: "myjar",
457 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900458 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900459 sdk_version: "none",
460 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900461 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900462 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000463 // TODO: remove //apex_available:platform
464 apex_available: [
465 "//apex_available:platform",
466 "myapex",
467 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900468 }
469
Jiyong Park77acec62020-06-01 21:39:15 +0900470 dex_import {
471 name: "myjar_dex",
472 jars: ["prebuilt.jar"],
473 apex_available: [
474 "//apex_available:platform",
475 "myapex",
476 ],
477 }
478
Jiyong Park7f7766d2019-07-25 22:02:35 +0900479 java_library {
480 name: "myotherjar",
481 srcs: ["foo/bar/MyClass.java"],
482 sdk_version: "none",
483 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900484 // TODO: remove //apex_available:platform
485 apex_available: [
486 "//apex_available:platform",
487 "myapex",
488 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900490
491 java_library {
492 name: "mysharedjar",
493 srcs: ["foo/bar/MyClass.java"],
494 sdk_version: "none",
495 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900496 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900497 `)
498
Sundong Ahnabb64432019-10-22 13:58:29 +0900499 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900500
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900501 // Make sure that Android.mk is created
502 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
503 data := android.AndroidMkDataForTest(t, config, "", ab)
504 var builder strings.Builder
505 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
506
507 androidMk := builder.String()
508 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
509 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
510
Jiyong Park42cca6c2019-04-01 11:15:50 +0900511 optFlags := apexRule.Args["opt_flags"]
512 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700513 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900514 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900515
Jiyong Park25fc6a92018-11-18 18:02:45 +0900516 copyCmds := apexRule.Args["copy_commands"]
517
518 // Ensure that main rule creates an output
519 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
520
521 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800522 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900523 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park77acec62020-06-01 21:39:15 +0900524 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900525
526 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800527 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900528 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900529
530 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800531 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
532 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900533 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900534 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900535 // .. but not for java libs
536 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900537 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800538
Colin Cross7113d202019-11-20 16:39:12 -0800539 // Ensure that the platform variant ends with _shared or _common
540 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
541 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900542 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
543 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900544 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
545
546 // Ensure that dynamic dependency to java libs are not included
547 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800548
549 // Ensure that all symlinks are present.
550 found_foo_link_64 := false
551 found_foo := false
552 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900553 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800554 if strings.HasSuffix(cmd, "bin/foo") {
555 found_foo = true
556 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
557 found_foo_link_64 = true
558 }
559 }
560 }
561 good := found_foo && found_foo_link_64
562 if !good {
563 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
564 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900565
Sundong Ahnabb64432019-10-22 13:58:29 +0900566 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700567 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900568 if len(noticeInputs) != 3 {
569 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900570 }
571 ensureListContains(t, noticeInputs, "NOTICE")
572 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900573 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900574
Artur Satayeva8bd1132020-04-27 18:07:06 +0100575 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
satayev53bbc082020-07-23 15:15:54 +0000576 ensureListContains(t, fullDepsInfo, "myjar(minSdkVersion:(no version)) <- myapex")
577 ensureListContains(t, fullDepsInfo, "mylib(minSdkVersion:(no version)) <- myapex")
578 ensureListContains(t, fullDepsInfo, "mylib2(minSdkVersion:(no version)) <- mylib")
579 ensureListContains(t, fullDepsInfo, "myotherjar(minSdkVersion:(no version)) <- myjar")
580 ensureListContains(t, fullDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100581
582 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
satayev53bbc082020-07-23 15:15:54 +0000583 ensureListContains(t, flatDepsInfo, " myjar(minSdkVersion:(no version))")
584 ensureListContains(t, flatDepsInfo, " mylib(minSdkVersion:(no version))")
585 ensureListContains(t, flatDepsInfo, " mylib2(minSdkVersion:(no version))")
586 ensureListContains(t, flatDepsInfo, " myotherjar(minSdkVersion:(no version))")
587 ensureListContains(t, flatDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800588}
589
Jooyung Hanf21c7972019-12-16 22:32:06 +0900590func TestDefaults(t *testing.T) {
591 ctx, _ := testApex(t, `
592 apex_defaults {
593 name: "myapex-defaults",
594 key: "myapex.key",
595 prebuilts: ["myetc"],
596 native_shared_libs: ["mylib"],
597 java_libs: ["myjar"],
598 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900599 rros: ["rro"],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900600 }
601
602 prebuilt_etc {
603 name: "myetc",
604 src: "myprebuilt",
605 }
606
607 apex {
608 name: "myapex",
609 defaults: ["myapex-defaults"],
610 }
611
612 apex_key {
613 name: "myapex.key",
614 public_key: "testkey.avbpubkey",
615 private_key: "testkey.pem",
616 }
617
618 cc_library {
619 name: "mylib",
620 system_shared_libs: [],
621 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000622 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900623 }
624
625 java_library {
626 name: "myjar",
627 srcs: ["foo/bar/MyClass.java"],
628 sdk_version: "none",
629 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000630 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900631 }
632
633 android_app {
634 name: "AppFoo",
635 srcs: ["foo/bar/MyClass.java"],
636 sdk_version: "none",
637 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000638 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900639 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900640
641 runtime_resource_overlay {
642 name: "rro",
643 theme: "blue",
644 }
645
Jooyung Hanf21c7972019-12-16 22:32:06 +0900646 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000647 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900648 "etc/myetc",
649 "javalib/myjar.jar",
650 "lib64/mylib.so",
651 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900652 "overlay/blue/rro.apk",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900653 })
654}
655
Jooyung Han01a3ee22019-11-02 02:52:25 +0900656func TestApexManifest(t *testing.T) {
657 ctx, _ := testApex(t, `
658 apex {
659 name: "myapex",
660 key: "myapex.key",
661 }
662
663 apex_key {
664 name: "myapex.key",
665 public_key: "testkey.avbpubkey",
666 private_key: "testkey.pem",
667 }
668 `)
669
670 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900671 args := module.Rule("apexRule").Args
672 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
673 t.Error("manifest should be apex_manifest.pb, but " + manifest)
674 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900675}
676
Alex Light5098a612018-11-29 17:12:15 -0800677func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700678 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800679 apex {
680 name: "myapex",
681 key: "myapex.key",
682 payload_type: "zip",
683 native_shared_libs: ["mylib"],
684 }
685
686 apex_key {
687 name: "myapex.key",
688 public_key: "testkey.avbpubkey",
689 private_key: "testkey.pem",
690 }
691
692 cc_library {
693 name: "mylib",
694 srcs: ["mylib.cpp"],
695 shared_libs: ["mylib2"],
696 system_shared_libs: [],
697 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000698 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800699 }
700
701 cc_library {
702 name: "mylib2",
703 srcs: ["mylib.cpp"],
704 system_shared_libs: [],
705 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000706 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800707 }
708 `)
709
Sundong Ahnabb64432019-10-22 13:58:29 +0900710 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800711 copyCmds := zipApexRule.Args["copy_commands"]
712
713 // Ensure that main rule creates an output
714 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
715
716 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800717 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800718
719 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800720 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800721
722 // Ensure that both direct and indirect deps are copied into apex
723 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
724 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900725}
726
727func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700728 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900729 apex {
730 name: "myapex",
731 key: "myapex.key",
732 native_shared_libs: ["mylib", "mylib3"],
733 }
734
735 apex_key {
736 name: "myapex.key",
737 public_key: "testkey.avbpubkey",
738 private_key: "testkey.pem",
739 }
740
741 cc_library {
742 name: "mylib",
743 srcs: ["mylib.cpp"],
744 shared_libs: ["mylib2", "mylib3"],
745 system_shared_libs: [],
746 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000747 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900748 }
749
750 cc_library {
751 name: "mylib2",
752 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900753 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900754 system_shared_libs: [],
755 stl: "none",
756 stubs: {
757 versions: ["1", "2", "3"],
758 },
759 }
760
761 cc_library {
762 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900763 srcs: ["mylib.cpp"],
764 shared_libs: ["mylib4"],
765 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900766 stl: "none",
767 stubs: {
768 versions: ["10", "11", "12"],
769 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000770 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900771 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900772
773 cc_library {
774 name: "mylib4",
775 srcs: ["mylib.cpp"],
776 system_shared_libs: [],
777 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000778 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900779 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900780 `)
781
Sundong Ahnabb64432019-10-22 13:58:29 +0900782 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900783 copyCmds := apexRule.Args["copy_commands"]
784
785 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800786 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900787
788 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800789 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900790
791 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800792 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900793
Colin Cross7113d202019-11-20 16:39:12 -0800794 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900795
796 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900797 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900798 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900799 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900800
801 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Cross7113d202019-11-20 16:39:12 -0800802 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900803 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800804 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900805
806 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900807 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900808 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900809
810 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900811 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900812
Jooyung Hana57af4a2020-01-23 05:36:59 +0000813 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900814 "lib64/mylib.so",
815 "lib64/mylib3.so",
816 "lib64/mylib4.so",
817 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900818}
819
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900820func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700821 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900822 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900823 name: "myapex2",
824 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900825 native_shared_libs: ["mylib"],
826 }
827
828 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900829 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900830 public_key: "testkey.avbpubkey",
831 private_key: "testkey.pem",
832 }
833
834 cc_library {
835 name: "mylib",
836 srcs: ["mylib.cpp"],
837 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900838 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900839 system_shared_libs: [],
840 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000841 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900842 }
843
844 cc_library {
845 name: "libfoo",
846 srcs: ["mylib.cpp"],
847 shared_libs: ["libbar"],
848 system_shared_libs: [],
849 stl: "none",
850 stubs: {
851 versions: ["10", "20", "30"],
852 },
853 }
854
855 cc_library {
856 name: "libbar",
857 srcs: ["mylib.cpp"],
858 system_shared_libs: [],
859 stl: "none",
860 }
861
Jiyong Park678c8812020-02-07 17:25:49 +0900862 cc_library_static {
863 name: "libbaz",
864 srcs: ["mylib.cpp"],
865 system_shared_libs: [],
866 stl: "none",
867 apex_available: [ "myapex2" ],
868 }
869
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900870 `)
871
Jiyong Park83dc74b2020-01-14 18:38:44 +0900872 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900873 copyCmds := apexRule.Args["copy_commands"]
874
875 // Ensure that direct non-stubs dep is always included
876 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
877
878 // Ensure that indirect stubs dep is not included
879 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
880
881 // Ensure that dependency of stubs is not included
882 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
883
Jiyong Park83dc74b2020-01-14 18:38:44 +0900884 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900885
886 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900887 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900888 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900889 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900890
Jiyong Park3ff16992019-12-27 14:11:47 +0900891 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900892
893 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
894 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900895
Artur Satayeva8bd1132020-04-27 18:07:06 +0100896 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
satayev53bbc082020-07-23 15:15:54 +0000897 ensureListContains(t, fullDepsInfo, "mylib(minSdkVersion:(no version)) <- myapex2")
898 ensureListContains(t, fullDepsInfo, "libbaz(minSdkVersion:(no version)) <- mylib")
899 ensureListContains(t, fullDepsInfo, "libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +0900900
Artur Satayeva8bd1132020-04-27 18:07:06 +0100901 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
satayev53bbc082020-07-23 15:15:54 +0000902 ensureListContains(t, flatDepsInfo, " mylib(minSdkVersion:(no version))")
903 ensureListContains(t, flatDepsInfo, " libbaz(minSdkVersion:(no version))")
904 ensureListContains(t, flatDepsInfo, " libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900905}
906
Jooyung Hand3639552019-08-09 12:57:43 +0900907func TestApexWithRuntimeLibsDependency(t *testing.T) {
908 /*
909 myapex
910 |
911 v (runtime_libs)
912 mylib ------+------> libfoo [provides stub]
913 |
914 `------> libbar
915 */
916 ctx, _ := testApex(t, `
917 apex {
918 name: "myapex",
919 key: "myapex.key",
920 native_shared_libs: ["mylib"],
921 }
922
923 apex_key {
924 name: "myapex.key",
925 public_key: "testkey.avbpubkey",
926 private_key: "testkey.pem",
927 }
928
929 cc_library {
930 name: "mylib",
931 srcs: ["mylib.cpp"],
932 runtime_libs: ["libfoo", "libbar"],
933 system_shared_libs: [],
934 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000935 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900936 }
937
938 cc_library {
939 name: "libfoo",
940 srcs: ["mylib.cpp"],
941 system_shared_libs: [],
942 stl: "none",
943 stubs: {
944 versions: ["10", "20", "30"],
945 },
946 }
947
948 cc_library {
949 name: "libbar",
950 srcs: ["mylib.cpp"],
951 system_shared_libs: [],
952 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000953 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900954 }
955
956 `)
957
Sundong Ahnabb64432019-10-22 13:58:29 +0900958 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900959 copyCmds := apexRule.Args["copy_commands"]
960
961 // Ensure that direct non-stubs dep is always included
962 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
963
964 // Ensure that indirect stubs dep is not included
965 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
966
967 // Ensure that runtime_libs dep in included
968 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
969
Sundong Ahnabb64432019-10-22 13:58:29 +0900970 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900971 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
972 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900973
974}
975
Jooyung Han8ce8db92020-05-15 19:05:05 +0900976func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
977 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
978 bp := `
979 apex {
980 name: "com.android.runtime",
981 key: "com.android.runtime.key",
982 native_shared_libs: ["libc"],
983 }
984
985 apex_key {
986 name: "com.android.runtime.key",
987 public_key: "testkey.avbpubkey",
988 private_key: "testkey.pem",
989 }
990
991 cc_library {
992 name: "libc",
993 no_libcrt: true,
994 nocrt: true,
995 stl: "none",
996 system_shared_libs: [],
997 stubs: { versions: ["1"] },
998 apex_available: ["com.android.runtime"],
999
1000 sanitize: {
1001 hwaddress: true,
1002 }
1003 }
1004
1005 cc_prebuilt_library_shared {
1006 name: "libclang_rt.hwasan-aarch64-android",
1007 no_libcrt: true,
1008 nocrt: true,
1009 stl: "none",
1010 system_shared_libs: [],
1011 srcs: [""],
1012 stubs: { versions: ["1"] },
1013
1014 sanitize: {
1015 never: true,
1016 },
1017 }
1018 `
1019 // override bp to use hard-coded names: com.android.runtime and libc
1020 fs["Android.bp"] = []byte(bp)
1021 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
1022 })
1023
1024 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1025 "lib64/bionic/libc.so",
1026 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1027 })
1028
1029 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1030
1031 installed := hwasan.Description("install libclang_rt.hwasan")
1032 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1033
1034 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1035 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1036 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1037}
1038
1039func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
1040 ctx, _ := testApex(t, "", func(fs map[string][]byte, config android.Config) {
1041 bp := `
1042 apex {
1043 name: "com.android.runtime",
1044 key: "com.android.runtime.key",
1045 native_shared_libs: ["libc"],
1046 }
1047
1048 apex_key {
1049 name: "com.android.runtime.key",
1050 public_key: "testkey.avbpubkey",
1051 private_key: "testkey.pem",
1052 }
1053
1054 cc_library {
1055 name: "libc",
1056 no_libcrt: true,
1057 nocrt: true,
1058 stl: "none",
1059 system_shared_libs: [],
1060 stubs: { versions: ["1"] },
1061 apex_available: ["com.android.runtime"],
1062 }
1063
1064 cc_prebuilt_library_shared {
1065 name: "libclang_rt.hwasan-aarch64-android",
1066 no_libcrt: true,
1067 nocrt: true,
1068 stl: "none",
1069 system_shared_libs: [],
1070 srcs: [""],
1071 stubs: { versions: ["1"] },
1072
1073 sanitize: {
1074 never: true,
1075 },
1076 }
1077 `
1078 // override bp to use hard-coded names: com.android.runtime and libc
1079 fs["Android.bp"] = []byte(bp)
1080 fs["system/sepolicy/apex/com.android.runtime-file_contexts"] = nil
1081
1082 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1083 })
1084
1085 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1086 "lib64/bionic/libc.so",
1087 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1088 })
1089
1090 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1091
1092 installed := hwasan.Description("install libclang_rt.hwasan")
1093 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1094
1095 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1096 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1097 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1098}
1099
Jooyung Han61b66e92020-03-21 14:21:46 +00001100func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1101 testcases := []struct {
1102 name string
1103 minSdkVersion string
1104 shouldLink string
1105 shouldNotLink []string
1106 }{
1107 {
Jooyung Han75568392020-03-20 04:29:24 +09001108 name: "should link to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001109 minSdkVersion: "",
Jooyung Han61b66e92020-03-21 14:21:46 +00001110 shouldLink: "30",
1111 shouldNotLink: []string{"29"},
1112 },
1113 {
1114 name: "should link to llndk#29",
Jooyung Han749dc692020-04-15 11:03:39 +09001115 minSdkVersion: "min_sdk_version: \"29\",",
Jooyung Han61b66e92020-03-21 14:21:46 +00001116 shouldLink: "29",
1117 shouldNotLink: []string{"30"},
1118 },
1119 }
1120 for _, tc := range testcases {
1121 t.Run(tc.name, func(t *testing.T) {
1122 ctx, _ := testApex(t, `
1123 apex {
1124 name: "myapex",
1125 key: "myapex.key",
1126 use_vendor: true,
1127 native_shared_libs: ["mylib"],
Jooyung Han749dc692020-04-15 11:03:39 +09001128 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001129 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001130
Jooyung Han61b66e92020-03-21 14:21:46 +00001131 apex_key {
1132 name: "myapex.key",
1133 public_key: "testkey.avbpubkey",
1134 private_key: "testkey.pem",
1135 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001136
Jooyung Han61b66e92020-03-21 14:21:46 +00001137 cc_library {
1138 name: "mylib",
1139 srcs: ["mylib.cpp"],
1140 vendor_available: true,
1141 shared_libs: ["libbar"],
1142 system_shared_libs: [],
1143 stl: "none",
1144 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001145 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001146 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001147
Jooyung Han61b66e92020-03-21 14:21:46 +00001148 cc_library {
1149 name: "libbar",
1150 srcs: ["mylib.cpp"],
1151 system_shared_libs: [],
1152 stl: "none",
1153 stubs: { versions: ["29","30"] },
1154 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001155
Jooyung Han61b66e92020-03-21 14:21:46 +00001156 llndk_library {
1157 name: "libbar",
1158 symbol_file: "",
1159 }
1160 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07001161 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Han61b66e92020-03-21 14:21:46 +00001162 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +09001163
Jooyung Han61b66e92020-03-21 14:21:46 +00001164 // Ensure that LLNDK dep is not included
1165 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1166 "lib64/mylib.so",
1167 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001168
Jooyung Han61b66e92020-03-21 14:21:46 +00001169 // Ensure that LLNDK dep is required
1170 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1171 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1172 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001173
Jooyung Han61b66e92020-03-21 14:21:46 +00001174 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1175 ensureContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
1176 for _, ver := range tc.shouldNotLink {
1177 ensureNotContains(t, mylibLdFlags, "libbar.llndk/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
1178 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001179
Jooyung Han61b66e92020-03-21 14:21:46 +00001180 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1181 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1182 })
1183 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001184}
1185
Jiyong Park25fc6a92018-11-18 18:02:45 +09001186func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001187 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001188 apex {
1189 name: "myapex",
1190 key: "myapex.key",
1191 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
1192 }
1193
1194 apex_key {
1195 name: "myapex.key",
1196 public_key: "testkey.avbpubkey",
1197 private_key: "testkey.pem",
1198 }
1199
1200 cc_library {
1201 name: "mylib",
1202 srcs: ["mylib.cpp"],
1203 shared_libs: ["libdl#27"],
1204 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001205 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001206 }
1207
1208 cc_library_shared {
1209 name: "mylib_shared",
1210 srcs: ["mylib.cpp"],
1211 shared_libs: ["libdl#27"],
1212 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001213 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001214 }
1215
1216 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001217 name: "libBootstrap",
1218 srcs: ["mylib.cpp"],
1219 stl: "none",
1220 bootstrap: true,
1221 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001222 `)
1223
Sundong Ahnabb64432019-10-22 13:58:29 +09001224 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001225 copyCmds := apexRule.Args["copy_commands"]
1226
1227 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001228 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001229 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1230 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001231
1232 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001233 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001234
Colin Cross7113d202019-11-20 16:39:12 -08001235 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1236 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1237 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001238
1239 // For dependency to libc
1240 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001241 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001242 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001243 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001244 // ... Cflags from stub is correctly exported to mylib
1245 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1246 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1247
1248 // For dependency to libm
1249 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001250 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001251 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001252 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001253 // ... and is not compiling with the stub
1254 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1255 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1256
1257 // For dependency to libdl
1258 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001259 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001260 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001261 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1262 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001263 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001264 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001265 // ... Cflags from stub is correctly exported to mylib
1266 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1267 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001268
1269 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001270 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1271 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1272 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1273 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001274}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001275
Jooyung Han749dc692020-04-15 11:03:39 +09001276func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001277 // there are three links between liba --> libz
Jooyung Han749dc692020-04-15 11:03:39 +09001278 // 1) myapex -> libx -> liba -> libz : this should be #29 link, but fallback to #28
1279 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001280 // 3) (platform) -> liba -> libz : this should be non-stub link
1281 ctx, _ := testApex(t, `
1282 apex {
1283 name: "myapex",
1284 key: "myapex.key",
1285 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001286 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001287 }
1288
1289 apex {
1290 name: "otherapex",
1291 key: "myapex.key",
1292 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001293 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001294 }
1295
1296 apex_key {
1297 name: "myapex.key",
1298 public_key: "testkey.avbpubkey",
1299 private_key: "testkey.pem",
1300 }
1301
1302 cc_library {
1303 name: "libx",
1304 shared_libs: ["liba"],
1305 system_shared_libs: [],
1306 stl: "none",
1307 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001308 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001309 }
1310
1311 cc_library {
1312 name: "liby",
1313 shared_libs: ["liba"],
1314 system_shared_libs: [],
1315 stl: "none",
1316 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001317 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001318 }
1319
1320 cc_library {
1321 name: "liba",
1322 shared_libs: ["libz"],
1323 system_shared_libs: [],
1324 stl: "none",
1325 apex_available: [
1326 "//apex_available:anyapex",
1327 "//apex_available:platform",
1328 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001329 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001330 }
1331
1332 cc_library {
1333 name: "libz",
1334 system_shared_libs: [],
1335 stl: "none",
1336 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001337 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001338 },
1339 }
Jooyung Han749dc692020-04-15 11:03:39 +09001340 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001341
1342 expectLink := func(from, from_variant, to, to_variant string) {
1343 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1344 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1345 }
1346 expectNoLink := func(from, from_variant, to, to_variant string) {
1347 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1348 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1349 }
1350 // platform liba is linked to non-stub version
1351 expectLink("liba", "shared", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001352 // liba in myapex is linked to #28
1353 expectLink("liba", "shared_myapex", "libz", "shared_28")
1354 expectNoLink("liba", "shared_myapex", "libz", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001355 expectNoLink("liba", "shared_myapex", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001356 // liba in otherapex is linked to #30
1357 expectLink("liba", "shared_otherapex", "libz", "shared_30")
1358 expectNoLink("liba", "shared_otherapex", "libz", "shared_28")
Jooyung Han03b51852020-02-26 22:45:42 +09001359 expectNoLink("liba", "shared_otherapex", "libz", "shared")
1360}
1361
Jooyung Hanaed150d2020-04-02 01:41:41 +09001362func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
1363 ctx, _ := testApex(t, `
1364 apex {
1365 name: "myapex",
1366 key: "myapex.key",
1367 native_shared_libs: ["libx"],
1368 min_sdk_version: "R",
1369 }
1370
1371 apex_key {
1372 name: "myapex.key",
1373 public_key: "testkey.avbpubkey",
1374 private_key: "testkey.pem",
1375 }
1376
1377 cc_library {
1378 name: "libx",
1379 shared_libs: ["libz"],
1380 system_shared_libs: [],
1381 stl: "none",
1382 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001383 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001384 }
1385
1386 cc_library {
1387 name: "libz",
1388 system_shared_libs: [],
1389 stl: "none",
1390 stubs: {
1391 versions: ["29", "R"],
1392 },
1393 }
1394 `, func(fs map[string][]byte, config android.Config) {
1395 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
1396 })
1397
1398 expectLink := func(from, from_variant, to, to_variant string) {
1399 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1400 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1401 }
1402 expectNoLink := func(from, from_variant, to, to_variant string) {
1403 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1404 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1405 }
1406 // 9000 is quite a magic number.
1407 // Finalized SDK codenames are mapped as P(28), Q(29), ...
1408 // And, codenames which are not finalized yet(active_codenames + future_codenames) are numbered from 9000, 9001, ...
1409 // to distinguish them from finalized and future_api(10000)
1410 // In this test, "R" is assumed not finalized yet( listed in Platform_version_active_codenames) and translated into 9000
1411 // (refer android/api_levels.go)
1412 expectLink("libx", "shared_myapex", "libz", "shared_9000")
1413 expectNoLink("libx", "shared_myapex", "libz", "shared_29")
1414 expectNoLink("libx", "shared_myapex", "libz", "shared")
1415}
1416
Jooyung Han749dc692020-04-15 11:03:39 +09001417func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001418 ctx, _ := testApex(t, `
1419 apex {
1420 name: "myapex",
1421 key: "myapex.key",
1422 native_shared_libs: ["libx"],
1423 }
1424
1425 apex_key {
1426 name: "myapex.key",
1427 public_key: "testkey.avbpubkey",
1428 private_key: "testkey.pem",
1429 }
1430
1431 cc_library {
1432 name: "libx",
1433 shared_libs: ["libz"],
1434 system_shared_libs: [],
1435 stl: "none",
1436 apex_available: [ "myapex" ],
1437 }
1438
1439 cc_library {
1440 name: "libz",
1441 system_shared_libs: [],
1442 stl: "none",
1443 stubs: {
1444 versions: ["1", "2"],
1445 },
1446 }
1447 `)
1448
1449 expectLink := func(from, from_variant, to, to_variant string) {
1450 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1451 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1452 }
1453 expectNoLink := func(from, from_variant, to, to_variant string) {
1454 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1455 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1456 }
1457 expectLink("libx", "shared_myapex", "libz", "shared_2")
1458 expectNoLink("libx", "shared_myapex", "libz", "shared_1")
1459 expectNoLink("libx", "shared_myapex", "libz", "shared")
1460}
1461
1462func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
1463 ctx, _ := testApex(t, `
1464 apex {
1465 name: "myapex",
1466 key: "myapex.key",
1467 native_shared_libs: ["libx"],
1468 }
1469
1470 apex_key {
1471 name: "myapex.key",
1472 public_key: "testkey.avbpubkey",
1473 private_key: "testkey.pem",
1474 }
1475
1476 cc_library {
1477 name: "libx",
1478 system_shared_libs: [],
1479 stl: "none",
1480 apex_available: [ "myapex" ],
1481 stubs: {
1482 versions: ["1", "2"],
1483 },
1484 }
1485
1486 cc_library {
1487 name: "libz",
1488 shared_libs: ["libx"],
1489 system_shared_libs: [],
1490 stl: "none",
1491 }
1492 `)
1493
1494 expectLink := func(from, from_variant, to, to_variant string) {
1495 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1496 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1497 }
1498 expectNoLink := func(from, from_variant, to, to_variant string) {
1499 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1500 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1501 }
1502 expectLink("libz", "shared", "libx", "shared_2")
1503 expectNoLink("libz", "shared", "libz", "shared_1")
1504 expectNoLink("libz", "shared", "libz", "shared")
1505}
1506
Jooyung Han75568392020-03-20 04:29:24 +09001507func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001508 ctx, _ := testApex(t, `
1509 apex {
1510 name: "myapex",
1511 key: "myapex.key",
1512 native_shared_libs: ["libx"],
1513 min_sdk_version: "29",
1514 }
1515
1516 apex_key {
1517 name: "myapex.key",
1518 public_key: "testkey.avbpubkey",
1519 private_key: "testkey.pem",
1520 }
1521
1522 cc_library {
1523 name: "libx",
1524 shared_libs: ["libbar"],
1525 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001526 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001527 }
1528
1529 cc_library {
1530 name: "libbar",
1531 stubs: {
1532 versions: ["29", "30"],
1533 },
1534 }
Jooyung Han75568392020-03-20 04:29:24 +09001535 `, func(fs map[string][]byte, config android.Config) {
1536 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1537 })
Jooyung Han03b51852020-02-26 22:45:42 +09001538 expectLink := func(from, from_variant, to, to_variant string) {
1539 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1540 libFlags := ld.Args["libFlags"]
1541 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1542 }
Jooyung Han75568392020-03-20 04:29:24 +09001543 expectLink("libx", "shared_hwasan_myapex", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001544}
1545
Jooyung Han75568392020-03-20 04:29:24 +09001546func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Jooyung Han03b51852020-02-26 22:45:42 +09001547 ctx, _ := testApex(t, `
1548 apex {
1549 name: "myapex",
1550 key: "myapex.key",
1551 native_shared_libs: ["libx"],
1552 min_sdk_version: "29",
1553 }
1554
1555 apex_key {
1556 name: "myapex.key",
1557 public_key: "testkey.avbpubkey",
1558 private_key: "testkey.pem",
1559 }
1560
1561 cc_library {
1562 name: "libx",
1563 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001564 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001565 }
Jooyung Han75568392020-03-20 04:29:24 +09001566 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001567
1568 // ensure apex variant of c++ is linked with static unwinder
1569 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module)
1570 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
1571 // note that platform variant is not.
1572 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
1573 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libgcc_stripped")
Jooyung Han03b51852020-02-26 22:45:42 +09001574}
1575
Jooyung Han749dc692020-04-15 11:03:39 +09001576func TestApexMinSdkVersion_ErrorIfIncompatibleStubs(t *testing.T) {
Jooyung Han75568392020-03-20 04:29:24 +09001577 testApexError(t, `"libz" .*: not found a version\(<=29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001578 apex {
1579 name: "myapex",
1580 key: "myapex.key",
1581 native_shared_libs: ["libx"],
1582 min_sdk_version: "29",
1583 }
1584
1585 apex_key {
1586 name: "myapex.key",
1587 public_key: "testkey.avbpubkey",
1588 private_key: "testkey.pem",
1589 }
1590
1591 cc_library {
1592 name: "libx",
1593 shared_libs: ["libz"],
1594 system_shared_libs: [],
1595 stl: "none",
1596 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001597 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001598 }
1599
1600 cc_library {
1601 name: "libz",
1602 system_shared_libs: [],
1603 stl: "none",
1604 stubs: {
1605 versions: ["30"],
1606 },
1607 }
Jooyung Han75568392020-03-20 04:29:24 +09001608 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001609}
Jooyung Han03b51852020-02-26 22:45:42 +09001610
Jooyung Han749dc692020-04-15 11:03:39 +09001611func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1612 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001613 apex {
1614 name: "myapex",
1615 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001616 native_shared_libs: ["mylib"],
1617 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001618 }
1619
1620 apex_key {
1621 name: "myapex.key",
1622 public_key: "testkey.avbpubkey",
1623 private_key: "testkey.pem",
1624 }
Jooyung Han749dc692020-04-15 11:03:39 +09001625
1626 cc_library {
1627 name: "mylib",
1628 srcs: ["mylib.cpp"],
1629 system_shared_libs: [],
1630 stl: "none",
1631 apex_available: [
1632 "myapex",
1633 ],
1634 min_sdk_version: "30",
1635 }
1636 `)
1637}
1638
1639func TestApexMinSdkVersion_Okay(t *testing.T) {
1640 testApex(t, `
1641 apex {
1642 name: "myapex",
1643 key: "myapex.key",
1644 native_shared_libs: ["libfoo"],
1645 java_libs: ["libbar"],
1646 min_sdk_version: "29",
1647 }
1648
1649 apex_key {
1650 name: "myapex.key",
1651 public_key: "testkey.avbpubkey",
1652 private_key: "testkey.pem",
1653 }
1654
1655 cc_library {
1656 name: "libfoo",
1657 srcs: ["mylib.cpp"],
1658 shared_libs: ["libfoo_dep"],
1659 apex_available: ["myapex"],
1660 min_sdk_version: "29",
1661 }
1662
1663 cc_library {
1664 name: "libfoo_dep",
1665 srcs: ["mylib.cpp"],
1666 apex_available: ["myapex"],
1667 min_sdk_version: "29",
1668 }
1669
1670 java_library {
1671 name: "libbar",
1672 sdk_version: "current",
1673 srcs: ["a.java"],
1674 static_libs: ["libbar_dep"],
1675 apex_available: ["myapex"],
1676 min_sdk_version: "29",
1677 }
1678
1679 java_library {
1680 name: "libbar_dep",
1681 sdk_version: "current",
1682 srcs: ["a.java"],
1683 apex_available: ["myapex"],
1684 min_sdk_version: "29",
1685 }
Jooyung Han03b51852020-02-26 22:45:42 +09001686 `)
1687}
1688
Artur Satayev8cf899a2020-04-15 17:29:42 +01001689func TestJavaStableSdkVersion(t *testing.T) {
1690 testCases := []struct {
1691 name string
1692 expectedError string
1693 bp string
1694 }{
1695 {
1696 name: "Non-updatable apex with non-stable dep",
1697 bp: `
1698 apex {
1699 name: "myapex",
1700 java_libs: ["myjar"],
1701 key: "myapex.key",
1702 }
1703 apex_key {
1704 name: "myapex.key",
1705 public_key: "testkey.avbpubkey",
1706 private_key: "testkey.pem",
1707 }
1708 java_library {
1709 name: "myjar",
1710 srcs: ["foo/bar/MyClass.java"],
1711 sdk_version: "core_platform",
1712 apex_available: ["myapex"],
1713 }
1714 `,
1715 },
1716 {
1717 name: "Updatable apex with stable dep",
1718 bp: `
1719 apex {
1720 name: "myapex",
1721 java_libs: ["myjar"],
1722 key: "myapex.key",
1723 updatable: true,
1724 min_sdk_version: "29",
1725 }
1726 apex_key {
1727 name: "myapex.key",
1728 public_key: "testkey.avbpubkey",
1729 private_key: "testkey.pem",
1730 }
1731 java_library {
1732 name: "myjar",
1733 srcs: ["foo/bar/MyClass.java"],
1734 sdk_version: "current",
1735 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001736 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001737 }
1738 `,
1739 },
1740 {
1741 name: "Updatable apex with non-stable dep",
1742 expectedError: "cannot depend on \"myjar\"",
1743 bp: `
1744 apex {
1745 name: "myapex",
1746 java_libs: ["myjar"],
1747 key: "myapex.key",
1748 updatable: true,
1749 }
1750 apex_key {
1751 name: "myapex.key",
1752 public_key: "testkey.avbpubkey",
1753 private_key: "testkey.pem",
1754 }
1755 java_library {
1756 name: "myjar",
1757 srcs: ["foo/bar/MyClass.java"],
1758 sdk_version: "core_platform",
1759 apex_available: ["myapex"],
1760 }
1761 `,
1762 },
1763 {
1764 name: "Updatable apex with non-stable transitive dep",
1765 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against non-public Android API.",
1766 bp: `
1767 apex {
1768 name: "myapex",
1769 java_libs: ["myjar"],
1770 key: "myapex.key",
1771 updatable: true,
1772 }
1773 apex_key {
1774 name: "myapex.key",
1775 public_key: "testkey.avbpubkey",
1776 private_key: "testkey.pem",
1777 }
1778 java_library {
1779 name: "myjar",
1780 srcs: ["foo/bar/MyClass.java"],
1781 sdk_version: "current",
1782 apex_available: ["myapex"],
1783 static_libs: ["transitive-jar"],
1784 }
1785 java_library {
1786 name: "transitive-jar",
1787 srcs: ["foo/bar/MyClass.java"],
1788 sdk_version: "core_platform",
1789 apex_available: ["myapex"],
1790 }
1791 `,
1792 },
1793 }
1794
1795 for _, test := range testCases {
1796 t.Run(test.name, func(t *testing.T) {
1797 if test.expectedError == "" {
1798 testApex(t, test.bp)
1799 } else {
1800 testApexError(t, test.expectedError, test.bp)
1801 }
1802 })
1803 }
1804}
1805
Jooyung Han749dc692020-04-15 11:03:39 +09001806func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
1807 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
1808 apex {
1809 name: "myapex",
1810 key: "myapex.key",
1811 native_shared_libs: ["mylib"],
1812 min_sdk_version: "29",
1813 }
1814
1815 apex_key {
1816 name: "myapex.key",
1817 public_key: "testkey.avbpubkey",
1818 private_key: "testkey.pem",
1819 }
1820
1821 cc_library {
1822 name: "mylib",
1823 srcs: ["mylib.cpp"],
1824 shared_libs: ["mylib2"],
1825 system_shared_libs: [],
1826 stl: "none",
1827 apex_available: [
1828 "myapex",
1829 ],
1830 min_sdk_version: "29",
1831 }
1832
1833 // indirect part of the apex
1834 cc_library {
1835 name: "mylib2",
1836 srcs: ["mylib.cpp"],
1837 system_shared_libs: [],
1838 stl: "none",
1839 apex_available: [
1840 "myapex",
1841 ],
1842 min_sdk_version: "30",
1843 }
1844 `)
1845}
1846
1847func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
1848 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
1849 apex {
1850 name: "myapex",
1851 key: "myapex.key",
1852 apps: ["AppFoo"],
1853 min_sdk_version: "29",
1854 }
1855
1856 apex_key {
1857 name: "myapex.key",
1858 public_key: "testkey.avbpubkey",
1859 private_key: "testkey.pem",
1860 }
1861
1862 android_app {
1863 name: "AppFoo",
1864 srcs: ["foo/bar/MyClass.java"],
1865 sdk_version: "current",
1866 min_sdk_version: "29",
1867 system_modules: "none",
1868 stl: "none",
1869 static_libs: ["bar"],
1870 apex_available: [ "myapex" ],
1871 }
1872
1873 java_library {
1874 name: "bar",
1875 sdk_version: "current",
1876 srcs: ["a.java"],
1877 apex_available: [ "myapex" ],
1878 }
1879 `)
1880}
1881
1882func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
1883 ctx, _ := testApex(t, `
1884 apex {
1885 name: "myapex",
1886 key: "myapex.key",
1887 native_shared_libs: ["mylib"],
1888 min_sdk_version: "29",
1889 }
1890
1891 apex_key {
1892 name: "myapex.key",
1893 public_key: "testkey.avbpubkey",
1894 private_key: "testkey.pem",
1895 }
1896
1897 // mylib in myapex will link to mylib2#29
1898 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
1899 cc_library {
1900 name: "mylib",
1901 srcs: ["mylib.cpp"],
1902 shared_libs: ["mylib2"],
1903 system_shared_libs: [],
1904 stl: "none",
1905 apex_available: ["myapex", "otherapex"],
1906 min_sdk_version: "29",
1907 }
1908
1909 cc_library {
1910 name: "mylib2",
1911 srcs: ["mylib.cpp"],
1912 system_shared_libs: [],
1913 stl: "none",
1914 apex_available: ["otherapex"],
1915 stubs: { versions: ["29", "30"] },
1916 min_sdk_version: "30",
1917 }
1918
1919 apex {
1920 name: "otherapex",
1921 key: "myapex.key",
1922 native_shared_libs: ["mylib", "mylib2"],
1923 min_sdk_version: "30",
1924 }
1925 `)
1926 expectLink := func(from, from_variant, to, to_variant string) {
1927 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1928 libFlags := ld.Args["libFlags"]
1929 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1930 }
1931 expectLink("mylib", "shared_myapex", "mylib2", "shared_29")
1932 expectLink("mylib", "shared_otherapex", "mylib2", "shared_otherapex")
1933}
1934
Jiyong Park7c2ee712018-12-07 00:42:25 +09001935func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001936 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001937 apex {
1938 name: "myapex",
1939 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001940 native_shared_libs: ["mylib"],
1941 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001942 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001943 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001944 }
1945
1946 apex_key {
1947 name: "myapex.key",
1948 public_key: "testkey.avbpubkey",
1949 private_key: "testkey.pem",
1950 }
1951
1952 prebuilt_etc {
1953 name: "myetc",
1954 src: "myprebuilt",
1955 sub_dir: "foo/bar",
1956 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001957
1958 cc_library {
1959 name: "mylib",
1960 srcs: ["mylib.cpp"],
1961 relative_install_path: "foo/bar",
1962 system_shared_libs: [],
1963 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001964 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001965 }
1966
1967 cc_binary {
1968 name: "mybin",
1969 srcs: ["mylib.cpp"],
1970 relative_install_path: "foo/bar",
1971 system_shared_libs: [],
1972 static_executable: true,
1973 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001974 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001975 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001976 `)
1977
Sundong Ahnabb64432019-10-22 13:58:29 +09001978 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001979 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1980
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001981 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001982 ensureListContains(t, dirs, "etc")
1983 ensureListContains(t, dirs, "etc/foo")
1984 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001985 ensureListContains(t, dirs, "lib64")
1986 ensureListContains(t, dirs, "lib64/foo")
1987 ensureListContains(t, dirs, "lib64/foo/bar")
1988 ensureListContains(t, dirs, "lib")
1989 ensureListContains(t, dirs, "lib/foo")
1990 ensureListContains(t, dirs, "lib/foo/bar")
1991
Jiyong Parkbd13e442019-03-15 18:10:35 +09001992 ensureListContains(t, dirs, "bin")
1993 ensureListContains(t, dirs, "bin/foo")
1994 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001995}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001996
Jooyung Han35155c42020-02-06 17:33:20 +09001997func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
1998 ctx, _ := testApex(t, `
1999 apex {
2000 name: "myapex",
2001 key: "myapex.key",
2002 multilib: {
2003 both: {
2004 native_shared_libs: ["mylib"],
2005 binaries: ["mybin"],
2006 },
2007 },
2008 compile_multilib: "both",
2009 native_bridge_supported: true,
2010 }
2011
2012 apex_key {
2013 name: "myapex.key",
2014 public_key: "testkey.avbpubkey",
2015 private_key: "testkey.pem",
2016 }
2017
2018 cc_library {
2019 name: "mylib",
2020 relative_install_path: "foo/bar",
2021 system_shared_libs: [],
2022 stl: "none",
2023 apex_available: [ "myapex" ],
2024 native_bridge_supported: true,
2025 }
2026
2027 cc_binary {
2028 name: "mybin",
2029 relative_install_path: "foo/bar",
2030 system_shared_libs: [],
2031 static_executable: true,
2032 stl: "none",
2033 apex_available: [ "myapex" ],
2034 native_bridge_supported: true,
2035 compile_multilib: "both", // default is "first" for binary
2036 multilib: {
2037 lib64: {
2038 suffix: "64",
2039 },
2040 },
2041 }
2042 `, withNativeBridgeEnabled)
2043 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2044 "bin/foo/bar/mybin",
2045 "bin/foo/bar/mybin64",
2046 "bin/arm/foo/bar/mybin",
2047 "bin/arm64/foo/bar/mybin64",
2048 "lib/foo/bar/mylib.so",
2049 "lib/arm/foo/bar/mylib.so",
2050 "lib64/foo/bar/mylib.so",
2051 "lib64/arm64/foo/bar/mylib.so",
2052 })
2053}
2054
Jiyong Parkda6eb592018-12-19 17:12:36 +09002055func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002056 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002057 apex {
2058 name: "myapex",
2059 key: "myapex.key",
2060 native_shared_libs: ["mylib"],
2061 use_vendor: true,
2062 }
2063
2064 apex_key {
2065 name: "myapex.key",
2066 public_key: "testkey.avbpubkey",
2067 private_key: "testkey.pem",
2068 }
2069
2070 cc_library {
2071 name: "mylib",
2072 srcs: ["mylib.cpp"],
2073 shared_libs: ["mylib2"],
2074 system_shared_libs: [],
2075 vendor_available: true,
2076 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002077 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002078 }
2079
2080 cc_library {
2081 name: "mylib2",
2082 srcs: ["mylib.cpp"],
2083 system_shared_libs: [],
2084 vendor_available: true,
2085 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002086 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002087 }
Jooyung Handc782442019-11-01 03:14:38 +09002088 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002089 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002090 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09002091
2092 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002093 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002094 for _, implicit := range i.Implicits {
2095 inputsList = append(inputsList, implicit.String())
2096 }
2097 }
2098 inputsString := strings.Join(inputsList, " ")
2099
2100 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08002101 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
2102 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002103
2104 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08002105 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
2106 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002107}
Jiyong Park16e91a02018-12-20 18:18:08 +09002108
Jooyung Han85d61762020-06-24 23:50:26 +09002109func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002110 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2111 apex {
2112 name: "myapex",
2113 key: "myapex.key",
2114 use_vendor: true,
2115 }
2116 apex_key {
2117 name: "myapex.key",
2118 public_key: "testkey.avbpubkey",
2119 private_key: "testkey.pem",
2120 }
2121 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002122 setUseVendorAllowListForTest(config, []string{""})
Jooyung Handc782442019-11-01 03:14:38 +09002123 })
Colin Cross440e0d02020-06-11 11:32:11 -07002124 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002125 testApex(t, `
2126 apex {
2127 name: "myapex",
2128 key: "myapex.key",
2129 use_vendor: true,
2130 }
2131 apex_key {
2132 name: "myapex.key",
2133 public_key: "testkey.avbpubkey",
2134 private_key: "testkey.pem",
2135 }
2136 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002137 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002138 })
2139}
2140
Jooyung Han5c998b92019-06-27 11:30:33 +09002141func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2142 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2143 apex {
2144 name: "myapex",
2145 key: "myapex.key",
2146 native_shared_libs: ["mylib"],
2147 use_vendor: true,
2148 }
2149
2150 apex_key {
2151 name: "myapex.key",
2152 public_key: "testkey.avbpubkey",
2153 private_key: "testkey.pem",
2154 }
2155
2156 cc_library {
2157 name: "mylib",
2158 srcs: ["mylib.cpp"],
2159 system_shared_libs: [],
2160 stl: "none",
2161 }
2162 `)
2163}
2164
Jooyung Han85d61762020-06-24 23:50:26 +09002165func TestVendorApex(t *testing.T) {
2166 ctx, config := testApex(t, `
2167 apex {
2168 name: "myapex",
2169 key: "myapex.key",
2170 binaries: ["mybin"],
2171 vendor: true,
2172 }
2173 apex_key {
2174 name: "myapex.key",
2175 public_key: "testkey.avbpubkey",
2176 private_key: "testkey.pem",
2177 }
2178 cc_binary {
2179 name: "mybin",
2180 vendor: true,
2181 shared_libs: ["libfoo"],
2182 }
2183 cc_library {
2184 name: "libfoo",
2185 proprietary: true,
2186 }
2187 `)
2188
2189 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2190 "bin/mybin",
2191 "lib64/libfoo.so",
2192 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2193 "lib64/libc++.so",
2194 })
2195
2196 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2197 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2198 name := apexBundle.BaseModuleName()
2199 prefix := "TARGET_"
2200 var builder strings.Builder
2201 data.Custom(&builder, name, prefix, "", data)
2202 androidMk := builder.String()
2203 ensureContains(t, androidMk, `LOCAL_MODULE_PATH := /tmp/target/product/test_device/vendor/apex`)
2204}
2205
Jooyung Handf78e212020-07-22 15:54:47 +09002206func TestVendorApex_use_vndk_as_stable(t *testing.T) {
2207 ctx, _ := testApex(t, `
2208 apex {
2209 name: "myapex",
2210 key: "myapex.key",
2211 binaries: ["mybin"],
2212 vendor: true,
2213 use_vndk_as_stable: true,
2214 }
2215 apex_key {
2216 name: "myapex.key",
2217 public_key: "testkey.avbpubkey",
2218 private_key: "testkey.pem",
2219 }
2220 cc_binary {
2221 name: "mybin",
2222 vendor: true,
2223 shared_libs: ["libvndk", "libvendor"],
2224 }
2225 cc_library {
2226 name: "libvndk",
2227 vndk: {
2228 enabled: true,
2229 },
2230 vendor_available: true,
2231 }
2232 cc_library {
2233 name: "libvendor",
2234 vendor: true,
2235 }
2236 `)
2237
2238 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2239
2240 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_myapex").Rule("ld")
2241 libs := names(ldRule.Args["libFlags"])
2242 // VNDK libs(libvndk/libc++) as they are
2243 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2244 ensureListContains(t, libs, buildDir+"/.intermediates/libc++/"+vendorVariant+"_shared/libc++.so")
2245 // non-stable Vendor libs as APEX variants
2246 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_myapex/libvendor.so")
2247
2248 // VNDK libs are not included when use_vndk_as_stable: true
2249 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2250 "bin/mybin",
2251 "lib64/libvendor.so",
2252 })
2253}
2254
Jooyung Hanefb184e2020-06-25 17:14:25 +09002255func TestAndroidMk_UseVendorRequired(t *testing.T) {
2256 ctx, config := testApex(t, `
2257 apex {
2258 name: "myapex",
2259 key: "myapex.key",
2260 use_vendor: true,
2261 native_shared_libs: ["mylib"],
2262 }
2263
2264 apex_key {
2265 name: "myapex.key",
2266 public_key: "testkey.avbpubkey",
2267 private_key: "testkey.pem",
2268 }
2269
2270 cc_library {
2271 name: "mylib",
2272 vendor_available: true,
2273 apex_available: ["myapex"],
2274 }
2275 `, func(fs map[string][]byte, config android.Config) {
2276 setUseVendorAllowListForTest(config, []string{"myapex"})
2277 })
2278
2279 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2280 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2281 name := apexBundle.BaseModuleName()
2282 prefix := "TARGET_"
2283 var builder strings.Builder
2284 data.Custom(&builder, name, prefix, "", data)
2285 androidMk := builder.String()
2286 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2287}
2288
2289func TestAndroidMk_VendorApexRequired(t *testing.T) {
2290 ctx, config := testApex(t, `
2291 apex {
2292 name: "myapex",
2293 key: "myapex.key",
2294 vendor: true,
2295 native_shared_libs: ["mylib"],
2296 }
2297
2298 apex_key {
2299 name: "myapex.key",
2300 public_key: "testkey.avbpubkey",
2301 private_key: "testkey.pem",
2302 }
2303
2304 cc_library {
2305 name: "mylib",
2306 vendor_available: true,
2307 }
2308 `)
2309
2310 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2311 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2312 name := apexBundle.BaseModuleName()
2313 prefix := "TARGET_"
2314 var builder strings.Builder
2315 data.Custom(&builder, name, prefix, "", data)
2316 androidMk := builder.String()
2317 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2318}
2319
Jooyung Han2ed99d02020-06-24 23:26:26 +09002320func TestAndroidMkWritesCommonProperties(t *testing.T) {
2321 ctx, config := testApex(t, `
2322 apex {
2323 name: "myapex",
2324 key: "myapex.key",
2325 vintf_fragments: ["fragment.xml"],
2326 init_rc: ["init.rc"],
2327 }
2328 apex_key {
2329 name: "myapex.key",
2330 public_key: "testkey.avbpubkey",
2331 private_key: "testkey.pem",
2332 }
2333 cc_binary {
2334 name: "mybin",
2335 }
2336 `)
2337
2338 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2339 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2340 name := apexBundle.BaseModuleName()
2341 prefix := "TARGET_"
2342 var builder strings.Builder
2343 data.Custom(&builder, name, prefix, "", data)
2344 androidMk := builder.String()
2345 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2346 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2347}
2348
Jiyong Park16e91a02018-12-20 18:18:08 +09002349func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002350 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002351 apex {
2352 name: "myapex",
2353 key: "myapex.key",
2354 native_shared_libs: ["mylib"],
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 srcs: ["mylib.cpp"],
2366 system_shared_libs: [],
2367 stl: "none",
2368 stubs: {
2369 versions: ["1", "2", "3"],
2370 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002371 apex_available: [
2372 "//apex_available:platform",
2373 "myapex",
2374 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002375 }
2376
2377 cc_binary {
2378 name: "not_in_apex",
2379 srcs: ["mylib.cpp"],
2380 static_libs: ["mylib"],
2381 static_executable: true,
2382 system_shared_libs: [],
2383 stl: "none",
2384 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002385 `)
2386
Colin Cross7113d202019-11-20 16:39:12 -08002387 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002388
2389 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002390 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002391}
Jiyong Park9335a262018-12-24 11:31:58 +09002392
2393func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002394 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002395 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002396 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002397 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002398 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002399 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002400 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09002401 }
2402
2403 cc_library {
2404 name: "mylib",
2405 srcs: ["mylib.cpp"],
2406 system_shared_libs: [],
2407 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002408 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002409 }
2410
2411 apex_key {
2412 name: "myapex.key",
2413 public_key: "testkey.avbpubkey",
2414 private_key: "testkey.pem",
2415 }
2416
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002417 android_app_certificate {
2418 name: "myapex.certificate",
2419 certificate: "testkey",
2420 }
2421
2422 android_app_certificate {
2423 name: "myapex.certificate.override",
2424 certificate: "testkey.override",
2425 }
2426
Jiyong Park9335a262018-12-24 11:31:58 +09002427 `)
2428
2429 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002430 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002431
2432 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2433 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
2434 "vendor/foo/devkeys/testkey.avbpubkey")
2435 }
2436 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
2437 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
2438 "vendor/foo/devkeys/testkey.pem")
2439 }
2440
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002441 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002442 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002443 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002444 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002445 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002446 }
2447}
Jiyong Park58e364a2019-01-19 19:24:06 +09002448
Jooyung Hanf121a652019-12-17 14:30:11 +09002449func TestCertificate(t *testing.T) {
2450 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
2451 ctx, _ := testApex(t, `
2452 apex {
2453 name: "myapex",
2454 key: "myapex.key",
2455 }
2456 apex_key {
2457 name: "myapex.key",
2458 public_key: "testkey.avbpubkey",
2459 private_key: "testkey.pem",
2460 }`)
2461 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2462 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2463 if actual := rule.Args["certificates"]; actual != expected {
2464 t.Errorf("certificates should be %q, not %q", expected, actual)
2465 }
2466 })
2467 t.Run("override when unspecified", func(t *testing.T) {
2468 ctx, _ := testApex(t, `
2469 apex {
2470 name: "myapex_keytest",
2471 key: "myapex.key",
2472 file_contexts: ":myapex-file_contexts",
2473 }
2474 apex_key {
2475 name: "myapex.key",
2476 public_key: "testkey.avbpubkey",
2477 private_key: "testkey.pem",
2478 }
2479 android_app_certificate {
2480 name: "myapex.certificate.override",
2481 certificate: "testkey.override",
2482 }`)
2483 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2484 expected := "testkey.override.x509.pem testkey.override.pk8"
2485 if actual := rule.Args["certificates"]; actual != expected {
2486 t.Errorf("certificates should be %q, not %q", expected, actual)
2487 }
2488 })
2489 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
2490 ctx, _ := testApex(t, `
2491 apex {
2492 name: "myapex",
2493 key: "myapex.key",
2494 certificate: ":myapex.certificate",
2495 }
2496 apex_key {
2497 name: "myapex.key",
2498 public_key: "testkey.avbpubkey",
2499 private_key: "testkey.pem",
2500 }
2501 android_app_certificate {
2502 name: "myapex.certificate",
2503 certificate: "testkey",
2504 }`)
2505 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2506 expected := "testkey.x509.pem testkey.pk8"
2507 if actual := rule.Args["certificates"]; actual != expected {
2508 t.Errorf("certificates should be %q, not %q", expected, actual)
2509 }
2510 })
2511 t.Run("override when specifiec as <:module>", func(t *testing.T) {
2512 ctx, _ := testApex(t, `
2513 apex {
2514 name: "myapex_keytest",
2515 key: "myapex.key",
2516 file_contexts: ":myapex-file_contexts",
2517 certificate: ":myapex.certificate",
2518 }
2519 apex_key {
2520 name: "myapex.key",
2521 public_key: "testkey.avbpubkey",
2522 private_key: "testkey.pem",
2523 }
2524 android_app_certificate {
2525 name: "myapex.certificate.override",
2526 certificate: "testkey.override",
2527 }`)
2528 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2529 expected := "testkey.override.x509.pem testkey.override.pk8"
2530 if actual := rule.Args["certificates"]; actual != expected {
2531 t.Errorf("certificates should be %q, not %q", expected, actual)
2532 }
2533 })
2534 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
2535 ctx, _ := testApex(t, `
2536 apex {
2537 name: "myapex",
2538 key: "myapex.key",
2539 certificate: "testkey",
2540 }
2541 apex_key {
2542 name: "myapex.key",
2543 public_key: "testkey.avbpubkey",
2544 private_key: "testkey.pem",
2545 }`)
2546 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2547 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2548 if actual := rule.Args["certificates"]; actual != expected {
2549 t.Errorf("certificates should be %q, not %q", expected, actual)
2550 }
2551 })
2552 t.Run("override when specified as <name>", func(t *testing.T) {
2553 ctx, _ := testApex(t, `
2554 apex {
2555 name: "myapex_keytest",
2556 key: "myapex.key",
2557 file_contexts: ":myapex-file_contexts",
2558 certificate: "testkey",
2559 }
2560 apex_key {
2561 name: "myapex.key",
2562 public_key: "testkey.avbpubkey",
2563 private_key: "testkey.pem",
2564 }
2565 android_app_certificate {
2566 name: "myapex.certificate.override",
2567 certificate: "testkey.override",
2568 }`)
2569 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2570 expected := "testkey.override.x509.pem testkey.override.pk8"
2571 if actual := rule.Args["certificates"]; actual != expected {
2572 t.Errorf("certificates should be %q, not %q", expected, actual)
2573 }
2574 })
2575}
2576
Jiyong Park58e364a2019-01-19 19:24:06 +09002577func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002578 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002579 apex {
2580 name: "myapex",
2581 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002582 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09002583 }
2584
2585 apex {
2586 name: "otherapex",
2587 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002588 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002589 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002590 }
2591
2592 apex_key {
2593 name: "myapex.key",
2594 public_key: "testkey.avbpubkey",
2595 private_key: "testkey.pem",
2596 }
2597
2598 cc_library {
2599 name: "mylib",
2600 srcs: ["mylib.cpp"],
2601 system_shared_libs: [],
2602 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002603 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002604 "myapex",
2605 "otherapex",
2606 ],
Jooyung Han24282772020-03-21 23:20:55 +09002607 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002608 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002609 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002610 cc_library {
2611 name: "mylib2",
2612 srcs: ["mylib.cpp"],
2613 system_shared_libs: [],
2614 stl: "none",
2615 apex_available: [
2616 "myapex",
2617 "otherapex",
2618 ],
2619 use_apex_name_macro: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002620 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002621 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002622 `)
2623
Jooyung Hanc87a0592020-03-02 17:44:33 +09002624 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002625 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002626 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han75568392020-03-20 04:29:24 +09002627 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002628
Jooyung Hanccce2f22020-03-07 03:45:53 +09002629 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09002630 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
2631 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09002632 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09002633 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002634
Jooyung Hanccce2f22020-03-07 03:45:53 +09002635 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09002636 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
2637 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09002638 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09002639 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002640
Jooyung Hanc87a0592020-03-02 17:44:33 +09002641 // When cc_library sets use_apex_name_macro: true
2642 // apex variants define additional macro to distinguish which apex variant it is built for
2643
2644 // non-APEX variant does not have __ANDROID_APEX__ defined
2645 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2646 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2647
2648 // APEX variant has __ANDROID_APEX__ defined
2649 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002650 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002651 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2652 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002653
Jooyung Hanc87a0592020-03-02 17:44:33 +09002654 // APEX variant has __ANDROID_APEX__ defined
2655 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002656 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002657 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2658 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09002659
2660 // recovery variant does not set __ANDROID_SDK_VERSION__
2661 mylibCFlags = ctx.ModuleForTests("mylib", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2662 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2663 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002664}
Jiyong Park7e636d02019-01-28 16:16:54 +09002665
2666func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002667 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09002668 apex {
2669 name: "myapex",
2670 key: "myapex.key",
2671 native_shared_libs: ["mylib"],
2672 }
2673
2674 apex_key {
2675 name: "myapex.key",
2676 public_key: "testkey.avbpubkey",
2677 private_key: "testkey.pem",
2678 }
2679
2680 cc_library_headers {
2681 name: "mylib_headers",
2682 export_include_dirs: ["my_include"],
2683 system_shared_libs: [],
2684 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09002685 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002686 }
2687
2688 cc_library {
2689 name: "mylib",
2690 srcs: ["mylib.cpp"],
2691 system_shared_libs: [],
2692 stl: "none",
2693 header_libs: ["mylib_headers"],
2694 export_header_lib_headers: ["mylib_headers"],
2695 stubs: {
2696 versions: ["1", "2", "3"],
2697 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002698 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002699 }
2700
2701 cc_library {
2702 name: "otherlib",
2703 srcs: ["mylib.cpp"],
2704 system_shared_libs: [],
2705 stl: "none",
2706 shared_libs: ["mylib"],
2707 }
2708 `)
2709
Colin Cross7113d202019-11-20 16:39:12 -08002710 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09002711
2712 // Ensure that the include path of the header lib is exported to 'otherlib'
2713 ensureContains(t, cFlags, "-Imy_include")
2714}
Alex Light9670d332019-01-29 18:07:33 -08002715
Jiyong Park7cd10e32020-01-14 09:22:18 +09002716type fileInApex struct {
2717 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00002718 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09002719 isLink bool
2720}
2721
Jooyung Hana57af4a2020-01-23 05:36:59 +00002722func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09002723 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00002724 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09002725 copyCmds := apexRule.Args["copy_commands"]
2726 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09002727 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09002728 for _, cmd := range strings.Split(copyCmds, "&&") {
2729 cmd = strings.TrimSpace(cmd)
2730 if cmd == "" {
2731 continue
2732 }
2733 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00002734 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09002735 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09002736 switch terms[0] {
2737 case "mkdir":
2738 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09002739 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09002740 t.Fatal("copyCmds contains invalid cp command", cmd)
2741 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002742 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002743 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002744 isLink = false
2745 case "ln":
2746 if len(terms) != 3 && len(terms) != 4 {
2747 // ln LINK TARGET or ln -s LINK TARGET
2748 t.Fatal("copyCmds contains invalid ln command", cmd)
2749 }
2750 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002751 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002752 isLink = true
2753 default:
2754 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
2755 }
2756 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09002757 index := strings.Index(dst, imageApexDir)
2758 if index == -1 {
2759 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
2760 }
2761 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002762 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09002763 }
2764 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002765 return ret
2766}
2767
Jooyung Hana57af4a2020-01-23 05:36:59 +00002768func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
2769 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09002770 var failed bool
2771 var surplus []string
2772 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00002773 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09002774 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09002775 for _, expected := range files {
2776 if matched, _ := path.Match(expected, file.path); matched {
2777 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09002778 mactchFound = true
2779 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09002780 }
2781 }
Jooyung Hane6436d72020-02-27 13:31:56 +09002782 if !mactchFound {
2783 surplus = append(surplus, file.path)
2784 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002785 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002786
Jooyung Han31c470b2019-10-18 16:26:59 +09002787 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002788 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09002789 t.Log("surplus files", surplus)
2790 failed = true
2791 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002792
2793 if len(files) > len(filesMatched) {
2794 var missing []string
2795 for _, expected := range files {
2796 if !filesMatched[expected] {
2797 missing = append(missing, expected)
2798 }
2799 }
2800 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09002801 t.Log("missing files", missing)
2802 failed = true
2803 }
2804 if failed {
2805 t.Fail()
2806 }
2807}
2808
Jooyung Han344d5432019-08-23 11:17:39 +09002809func TestVndkApexCurrent(t *testing.T) {
2810 ctx, _ := testApex(t, `
2811 apex_vndk {
2812 name: "myapex",
2813 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002814 }
2815
2816 apex_key {
2817 name: "myapex.key",
2818 public_key: "testkey.avbpubkey",
2819 private_key: "testkey.pem",
2820 }
2821
2822 cc_library {
2823 name: "libvndk",
2824 srcs: ["mylib.cpp"],
2825 vendor_available: true,
2826 vndk: {
2827 enabled: true,
2828 },
2829 system_shared_libs: [],
2830 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002831 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002832 }
2833
2834 cc_library {
2835 name: "libvndksp",
2836 srcs: ["mylib.cpp"],
2837 vendor_available: true,
2838 vndk: {
2839 enabled: true,
2840 support_system_process: true,
2841 },
2842 system_shared_libs: [],
2843 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002844 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002845 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002846 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002847
Jooyung Hana57af4a2020-01-23 05:36:59 +00002848 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002849 "lib/libvndk.so",
2850 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002851 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002852 "lib64/libvndk.so",
2853 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002854 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002855 "etc/llndk.libraries.VER.txt",
2856 "etc/vndkcore.libraries.VER.txt",
2857 "etc/vndksp.libraries.VER.txt",
2858 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002859 })
Jooyung Han344d5432019-08-23 11:17:39 +09002860}
2861
2862func TestVndkApexWithPrebuilt(t *testing.T) {
2863 ctx, _ := testApex(t, `
2864 apex_vndk {
2865 name: "myapex",
2866 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002867 }
2868
2869 apex_key {
2870 name: "myapex.key",
2871 public_key: "testkey.avbpubkey",
2872 private_key: "testkey.pem",
2873 }
2874
2875 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002876 name: "libvndk",
2877 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002878 vendor_available: true,
2879 vndk: {
2880 enabled: true,
2881 },
2882 system_shared_libs: [],
2883 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002884 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002885 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002886
2887 cc_prebuilt_library_shared {
2888 name: "libvndk.arm",
2889 srcs: ["libvndk.arm.so"],
2890 vendor_available: true,
2891 vndk: {
2892 enabled: true,
2893 },
2894 enabled: false,
2895 arch: {
2896 arm: {
2897 enabled: true,
2898 },
2899 },
2900 system_shared_libs: [],
2901 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002902 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002903 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002904 `+vndkLibrariesTxtFiles("current"),
2905 withFiles(map[string][]byte{
2906 "libvndk.so": nil,
2907 "libvndk.arm.so": nil,
2908 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002909
Jooyung Hana57af4a2020-01-23 05:36:59 +00002910 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002911 "lib/libvndk.so",
2912 "lib/libvndk.arm.so",
2913 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002914 "lib/libc++.so",
2915 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002916 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002917 })
Jooyung Han344d5432019-08-23 11:17:39 +09002918}
2919
Jooyung Han39edb6c2019-11-06 16:53:07 +09002920func vndkLibrariesTxtFiles(vers ...string) (result string) {
2921 for _, v := range vers {
2922 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002923 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002924 result += `
2925 vndk_libraries_txt {
2926 name: "` + txt + `.libraries.txt",
2927 }
2928 `
2929 }
2930 } else {
2931 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2932 result += `
2933 prebuilt_etc {
2934 name: "` + txt + `.libraries.` + v + `.txt",
2935 src: "dummy.txt",
2936 }
2937 `
2938 }
2939 }
2940 }
2941 return
2942}
2943
Jooyung Han344d5432019-08-23 11:17:39 +09002944func TestVndkApexVersion(t *testing.T) {
2945 ctx, _ := testApex(t, `
2946 apex_vndk {
2947 name: "myapex_v27",
2948 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002949 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002950 vndk_version: "27",
2951 }
2952
2953 apex_key {
2954 name: "myapex.key",
2955 public_key: "testkey.avbpubkey",
2956 private_key: "testkey.pem",
2957 }
2958
Jooyung Han31c470b2019-10-18 16:26:59 +09002959 vndk_prebuilt_shared {
2960 name: "libvndk27",
2961 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002962 vendor_available: true,
2963 vndk: {
2964 enabled: true,
2965 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002966 target_arch: "arm64",
2967 arch: {
2968 arm: {
2969 srcs: ["libvndk27_arm.so"],
2970 },
2971 arm64: {
2972 srcs: ["libvndk27_arm64.so"],
2973 },
2974 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002975 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002976 }
2977
2978 vndk_prebuilt_shared {
2979 name: "libvndk27",
2980 version: "27",
2981 vendor_available: true,
2982 vndk: {
2983 enabled: true,
2984 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002985 target_arch: "x86_64",
2986 arch: {
2987 x86: {
2988 srcs: ["libvndk27_x86.so"],
2989 },
2990 x86_64: {
2991 srcs: ["libvndk27_x86_64.so"],
2992 },
2993 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002994 }
2995 `+vndkLibrariesTxtFiles("27"),
2996 withFiles(map[string][]byte{
2997 "libvndk27_arm.so": nil,
2998 "libvndk27_arm64.so": nil,
2999 "libvndk27_x86.so": nil,
3000 "libvndk27_x86_64.so": nil,
3001 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003002
Jooyung Hana57af4a2020-01-23 05:36:59 +00003003 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003004 "lib/libvndk27_arm.so",
3005 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003006 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003007 })
Jooyung Han344d5432019-08-23 11:17:39 +09003008}
3009
3010func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
3011 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
3012 apex_vndk {
3013 name: "myapex_v27",
3014 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003015 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003016 vndk_version: "27",
3017 }
3018 apex_vndk {
3019 name: "myapex_v27_other",
3020 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003021 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003022 vndk_version: "27",
3023 }
3024
3025 apex_key {
3026 name: "myapex.key",
3027 public_key: "testkey.avbpubkey",
3028 private_key: "testkey.pem",
3029 }
3030
3031 cc_library {
3032 name: "libvndk",
3033 srcs: ["mylib.cpp"],
3034 vendor_available: true,
3035 vndk: {
3036 enabled: true,
3037 },
3038 system_shared_libs: [],
3039 stl: "none",
3040 }
3041
3042 vndk_prebuilt_shared {
3043 name: "libvndk",
3044 version: "27",
3045 vendor_available: true,
3046 vndk: {
3047 enabled: true,
3048 },
3049 srcs: ["libvndk.so"],
3050 }
3051 `, withFiles(map[string][]byte{
3052 "libvndk.so": nil,
3053 }))
3054}
3055
Jooyung Han90eee022019-10-01 20:02:42 +09003056func TestVndkApexNameRule(t *testing.T) {
3057 ctx, _ := testApex(t, `
3058 apex_vndk {
3059 name: "myapex",
3060 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003061 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003062 }
3063 apex_vndk {
3064 name: "myapex_v28",
3065 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003066 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003067 vndk_version: "28",
3068 }
3069 apex_key {
3070 name: "myapex.key",
3071 public_key: "testkey.avbpubkey",
3072 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003073 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003074
3075 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003076 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003077 actual := proptools.String(bundle.properties.Apex_name)
3078 if !reflect.DeepEqual(actual, expected) {
3079 t.Errorf("Got '%v', expected '%v'", actual, expected)
3080 }
3081 }
3082
3083 assertApexName("com.android.vndk.vVER", "myapex")
3084 assertApexName("com.android.vndk.v28", "myapex_v28")
3085}
3086
Jooyung Han344d5432019-08-23 11:17:39 +09003087func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
3088 ctx, _ := testApex(t, `
3089 apex_vndk {
3090 name: "myapex",
3091 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003092 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003093 }
3094
3095 apex_key {
3096 name: "myapex.key",
3097 public_key: "testkey.avbpubkey",
3098 private_key: "testkey.pem",
3099 }
3100
3101 cc_library {
3102 name: "libvndk",
3103 srcs: ["mylib.cpp"],
3104 vendor_available: true,
3105 native_bridge_supported: true,
3106 host_supported: true,
3107 vndk: {
3108 enabled: true,
3109 },
3110 system_shared_libs: [],
3111 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003112 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003113 }
Jooyung Han35155c42020-02-06 17:33:20 +09003114 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003115
Jooyung Hana57af4a2020-01-23 05:36:59 +00003116 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003117 "lib/libvndk.so",
3118 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003119 "lib/libc++.so",
3120 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003121 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003122 })
Jooyung Han344d5432019-08-23 11:17:39 +09003123}
3124
3125func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
3126 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
3127 apex_vndk {
3128 name: "myapex",
3129 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003130 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003131 native_bridge_supported: true,
3132 }
3133
3134 apex_key {
3135 name: "myapex.key",
3136 public_key: "testkey.avbpubkey",
3137 private_key: "testkey.pem",
3138 }
3139
3140 cc_library {
3141 name: "libvndk",
3142 srcs: ["mylib.cpp"],
3143 vendor_available: true,
3144 native_bridge_supported: true,
3145 host_supported: true,
3146 vndk: {
3147 enabled: true,
3148 },
3149 system_shared_libs: [],
3150 stl: "none",
3151 }
3152 `)
3153}
3154
Jooyung Han31c470b2019-10-18 16:26:59 +09003155func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003156 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003157 apex_vndk {
3158 name: "myapex_v27",
3159 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003160 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003161 vndk_version: "27",
3162 }
3163
3164 apex_key {
3165 name: "myapex.key",
3166 public_key: "testkey.avbpubkey",
3167 private_key: "testkey.pem",
3168 }
3169
3170 vndk_prebuilt_shared {
3171 name: "libvndk27",
3172 version: "27",
3173 target_arch: "arm",
3174 vendor_available: true,
3175 vndk: {
3176 enabled: true,
3177 },
3178 arch: {
3179 arm: {
3180 srcs: ["libvndk27.so"],
3181 }
3182 },
3183 }
3184
3185 vndk_prebuilt_shared {
3186 name: "libvndk27",
3187 version: "27",
3188 target_arch: "arm",
3189 binder32bit: true,
3190 vendor_available: true,
3191 vndk: {
3192 enabled: true,
3193 },
3194 arch: {
3195 arm: {
3196 srcs: ["libvndk27binder32.so"],
3197 }
3198 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003199 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003200 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003201 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003202 withFiles(map[string][]byte{
3203 "libvndk27.so": nil,
3204 "libvndk27binder32.so": nil,
3205 }),
3206 withBinder32bit,
3207 withTargets(map[android.OsType][]android.Target{
3208 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003209 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3210 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003211 },
3212 }),
3213 )
3214
Jooyung Hana57af4a2020-01-23 05:36:59 +00003215 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003216 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003217 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003218 })
3219}
3220
Jooyung Han45a96772020-06-15 14:59:42 +09003221func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
3222 ctx, _ := testApex(t, `
3223 apex_vndk {
3224 name: "myapex",
3225 key: "myapex.key",
3226 file_contexts: ":myapex-file_contexts",
3227 }
3228
3229 apex_key {
3230 name: "myapex.key",
3231 public_key: "testkey.avbpubkey",
3232 private_key: "testkey.pem",
3233 }
3234
3235 cc_library {
3236 name: "libz",
3237 vendor_available: true,
3238 vndk: {
3239 enabled: true,
3240 },
3241 stubs: {
3242 symbol_file: "libz.map.txt",
3243 versions: ["30"],
3244 }
3245 }
3246 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3247 "libz.map.txt": nil,
3248 }))
3249
3250 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_image").Rule("apexManifestRule")
3251 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3252 ensureListEmpty(t, provideNativeLibs)
3253}
3254
Jooyung Hane1633032019-08-01 17:41:43 +09003255func TestDependenciesInApexManifest(t *testing.T) {
3256 ctx, _ := testApex(t, `
3257 apex {
3258 name: "myapex_nodep",
3259 key: "myapex.key",
3260 native_shared_libs: ["lib_nodep"],
3261 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003262 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003263 }
3264
3265 apex {
3266 name: "myapex_dep",
3267 key: "myapex.key",
3268 native_shared_libs: ["lib_dep"],
3269 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003270 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003271 }
3272
3273 apex {
3274 name: "myapex_provider",
3275 key: "myapex.key",
3276 native_shared_libs: ["libfoo"],
3277 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003278 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003279 }
3280
3281 apex {
3282 name: "myapex_selfcontained",
3283 key: "myapex.key",
3284 native_shared_libs: ["lib_dep", "libfoo"],
3285 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003286 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003287 }
3288
3289 apex_key {
3290 name: "myapex.key",
3291 public_key: "testkey.avbpubkey",
3292 private_key: "testkey.pem",
3293 }
3294
3295 cc_library {
3296 name: "lib_nodep",
3297 srcs: ["mylib.cpp"],
3298 system_shared_libs: [],
3299 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003300 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003301 }
3302
3303 cc_library {
3304 name: "lib_dep",
3305 srcs: ["mylib.cpp"],
3306 shared_libs: ["libfoo"],
3307 system_shared_libs: [],
3308 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003309 apex_available: [
3310 "myapex_dep",
3311 "myapex_provider",
3312 "myapex_selfcontained",
3313 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003314 }
3315
3316 cc_library {
3317 name: "libfoo",
3318 srcs: ["mytest.cpp"],
3319 stubs: {
3320 versions: ["1"],
3321 },
3322 system_shared_libs: [],
3323 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003324 apex_available: [
3325 "myapex_provider",
3326 "myapex_selfcontained",
3327 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003328 }
3329 `)
3330
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003331 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003332 var provideNativeLibs, requireNativeLibs []string
3333
Sundong Ahnabb64432019-10-22 13:58:29 +09003334 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003335 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3336 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003337 ensureListEmpty(t, provideNativeLibs)
3338 ensureListEmpty(t, requireNativeLibs)
3339
Sundong Ahnabb64432019-10-22 13:58:29 +09003340 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003341 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3342 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003343 ensureListEmpty(t, provideNativeLibs)
3344 ensureListContains(t, requireNativeLibs, "libfoo.so")
3345
Sundong Ahnabb64432019-10-22 13:58:29 +09003346 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003347 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3348 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003349 ensureListContains(t, provideNativeLibs, "libfoo.so")
3350 ensureListEmpty(t, requireNativeLibs)
3351
Sundong Ahnabb64432019-10-22 13:58:29 +09003352 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003353 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3354 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003355 ensureListContains(t, provideNativeLibs, "libfoo.so")
3356 ensureListEmpty(t, requireNativeLibs)
3357}
3358
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003359func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09003360 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003361 apex {
3362 name: "myapex",
3363 key: "myapex.key",
3364 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003365 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003366 }
3367
3368 apex_key {
3369 name: "myapex.key",
3370 public_key: "testkey.avbpubkey",
3371 private_key: "testkey.pem",
3372 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003373
3374 cc_library {
3375 name: "mylib",
3376 srcs: ["mylib.cpp"],
3377 system_shared_libs: [],
3378 stl: "none",
3379 apex_available: [
3380 "//apex_available:platform",
3381 "myapex",
3382 ],
3383 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003384 `)
3385
Sundong Ahnabb64432019-10-22 13:58:29 +09003386 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003387 apexManifestRule := module.Rule("apexManifestRule")
3388 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3389 apexRule := module.Rule("apexRule")
3390 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003391
3392 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3393 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3394 name := apexBundle.BaseModuleName()
3395 prefix := "TARGET_"
3396 var builder strings.Builder
3397 data.Custom(&builder, name, prefix, "", data)
3398 androidMk := builder.String()
3399 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3400 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003401}
3402
Alex Light0851b882019-02-07 13:20:53 -08003403func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003404 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003405 apex {
3406 name: "myapex",
3407 key: "myapex.key",
3408 native_shared_libs: ["mylib_common"],
3409 }
3410
3411 apex_key {
3412 name: "myapex.key",
3413 public_key: "testkey.avbpubkey",
3414 private_key: "testkey.pem",
3415 }
3416
3417 cc_library {
3418 name: "mylib_common",
3419 srcs: ["mylib.cpp"],
3420 system_shared_libs: [],
3421 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003422 apex_available: [
3423 "//apex_available:platform",
3424 "myapex",
3425 ],
Alex Light0851b882019-02-07 13:20:53 -08003426 }
3427 `)
3428
Sundong Ahnabb64432019-10-22 13:58:29 +09003429 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003430 apexRule := module.Rule("apexRule")
3431 copyCmds := apexRule.Args["copy_commands"]
3432
3433 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3434 t.Log("Apex was a test apex!")
3435 t.Fail()
3436 }
3437 // Ensure that main rule creates an output
3438 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3439
3440 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08003441 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08003442
3443 // Ensure that both direct and indirect deps are copied into apex
3444 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3445
Colin Cross7113d202019-11-20 16:39:12 -08003446 // Ensure that the platform variant ends with _shared
3447 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003448
3449 if !android.InAnyApex("mylib_common") {
3450 t.Log("Found mylib_common not in any apex!")
3451 t.Fail()
3452 }
3453}
3454
3455func TestTestApex(t *testing.T) {
3456 if android.InAnyApex("mylib_common_test") {
3457 t.Fatal("mylib_common_test must not be used in any other tests since this checks that global state is not updated in an illegal way!")
3458 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003459 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003460 apex_test {
3461 name: "myapex",
3462 key: "myapex.key",
3463 native_shared_libs: ["mylib_common_test"],
3464 }
3465
3466 apex_key {
3467 name: "myapex.key",
3468 public_key: "testkey.avbpubkey",
3469 private_key: "testkey.pem",
3470 }
3471
3472 cc_library {
3473 name: "mylib_common_test",
3474 srcs: ["mylib.cpp"],
3475 system_shared_libs: [],
3476 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003477 // TODO: remove //apex_available:platform
3478 apex_available: [
3479 "//apex_available:platform",
3480 "myapex",
3481 ],
Alex Light0851b882019-02-07 13:20:53 -08003482 }
3483 `)
3484
Sundong Ahnabb64432019-10-22 13:58:29 +09003485 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003486 apexRule := module.Rule("apexRule")
3487 copyCmds := apexRule.Args["copy_commands"]
3488
3489 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3490 t.Log("Apex was not a test apex!")
3491 t.Fail()
3492 }
3493 // Ensure that main rule creates an output
3494 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3495
3496 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08003497 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08003498
3499 // Ensure that both direct and indirect deps are copied into apex
3500 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3501
Colin Cross7113d202019-11-20 16:39:12 -08003502 // Ensure that the platform variant ends with _shared
3503 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003504}
3505
Alex Light9670d332019-01-29 18:07:33 -08003506func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003507 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003508 apex {
3509 name: "myapex",
3510 key: "myapex.key",
3511 multilib: {
3512 first: {
3513 native_shared_libs: ["mylib_common"],
3514 }
3515 },
3516 target: {
3517 android: {
3518 multilib: {
3519 first: {
3520 native_shared_libs: ["mylib"],
3521 }
3522 }
3523 },
3524 host: {
3525 multilib: {
3526 first: {
3527 native_shared_libs: ["mylib2"],
3528 }
3529 }
3530 }
3531 }
3532 }
3533
3534 apex_key {
3535 name: "myapex.key",
3536 public_key: "testkey.avbpubkey",
3537 private_key: "testkey.pem",
3538 }
3539
3540 cc_library {
3541 name: "mylib",
3542 srcs: ["mylib.cpp"],
3543 system_shared_libs: [],
3544 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003545 // TODO: remove //apex_available:platform
3546 apex_available: [
3547 "//apex_available:platform",
3548 "myapex",
3549 ],
Alex Light9670d332019-01-29 18:07:33 -08003550 }
3551
3552 cc_library {
3553 name: "mylib_common",
3554 srcs: ["mylib.cpp"],
3555 system_shared_libs: [],
3556 stl: "none",
3557 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003558 // TODO: remove //apex_available:platform
3559 apex_available: [
3560 "//apex_available:platform",
3561 "myapex",
3562 ],
Alex Light9670d332019-01-29 18:07:33 -08003563 }
3564
3565 cc_library {
3566 name: "mylib2",
3567 srcs: ["mylib.cpp"],
3568 system_shared_libs: [],
3569 stl: "none",
3570 compile_multilib: "first",
3571 }
3572 `)
3573
Sundong Ahnabb64432019-10-22 13:58:29 +09003574 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003575 copyCmds := apexRule.Args["copy_commands"]
3576
3577 // Ensure that main rule creates an output
3578 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3579
3580 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08003581 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3582 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
3583 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08003584
3585 // Ensure that both direct and indirect deps are copied into apex
3586 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3587 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3588 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3589
Colin Cross7113d202019-11-20 16:39:12 -08003590 // Ensure that the platform variant ends with _shared
3591 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3592 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3593 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003594}
Jiyong Park04480cf2019-02-06 00:16:29 +09003595
3596func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003597 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09003598 apex {
3599 name: "myapex",
3600 key: "myapex.key",
3601 binaries: ["myscript"],
3602 }
3603
3604 apex_key {
3605 name: "myapex.key",
3606 public_key: "testkey.avbpubkey",
3607 private_key: "testkey.pem",
3608 }
3609
3610 sh_binary {
3611 name: "myscript",
3612 src: "mylib.cpp",
3613 filename: "myscript.sh",
3614 sub_dir: "script",
3615 }
3616 `)
3617
Sundong Ahnabb64432019-10-22 13:58:29 +09003618 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09003619 copyCmds := apexRule.Args["copy_commands"]
3620
3621 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
3622}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003623
Jooyung Han91df2082019-11-20 01:49:42 +09003624func TestApexInVariousPartition(t *testing.T) {
3625 testcases := []struct {
3626 propName, parition, flattenedPartition string
3627 }{
3628 {"", "system", "system_ext"},
3629 {"product_specific: true", "product", "product"},
3630 {"soc_specific: true", "vendor", "vendor"},
3631 {"proprietary: true", "vendor", "vendor"},
3632 {"vendor: true", "vendor", "vendor"},
3633 {"system_ext_specific: true", "system_ext", "system_ext"},
3634 }
3635 for _, tc := range testcases {
3636 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
3637 ctx, _ := testApex(t, `
3638 apex {
3639 name: "myapex",
3640 key: "myapex.key",
3641 `+tc.propName+`
3642 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003643
Jooyung Han91df2082019-11-20 01:49:42 +09003644 apex_key {
3645 name: "myapex.key",
3646 public_key: "testkey.avbpubkey",
3647 private_key: "testkey.pem",
3648 }
3649 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003650
Jooyung Han91df2082019-11-20 01:49:42 +09003651 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3652 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
3653 actual := apex.installDir.String()
3654 if actual != expected {
3655 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
3656 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003657
Jooyung Han91df2082019-11-20 01:49:42 +09003658 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
3659 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
3660 actual = flattened.installDir.String()
3661 if actual != expected {
3662 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
3663 }
3664 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003665 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003666}
Jiyong Park67882562019-03-21 01:11:21 +09003667
Jooyung Han580eb4f2020-06-24 19:33:06 +09003668func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09003669 ctx, _ := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09003670 apex {
3671 name: "myapex",
3672 key: "myapex.key",
3673 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003674
Jooyung Han580eb4f2020-06-24 19:33:06 +09003675 apex_key {
3676 name: "myapex.key",
3677 public_key: "testkey.avbpubkey",
3678 private_key: "testkey.pem",
3679 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003680 `)
3681 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09003682 rule := module.Output("file_contexts")
3683 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
3684}
Jooyung Han54aca7b2019-11-20 02:26:02 +09003685
Jooyung Han580eb4f2020-06-24 19:33:06 +09003686func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09003687 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09003688 apex {
3689 name: "myapex",
3690 key: "myapex.key",
3691 file_contexts: "my_own_file_contexts",
3692 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003693
Jooyung Han580eb4f2020-06-24 19:33:06 +09003694 apex_key {
3695 name: "myapex.key",
3696 public_key: "testkey.avbpubkey",
3697 private_key: "testkey.pem",
3698 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003699 `, withFiles(map[string][]byte{
3700 "my_own_file_contexts": nil,
3701 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09003702}
Jooyung Han54aca7b2019-11-20 02:26:02 +09003703
Jooyung Han580eb4f2020-06-24 19:33:06 +09003704func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09003705 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09003706 apex {
3707 name: "myapex",
3708 key: "myapex.key",
3709 product_specific: true,
3710 file_contexts: "product_specific_file_contexts",
3711 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003712
Jooyung Han580eb4f2020-06-24 19:33:06 +09003713 apex_key {
3714 name: "myapex.key",
3715 public_key: "testkey.avbpubkey",
3716 private_key: "testkey.pem",
3717 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003718 `)
3719
Jooyung Han580eb4f2020-06-24 19:33:06 +09003720 ctx, _ := testApex(t, `
3721 apex {
3722 name: "myapex",
3723 key: "myapex.key",
3724 product_specific: true,
3725 file_contexts: "product_specific_file_contexts",
3726 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003727
Jooyung Han580eb4f2020-06-24 19:33:06 +09003728 apex_key {
3729 name: "myapex.key",
3730 public_key: "testkey.avbpubkey",
3731 private_key: "testkey.pem",
3732 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003733 `, withFiles(map[string][]byte{
3734 "product_specific_file_contexts": nil,
3735 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09003736 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3737 rule := module.Output("file_contexts")
3738 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
3739}
Jooyung Han54aca7b2019-11-20 02:26:02 +09003740
Jooyung Han580eb4f2020-06-24 19:33:06 +09003741func TestFileContexts_SetViaFileGroup(t *testing.T) {
3742 ctx, _ := testApex(t, `
3743 apex {
3744 name: "myapex",
3745 key: "myapex.key",
3746 product_specific: true,
3747 file_contexts: ":my-file-contexts",
3748 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003749
Jooyung Han580eb4f2020-06-24 19:33:06 +09003750 apex_key {
3751 name: "myapex.key",
3752 public_key: "testkey.avbpubkey",
3753 private_key: "testkey.pem",
3754 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003755
Jooyung Han580eb4f2020-06-24 19:33:06 +09003756 filegroup {
3757 name: "my-file-contexts",
3758 srcs: ["product_specific_file_contexts"],
3759 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003760 `, withFiles(map[string][]byte{
3761 "product_specific_file_contexts": nil,
3762 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09003763 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3764 rule := module.Output("file_contexts")
3765 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09003766}
3767
Jiyong Park67882562019-03-21 01:11:21 +09003768func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003769 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09003770 apex_key {
3771 name: "myapex.key",
3772 public_key: ":my.avbpubkey",
3773 private_key: ":my.pem",
3774 product_specific: true,
3775 }
3776
3777 filegroup {
3778 name: "my.avbpubkey",
3779 srcs: ["testkey2.avbpubkey"],
3780 }
3781
3782 filegroup {
3783 name: "my.pem",
3784 srcs: ["testkey2.pem"],
3785 }
3786 `)
3787
3788 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
3789 expected_pubkey := "testkey2.avbpubkey"
3790 actual_pubkey := apex_key.public_key_file.String()
3791 if actual_pubkey != expected_pubkey {
3792 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
3793 }
3794 expected_privkey := "testkey2.pem"
3795 actual_privkey := apex_key.private_key_file.String()
3796 if actual_privkey != expected_privkey {
3797 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
3798 }
3799}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003800
3801func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003802 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003803 prebuilt_apex {
3804 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09003805 arch: {
3806 arm64: {
3807 src: "myapex-arm64.apex",
3808 },
3809 arm: {
3810 src: "myapex-arm.apex",
3811 },
3812 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003813 }
3814 `)
3815
3816 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3817
Jiyong Parkc95714e2019-03-29 14:23:10 +09003818 expectedInput := "myapex-arm64.apex"
3819 if prebuilt.inputApex.String() != expectedInput {
3820 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
3821 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003822}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003823
3824func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003825 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003826 prebuilt_apex {
3827 name: "myapex",
3828 src: "myapex-arm.apex",
3829 filename: "notmyapex.apex",
3830 }
3831 `)
3832
3833 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3834
3835 expected := "notmyapex.apex"
3836 if p.installFilename != expected {
3837 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
3838 }
3839}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003840
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003841func TestPrebuiltOverrides(t *testing.T) {
3842 ctx, config := testApex(t, `
3843 prebuilt_apex {
3844 name: "myapex.prebuilt",
3845 src: "myapex-arm.apex",
3846 overrides: [
3847 "myapex",
3848 ],
3849 }
3850 `)
3851
3852 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3853
3854 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003855 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003856 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003857 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003858 }
3859}
3860
Roland Levillain630846d2019-06-26 12:48:34 +01003861func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003862 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003863 apex_test {
3864 name: "myapex",
3865 key: "myapex.key",
3866 tests: [
3867 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003868 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003869 ],
3870 }
3871
3872 apex_key {
3873 name: "myapex.key",
3874 public_key: "testkey.avbpubkey",
3875 private_key: "testkey.pem",
3876 }
3877
Liz Kammer1c14a212020-05-12 15:26:55 -07003878 filegroup {
3879 name: "fg",
3880 srcs: [
3881 "baz",
3882 "bar/baz"
3883 ],
3884 }
3885
Roland Levillain630846d2019-06-26 12:48:34 +01003886 cc_test {
3887 name: "mytest",
3888 gtest: false,
3889 srcs: ["mytest.cpp"],
3890 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003891 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01003892 system_shared_libs: [],
3893 static_executable: true,
3894 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07003895 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01003896 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003897
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003898 cc_library {
3899 name: "mylib",
3900 srcs: ["mylib.cpp"],
3901 system_shared_libs: [],
3902 stl: "none",
3903 }
3904
Liz Kammer5bd365f2020-05-27 15:15:11 -07003905 filegroup {
3906 name: "fg2",
3907 srcs: [
3908 "testdata/baz"
3909 ],
3910 }
3911
Roland Levillain9b5fde92019-06-28 15:41:19 +01003912 cc_test {
3913 name: "mytests",
3914 gtest: false,
3915 srcs: [
3916 "mytest1.cpp",
3917 "mytest2.cpp",
3918 "mytest3.cpp",
3919 ],
3920 test_per_src: true,
3921 relative_install_path: "test",
3922 system_shared_libs: [],
3923 static_executable: true,
3924 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07003925 data: [
3926 ":fg",
3927 ":fg2",
3928 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01003929 }
Roland Levillain630846d2019-06-26 12:48:34 +01003930 `)
3931
Sundong Ahnabb64432019-10-22 13:58:29 +09003932 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003933 copyCmds := apexRule.Args["copy_commands"]
3934
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003935 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01003936 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003937 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003938
Liz Kammer1c14a212020-05-12 15:26:55 -07003939 //Ensure that test data are copied into apex.
3940 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
3941 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
3942
Roland Levillain9b5fde92019-06-28 15:41:19 +01003943 // Ensure that test deps built with `test_per_src` are copied into apex.
3944 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3945 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3946 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003947
3948 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07003949 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3950 data := android.AndroidMkDataForTest(t, config, "", bundle)
3951 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01003952 prefix := "TARGET_"
3953 var builder strings.Builder
3954 data.Custom(&builder, name, prefix, "", data)
3955 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003956 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3957 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3958 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3959 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003960 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003961 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003962 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07003963
3964 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
3965 data = android.AndroidMkDataForTest(t, config, "", flatBundle)
3966 data.Custom(&builder, name, prefix, "", data)
3967 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07003968 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
3969 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003970}
3971
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003972func TestInstallExtraFlattenedApexes(t *testing.T) {
3973 ctx, config := testApex(t, `
3974 apex {
3975 name: "myapex",
3976 key: "myapex.key",
3977 }
3978 apex_key {
3979 name: "myapex.key",
3980 public_key: "testkey.avbpubkey",
3981 private_key: "testkey.pem",
3982 }
3983 `, func(fs map[string][]byte, config android.Config) {
3984 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3985 })
3986 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003987 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003988 mk := android.AndroidMkDataForTest(t, config, "", ab)
3989 var builder strings.Builder
3990 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3991 androidMk := builder.String()
3992 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3993}
3994
Jooyung Han5c998b92019-06-27 11:30:33 +09003995func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003996 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003997 apex {
3998 name: "myapex",
3999 key: "myapex.key",
4000 native_shared_libs: ["mylib"],
4001 uses: ["commonapex"],
4002 }
4003
4004 apex {
4005 name: "commonapex",
4006 key: "myapex.key",
4007 native_shared_libs: ["libcommon"],
4008 provide_cpp_shared_libs: true,
4009 }
4010
4011 apex_key {
4012 name: "myapex.key",
4013 public_key: "testkey.avbpubkey",
4014 private_key: "testkey.pem",
4015 }
4016
4017 cc_library {
4018 name: "mylib",
4019 srcs: ["mylib.cpp"],
4020 shared_libs: ["libcommon"],
4021 system_shared_libs: [],
4022 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004023 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09004024 }
4025
4026 cc_library {
4027 name: "libcommon",
4028 srcs: ["mylib_common.cpp"],
4029 system_shared_libs: [],
4030 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004031 // TODO: remove //apex_available:platform
4032 apex_available: [
4033 "//apex_available:platform",
4034 "commonapex",
4035 "myapex",
4036 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09004037 }
4038 `)
4039
Sundong Ahnabb64432019-10-22 13:58:29 +09004040 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09004041 apexRule1 := module1.Rule("apexRule")
4042 copyCmds1 := apexRule1.Args["copy_commands"]
4043
Sundong Ahnabb64432019-10-22 13:58:29 +09004044 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09004045 apexRule2 := module2.Rule("apexRule")
4046 copyCmds2 := apexRule2.Args["copy_commands"]
4047
Colin Cross7113d202019-11-20 16:39:12 -08004048 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
4049 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09004050 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
4051 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
4052 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
4053}
4054
4055func TestApexUsesFailsIfNotProvided(t *testing.T) {
4056 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
4057 apex {
4058 name: "myapex",
4059 key: "myapex.key",
4060 uses: ["commonapex"],
4061 }
4062
4063 apex {
4064 name: "commonapex",
4065 key: "myapex.key",
4066 }
4067
4068 apex_key {
4069 name: "myapex.key",
4070 public_key: "testkey.avbpubkey",
4071 private_key: "testkey.pem",
4072 }
4073 `)
4074 testApexError(t, `uses: "commonapex" is not a provider`, `
4075 apex {
4076 name: "myapex",
4077 key: "myapex.key",
4078 uses: ["commonapex"],
4079 }
4080
4081 cc_library {
4082 name: "commonapex",
4083 system_shared_libs: [],
4084 stl: "none",
4085 }
4086
4087 apex_key {
4088 name: "myapex.key",
4089 public_key: "testkey.avbpubkey",
4090 private_key: "testkey.pem",
4091 }
4092 `)
4093}
4094
4095func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
4096 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
4097 apex {
4098 name: "myapex",
4099 key: "myapex.key",
4100 use_vendor: true,
4101 uses: ["commonapex"],
4102 }
4103
4104 apex {
4105 name: "commonapex",
4106 key: "myapex.key",
4107 provide_cpp_shared_libs: true,
4108 }
4109
4110 apex_key {
4111 name: "myapex.key",
4112 public_key: "testkey.avbpubkey",
4113 private_key: "testkey.pem",
4114 }
Jooyung Handc782442019-11-01 03:14:38 +09004115 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07004116 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09004117 })
Jooyung Han5c998b92019-06-27 11:30:33 +09004118}
4119
Jooyung Hand48f3c32019-08-23 11:18:57 +09004120func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4121 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4122 apex {
4123 name: "myapex",
4124 key: "myapex.key",
4125 native_shared_libs: ["libfoo"],
4126 }
4127
4128 apex_key {
4129 name: "myapex.key",
4130 public_key: "testkey.avbpubkey",
4131 private_key: "testkey.pem",
4132 }
4133
4134 cc_library {
4135 name: "libfoo",
4136 stl: "none",
4137 system_shared_libs: [],
4138 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004139 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004140 }
4141 `)
4142 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4143 apex {
4144 name: "myapex",
4145 key: "myapex.key",
4146 java_libs: ["myjar"],
4147 }
4148
4149 apex_key {
4150 name: "myapex.key",
4151 public_key: "testkey.avbpubkey",
4152 private_key: "testkey.pem",
4153 }
4154
4155 java_library {
4156 name: "myjar",
4157 srcs: ["foo/bar/MyClass.java"],
4158 sdk_version: "none",
4159 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004160 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004161 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004162 }
4163 `)
4164}
4165
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004166func TestApexWithApps(t *testing.T) {
4167 ctx, _ := testApex(t, `
4168 apex {
4169 name: "myapex",
4170 key: "myapex.key",
4171 apps: [
4172 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09004173 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004174 ],
4175 }
4176
4177 apex_key {
4178 name: "myapex.key",
4179 public_key: "testkey.avbpubkey",
4180 private_key: "testkey.pem",
4181 }
4182
4183 android_app {
4184 name: "AppFoo",
4185 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004186 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004187 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09004188 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08004189 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004190 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004191 }
Jiyong Parkf7487312019-10-17 12:54:30 +09004192
4193 android_app {
4194 name: "AppFooPriv",
4195 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004196 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09004197 system_modules: "none",
4198 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08004199 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004200 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09004201 }
Jiyong Park8be103b2019-11-08 15:53:48 +09004202
4203 cc_library_shared {
4204 name: "libjni",
4205 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004206 shared_libs: ["libfoo"],
4207 stl: "none",
4208 system_shared_libs: [],
4209 apex_available: [ "myapex" ],
4210 sdk_version: "current",
4211 }
4212
4213 cc_library_shared {
4214 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09004215 stl: "none",
4216 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09004217 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08004218 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09004219 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004220 `)
4221
Sundong Ahnabb64432019-10-22 13:58:29 +09004222 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004223 apexRule := module.Rule("apexRule")
4224 copyCmds := apexRule.Args["copy_commands"]
4225
4226 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09004227 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004228
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004229 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
4230 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09004231 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004232 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004233 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004234 // JNI libraries including transitive deps are
4235 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossc511bc52020-04-07 16:50:32 +00004236 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_myapex").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004237 // ... embedded inside APK (jnilibs.zip)
4238 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
4239 // ... and not directly inside the APEX
4240 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
4241 }
Dario Frenicde2a032019-10-27 00:29:22 +01004242}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004243
Dario Frenicde2a032019-10-27 00:29:22 +01004244func TestApexWithAppImports(t *testing.T) {
4245 ctx, _ := testApex(t, `
4246 apex {
4247 name: "myapex",
4248 key: "myapex.key",
4249 apps: [
4250 "AppFooPrebuilt",
4251 "AppFooPrivPrebuilt",
4252 ],
4253 }
4254
4255 apex_key {
4256 name: "myapex.key",
4257 public_key: "testkey.avbpubkey",
4258 private_key: "testkey.pem",
4259 }
4260
4261 android_app_import {
4262 name: "AppFooPrebuilt",
4263 apk: "PrebuiltAppFoo.apk",
4264 presigned: true,
4265 dex_preopt: {
4266 enabled: false,
4267 },
Jiyong Park592a6a42020-04-21 22:34:28 +09004268 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01004269 }
4270
4271 android_app_import {
4272 name: "AppFooPrivPrebuilt",
4273 apk: "PrebuiltAppFooPriv.apk",
4274 privileged: true,
4275 presigned: true,
4276 dex_preopt: {
4277 enabled: false,
4278 },
Jooyung Han39ee1192020-03-23 20:21:11 +09004279 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09004280 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01004281 }
4282 `)
4283
Sundong Ahnabb64432019-10-22 13:58:29 +09004284 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01004285 apexRule := module.Rule("apexRule")
4286 copyCmds := apexRule.Args["copy_commands"]
4287
4288 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09004289 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
4290}
4291
4292func TestApexWithAppImportsPrefer(t *testing.T) {
4293 ctx, _ := testApex(t, `
4294 apex {
4295 name: "myapex",
4296 key: "myapex.key",
4297 apps: [
4298 "AppFoo",
4299 ],
4300 }
4301
4302 apex_key {
4303 name: "myapex.key",
4304 public_key: "testkey.avbpubkey",
4305 private_key: "testkey.pem",
4306 }
4307
4308 android_app {
4309 name: "AppFoo",
4310 srcs: ["foo/bar/MyClass.java"],
4311 sdk_version: "none",
4312 system_modules: "none",
4313 apex_available: [ "myapex" ],
4314 }
4315
4316 android_app_import {
4317 name: "AppFoo",
4318 apk: "AppFooPrebuilt.apk",
4319 filename: "AppFooPrebuilt.apk",
4320 presigned: true,
4321 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09004322 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09004323 }
4324 `, withFiles(map[string][]byte{
4325 "AppFooPrebuilt.apk": nil,
4326 }))
4327
4328 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4329 "app/AppFoo/AppFooPrebuilt.apk",
4330 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004331}
4332
Dario Freni6f3937c2019-12-20 22:58:03 +00004333func TestApexWithTestHelperApp(t *testing.T) {
4334 ctx, _ := testApex(t, `
4335 apex {
4336 name: "myapex",
4337 key: "myapex.key",
4338 apps: [
4339 "TesterHelpAppFoo",
4340 ],
4341 }
4342
4343 apex_key {
4344 name: "myapex.key",
4345 public_key: "testkey.avbpubkey",
4346 private_key: "testkey.pem",
4347 }
4348
4349 android_test_helper_app {
4350 name: "TesterHelpAppFoo",
4351 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004352 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00004353 }
4354
4355 `)
4356
4357 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4358 apexRule := module.Rule("apexRule")
4359 copyCmds := apexRule.Args["copy_commands"]
4360
4361 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
4362}
4363
Jooyung Han18020ea2019-11-13 10:50:48 +09004364func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
4365 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09004366 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09004367 apex {
4368 name: "myapex",
4369 key: "myapex.key",
4370 native_shared_libs: ["libfoo"],
4371 }
4372
4373 apex_key {
4374 name: "myapex.key",
4375 public_key: "testkey.avbpubkey",
4376 private_key: "testkey.pem",
4377 }
4378
4379 apex {
4380 name: "otherapex",
4381 key: "myapex.key",
4382 native_shared_libs: ["libfoo"],
4383 }
4384
4385 cc_defaults {
4386 name: "libfoo-defaults",
4387 apex_available: ["otherapex"],
4388 }
4389
4390 cc_library {
4391 name: "libfoo",
4392 defaults: ["libfoo-defaults"],
4393 stl: "none",
4394 system_shared_libs: [],
4395 }`)
4396}
4397
Paul Duffine52e66f2020-03-30 17:54:29 +01004398func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09004399 // libfoo is not available to myapex, but only to otherapex
4400 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
4401 apex {
4402 name: "myapex",
4403 key: "myapex.key",
4404 native_shared_libs: ["libfoo"],
4405 }
4406
4407 apex_key {
4408 name: "myapex.key",
4409 public_key: "testkey.avbpubkey",
4410 private_key: "testkey.pem",
4411 }
4412
4413 apex {
4414 name: "otherapex",
4415 key: "otherapex.key",
4416 native_shared_libs: ["libfoo"],
4417 }
4418
4419 apex_key {
4420 name: "otherapex.key",
4421 public_key: "testkey.avbpubkey",
4422 private_key: "testkey.pem",
4423 }
4424
4425 cc_library {
4426 name: "libfoo",
4427 stl: "none",
4428 system_shared_libs: [],
4429 apex_available: ["otherapex"],
4430 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004431}
Jiyong Park127b40b2019-09-30 16:04:35 +09004432
Paul Duffine52e66f2020-03-30 17:54:29 +01004433func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09004434 // libbbaz is an indirect dep
Paul Duffindf915ff2020-03-30 17:58:21 +01004435 testApexError(t, `requires "libbaz" that is not available for the APEX. Dependency path:
Paul Duffinc5192442020-03-31 11:31:36 +01004436.*via tag apex\.dependencyTag.*"sharedLib".*
Paul Duffindf915ff2020-03-30 17:58:21 +01004437.*-> libfoo.*link:shared.*
Paul Duffin65347702020-03-31 15:23:40 +01004438.*via tag cc\.DependencyTag.*"shared".*
Paul Duffindf915ff2020-03-30 17:58:21 +01004439.*-> libbar.*link:shared.*
Paul Duffin65347702020-03-31 15:23:40 +01004440.*via tag cc\.DependencyTag.*"shared".*
4441.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004442 apex {
4443 name: "myapex",
4444 key: "myapex.key",
4445 native_shared_libs: ["libfoo"],
4446 }
4447
4448 apex_key {
4449 name: "myapex.key",
4450 public_key: "testkey.avbpubkey",
4451 private_key: "testkey.pem",
4452 }
4453
Jiyong Park127b40b2019-09-30 16:04:35 +09004454 cc_library {
4455 name: "libfoo",
4456 stl: "none",
4457 shared_libs: ["libbar"],
4458 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09004459 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004460 }
4461
4462 cc_library {
4463 name: "libbar",
4464 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09004465 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004466 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09004467 apex_available: ["myapex"],
4468 }
4469
4470 cc_library {
4471 name: "libbaz",
4472 stl: "none",
4473 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09004474 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004475}
Jiyong Park127b40b2019-09-30 16:04:35 +09004476
Paul Duffine52e66f2020-03-30 17:54:29 +01004477func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09004478 testApexError(t, "\"otherapex\" is not a valid module name", `
4479 apex {
4480 name: "myapex",
4481 key: "myapex.key",
4482 native_shared_libs: ["libfoo"],
4483 }
4484
4485 apex_key {
4486 name: "myapex.key",
4487 public_key: "testkey.avbpubkey",
4488 private_key: "testkey.pem",
4489 }
4490
4491 cc_library {
4492 name: "libfoo",
4493 stl: "none",
4494 system_shared_libs: [],
4495 apex_available: ["otherapex"],
4496 }`)
4497
Paul Duffine52e66f2020-03-30 17:54:29 +01004498 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004499 apex {
4500 name: "myapex",
4501 key: "myapex.key",
4502 native_shared_libs: ["libfoo", "libbar"],
4503 }
4504
4505 apex_key {
4506 name: "myapex.key",
4507 public_key: "testkey.avbpubkey",
4508 private_key: "testkey.pem",
4509 }
4510
4511 cc_library {
4512 name: "libfoo",
4513 stl: "none",
4514 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09004515 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004516 apex_available: ["myapex"],
4517 }
4518
4519 cc_library {
4520 name: "libbar",
4521 stl: "none",
4522 system_shared_libs: [],
4523 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09004524 }
4525
4526 cc_library {
4527 name: "libbaz",
4528 stl: "none",
4529 system_shared_libs: [],
4530 stubs: {
4531 versions: ["10", "20", "30"],
4532 },
Jiyong Park127b40b2019-09-30 16:04:35 +09004533 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004534}
Jiyong Park127b40b2019-09-30 16:04:35 +09004535
Jiyong Park89e850a2020-04-07 16:37:39 +09004536func TestApexAvailable_CheckForPlatform(t *testing.T) {
Paul Duffine52e66f2020-03-30 17:54:29 +01004537 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004538 apex {
4539 name: "myapex",
4540 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09004541 native_shared_libs: ["libbar", "libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004542 }
4543
4544 apex_key {
4545 name: "myapex.key",
4546 public_key: "testkey.avbpubkey",
4547 private_key: "testkey.pem",
4548 }
4549
4550 cc_library {
4551 name: "libfoo",
4552 stl: "none",
4553 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09004554 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004555 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09004556 }
4557
4558 cc_library {
4559 name: "libfoo2",
4560 stl: "none",
4561 system_shared_libs: [],
4562 shared_libs: ["libbaz"],
4563 apex_available: ["//apex_available:platform"],
4564 }
4565
4566 cc_library {
4567 name: "libbar",
4568 stl: "none",
4569 system_shared_libs: [],
4570 apex_available: ["myapex"],
4571 }
4572
4573 cc_library {
4574 name: "libbaz",
4575 stl: "none",
4576 system_shared_libs: [],
4577 apex_available: ["myapex"],
4578 stubs: {
4579 versions: ["1"],
4580 },
Jiyong Park127b40b2019-09-30 16:04:35 +09004581 }`)
4582
Jiyong Park89e850a2020-04-07 16:37:39 +09004583 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
4584 // because it depends on libbar which isn't available to platform
4585 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4586 if libfoo.NotAvailableForPlatform() != true {
4587 t.Errorf("%q shouldn't be available to platform", libfoo.String())
4588 }
4589
4590 // libfoo2 however can be available to platform because it depends on libbaz which provides
4591 // stubs
4592 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4593 if libfoo2.NotAvailableForPlatform() == true {
4594 t.Errorf("%q should be available to platform", libfoo2.String())
4595 }
Paul Duffine52e66f2020-03-30 17:54:29 +01004596}
Jiyong Parka90ca002019-10-07 15:47:24 +09004597
Paul Duffine52e66f2020-03-30 17:54:29 +01004598func TestApexAvailable_CreatedForApex(t *testing.T) {
Jiyong Park89e850a2020-04-07 16:37:39 +09004599 ctx, _ := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09004600 apex {
4601 name: "myapex",
4602 key: "myapex.key",
4603 native_shared_libs: ["libfoo"],
4604 }
4605
4606 apex_key {
4607 name: "myapex.key",
4608 public_key: "testkey.avbpubkey",
4609 private_key: "testkey.pem",
4610 }
4611
4612 cc_library {
4613 name: "libfoo",
4614 stl: "none",
4615 system_shared_libs: [],
4616 apex_available: ["myapex"],
4617 static: {
4618 apex_available: ["//apex_available:platform"],
4619 },
4620 }`)
4621
Jiyong Park89e850a2020-04-07 16:37:39 +09004622 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4623 if libfooShared.NotAvailableForPlatform() != true {
4624 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
4625 }
4626 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
4627 if libfooStatic.NotAvailableForPlatform() != false {
4628 t.Errorf("%q should be available to platform", libfooStatic.String())
4629 }
Jiyong Park127b40b2019-09-30 16:04:35 +09004630}
4631
Jiyong Park5d790c32019-11-15 18:40:32 +09004632func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004633 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09004634 apex {
4635 name: "myapex",
4636 key: "myapex.key",
4637 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004638 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09004639 }
4640
4641 override_apex {
4642 name: "override_myapex",
4643 base: "myapex",
4644 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004645 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08004646 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07004647 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09004648 }
4649
4650 apex_key {
4651 name: "myapex.key",
4652 public_key: "testkey.avbpubkey",
4653 private_key: "testkey.pem",
4654 }
4655
4656 android_app {
4657 name: "app",
4658 srcs: ["foo/bar/MyClass.java"],
4659 package_name: "foo",
4660 sdk_version: "none",
4661 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004662 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09004663 }
4664
4665 override_android_app {
4666 name: "override_app",
4667 base: "app",
4668 package_name: "bar",
4669 }
Jiyong Park20bacab2020-03-03 11:45:41 +09004670 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09004671
Jiyong Park317645e2019-12-05 13:20:58 +09004672 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
4673 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
4674 if originalVariant.GetOverriddenBy() != "" {
4675 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
4676 }
4677 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
4678 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
4679 }
4680
Jiyong Park5d790c32019-11-15 18:40:32 +09004681 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
4682 apexRule := module.Rule("apexRule")
4683 copyCmds := apexRule.Args["copy_commands"]
4684
4685 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09004686 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004687
4688 apexBundle := module.Module().(*apexBundle)
4689 name := apexBundle.Name()
4690 if name != "override_myapex" {
4691 t.Errorf("name should be \"override_myapex\", but was %q", name)
4692 }
4693
Baligh Uddin004d7172020-02-19 21:29:28 -08004694 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
4695 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
4696 }
4697
Jiyong Park20bacab2020-03-03 11:45:41 +09004698 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07004699 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09004700
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004701 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
4702 var builder strings.Builder
4703 data.Custom(&builder, name, "TARGET_", "", data)
4704 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09004705 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004706 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
4707 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004708 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004709 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09004710 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004711 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
4712 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09004713}
4714
Jooyung Han214bf372019-11-12 13:03:50 +09004715func TestLegacyAndroid10Support(t *testing.T) {
4716 ctx, _ := testApex(t, `
4717 apex {
4718 name: "myapex",
4719 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004720 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09004721 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09004722 }
4723
4724 apex_key {
4725 name: "myapex.key",
4726 public_key: "testkey.avbpubkey",
4727 private_key: "testkey.pem",
4728 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004729
4730 cc_library {
4731 name: "mylib",
4732 srcs: ["mylib.cpp"],
4733 stl: "libc++",
4734 system_shared_libs: [],
4735 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09004736 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004737 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004738 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09004739
4740 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4741 args := module.Rule("apexRule").Args
4742 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00004743 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004744
4745 // The copies of the libraries in the apex should have one more dependency than
4746 // the ones outside the apex, namely the unwinder. Ideally we should check
4747 // the dependency names directly here but for some reason the names are blank in
4748 // this test.
4749 for _, lib := range []string{"libc++", "mylib"} {
4750 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
4751 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
4752 if len(apexImplicits) != len(nonApexImplicits)+1 {
4753 t.Errorf("%q missing unwinder dep", lib)
4754 }
4755 }
Jooyung Han214bf372019-11-12 13:03:50 +09004756}
4757
Paul Duffin9b879592020-05-26 13:21:35 +01004758var filesForSdkLibrary = map[string][]byte{
4759 "api/current.txt": nil,
4760 "api/removed.txt": nil,
4761 "api/system-current.txt": nil,
4762 "api/system-removed.txt": nil,
4763 "api/test-current.txt": nil,
4764 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01004765
4766 // For java_sdk_library_import
4767 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01004768}
4769
Jooyung Han58f26ab2019-12-18 15:34:32 +09004770func TestJavaSDKLibrary(t *testing.T) {
4771 ctx, _ := testApex(t, `
4772 apex {
4773 name: "myapex",
4774 key: "myapex.key",
4775 java_libs: ["foo"],
4776 }
4777
4778 apex_key {
4779 name: "myapex.key",
4780 public_key: "testkey.avbpubkey",
4781 private_key: "testkey.pem",
4782 }
4783
4784 java_sdk_library {
4785 name: "foo",
4786 srcs: ["a.java"],
4787 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004788 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09004789 }
Paul Duffin9b879592020-05-26 13:21:35 +01004790 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09004791
4792 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00004793 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09004794 "javalib/foo.jar",
4795 "etc/permissions/foo.xml",
4796 })
4797 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09004798 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
4799 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09004800}
4801
Paul Duffin9b879592020-05-26 13:21:35 +01004802func TestJavaSDKLibrary_WithinApex(t *testing.T) {
4803 ctx, _ := testApex(t, `
4804 apex {
4805 name: "myapex",
4806 key: "myapex.key",
4807 java_libs: ["foo", "bar"],
4808 }
4809
4810 apex_key {
4811 name: "myapex.key",
4812 public_key: "testkey.avbpubkey",
4813 private_key: "testkey.pem",
4814 }
4815
4816 java_sdk_library {
4817 name: "foo",
4818 srcs: ["a.java"],
4819 api_packages: ["foo"],
4820 apex_available: ["myapex"],
4821 sdk_version: "none",
4822 system_modules: "none",
4823 }
4824
4825 java_library {
4826 name: "bar",
4827 srcs: ["a.java"],
4828 libs: ["foo"],
4829 apex_available: ["myapex"],
4830 sdk_version: "none",
4831 system_modules: "none",
4832 }
4833 `, withFiles(filesForSdkLibrary))
4834
4835 // java_sdk_library installs both impl jar and permission XML
4836 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4837 "javalib/bar.jar",
4838 "javalib/foo.jar",
4839 "etc/permissions/foo.xml",
4840 })
4841
4842 // The bar library should depend on the implementation jar.
4843 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
4844 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
4845 t.Errorf("expected %q, found %#q", expected, actual)
4846 }
4847}
4848
4849func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
4850 ctx, _ := testApex(t, `
4851 apex {
4852 name: "myapex",
4853 key: "myapex.key",
4854 java_libs: ["foo"],
4855 }
4856
4857 apex_key {
4858 name: "myapex.key",
4859 public_key: "testkey.avbpubkey",
4860 private_key: "testkey.pem",
4861 }
4862
4863 java_sdk_library {
4864 name: "foo",
4865 srcs: ["a.java"],
4866 api_packages: ["foo"],
4867 apex_available: ["myapex"],
4868 sdk_version: "none",
4869 system_modules: "none",
4870 }
4871
4872 java_library {
4873 name: "bar",
4874 srcs: ["a.java"],
4875 libs: ["foo"],
4876 sdk_version: "none",
4877 system_modules: "none",
4878 }
4879 `, withFiles(filesForSdkLibrary))
4880
4881 // java_sdk_library installs both impl jar and permission XML
4882 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4883 "javalib/foo.jar",
4884 "etc/permissions/foo.xml",
4885 })
4886
4887 // The bar library should depend on the stubs jar.
4888 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
4889 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
4890 t.Errorf("expected %q, found %#q", expected, actual)
4891 }
4892}
4893
Paul Duffineedc5d52020-06-12 17:46:39 +01004894func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
4895 ctx, _ := testApex(t, ``,
4896 withFiles(map[string][]byte{
4897 "apex/a.java": nil,
4898 "apex/apex_manifest.json": nil,
4899 "apex/Android.bp": []byte(`
4900 package {
4901 default_visibility: ["//visibility:private"],
4902 }
4903
4904 apex {
4905 name: "myapex",
4906 key: "myapex.key",
4907 java_libs: ["foo", "bar"],
4908 }
4909
4910 apex_key {
4911 name: "myapex.key",
4912 public_key: "testkey.avbpubkey",
4913 private_key: "testkey.pem",
4914 }
4915
4916 java_library {
4917 name: "bar",
4918 srcs: ["a.java"],
4919 libs: ["foo"],
4920 apex_available: ["myapex"],
4921 sdk_version: "none",
4922 system_modules: "none",
4923 }
4924`),
4925 "source/a.java": nil,
4926 "source/api/current.txt": nil,
4927 "source/api/removed.txt": nil,
4928 "source/Android.bp": []byte(`
4929 package {
4930 default_visibility: ["//visibility:private"],
4931 }
4932
4933 java_sdk_library {
4934 name: "foo",
4935 visibility: ["//apex"],
4936 srcs: ["a.java"],
4937 api_packages: ["foo"],
4938 apex_available: ["myapex"],
4939 sdk_version: "none",
4940 system_modules: "none",
4941 public: {
4942 enabled: true,
4943 },
4944 }
4945`),
4946 "prebuilt/a.jar": nil,
4947 "prebuilt/Android.bp": []byte(`
4948 package {
4949 default_visibility: ["//visibility:private"],
4950 }
4951
4952 java_sdk_library_import {
4953 name: "foo",
4954 visibility: ["//apex", "//source"],
4955 apex_available: ["myapex"],
4956 prefer: true,
4957 public: {
4958 jars: ["a.jar"],
4959 },
4960 }
4961`),
4962 }),
4963 )
4964
4965 // java_sdk_library installs both impl jar and permission XML
4966 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4967 "javalib/bar.jar",
4968 "javalib/foo.jar",
4969 "etc/permissions/foo.xml",
4970 })
4971
4972 // The bar library should depend on the implementation jar.
4973 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
4974 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
4975 t.Errorf("expected %q, found %#q", expected, actual)
4976 }
4977}
4978
4979func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
4980 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
4981 apex {
4982 name: "myapex",
4983 key: "myapex.key",
4984 java_libs: ["foo"],
4985 }
4986
4987 apex_key {
4988 name: "myapex.key",
4989 public_key: "testkey.avbpubkey",
4990 private_key: "testkey.pem",
4991 }
4992
4993 java_sdk_library_import {
4994 name: "foo",
4995 apex_available: ["myapex"],
4996 prefer: true,
4997 public: {
4998 jars: ["a.jar"],
4999 },
5000 }
5001
5002 `, withFiles(filesForSdkLibrary))
5003}
5004
atrost6e126252020-01-27 17:01:16 +00005005func TestCompatConfig(t *testing.T) {
5006 ctx, _ := testApex(t, `
5007 apex {
5008 name: "myapex",
5009 key: "myapex.key",
5010 prebuilts: ["myjar-platform-compat-config"],
5011 java_libs: ["myjar"],
5012 }
5013
5014 apex_key {
5015 name: "myapex.key",
5016 public_key: "testkey.avbpubkey",
5017 private_key: "testkey.pem",
5018 }
5019
5020 platform_compat_config {
5021 name: "myjar-platform-compat-config",
5022 src: ":myjar",
5023 }
5024
5025 java_library {
5026 name: "myjar",
5027 srcs: ["foo/bar/MyClass.java"],
5028 sdk_version: "none",
5029 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005030 apex_available: [ "myapex" ],
5031 }
5032 `)
5033 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5034 "etc/compatconfig/myjar-platform-compat-config.xml",
5035 "javalib/myjar.jar",
5036 })
5037}
5038
Jiyong Park479321d2019-12-16 11:47:12 +09005039func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5040 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5041 apex {
5042 name: "myapex",
5043 key: "myapex.key",
5044 java_libs: ["myjar"],
5045 }
5046
5047 apex_key {
5048 name: "myapex.key",
5049 public_key: "testkey.avbpubkey",
5050 private_key: "testkey.pem",
5051 }
5052
5053 java_library {
5054 name: "myjar",
5055 srcs: ["foo/bar/MyClass.java"],
5056 sdk_version: "none",
5057 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005058 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005059 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005060 }
5061 `)
5062}
5063
Jiyong Park7afd1072019-12-30 16:56:33 +09005064func TestCarryRequiredModuleNames(t *testing.T) {
5065 ctx, config := testApex(t, `
5066 apex {
5067 name: "myapex",
5068 key: "myapex.key",
5069 native_shared_libs: ["mylib"],
5070 }
5071
5072 apex_key {
5073 name: "myapex.key",
5074 public_key: "testkey.avbpubkey",
5075 private_key: "testkey.pem",
5076 }
5077
5078 cc_library {
5079 name: "mylib",
5080 srcs: ["mylib.cpp"],
5081 system_shared_libs: [],
5082 stl: "none",
5083 required: ["a", "b"],
5084 host_required: ["c", "d"],
5085 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005086 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005087 }
5088 `)
5089
5090 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
5091 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
5092 name := apexBundle.BaseModuleName()
5093 prefix := "TARGET_"
5094 var builder strings.Builder
5095 data.Custom(&builder, name, prefix, "", data)
5096 androidMk := builder.String()
5097 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
5098 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
5099 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
5100}
5101
Jiyong Park7cd10e32020-01-14 09:22:18 +09005102func TestSymlinksFromApexToSystem(t *testing.T) {
5103 bp := `
5104 apex {
5105 name: "myapex",
5106 key: "myapex.key",
5107 native_shared_libs: ["mylib"],
5108 java_libs: ["myjar"],
5109 }
5110
Jiyong Park9d677202020-02-19 16:29:35 +09005111 apex {
5112 name: "myapex.updatable",
5113 key: "myapex.key",
5114 native_shared_libs: ["mylib"],
5115 java_libs: ["myjar"],
5116 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09005117 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09005118 }
5119
Jiyong Park7cd10e32020-01-14 09:22:18 +09005120 apex_key {
5121 name: "myapex.key",
5122 public_key: "testkey.avbpubkey",
5123 private_key: "testkey.pem",
5124 }
5125
5126 cc_library {
5127 name: "mylib",
5128 srcs: ["mylib.cpp"],
5129 shared_libs: ["myotherlib"],
5130 system_shared_libs: [],
5131 stl: "none",
5132 apex_available: [
5133 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005134 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005135 "//apex_available:platform",
5136 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005137 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005138 }
5139
5140 cc_library {
5141 name: "myotherlib",
5142 srcs: ["mylib.cpp"],
5143 system_shared_libs: [],
5144 stl: "none",
5145 apex_available: [
5146 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005147 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005148 "//apex_available:platform",
5149 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005150 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005151 }
5152
5153 java_library {
5154 name: "myjar",
5155 srcs: ["foo/bar/MyClass.java"],
5156 sdk_version: "none",
5157 system_modules: "none",
5158 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09005159 apex_available: [
5160 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005161 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005162 "//apex_available:platform",
5163 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005164 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005165 }
5166
5167 java_library {
5168 name: "myotherjar",
5169 srcs: ["foo/bar/MyClass.java"],
5170 sdk_version: "none",
5171 system_modules: "none",
5172 apex_available: [
5173 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005174 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005175 "//apex_available:platform",
5176 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005177 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005178 }
5179 `
5180
5181 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
5182 for _, f := range files {
5183 if f.path == file {
5184 if f.isLink {
5185 t.Errorf("%q is not a real file", file)
5186 }
5187 return
5188 }
5189 }
5190 t.Errorf("%q is not found", file)
5191 }
5192
5193 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
5194 for _, f := range files {
5195 if f.path == file {
5196 if !f.isLink {
5197 t.Errorf("%q is not a symlink", file)
5198 }
5199 return
5200 }
5201 }
5202 t.Errorf("%q is not found", file)
5203 }
5204
Jiyong Park9d677202020-02-19 16:29:35 +09005205 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
5206 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09005207 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005208 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005209 ensureRealfileExists(t, files, "javalib/myjar.jar")
5210 ensureRealfileExists(t, files, "lib64/mylib.so")
5211 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5212
Jiyong Park9d677202020-02-19 16:29:35 +09005213 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5214 ensureRealfileExists(t, files, "javalib/myjar.jar")
5215 ensureRealfileExists(t, files, "lib64/mylib.so")
5216 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5217
5218 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09005219 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005220 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005221 ensureRealfileExists(t, files, "javalib/myjar.jar")
5222 ensureRealfileExists(t, files, "lib64/mylib.so")
5223 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09005224
5225 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5226 ensureRealfileExists(t, files, "javalib/myjar.jar")
5227 ensureRealfileExists(t, files, "lib64/mylib.so")
5228 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09005229}
5230
Jooyung Han643adc42020-02-27 13:50:06 +09005231func TestApexWithJniLibs(t *testing.T) {
5232 ctx, _ := testApex(t, `
5233 apex {
5234 name: "myapex",
5235 key: "myapex.key",
5236 jni_libs: ["mylib"],
5237 }
5238
5239 apex_key {
5240 name: "myapex.key",
5241 public_key: "testkey.avbpubkey",
5242 private_key: "testkey.pem",
5243 }
5244
5245 cc_library {
5246 name: "mylib",
5247 srcs: ["mylib.cpp"],
5248 shared_libs: ["mylib2"],
5249 system_shared_libs: [],
5250 stl: "none",
5251 apex_available: [ "myapex" ],
5252 }
5253
5254 cc_library {
5255 name: "mylib2",
5256 srcs: ["mylib.cpp"],
5257 system_shared_libs: [],
5258 stl: "none",
5259 apex_available: [ "myapex" ],
5260 }
5261 `)
5262
5263 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
5264 // Notice mylib2.so (transitive dep) is not added as a jni_lib
5265 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
5266 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5267 "lib64/mylib.so",
5268 "lib64/mylib2.so",
5269 })
5270}
5271
Jooyung Han49f67012020-04-17 13:43:10 +09005272func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
5273 ctx, _ := testApex(t, `
5274 apex {
5275 name: "myapex",
5276 key: "myapex.key",
5277 }
5278 apex_key {
5279 name: "myapex.key",
5280 public_key: "testkey.avbpubkey",
5281 private_key: "testkey.pem",
5282 }
5283 `, func(fs map[string][]byte, config android.Config) {
5284 delete(config.Targets, android.Android)
5285 config.AndroidCommonTarget = android.Target{}
5286 })
5287
5288 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
5289 t.Errorf("Expected variants: %v, but got: %v", expected, got)
5290 }
5291}
5292
Jooyung Han643adc42020-02-27 13:50:06 +09005293func TestApexWithJniLibs_Errors(t *testing.T) {
5294 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
5295 apex {
5296 name: "myapex",
5297 key: "myapex.key",
5298 jni_libs: ["xxx"],
5299 }
5300
5301 apex_key {
5302 name: "myapex.key",
5303 public_key: "testkey.avbpubkey",
5304 private_key: "testkey.pem",
5305 }
5306
5307 prebuilt_etc {
5308 name: "xxx",
5309 src: "xxx",
5310 }
5311 `, withFiles(map[string][]byte{
5312 "xxx": nil,
5313 }))
5314}
5315
Jiyong Parkbd159612020-02-28 15:22:21 +09005316func TestAppBundle(t *testing.T) {
5317 ctx, _ := testApex(t, `
5318 apex {
5319 name: "myapex",
5320 key: "myapex.key",
5321 apps: ["AppFoo"],
5322 }
5323
5324 apex_key {
5325 name: "myapex.key",
5326 public_key: "testkey.avbpubkey",
5327 private_key: "testkey.pem",
5328 }
5329
5330 android_app {
5331 name: "AppFoo",
5332 srcs: ["foo/bar/MyClass.java"],
5333 sdk_version: "none",
5334 system_modules: "none",
5335 apex_available: [ "myapex" ],
5336 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09005337 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09005338
5339 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
5340 content := bundleConfigRule.Args["content"]
5341
5342 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09005343 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 +09005344}
5345
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005346func TestAppSetBundle(t *testing.T) {
5347 ctx, _ := testApex(t, `
5348 apex {
5349 name: "myapex",
5350 key: "myapex.key",
5351 apps: ["AppSet"],
5352 }
5353
5354 apex_key {
5355 name: "myapex.key",
5356 public_key: "testkey.avbpubkey",
5357 private_key: "testkey.pem",
5358 }
5359
5360 android_app_set {
5361 name: "AppSet",
5362 set: "AppSet.apks",
5363 }`)
5364 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5365 bundleConfigRule := mod.Description("Bundle Config")
5366 content := bundleConfigRule.Args["content"]
5367 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
5368 s := mod.Rule("apexRule").Args["copy_commands"]
5369 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
5370 if len(copyCmds) != 3 {
5371 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
5372 }
5373 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
5374 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
5375 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
5376}
5377
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005378func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005379 t.Helper()
5380
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005381 bp := `
5382 java_library {
5383 name: "some-updatable-apex-lib",
5384 srcs: ["a.java"],
5385 sdk_version: "current",
5386 apex_available: [
5387 "some-updatable-apex",
5388 ],
5389 }
5390
5391 java_library {
5392 name: "some-non-updatable-apex-lib",
5393 srcs: ["a.java"],
5394 apex_available: [
5395 "some-non-updatable-apex",
5396 ],
5397 }
5398
5399 java_library {
5400 name: "some-platform-lib",
5401 srcs: ["a.java"],
5402 sdk_version: "current",
5403 installable: true,
5404 }
5405
5406 java_library {
5407 name: "some-art-lib",
5408 srcs: ["a.java"],
5409 sdk_version: "current",
5410 apex_available: [
5411 "com.android.art.something",
5412 ],
5413 hostdex: true,
5414 }
5415
5416 apex {
5417 name: "some-updatable-apex",
5418 key: "some-updatable-apex.key",
5419 java_libs: ["some-updatable-apex-lib"],
5420 updatable: true,
5421 min_sdk_version: "current",
5422 }
5423
5424 apex {
5425 name: "some-non-updatable-apex",
5426 key: "some-non-updatable-apex.key",
5427 java_libs: ["some-non-updatable-apex-lib"],
5428 }
5429
5430 apex_key {
5431 name: "some-updatable-apex.key",
5432 }
5433
5434 apex_key {
5435 name: "some-non-updatable-apex.key",
5436 }
5437
5438 apex {
5439 name: "com.android.art.something",
5440 key: "com.android.art.something.key",
5441 java_libs: ["some-art-lib"],
5442 updatable: true,
5443 min_sdk_version: "current",
5444 }
5445
5446 apex_key {
5447 name: "com.android.art.something.key",
5448 }
5449
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005450 filegroup {
5451 name: "some-updatable-apex-file_contexts",
5452 srcs: [
5453 "system/sepolicy/apex/some-updatable-apex-file_contexts",
5454 ],
5455 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005456
5457 filegroup {
5458 name: "some-non-updatable-apex-file_contexts",
5459 srcs: [
5460 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
5461 ],
5462 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005463 `
5464 bp += cc.GatherRequiredDepsForTest(android.Android)
5465 bp += java.GatherRequiredDepsForTest()
5466 bp += dexpreopt.BpToolModulesForTest()
5467
5468 fs := map[string][]byte{
5469 "a.java": nil,
5470 "a.jar": nil,
5471 "build/make/target/product/security": nil,
5472 "apex_manifest.json": nil,
5473 "AndroidManifest.xml": nil,
5474 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005475 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005476 "system/sepolicy/apex/com.android.art.something-file_contexts": nil,
5477 "framework/aidl/a.aidl": nil,
5478 }
5479 cc.GatherRequiredFilesForTest(fs)
5480
5481 ctx := android.NewTestArchContext()
5482 ctx.RegisterModuleType("apex", BundleFactory)
5483 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
5484 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01005485 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005486 cc.RegisterRequiredBuildComponentsForTest(ctx)
5487 java.RegisterJavaBuildComponents(ctx)
5488 java.RegisterSystemModulesBuildComponents(ctx)
5489 java.RegisterAppBuildComponents(ctx)
5490 java.RegisterDexpreoptBootJarsComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005491 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
5492 ctx.PreDepsMutators(RegisterPreDepsMutators)
5493 ctx.PostDepsMutators(RegisterPostDepsMutators)
5494
5495 config := android.TestArchConfig(buildDir, nil, bp, fs)
5496 ctx.Register(config)
5497
5498 _ = dexpreopt.GlobalSoongConfigForTests(config)
5499 dexpreopt.RegisterToolModulesForTest(ctx)
5500 pathCtx := android.PathContextForTesting(config)
5501 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
5502 transformDexpreoptConfig(dexpreoptConfig)
5503 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
5504
5505 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
5506 android.FailIfErrored(t, errs)
5507
5508 _, errs = ctx.PrepareBuildActions(config)
5509 if errmsg == "" {
5510 android.FailIfErrored(t, errs)
5511 } else if len(errs) > 0 {
5512 android.FailIfNoMatchingErrors(t, errmsg, errs)
5513 return
5514 } else {
5515 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
5516 }
5517}
5518
Jooyung Han548640b2020-04-27 12:10:30 +09005519func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
5520 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
5521 apex {
5522 name: "myapex",
5523 key: "myapex.key",
5524 updatable: true,
5525 }
5526
5527 apex_key {
5528 name: "myapex.key",
5529 public_key: "testkey.avbpubkey",
5530 private_key: "testkey.pem",
5531 }
5532 `)
5533}
5534
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005535func TestNoUpdatableJarsInBootImage(t *testing.T) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005536
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005537 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005538 var transform func(*dexpreopt.GlobalConfig)
5539
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005540 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
5541 transform = func(config *dexpreopt.GlobalConfig) {
5542 config.ArtApexJars = []string{"com.android.art.something:some-art-lib"}
5543 }
5544 testNoUpdatableJarsInBootImage(t, "", transform)
5545 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005546
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005547 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005548 err = "module 'some-art-lib' from updatable apex 'com.android.art.something' is not allowed in the framework boot image"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005549 transform = func(config *dexpreopt.GlobalConfig) {
5550 config.BootJars = []string{"com.android.art.something:some-art-lib"}
5551 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005552 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005553 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005554
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005555 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005556 err = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the ART boot image"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005557 transform = func(config *dexpreopt.GlobalConfig) {
5558 config.ArtApexJars = []string{"some-updatable-apex:some-updatable-apex-lib"}
5559 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005560 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005561 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005562
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005563 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005564 err = "module 'some-non-updatable-apex-lib' is not allowed in the ART boot image"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005565 transform = func(config *dexpreopt.GlobalConfig) {
5566 config.ArtApexJars = []string{"some-non-updatable-apex:some-non-updatable-apex-lib"}
5567 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005568 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005569 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005570
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005571 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005572 err = "module 'some-updatable-apex-lib' from updatable apex 'some-updatable-apex' is not allowed in the framework boot image"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005573 transform = func(config *dexpreopt.GlobalConfig) {
5574 config.BootJars = []string{"some-updatable-apex:some-updatable-apex-lib"}
5575 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005576 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005577 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005578
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005579 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
5580 transform = func(config *dexpreopt.GlobalConfig) {
5581 config.BootJars = []string{"some-non-updatable-apex:some-non-updatable-apex-lib"}
5582 }
5583 testNoUpdatableJarsInBootImage(t, "", transform)
5584 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005585
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005586 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005587 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005588 transform = func(config *dexpreopt.GlobalConfig) {
5589 config.ArtApexJars = []string{"platform:nonexistent"}
5590 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005591 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005592 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005593
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005594 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005595 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005596 transform = func(config *dexpreopt.GlobalConfig) {
5597 config.BootJars = []string{"platform:nonexistent"}
5598 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005599 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005600 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005601
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005602 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005603 err = "module 'some-platform-lib' is not allowed in the ART boot image"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005604 transform = func(config *dexpreopt.GlobalConfig) {
5605 config.ArtApexJars = []string{"platform:some-platform-lib"}
5606 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005607 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005608 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005609
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005610 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
5611 transform = func(config *dexpreopt.GlobalConfig) {
5612 config.BootJars = []string{"platform:some-platform-lib"}
5613 }
5614 testNoUpdatableJarsInBootImage(t, "", transform)
5615 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005616}
5617
Jiyong Park62304bb2020-04-13 16:19:48 +09005618func TestTestFor(t *testing.T) {
5619 ctx, _ := testApex(t, `
5620 apex {
5621 name: "myapex",
5622 key: "myapex.key",
5623 native_shared_libs: ["mylib", "myprivlib"],
5624 }
5625
5626 apex_key {
5627 name: "myapex.key",
5628 public_key: "testkey.avbpubkey",
5629 private_key: "testkey.pem",
5630 }
5631
5632 cc_library {
5633 name: "mylib",
5634 srcs: ["mylib.cpp"],
5635 system_shared_libs: [],
5636 stl: "none",
5637 stubs: {
5638 versions: ["1"],
5639 },
5640 apex_available: ["myapex"],
5641 }
5642
5643 cc_library {
5644 name: "myprivlib",
5645 srcs: ["mylib.cpp"],
5646 system_shared_libs: [],
5647 stl: "none",
5648 apex_available: ["myapex"],
5649 }
5650
5651
5652 cc_test {
5653 name: "mytest",
5654 gtest: false,
5655 srcs: ["mylib.cpp"],
5656 system_shared_libs: [],
5657 stl: "none",
5658 shared_libs: ["mylib", "myprivlib"],
5659 test_for: ["myapex"]
5660 }
5661 `)
5662
5663 // the test 'mytest' is a test for the apex, therefore is linked to the
5664 // actual implementation of mylib instead of its stub.
5665 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
5666 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
5667 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
5668}
5669
Jaewoong Jungfa00c062020-05-14 14:15:24 -07005670// TODO(jungjw): Move this to proptools
5671func intPtr(i int) *int {
5672 return &i
5673}
5674
5675func TestApexSet(t *testing.T) {
5676 ctx, config := testApex(t, `
5677 apex_set {
5678 name: "myapex",
5679 set: "myapex.apks",
5680 filename: "foo_v2.apex",
5681 overrides: ["foo"],
5682 }
5683 `, func(fs map[string][]byte, config android.Config) {
5684 config.TestProductVariables.Platform_sdk_version = intPtr(30)
5685 config.TestProductVariables.DeviceArch = proptools.StringPtr("arm")
5686 config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm64")
5687 })
5688
5689 m := ctx.ModuleForTests("myapex", "android_common")
5690
5691 // Check extract_apks tool parameters.
5692 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
5693 actual := extractedApex.Args["abis"]
5694 expected := "ARMEABI_V7A,ARM64_V8A"
5695 if actual != expected {
5696 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
5697 }
5698 actual = extractedApex.Args["sdk-version"]
5699 expected = "30"
5700 if actual != expected {
5701 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
5702 }
5703
5704 a := m.Module().(*ApexSet)
5705 expectedOverrides := []string{"foo"}
5706 actualOverrides := android.AndroidMkEntriesForTest(t, config, "", a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
5707 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
5708 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
5709 }
5710}
5711
Jiyong Park7d95a512020-05-10 15:16:24 +09005712func TestNoStaticLinkingToStubsLib(t *testing.T) {
5713 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
5714 apex {
5715 name: "myapex",
5716 key: "myapex.key",
5717 native_shared_libs: ["mylib"],
5718 }
5719
5720 apex_key {
5721 name: "myapex.key",
5722 public_key: "testkey.avbpubkey",
5723 private_key: "testkey.pem",
5724 }
5725
5726 cc_library {
5727 name: "mylib",
5728 srcs: ["mylib.cpp"],
5729 static_libs: ["otherlib"],
5730 system_shared_libs: [],
5731 stl: "none",
5732 apex_available: [ "myapex" ],
5733 }
5734
5735 cc_library {
5736 name: "otherlib",
5737 srcs: ["mylib.cpp"],
5738 system_shared_libs: [],
5739 stl: "none",
5740 stubs: {
5741 versions: ["1", "2", "3"],
5742 },
5743 apex_available: [ "myapex" ],
5744 }
5745 `)
5746}
5747
Jiyong Park8d6c51e2020-06-12 17:26:31 +09005748func TestApexKeysTxt(t *testing.T) {
5749 ctx, _ := testApex(t, `
5750 apex {
5751 name: "myapex",
5752 key: "myapex.key",
5753 }
5754
5755 apex_key {
5756 name: "myapex.key",
5757 public_key: "testkey.avbpubkey",
5758 private_key: "testkey.pem",
5759 }
5760
5761 prebuilt_apex {
5762 name: "myapex",
5763 prefer: true,
5764 arch: {
5765 arm64: {
5766 src: "myapex-arm64.apex",
5767 },
5768 arm: {
5769 src: "myapex-arm.apex",
5770 },
5771 },
5772 }
5773
5774 apex_set {
5775 name: "myapex_set",
5776 set: "myapex.apks",
5777 filename: "myapex_set.apex",
5778 overrides: ["myapex"],
5779 }
5780 `)
5781
5782 apexKeysText := ctx.SingletonForTests("apex_keys_text")
5783 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
5784 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 +09005785 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 +09005786}
5787
Jooyung Han938b5932020-06-20 12:47:47 +09005788func TestAllowedFiles(t *testing.T) {
5789 ctx, _ := testApex(t, `
5790 apex {
5791 name: "myapex",
5792 key: "myapex.key",
5793 apps: ["app"],
5794 allowed_files: "allowed.txt",
5795 }
5796
5797 apex_key {
5798 name: "myapex.key",
5799 public_key: "testkey.avbpubkey",
5800 private_key: "testkey.pem",
5801 }
5802
5803 android_app {
5804 name: "app",
5805 srcs: ["foo/bar/MyClass.java"],
5806 package_name: "foo",
5807 sdk_version: "none",
5808 system_modules: "none",
5809 apex_available: [ "myapex" ],
5810 }
5811 `, withFiles(map[string][]byte{
5812 "sub/Android.bp": []byte(`
5813 override_apex {
5814 name: "override_myapex",
5815 base: "myapex",
5816 apps: ["override_app"],
5817 allowed_files: ":allowed",
5818 }
5819 // Overridable "path" property should be referenced indirectly
5820 filegroup {
5821 name: "allowed",
5822 srcs: ["allowed.txt"],
5823 }
5824 override_android_app {
5825 name: "override_app",
5826 base: "app",
5827 package_name: "bar",
5828 }
5829 `),
5830 }))
5831
5832 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
5833 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
5834 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
5835 }
5836
5837 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
5838 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
5839 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
5840 }
5841}
5842
Martin Stjernholm58c33f02020-07-06 22:56:01 +01005843func TestNonPreferredPrebuiltDependency(t *testing.T) {
5844 _, _ = testApex(t, `
5845 apex {
5846 name: "myapex",
5847 key: "myapex.key",
5848 native_shared_libs: ["mylib"],
5849 }
5850
5851 apex_key {
5852 name: "myapex.key",
5853 public_key: "testkey.avbpubkey",
5854 private_key: "testkey.pem",
5855 }
5856
5857 cc_library {
5858 name: "mylib",
5859 srcs: ["mylib.cpp"],
5860 stubs: {
5861 versions: ["10000"],
5862 },
5863 apex_available: ["myapex"],
5864 }
5865
5866 cc_prebuilt_library_shared {
5867 name: "mylib",
5868 prefer: false,
5869 srcs: ["prebuilt.so"],
5870 stubs: {
5871 versions: ["10000"],
5872 },
5873 apex_available: ["myapex"],
5874 }
5875 `)
5876}
5877
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07005878func TestMain(m *testing.M) {
5879 run := func() int {
5880 setUp()
5881 defer tearDown()
5882
5883 return m.Run()
5884 }
5885
5886 os.Exit(run())
5887}