blob: f064338487a4f36edb7200b568ae70a976a5426e [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 Hanefb184e2020-06-25 17:14:25 +09002206func TestAndroidMk_UseVendorRequired(t *testing.T) {
2207 ctx, config := testApex(t, `
2208 apex {
2209 name: "myapex",
2210 key: "myapex.key",
2211 use_vendor: true,
2212 native_shared_libs: ["mylib"],
2213 }
2214
2215 apex_key {
2216 name: "myapex.key",
2217 public_key: "testkey.avbpubkey",
2218 private_key: "testkey.pem",
2219 }
2220
2221 cc_library {
2222 name: "mylib",
2223 vendor_available: true,
2224 apex_available: ["myapex"],
2225 }
2226 `, func(fs map[string][]byte, config android.Config) {
2227 setUseVendorAllowListForTest(config, []string{"myapex"})
2228 })
2229
2230 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2231 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2232 name := apexBundle.BaseModuleName()
2233 prefix := "TARGET_"
2234 var builder strings.Builder
2235 data.Custom(&builder, name, prefix, "", data)
2236 androidMk := builder.String()
2237 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2238}
2239
2240func TestAndroidMk_VendorApexRequired(t *testing.T) {
2241 ctx, config := testApex(t, `
2242 apex {
2243 name: "myapex",
2244 key: "myapex.key",
2245 vendor: true,
2246 native_shared_libs: ["mylib"],
2247 }
2248
2249 apex_key {
2250 name: "myapex.key",
2251 public_key: "testkey.avbpubkey",
2252 private_key: "testkey.pem",
2253 }
2254
2255 cc_library {
2256 name: "mylib",
2257 vendor_available: true,
2258 }
2259 `)
2260
2261 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2262 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2263 name := apexBundle.BaseModuleName()
2264 prefix := "TARGET_"
2265 var builder strings.Builder
2266 data.Custom(&builder, name, prefix, "", data)
2267 androidMk := builder.String()
2268 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2269}
2270
Jooyung Han2ed99d02020-06-24 23:26:26 +09002271func TestAndroidMkWritesCommonProperties(t *testing.T) {
2272 ctx, config := testApex(t, `
2273 apex {
2274 name: "myapex",
2275 key: "myapex.key",
2276 vintf_fragments: ["fragment.xml"],
2277 init_rc: ["init.rc"],
2278 }
2279 apex_key {
2280 name: "myapex.key",
2281 public_key: "testkey.avbpubkey",
2282 private_key: "testkey.pem",
2283 }
2284 cc_binary {
2285 name: "mybin",
2286 }
2287 `)
2288
2289 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2290 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2291 name := apexBundle.BaseModuleName()
2292 prefix := "TARGET_"
2293 var builder strings.Builder
2294 data.Custom(&builder, name, prefix, "", data)
2295 androidMk := builder.String()
2296 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2297 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2298}
2299
Jiyong Park16e91a02018-12-20 18:18:08 +09002300func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002301 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002302 apex {
2303 name: "myapex",
2304 key: "myapex.key",
2305 native_shared_libs: ["mylib"],
2306 }
2307
2308 apex_key {
2309 name: "myapex.key",
2310 public_key: "testkey.avbpubkey",
2311 private_key: "testkey.pem",
2312 }
2313
2314 cc_library {
2315 name: "mylib",
2316 srcs: ["mylib.cpp"],
2317 system_shared_libs: [],
2318 stl: "none",
2319 stubs: {
2320 versions: ["1", "2", "3"],
2321 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002322 apex_available: [
2323 "//apex_available:platform",
2324 "myapex",
2325 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002326 }
2327
2328 cc_binary {
2329 name: "not_in_apex",
2330 srcs: ["mylib.cpp"],
2331 static_libs: ["mylib"],
2332 static_executable: true,
2333 system_shared_libs: [],
2334 stl: "none",
2335 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002336 `)
2337
Colin Cross7113d202019-11-20 16:39:12 -08002338 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002339
2340 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002341 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002342}
Jiyong Park9335a262018-12-24 11:31:58 +09002343
2344func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002345 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002346 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002347 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002348 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002349 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002350 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002351 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09002352 }
2353
2354 cc_library {
2355 name: "mylib",
2356 srcs: ["mylib.cpp"],
2357 system_shared_libs: [],
2358 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002359 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002360 }
2361
2362 apex_key {
2363 name: "myapex.key",
2364 public_key: "testkey.avbpubkey",
2365 private_key: "testkey.pem",
2366 }
2367
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002368 android_app_certificate {
2369 name: "myapex.certificate",
2370 certificate: "testkey",
2371 }
2372
2373 android_app_certificate {
2374 name: "myapex.certificate.override",
2375 certificate: "testkey.override",
2376 }
2377
Jiyong Park9335a262018-12-24 11:31:58 +09002378 `)
2379
2380 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002381 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002382
2383 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2384 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
2385 "vendor/foo/devkeys/testkey.avbpubkey")
2386 }
2387 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
2388 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
2389 "vendor/foo/devkeys/testkey.pem")
2390 }
2391
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002392 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002393 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002394 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002395 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002396 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002397 }
2398}
Jiyong Park58e364a2019-01-19 19:24:06 +09002399
Jooyung Hanf121a652019-12-17 14:30:11 +09002400func TestCertificate(t *testing.T) {
2401 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
2402 ctx, _ := testApex(t, `
2403 apex {
2404 name: "myapex",
2405 key: "myapex.key",
2406 }
2407 apex_key {
2408 name: "myapex.key",
2409 public_key: "testkey.avbpubkey",
2410 private_key: "testkey.pem",
2411 }`)
2412 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2413 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2414 if actual := rule.Args["certificates"]; actual != expected {
2415 t.Errorf("certificates should be %q, not %q", expected, actual)
2416 }
2417 })
2418 t.Run("override when unspecified", func(t *testing.T) {
2419 ctx, _ := testApex(t, `
2420 apex {
2421 name: "myapex_keytest",
2422 key: "myapex.key",
2423 file_contexts: ":myapex-file_contexts",
2424 }
2425 apex_key {
2426 name: "myapex.key",
2427 public_key: "testkey.avbpubkey",
2428 private_key: "testkey.pem",
2429 }
2430 android_app_certificate {
2431 name: "myapex.certificate.override",
2432 certificate: "testkey.override",
2433 }`)
2434 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2435 expected := "testkey.override.x509.pem testkey.override.pk8"
2436 if actual := rule.Args["certificates"]; actual != expected {
2437 t.Errorf("certificates should be %q, not %q", expected, actual)
2438 }
2439 })
2440 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
2441 ctx, _ := testApex(t, `
2442 apex {
2443 name: "myapex",
2444 key: "myapex.key",
2445 certificate: ":myapex.certificate",
2446 }
2447 apex_key {
2448 name: "myapex.key",
2449 public_key: "testkey.avbpubkey",
2450 private_key: "testkey.pem",
2451 }
2452 android_app_certificate {
2453 name: "myapex.certificate",
2454 certificate: "testkey",
2455 }`)
2456 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2457 expected := "testkey.x509.pem testkey.pk8"
2458 if actual := rule.Args["certificates"]; actual != expected {
2459 t.Errorf("certificates should be %q, not %q", expected, actual)
2460 }
2461 })
2462 t.Run("override when specifiec as <:module>", func(t *testing.T) {
2463 ctx, _ := testApex(t, `
2464 apex {
2465 name: "myapex_keytest",
2466 key: "myapex.key",
2467 file_contexts: ":myapex-file_contexts",
2468 certificate: ":myapex.certificate",
2469 }
2470 apex_key {
2471 name: "myapex.key",
2472 public_key: "testkey.avbpubkey",
2473 private_key: "testkey.pem",
2474 }
2475 android_app_certificate {
2476 name: "myapex.certificate.override",
2477 certificate: "testkey.override",
2478 }`)
2479 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2480 expected := "testkey.override.x509.pem testkey.override.pk8"
2481 if actual := rule.Args["certificates"]; actual != expected {
2482 t.Errorf("certificates should be %q, not %q", expected, actual)
2483 }
2484 })
2485 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
2486 ctx, _ := testApex(t, `
2487 apex {
2488 name: "myapex",
2489 key: "myapex.key",
2490 certificate: "testkey",
2491 }
2492 apex_key {
2493 name: "myapex.key",
2494 public_key: "testkey.avbpubkey",
2495 private_key: "testkey.pem",
2496 }`)
2497 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2498 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2499 if actual := rule.Args["certificates"]; actual != expected {
2500 t.Errorf("certificates should be %q, not %q", expected, actual)
2501 }
2502 })
2503 t.Run("override when specified as <name>", func(t *testing.T) {
2504 ctx, _ := testApex(t, `
2505 apex {
2506 name: "myapex_keytest",
2507 key: "myapex.key",
2508 file_contexts: ":myapex-file_contexts",
2509 certificate: "testkey",
2510 }
2511 apex_key {
2512 name: "myapex.key",
2513 public_key: "testkey.avbpubkey",
2514 private_key: "testkey.pem",
2515 }
2516 android_app_certificate {
2517 name: "myapex.certificate.override",
2518 certificate: "testkey.override",
2519 }`)
2520 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2521 expected := "testkey.override.x509.pem testkey.override.pk8"
2522 if actual := rule.Args["certificates"]; actual != expected {
2523 t.Errorf("certificates should be %q, not %q", expected, actual)
2524 }
2525 })
2526}
2527
Jiyong Park58e364a2019-01-19 19:24:06 +09002528func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002529 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002530 apex {
2531 name: "myapex",
2532 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002533 native_shared_libs: ["mylib", "mylib2"],
Jiyong Park58e364a2019-01-19 19:24:06 +09002534 }
2535
2536 apex {
2537 name: "otherapex",
2538 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002539 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002540 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002541 }
2542
2543 apex_key {
2544 name: "myapex.key",
2545 public_key: "testkey.avbpubkey",
2546 private_key: "testkey.pem",
2547 }
2548
2549 cc_library {
2550 name: "mylib",
2551 srcs: ["mylib.cpp"],
2552 system_shared_libs: [],
2553 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002554 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002555 "myapex",
2556 "otherapex",
2557 ],
Jooyung Han24282772020-03-21 23:20:55 +09002558 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002559 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002560 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002561 cc_library {
2562 name: "mylib2",
2563 srcs: ["mylib.cpp"],
2564 system_shared_libs: [],
2565 stl: "none",
2566 apex_available: [
2567 "myapex",
2568 "otherapex",
2569 ],
2570 use_apex_name_macro: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002571 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002572 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002573 `)
2574
Jooyung Hanc87a0592020-03-02 17:44:33 +09002575 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002576 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002577 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han75568392020-03-20 04:29:24 +09002578 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002579
Jooyung Hanccce2f22020-03-07 03:45:53 +09002580 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09002581 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
2582 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09002583 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09002584 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002585
Jooyung Hanccce2f22020-03-07 03:45:53 +09002586 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Jooyung Hanc87a0592020-03-02 17:44:33 +09002587 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
2588 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanccce2f22020-03-07 03:45:53 +09002589 ensureContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09002590 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002591
Jooyung Hanc87a0592020-03-02 17:44:33 +09002592 // When cc_library sets use_apex_name_macro: true
2593 // apex variants define additional macro to distinguish which apex variant it is built for
2594
2595 // non-APEX variant does not have __ANDROID_APEX__ defined
2596 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2597 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2598
2599 // APEX variant has __ANDROID_APEX__ defined
2600 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002601 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002602 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2603 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002604
Jooyung Hanc87a0592020-03-02 17:44:33 +09002605 // APEX variant has __ANDROID_APEX__ defined
2606 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002607 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002608 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2609 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09002610
2611 // recovery variant does not set __ANDROID_SDK_VERSION__
2612 mylibCFlags = ctx.ModuleForTests("mylib", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2613 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2614 ensureNotContains(t, mylibCFlags, "-D__ANDROID_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002615}
Jiyong Park7e636d02019-01-28 16:16:54 +09002616
2617func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002618 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09002619 apex {
2620 name: "myapex",
2621 key: "myapex.key",
2622 native_shared_libs: ["mylib"],
2623 }
2624
2625 apex_key {
2626 name: "myapex.key",
2627 public_key: "testkey.avbpubkey",
2628 private_key: "testkey.pem",
2629 }
2630
2631 cc_library_headers {
2632 name: "mylib_headers",
2633 export_include_dirs: ["my_include"],
2634 system_shared_libs: [],
2635 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09002636 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002637 }
2638
2639 cc_library {
2640 name: "mylib",
2641 srcs: ["mylib.cpp"],
2642 system_shared_libs: [],
2643 stl: "none",
2644 header_libs: ["mylib_headers"],
2645 export_header_lib_headers: ["mylib_headers"],
2646 stubs: {
2647 versions: ["1", "2", "3"],
2648 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002649 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002650 }
2651
2652 cc_library {
2653 name: "otherlib",
2654 srcs: ["mylib.cpp"],
2655 system_shared_libs: [],
2656 stl: "none",
2657 shared_libs: ["mylib"],
2658 }
2659 `)
2660
Colin Cross7113d202019-11-20 16:39:12 -08002661 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09002662
2663 // Ensure that the include path of the header lib is exported to 'otherlib'
2664 ensureContains(t, cFlags, "-Imy_include")
2665}
Alex Light9670d332019-01-29 18:07:33 -08002666
Jiyong Park7cd10e32020-01-14 09:22:18 +09002667type fileInApex struct {
2668 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00002669 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09002670 isLink bool
2671}
2672
Jooyung Hana57af4a2020-01-23 05:36:59 +00002673func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09002674 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00002675 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09002676 copyCmds := apexRule.Args["copy_commands"]
2677 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09002678 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09002679 for _, cmd := range strings.Split(copyCmds, "&&") {
2680 cmd = strings.TrimSpace(cmd)
2681 if cmd == "" {
2682 continue
2683 }
2684 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00002685 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09002686 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09002687 switch terms[0] {
2688 case "mkdir":
2689 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09002690 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09002691 t.Fatal("copyCmds contains invalid cp command", cmd)
2692 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002693 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002694 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002695 isLink = false
2696 case "ln":
2697 if len(terms) != 3 && len(terms) != 4 {
2698 // ln LINK TARGET or ln -s LINK TARGET
2699 t.Fatal("copyCmds contains invalid ln command", cmd)
2700 }
2701 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002702 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09002703 isLink = true
2704 default:
2705 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
2706 }
2707 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09002708 index := strings.Index(dst, imageApexDir)
2709 if index == -1 {
2710 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
2711 }
2712 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00002713 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09002714 }
2715 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002716 return ret
2717}
2718
Jooyung Hana57af4a2020-01-23 05:36:59 +00002719func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
2720 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09002721 var failed bool
2722 var surplus []string
2723 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00002724 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09002725 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09002726 for _, expected := range files {
2727 if matched, _ := path.Match(expected, file.path); matched {
2728 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09002729 mactchFound = true
2730 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09002731 }
2732 }
Jooyung Hane6436d72020-02-27 13:31:56 +09002733 if !mactchFound {
2734 surplus = append(surplus, file.path)
2735 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09002736 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002737
Jooyung Han31c470b2019-10-18 16:26:59 +09002738 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002739 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09002740 t.Log("surplus files", surplus)
2741 failed = true
2742 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002743
2744 if len(files) > len(filesMatched) {
2745 var missing []string
2746 for _, expected := range files {
2747 if !filesMatched[expected] {
2748 missing = append(missing, expected)
2749 }
2750 }
2751 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09002752 t.Log("missing files", missing)
2753 failed = true
2754 }
2755 if failed {
2756 t.Fail()
2757 }
2758}
2759
Jooyung Han344d5432019-08-23 11:17:39 +09002760func TestVndkApexCurrent(t *testing.T) {
2761 ctx, _ := testApex(t, `
2762 apex_vndk {
2763 name: "myapex",
2764 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002765 }
2766
2767 apex_key {
2768 name: "myapex.key",
2769 public_key: "testkey.avbpubkey",
2770 private_key: "testkey.pem",
2771 }
2772
2773 cc_library {
2774 name: "libvndk",
2775 srcs: ["mylib.cpp"],
2776 vendor_available: true,
2777 vndk: {
2778 enabled: true,
2779 },
2780 system_shared_libs: [],
2781 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002782 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002783 }
2784
2785 cc_library {
2786 name: "libvndksp",
2787 srcs: ["mylib.cpp"],
2788 vendor_available: true,
2789 vndk: {
2790 enabled: true,
2791 support_system_process: true,
2792 },
2793 system_shared_libs: [],
2794 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002795 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002796 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002797 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09002798
Jooyung Hana57af4a2020-01-23 05:36:59 +00002799 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002800 "lib/libvndk.so",
2801 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002802 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09002803 "lib64/libvndk.so",
2804 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002805 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002806 "etc/llndk.libraries.VER.txt",
2807 "etc/vndkcore.libraries.VER.txt",
2808 "etc/vndksp.libraries.VER.txt",
2809 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09002810 })
Jooyung Han344d5432019-08-23 11:17:39 +09002811}
2812
2813func TestVndkApexWithPrebuilt(t *testing.T) {
2814 ctx, _ := testApex(t, `
2815 apex_vndk {
2816 name: "myapex",
2817 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09002818 }
2819
2820 apex_key {
2821 name: "myapex.key",
2822 public_key: "testkey.avbpubkey",
2823 private_key: "testkey.pem",
2824 }
2825
2826 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09002827 name: "libvndk",
2828 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09002829 vendor_available: true,
2830 vndk: {
2831 enabled: true,
2832 },
2833 system_shared_libs: [],
2834 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002835 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002836 }
Jooyung Han31c470b2019-10-18 16:26:59 +09002837
2838 cc_prebuilt_library_shared {
2839 name: "libvndk.arm",
2840 srcs: ["libvndk.arm.so"],
2841 vendor_available: true,
2842 vndk: {
2843 enabled: true,
2844 },
2845 enabled: false,
2846 arch: {
2847 arm: {
2848 enabled: true,
2849 },
2850 },
2851 system_shared_libs: [],
2852 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002853 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002854 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002855 `+vndkLibrariesTxtFiles("current"),
2856 withFiles(map[string][]byte{
2857 "libvndk.so": nil,
2858 "libvndk.arm.so": nil,
2859 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002860
Jooyung Hana57af4a2020-01-23 05:36:59 +00002861 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002862 "lib/libvndk.so",
2863 "lib/libvndk.arm.so",
2864 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09002865 "lib/libc++.so",
2866 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002867 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002868 })
Jooyung Han344d5432019-08-23 11:17:39 +09002869}
2870
Jooyung Han39edb6c2019-11-06 16:53:07 +09002871func vndkLibrariesTxtFiles(vers ...string) (result string) {
2872 for _, v := range vers {
2873 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09002874 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002875 result += `
2876 vndk_libraries_txt {
2877 name: "` + txt + `.libraries.txt",
2878 }
2879 `
2880 }
2881 } else {
2882 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
2883 result += `
2884 prebuilt_etc {
2885 name: "` + txt + `.libraries.` + v + `.txt",
2886 src: "dummy.txt",
2887 }
2888 `
2889 }
2890 }
2891 }
2892 return
2893}
2894
Jooyung Han344d5432019-08-23 11:17:39 +09002895func TestVndkApexVersion(t *testing.T) {
2896 ctx, _ := testApex(t, `
2897 apex_vndk {
2898 name: "myapex_v27",
2899 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002900 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002901 vndk_version: "27",
2902 }
2903
2904 apex_key {
2905 name: "myapex.key",
2906 public_key: "testkey.avbpubkey",
2907 private_key: "testkey.pem",
2908 }
2909
Jooyung Han31c470b2019-10-18 16:26:59 +09002910 vndk_prebuilt_shared {
2911 name: "libvndk27",
2912 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09002913 vendor_available: true,
2914 vndk: {
2915 enabled: true,
2916 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002917 target_arch: "arm64",
2918 arch: {
2919 arm: {
2920 srcs: ["libvndk27_arm.so"],
2921 },
2922 arm64: {
2923 srcs: ["libvndk27_arm64.so"],
2924 },
2925 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002926 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002927 }
2928
2929 vndk_prebuilt_shared {
2930 name: "libvndk27",
2931 version: "27",
2932 vendor_available: true,
2933 vndk: {
2934 enabled: true,
2935 },
Jooyung Han31c470b2019-10-18 16:26:59 +09002936 target_arch: "x86_64",
2937 arch: {
2938 x86: {
2939 srcs: ["libvndk27_x86.so"],
2940 },
2941 x86_64: {
2942 srcs: ["libvndk27_x86_64.so"],
2943 },
2944 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09002945 }
2946 `+vndkLibrariesTxtFiles("27"),
2947 withFiles(map[string][]byte{
2948 "libvndk27_arm.so": nil,
2949 "libvndk27_arm64.so": nil,
2950 "libvndk27_x86.so": nil,
2951 "libvndk27_x86_64.so": nil,
2952 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002953
Jooyung Hana57af4a2020-01-23 05:36:59 +00002954 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002955 "lib/libvndk27_arm.so",
2956 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002957 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002958 })
Jooyung Han344d5432019-08-23 11:17:39 +09002959}
2960
2961func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
2962 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
2963 apex_vndk {
2964 name: "myapex_v27",
2965 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002966 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002967 vndk_version: "27",
2968 }
2969 apex_vndk {
2970 name: "myapex_v27_other",
2971 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002972 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002973 vndk_version: "27",
2974 }
2975
2976 apex_key {
2977 name: "myapex.key",
2978 public_key: "testkey.avbpubkey",
2979 private_key: "testkey.pem",
2980 }
2981
2982 cc_library {
2983 name: "libvndk",
2984 srcs: ["mylib.cpp"],
2985 vendor_available: true,
2986 vndk: {
2987 enabled: true,
2988 },
2989 system_shared_libs: [],
2990 stl: "none",
2991 }
2992
2993 vndk_prebuilt_shared {
2994 name: "libvndk",
2995 version: "27",
2996 vendor_available: true,
2997 vndk: {
2998 enabled: true,
2999 },
3000 srcs: ["libvndk.so"],
3001 }
3002 `, withFiles(map[string][]byte{
3003 "libvndk.so": nil,
3004 }))
3005}
3006
Jooyung Han90eee022019-10-01 20:02:42 +09003007func TestVndkApexNameRule(t *testing.T) {
3008 ctx, _ := testApex(t, `
3009 apex_vndk {
3010 name: "myapex",
3011 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003012 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003013 }
3014 apex_vndk {
3015 name: "myapex_v28",
3016 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003017 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003018 vndk_version: "28",
3019 }
3020 apex_key {
3021 name: "myapex.key",
3022 public_key: "testkey.avbpubkey",
3023 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003024 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003025
3026 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003027 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003028 actual := proptools.String(bundle.properties.Apex_name)
3029 if !reflect.DeepEqual(actual, expected) {
3030 t.Errorf("Got '%v', expected '%v'", actual, expected)
3031 }
3032 }
3033
3034 assertApexName("com.android.vndk.vVER", "myapex")
3035 assertApexName("com.android.vndk.v28", "myapex_v28")
3036}
3037
Jooyung Han344d5432019-08-23 11:17:39 +09003038func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
3039 ctx, _ := testApex(t, `
3040 apex_vndk {
3041 name: "myapex",
3042 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003043 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003044 }
3045
3046 apex_key {
3047 name: "myapex.key",
3048 public_key: "testkey.avbpubkey",
3049 private_key: "testkey.pem",
3050 }
3051
3052 cc_library {
3053 name: "libvndk",
3054 srcs: ["mylib.cpp"],
3055 vendor_available: true,
3056 native_bridge_supported: true,
3057 host_supported: true,
3058 vndk: {
3059 enabled: true,
3060 },
3061 system_shared_libs: [],
3062 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003063 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003064 }
Jooyung Han35155c42020-02-06 17:33:20 +09003065 `+vndkLibrariesTxtFiles("current"), withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003066
Jooyung Hana57af4a2020-01-23 05:36:59 +00003067 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003068 "lib/libvndk.so",
3069 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003070 "lib/libc++.so",
3071 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003072 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003073 })
Jooyung Han344d5432019-08-23 11:17:39 +09003074}
3075
3076func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
3077 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
3078 apex_vndk {
3079 name: "myapex",
3080 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003081 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003082 native_bridge_supported: true,
3083 }
3084
3085 apex_key {
3086 name: "myapex.key",
3087 public_key: "testkey.avbpubkey",
3088 private_key: "testkey.pem",
3089 }
3090
3091 cc_library {
3092 name: "libvndk",
3093 srcs: ["mylib.cpp"],
3094 vendor_available: true,
3095 native_bridge_supported: true,
3096 host_supported: true,
3097 vndk: {
3098 enabled: true,
3099 },
3100 system_shared_libs: [],
3101 stl: "none",
3102 }
3103 `)
3104}
3105
Jooyung Han31c470b2019-10-18 16:26:59 +09003106func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003107 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003108 apex_vndk {
3109 name: "myapex_v27",
3110 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003111 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003112 vndk_version: "27",
3113 }
3114
3115 apex_key {
3116 name: "myapex.key",
3117 public_key: "testkey.avbpubkey",
3118 private_key: "testkey.pem",
3119 }
3120
3121 vndk_prebuilt_shared {
3122 name: "libvndk27",
3123 version: "27",
3124 target_arch: "arm",
3125 vendor_available: true,
3126 vndk: {
3127 enabled: true,
3128 },
3129 arch: {
3130 arm: {
3131 srcs: ["libvndk27.so"],
3132 }
3133 },
3134 }
3135
3136 vndk_prebuilt_shared {
3137 name: "libvndk27",
3138 version: "27",
3139 target_arch: "arm",
3140 binder32bit: true,
3141 vendor_available: true,
3142 vndk: {
3143 enabled: true,
3144 },
3145 arch: {
3146 arm: {
3147 srcs: ["libvndk27binder32.so"],
3148 }
3149 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003150 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003151 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003152 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003153 withFiles(map[string][]byte{
3154 "libvndk27.so": nil,
3155 "libvndk27binder32.so": nil,
3156 }),
3157 withBinder32bit,
3158 withTargets(map[android.OsType][]android.Target{
3159 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003160 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3161 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003162 },
3163 }),
3164 )
3165
Jooyung Hana57af4a2020-01-23 05:36:59 +00003166 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003167 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003168 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003169 })
3170}
3171
Jooyung Han45a96772020-06-15 14:59:42 +09003172func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
3173 ctx, _ := testApex(t, `
3174 apex_vndk {
3175 name: "myapex",
3176 key: "myapex.key",
3177 file_contexts: ":myapex-file_contexts",
3178 }
3179
3180 apex_key {
3181 name: "myapex.key",
3182 public_key: "testkey.avbpubkey",
3183 private_key: "testkey.pem",
3184 }
3185
3186 cc_library {
3187 name: "libz",
3188 vendor_available: true,
3189 vndk: {
3190 enabled: true,
3191 },
3192 stubs: {
3193 symbol_file: "libz.map.txt",
3194 versions: ["30"],
3195 }
3196 }
3197 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3198 "libz.map.txt": nil,
3199 }))
3200
3201 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_image").Rule("apexManifestRule")
3202 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3203 ensureListEmpty(t, provideNativeLibs)
3204}
3205
Jooyung Hane1633032019-08-01 17:41:43 +09003206func TestDependenciesInApexManifest(t *testing.T) {
3207 ctx, _ := testApex(t, `
3208 apex {
3209 name: "myapex_nodep",
3210 key: "myapex.key",
3211 native_shared_libs: ["lib_nodep"],
3212 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003213 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003214 }
3215
3216 apex {
3217 name: "myapex_dep",
3218 key: "myapex.key",
3219 native_shared_libs: ["lib_dep"],
3220 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003221 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003222 }
3223
3224 apex {
3225 name: "myapex_provider",
3226 key: "myapex.key",
3227 native_shared_libs: ["libfoo"],
3228 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003229 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003230 }
3231
3232 apex {
3233 name: "myapex_selfcontained",
3234 key: "myapex.key",
3235 native_shared_libs: ["lib_dep", "libfoo"],
3236 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003237 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09003238 }
3239
3240 apex_key {
3241 name: "myapex.key",
3242 public_key: "testkey.avbpubkey",
3243 private_key: "testkey.pem",
3244 }
3245
3246 cc_library {
3247 name: "lib_nodep",
3248 srcs: ["mylib.cpp"],
3249 system_shared_libs: [],
3250 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003251 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003252 }
3253
3254 cc_library {
3255 name: "lib_dep",
3256 srcs: ["mylib.cpp"],
3257 shared_libs: ["libfoo"],
3258 system_shared_libs: [],
3259 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003260 apex_available: [
3261 "myapex_dep",
3262 "myapex_provider",
3263 "myapex_selfcontained",
3264 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003265 }
3266
3267 cc_library {
3268 name: "libfoo",
3269 srcs: ["mytest.cpp"],
3270 stubs: {
3271 versions: ["1"],
3272 },
3273 system_shared_libs: [],
3274 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003275 apex_available: [
3276 "myapex_provider",
3277 "myapex_selfcontained",
3278 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003279 }
3280 `)
3281
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003282 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003283 var provideNativeLibs, requireNativeLibs []string
3284
Sundong Ahnabb64432019-10-22 13:58:29 +09003285 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003286 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3287 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003288 ensureListEmpty(t, provideNativeLibs)
3289 ensureListEmpty(t, requireNativeLibs)
3290
Sundong Ahnabb64432019-10-22 13:58:29 +09003291 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003292 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3293 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003294 ensureListEmpty(t, provideNativeLibs)
3295 ensureListContains(t, requireNativeLibs, "libfoo.so")
3296
Sundong Ahnabb64432019-10-22 13:58:29 +09003297 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003298 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3299 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003300 ensureListContains(t, provideNativeLibs, "libfoo.so")
3301 ensureListEmpty(t, requireNativeLibs)
3302
Sundong Ahnabb64432019-10-22 13:58:29 +09003303 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003304 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3305 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003306 ensureListContains(t, provideNativeLibs, "libfoo.so")
3307 ensureListEmpty(t, requireNativeLibs)
3308}
3309
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003310func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09003311 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003312 apex {
3313 name: "myapex",
3314 key: "myapex.key",
3315 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003316 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003317 }
3318
3319 apex_key {
3320 name: "myapex.key",
3321 public_key: "testkey.avbpubkey",
3322 private_key: "testkey.pem",
3323 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003324
3325 cc_library {
3326 name: "mylib",
3327 srcs: ["mylib.cpp"],
3328 system_shared_libs: [],
3329 stl: "none",
3330 apex_available: [
3331 "//apex_available:platform",
3332 "myapex",
3333 ],
3334 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003335 `)
3336
Sundong Ahnabb64432019-10-22 13:58:29 +09003337 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003338 apexManifestRule := module.Rule("apexManifestRule")
3339 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3340 apexRule := module.Rule("apexRule")
3341 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003342
3343 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3344 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3345 name := apexBundle.BaseModuleName()
3346 prefix := "TARGET_"
3347 var builder strings.Builder
3348 data.Custom(&builder, name, prefix, "", data)
3349 androidMk := builder.String()
3350 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3351 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003352}
3353
Alex Light0851b882019-02-07 13:20:53 -08003354func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003355 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003356 apex {
3357 name: "myapex",
3358 key: "myapex.key",
3359 native_shared_libs: ["mylib_common"],
3360 }
3361
3362 apex_key {
3363 name: "myapex.key",
3364 public_key: "testkey.avbpubkey",
3365 private_key: "testkey.pem",
3366 }
3367
3368 cc_library {
3369 name: "mylib_common",
3370 srcs: ["mylib.cpp"],
3371 system_shared_libs: [],
3372 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003373 apex_available: [
3374 "//apex_available:platform",
3375 "myapex",
3376 ],
Alex Light0851b882019-02-07 13:20:53 -08003377 }
3378 `)
3379
Sundong Ahnabb64432019-10-22 13:58:29 +09003380 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003381 apexRule := module.Rule("apexRule")
3382 copyCmds := apexRule.Args["copy_commands"]
3383
3384 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3385 t.Log("Apex was a test apex!")
3386 t.Fail()
3387 }
3388 // Ensure that main rule creates an output
3389 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3390
3391 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08003392 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08003393
3394 // Ensure that both direct and indirect deps are copied into apex
3395 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3396
Colin Cross7113d202019-11-20 16:39:12 -08003397 // Ensure that the platform variant ends with _shared
3398 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003399
3400 if !android.InAnyApex("mylib_common") {
3401 t.Log("Found mylib_common not in any apex!")
3402 t.Fail()
3403 }
3404}
3405
3406func TestTestApex(t *testing.T) {
3407 if android.InAnyApex("mylib_common_test") {
3408 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!")
3409 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003410 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003411 apex_test {
3412 name: "myapex",
3413 key: "myapex.key",
3414 native_shared_libs: ["mylib_common_test"],
3415 }
3416
3417 apex_key {
3418 name: "myapex.key",
3419 public_key: "testkey.avbpubkey",
3420 private_key: "testkey.pem",
3421 }
3422
3423 cc_library {
3424 name: "mylib_common_test",
3425 srcs: ["mylib.cpp"],
3426 system_shared_libs: [],
3427 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003428 // TODO: remove //apex_available:platform
3429 apex_available: [
3430 "//apex_available:platform",
3431 "myapex",
3432 ],
Alex Light0851b882019-02-07 13:20:53 -08003433 }
3434 `)
3435
Sundong Ahnabb64432019-10-22 13:58:29 +09003436 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003437 apexRule := module.Rule("apexRule")
3438 copyCmds := apexRule.Args["copy_commands"]
3439
3440 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3441 t.Log("Apex was not a test apex!")
3442 t.Fail()
3443 }
3444 // Ensure that main rule creates an output
3445 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3446
3447 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08003448 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08003449
3450 // Ensure that both direct and indirect deps are copied into apex
3451 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3452
Colin Cross7113d202019-11-20 16:39:12 -08003453 // Ensure that the platform variant ends with _shared
3454 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003455}
3456
Alex Light9670d332019-01-29 18:07:33 -08003457func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003458 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003459 apex {
3460 name: "myapex",
3461 key: "myapex.key",
3462 multilib: {
3463 first: {
3464 native_shared_libs: ["mylib_common"],
3465 }
3466 },
3467 target: {
3468 android: {
3469 multilib: {
3470 first: {
3471 native_shared_libs: ["mylib"],
3472 }
3473 }
3474 },
3475 host: {
3476 multilib: {
3477 first: {
3478 native_shared_libs: ["mylib2"],
3479 }
3480 }
3481 }
3482 }
3483 }
3484
3485 apex_key {
3486 name: "myapex.key",
3487 public_key: "testkey.avbpubkey",
3488 private_key: "testkey.pem",
3489 }
3490
3491 cc_library {
3492 name: "mylib",
3493 srcs: ["mylib.cpp"],
3494 system_shared_libs: [],
3495 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003496 // TODO: remove //apex_available:platform
3497 apex_available: [
3498 "//apex_available:platform",
3499 "myapex",
3500 ],
Alex Light9670d332019-01-29 18:07:33 -08003501 }
3502
3503 cc_library {
3504 name: "mylib_common",
3505 srcs: ["mylib.cpp"],
3506 system_shared_libs: [],
3507 stl: "none",
3508 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003509 // TODO: remove //apex_available:platform
3510 apex_available: [
3511 "//apex_available:platform",
3512 "myapex",
3513 ],
Alex Light9670d332019-01-29 18:07:33 -08003514 }
3515
3516 cc_library {
3517 name: "mylib2",
3518 srcs: ["mylib.cpp"],
3519 system_shared_libs: [],
3520 stl: "none",
3521 compile_multilib: "first",
3522 }
3523 `)
3524
Sundong Ahnabb64432019-10-22 13:58:29 +09003525 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003526 copyCmds := apexRule.Args["copy_commands"]
3527
3528 // Ensure that main rule creates an output
3529 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3530
3531 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08003532 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
3533 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
3534 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08003535
3536 // Ensure that both direct and indirect deps are copied into apex
3537 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3538 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3539 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3540
Colin Cross7113d202019-11-20 16:39:12 -08003541 // Ensure that the platform variant ends with _shared
3542 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3543 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3544 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003545}
Jiyong Park04480cf2019-02-06 00:16:29 +09003546
3547func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003548 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09003549 apex {
3550 name: "myapex",
3551 key: "myapex.key",
3552 binaries: ["myscript"],
3553 }
3554
3555 apex_key {
3556 name: "myapex.key",
3557 public_key: "testkey.avbpubkey",
3558 private_key: "testkey.pem",
3559 }
3560
3561 sh_binary {
3562 name: "myscript",
3563 src: "mylib.cpp",
3564 filename: "myscript.sh",
3565 sub_dir: "script",
3566 }
3567 `)
3568
Sundong Ahnabb64432019-10-22 13:58:29 +09003569 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09003570 copyCmds := apexRule.Args["copy_commands"]
3571
3572 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
3573}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003574
Jooyung Han91df2082019-11-20 01:49:42 +09003575func TestApexInVariousPartition(t *testing.T) {
3576 testcases := []struct {
3577 propName, parition, flattenedPartition string
3578 }{
3579 {"", "system", "system_ext"},
3580 {"product_specific: true", "product", "product"},
3581 {"soc_specific: true", "vendor", "vendor"},
3582 {"proprietary: true", "vendor", "vendor"},
3583 {"vendor: true", "vendor", "vendor"},
3584 {"system_ext_specific: true", "system_ext", "system_ext"},
3585 }
3586 for _, tc := range testcases {
3587 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
3588 ctx, _ := testApex(t, `
3589 apex {
3590 name: "myapex",
3591 key: "myapex.key",
3592 `+tc.propName+`
3593 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003594
Jooyung Han91df2082019-11-20 01:49:42 +09003595 apex_key {
3596 name: "myapex.key",
3597 public_key: "testkey.avbpubkey",
3598 private_key: "testkey.pem",
3599 }
3600 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003601
Jooyung Han91df2082019-11-20 01:49:42 +09003602 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3603 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
3604 actual := apex.installDir.String()
3605 if actual != expected {
3606 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
3607 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003608
Jooyung Han91df2082019-11-20 01:49:42 +09003609 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
3610 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
3611 actual = flattened.installDir.String()
3612 if actual != expected {
3613 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
3614 }
3615 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003616 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003617}
Jiyong Park67882562019-03-21 01:11:21 +09003618
Jooyung Han580eb4f2020-06-24 19:33:06 +09003619func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09003620 ctx, _ := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09003621 apex {
3622 name: "myapex",
3623 key: "myapex.key",
3624 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003625
Jooyung Han580eb4f2020-06-24 19:33:06 +09003626 apex_key {
3627 name: "myapex.key",
3628 public_key: "testkey.avbpubkey",
3629 private_key: "testkey.pem",
3630 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003631 `)
3632 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09003633 rule := module.Output("file_contexts")
3634 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
3635}
Jooyung Han54aca7b2019-11-20 02:26:02 +09003636
Jooyung Han580eb4f2020-06-24 19:33:06 +09003637func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09003638 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09003639 apex {
3640 name: "myapex",
3641 key: "myapex.key",
3642 file_contexts: "my_own_file_contexts",
3643 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003644
Jooyung Han580eb4f2020-06-24 19:33:06 +09003645 apex_key {
3646 name: "myapex.key",
3647 public_key: "testkey.avbpubkey",
3648 private_key: "testkey.pem",
3649 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003650 `, withFiles(map[string][]byte{
3651 "my_own_file_contexts": nil,
3652 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09003653}
Jooyung Han54aca7b2019-11-20 02:26:02 +09003654
Jooyung Han580eb4f2020-06-24 19:33:06 +09003655func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09003656 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09003657 apex {
3658 name: "myapex",
3659 key: "myapex.key",
3660 product_specific: true,
3661 file_contexts: "product_specific_file_contexts",
3662 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003663
Jooyung Han580eb4f2020-06-24 19:33:06 +09003664 apex_key {
3665 name: "myapex.key",
3666 public_key: "testkey.avbpubkey",
3667 private_key: "testkey.pem",
3668 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003669 `)
3670
Jooyung Han580eb4f2020-06-24 19:33:06 +09003671 ctx, _ := testApex(t, `
3672 apex {
3673 name: "myapex",
3674 key: "myapex.key",
3675 product_specific: true,
3676 file_contexts: "product_specific_file_contexts",
3677 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003678
Jooyung Han580eb4f2020-06-24 19:33:06 +09003679 apex_key {
3680 name: "myapex.key",
3681 public_key: "testkey.avbpubkey",
3682 private_key: "testkey.pem",
3683 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003684 `, withFiles(map[string][]byte{
3685 "product_specific_file_contexts": nil,
3686 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09003687 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3688 rule := module.Output("file_contexts")
3689 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
3690}
Jooyung Han54aca7b2019-11-20 02:26:02 +09003691
Jooyung Han580eb4f2020-06-24 19:33:06 +09003692func TestFileContexts_SetViaFileGroup(t *testing.T) {
3693 ctx, _ := testApex(t, `
3694 apex {
3695 name: "myapex",
3696 key: "myapex.key",
3697 product_specific: true,
3698 file_contexts: ":my-file-contexts",
3699 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003700
Jooyung Han580eb4f2020-06-24 19:33:06 +09003701 apex_key {
3702 name: "myapex.key",
3703 public_key: "testkey.avbpubkey",
3704 private_key: "testkey.pem",
3705 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003706
Jooyung Han580eb4f2020-06-24 19:33:06 +09003707 filegroup {
3708 name: "my-file-contexts",
3709 srcs: ["product_specific_file_contexts"],
3710 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09003711 `, withFiles(map[string][]byte{
3712 "product_specific_file_contexts": nil,
3713 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09003714 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3715 rule := module.Output("file_contexts")
3716 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09003717}
3718
Jiyong Park67882562019-03-21 01:11:21 +09003719func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003720 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09003721 apex_key {
3722 name: "myapex.key",
3723 public_key: ":my.avbpubkey",
3724 private_key: ":my.pem",
3725 product_specific: true,
3726 }
3727
3728 filegroup {
3729 name: "my.avbpubkey",
3730 srcs: ["testkey2.avbpubkey"],
3731 }
3732
3733 filegroup {
3734 name: "my.pem",
3735 srcs: ["testkey2.pem"],
3736 }
3737 `)
3738
3739 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
3740 expected_pubkey := "testkey2.avbpubkey"
3741 actual_pubkey := apex_key.public_key_file.String()
3742 if actual_pubkey != expected_pubkey {
3743 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
3744 }
3745 expected_privkey := "testkey2.pem"
3746 actual_privkey := apex_key.private_key_file.String()
3747 if actual_privkey != expected_privkey {
3748 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
3749 }
3750}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003751
3752func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003753 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003754 prebuilt_apex {
3755 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09003756 arch: {
3757 arm64: {
3758 src: "myapex-arm64.apex",
3759 },
3760 arm: {
3761 src: "myapex-arm.apex",
3762 },
3763 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003764 }
3765 `)
3766
3767 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3768
Jiyong Parkc95714e2019-03-29 14:23:10 +09003769 expectedInput := "myapex-arm64.apex"
3770 if prebuilt.inputApex.String() != expectedInput {
3771 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
3772 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07003773}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003774
3775func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003776 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01003777 prebuilt_apex {
3778 name: "myapex",
3779 src: "myapex-arm.apex",
3780 filename: "notmyapex.apex",
3781 }
3782 `)
3783
3784 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
3785
3786 expected := "notmyapex.apex"
3787 if p.installFilename != expected {
3788 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
3789 }
3790}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003791
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003792func TestPrebuiltOverrides(t *testing.T) {
3793 ctx, config := testApex(t, `
3794 prebuilt_apex {
3795 name: "myapex.prebuilt",
3796 src: "myapex-arm.apex",
3797 overrides: [
3798 "myapex",
3799 ],
3800 }
3801 `)
3802
3803 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
3804
3805 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09003806 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003807 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09003808 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003809 }
3810}
3811
Roland Levillain630846d2019-06-26 12:48:34 +01003812func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01003813 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01003814 apex_test {
3815 name: "myapex",
3816 key: "myapex.key",
3817 tests: [
3818 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01003819 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01003820 ],
3821 }
3822
3823 apex_key {
3824 name: "myapex.key",
3825 public_key: "testkey.avbpubkey",
3826 private_key: "testkey.pem",
3827 }
3828
Liz Kammer1c14a212020-05-12 15:26:55 -07003829 filegroup {
3830 name: "fg",
3831 srcs: [
3832 "baz",
3833 "bar/baz"
3834 ],
3835 }
3836
Roland Levillain630846d2019-06-26 12:48:34 +01003837 cc_test {
3838 name: "mytest",
3839 gtest: false,
3840 srcs: ["mytest.cpp"],
3841 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003842 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01003843 system_shared_libs: [],
3844 static_executable: true,
3845 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07003846 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01003847 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01003848
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003849 cc_library {
3850 name: "mylib",
3851 srcs: ["mylib.cpp"],
3852 system_shared_libs: [],
3853 stl: "none",
3854 }
3855
Liz Kammer5bd365f2020-05-27 15:15:11 -07003856 filegroup {
3857 name: "fg2",
3858 srcs: [
3859 "testdata/baz"
3860 ],
3861 }
3862
Roland Levillain9b5fde92019-06-28 15:41:19 +01003863 cc_test {
3864 name: "mytests",
3865 gtest: false,
3866 srcs: [
3867 "mytest1.cpp",
3868 "mytest2.cpp",
3869 "mytest3.cpp",
3870 ],
3871 test_per_src: true,
3872 relative_install_path: "test",
3873 system_shared_libs: [],
3874 static_executable: true,
3875 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07003876 data: [
3877 ":fg",
3878 ":fg2",
3879 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01003880 }
Roland Levillain630846d2019-06-26 12:48:34 +01003881 `)
3882
Sundong Ahnabb64432019-10-22 13:58:29 +09003883 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01003884 copyCmds := apexRule.Args["copy_commands"]
3885
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003886 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01003887 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09003888 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01003889
Liz Kammer1c14a212020-05-12 15:26:55 -07003890 //Ensure that test data are copied into apex.
3891 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
3892 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
3893
Roland Levillain9b5fde92019-06-28 15:41:19 +01003894 // Ensure that test deps built with `test_per_src` are copied into apex.
3895 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
3896 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
3897 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01003898
3899 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07003900 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3901 data := android.AndroidMkDataForTest(t, config, "", bundle)
3902 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01003903 prefix := "TARGET_"
3904 var builder strings.Builder
3905 data.Custom(&builder, name, prefix, "", data)
3906 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09003907 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
3908 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
3909 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
3910 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09003911 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09003912 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01003913 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07003914
3915 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
3916 data = android.AndroidMkDataForTest(t, config, "", flatBundle)
3917 data.Custom(&builder, name, prefix, "", data)
3918 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07003919 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
3920 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01003921}
3922
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003923func TestInstallExtraFlattenedApexes(t *testing.T) {
3924 ctx, config := testApex(t, `
3925 apex {
3926 name: "myapex",
3927 key: "myapex.key",
3928 }
3929 apex_key {
3930 name: "myapex.key",
3931 public_key: "testkey.avbpubkey",
3932 private_key: "testkey.pem",
3933 }
3934 `, func(fs map[string][]byte, config android.Config) {
3935 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
3936 })
3937 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09003938 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09003939 mk := android.AndroidMkDataForTest(t, config, "", ab)
3940 var builder strings.Builder
3941 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
3942 androidMk := builder.String()
3943 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
3944}
3945
Jooyung Han5c998b92019-06-27 11:30:33 +09003946func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07003947 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09003948 apex {
3949 name: "myapex",
3950 key: "myapex.key",
3951 native_shared_libs: ["mylib"],
3952 uses: ["commonapex"],
3953 }
3954
3955 apex {
3956 name: "commonapex",
3957 key: "myapex.key",
3958 native_shared_libs: ["libcommon"],
3959 provide_cpp_shared_libs: true,
3960 }
3961
3962 apex_key {
3963 name: "myapex.key",
3964 public_key: "testkey.avbpubkey",
3965 private_key: "testkey.pem",
3966 }
3967
3968 cc_library {
3969 name: "mylib",
3970 srcs: ["mylib.cpp"],
3971 shared_libs: ["libcommon"],
3972 system_shared_libs: [],
3973 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003974 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003975 }
3976
3977 cc_library {
3978 name: "libcommon",
3979 srcs: ["mylib_common.cpp"],
3980 system_shared_libs: [],
3981 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003982 // TODO: remove //apex_available:platform
3983 apex_available: [
3984 "//apex_available:platform",
3985 "commonapex",
3986 "myapex",
3987 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09003988 }
3989 `)
3990
Sundong Ahnabb64432019-10-22 13:58:29 +09003991 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003992 apexRule1 := module1.Rule("apexRule")
3993 copyCmds1 := apexRule1.Args["copy_commands"]
3994
Sundong Ahnabb64432019-10-22 13:58:29 +09003995 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09003996 apexRule2 := module2.Rule("apexRule")
3997 copyCmds2 := apexRule2.Args["copy_commands"]
3998
Colin Cross7113d202019-11-20 16:39:12 -08003999 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
4000 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09004001 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
4002 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
4003 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
4004}
4005
4006func TestApexUsesFailsIfNotProvided(t *testing.T) {
4007 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
4008 apex {
4009 name: "myapex",
4010 key: "myapex.key",
4011 uses: ["commonapex"],
4012 }
4013
4014 apex {
4015 name: "commonapex",
4016 key: "myapex.key",
4017 }
4018
4019 apex_key {
4020 name: "myapex.key",
4021 public_key: "testkey.avbpubkey",
4022 private_key: "testkey.pem",
4023 }
4024 `)
4025 testApexError(t, `uses: "commonapex" is not a provider`, `
4026 apex {
4027 name: "myapex",
4028 key: "myapex.key",
4029 uses: ["commonapex"],
4030 }
4031
4032 cc_library {
4033 name: "commonapex",
4034 system_shared_libs: [],
4035 stl: "none",
4036 }
4037
4038 apex_key {
4039 name: "myapex.key",
4040 public_key: "testkey.avbpubkey",
4041 private_key: "testkey.pem",
4042 }
4043 `)
4044}
4045
4046func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
4047 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
4048 apex {
4049 name: "myapex",
4050 key: "myapex.key",
4051 use_vendor: true,
4052 uses: ["commonapex"],
4053 }
4054
4055 apex {
4056 name: "commonapex",
4057 key: "myapex.key",
4058 provide_cpp_shared_libs: true,
4059 }
4060
4061 apex_key {
4062 name: "myapex.key",
4063 public_key: "testkey.avbpubkey",
4064 private_key: "testkey.pem",
4065 }
Jooyung Handc782442019-11-01 03:14:38 +09004066 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07004067 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09004068 })
Jooyung Han5c998b92019-06-27 11:30:33 +09004069}
4070
Jooyung Hand48f3c32019-08-23 11:18:57 +09004071func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4072 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4073 apex {
4074 name: "myapex",
4075 key: "myapex.key",
4076 native_shared_libs: ["libfoo"],
4077 }
4078
4079 apex_key {
4080 name: "myapex.key",
4081 public_key: "testkey.avbpubkey",
4082 private_key: "testkey.pem",
4083 }
4084
4085 cc_library {
4086 name: "libfoo",
4087 stl: "none",
4088 system_shared_libs: [],
4089 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004090 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004091 }
4092 `)
4093 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4094 apex {
4095 name: "myapex",
4096 key: "myapex.key",
4097 java_libs: ["myjar"],
4098 }
4099
4100 apex_key {
4101 name: "myapex.key",
4102 public_key: "testkey.avbpubkey",
4103 private_key: "testkey.pem",
4104 }
4105
4106 java_library {
4107 name: "myjar",
4108 srcs: ["foo/bar/MyClass.java"],
4109 sdk_version: "none",
4110 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004111 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004112 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004113 }
4114 `)
4115}
4116
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004117func TestApexWithApps(t *testing.T) {
4118 ctx, _ := testApex(t, `
4119 apex {
4120 name: "myapex",
4121 key: "myapex.key",
4122 apps: [
4123 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09004124 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004125 ],
4126 }
4127
4128 apex_key {
4129 name: "myapex.key",
4130 public_key: "testkey.avbpubkey",
4131 private_key: "testkey.pem",
4132 }
4133
4134 android_app {
4135 name: "AppFoo",
4136 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004137 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004138 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09004139 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08004140 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004141 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004142 }
Jiyong Parkf7487312019-10-17 12:54:30 +09004143
4144 android_app {
4145 name: "AppFooPriv",
4146 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08004147 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09004148 system_modules: "none",
4149 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08004150 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004151 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09004152 }
Jiyong Park8be103b2019-11-08 15:53:48 +09004153
4154 cc_library_shared {
4155 name: "libjni",
4156 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004157 shared_libs: ["libfoo"],
4158 stl: "none",
4159 system_shared_libs: [],
4160 apex_available: [ "myapex" ],
4161 sdk_version: "current",
4162 }
4163
4164 cc_library_shared {
4165 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09004166 stl: "none",
4167 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09004168 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08004169 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09004170 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004171 `)
4172
Sundong Ahnabb64432019-10-22 13:58:29 +09004173 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004174 apexRule := module.Rule("apexRule")
4175 copyCmds := apexRule.Args["copy_commands"]
4176
4177 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09004178 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004179
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004180 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
4181 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09004182 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004183 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09004184 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004185 // JNI libraries including transitive deps are
4186 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossc511bc52020-04-07 16:50:32 +00004187 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_myapex").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09004188 // ... embedded inside APK (jnilibs.zip)
4189 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
4190 // ... and not directly inside the APEX
4191 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
4192 }
Dario Frenicde2a032019-10-27 00:29:22 +01004193}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004194
Dario Frenicde2a032019-10-27 00:29:22 +01004195func TestApexWithAppImports(t *testing.T) {
4196 ctx, _ := testApex(t, `
4197 apex {
4198 name: "myapex",
4199 key: "myapex.key",
4200 apps: [
4201 "AppFooPrebuilt",
4202 "AppFooPrivPrebuilt",
4203 ],
4204 }
4205
4206 apex_key {
4207 name: "myapex.key",
4208 public_key: "testkey.avbpubkey",
4209 private_key: "testkey.pem",
4210 }
4211
4212 android_app_import {
4213 name: "AppFooPrebuilt",
4214 apk: "PrebuiltAppFoo.apk",
4215 presigned: true,
4216 dex_preopt: {
4217 enabled: false,
4218 },
Jiyong Park592a6a42020-04-21 22:34:28 +09004219 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01004220 }
4221
4222 android_app_import {
4223 name: "AppFooPrivPrebuilt",
4224 apk: "PrebuiltAppFooPriv.apk",
4225 privileged: true,
4226 presigned: true,
4227 dex_preopt: {
4228 enabled: false,
4229 },
Jooyung Han39ee1192020-03-23 20:21:11 +09004230 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09004231 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01004232 }
4233 `)
4234
Sundong Ahnabb64432019-10-22 13:58:29 +09004235 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01004236 apexRule := module.Rule("apexRule")
4237 copyCmds := apexRule.Args["copy_commands"]
4238
4239 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09004240 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
4241}
4242
4243func TestApexWithAppImportsPrefer(t *testing.T) {
4244 ctx, _ := testApex(t, `
4245 apex {
4246 name: "myapex",
4247 key: "myapex.key",
4248 apps: [
4249 "AppFoo",
4250 ],
4251 }
4252
4253 apex_key {
4254 name: "myapex.key",
4255 public_key: "testkey.avbpubkey",
4256 private_key: "testkey.pem",
4257 }
4258
4259 android_app {
4260 name: "AppFoo",
4261 srcs: ["foo/bar/MyClass.java"],
4262 sdk_version: "none",
4263 system_modules: "none",
4264 apex_available: [ "myapex" ],
4265 }
4266
4267 android_app_import {
4268 name: "AppFoo",
4269 apk: "AppFooPrebuilt.apk",
4270 filename: "AppFooPrebuilt.apk",
4271 presigned: true,
4272 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09004273 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09004274 }
4275 `, withFiles(map[string][]byte{
4276 "AppFooPrebuilt.apk": nil,
4277 }))
4278
4279 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4280 "app/AppFoo/AppFooPrebuilt.apk",
4281 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004282}
4283
Dario Freni6f3937c2019-12-20 22:58:03 +00004284func TestApexWithTestHelperApp(t *testing.T) {
4285 ctx, _ := testApex(t, `
4286 apex {
4287 name: "myapex",
4288 key: "myapex.key",
4289 apps: [
4290 "TesterHelpAppFoo",
4291 ],
4292 }
4293
4294 apex_key {
4295 name: "myapex.key",
4296 public_key: "testkey.avbpubkey",
4297 private_key: "testkey.pem",
4298 }
4299
4300 android_test_helper_app {
4301 name: "TesterHelpAppFoo",
4302 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004303 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00004304 }
4305
4306 `)
4307
4308 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4309 apexRule := module.Rule("apexRule")
4310 copyCmds := apexRule.Args["copy_commands"]
4311
4312 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
4313}
4314
Jooyung Han18020ea2019-11-13 10:50:48 +09004315func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
4316 // libfoo's apex_available comes from cc_defaults
Jooyung Han5e9013b2020-03-10 06:23:13 +09004317 testApexError(t, `requires "libfoo" that is not available for the APEX`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09004318 apex {
4319 name: "myapex",
4320 key: "myapex.key",
4321 native_shared_libs: ["libfoo"],
4322 }
4323
4324 apex_key {
4325 name: "myapex.key",
4326 public_key: "testkey.avbpubkey",
4327 private_key: "testkey.pem",
4328 }
4329
4330 apex {
4331 name: "otherapex",
4332 key: "myapex.key",
4333 native_shared_libs: ["libfoo"],
4334 }
4335
4336 cc_defaults {
4337 name: "libfoo-defaults",
4338 apex_available: ["otherapex"],
4339 }
4340
4341 cc_library {
4342 name: "libfoo",
4343 defaults: ["libfoo-defaults"],
4344 stl: "none",
4345 system_shared_libs: [],
4346 }`)
4347}
4348
Paul Duffine52e66f2020-03-30 17:54:29 +01004349func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09004350 // libfoo is not available to myapex, but only to otherapex
4351 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
4352 apex {
4353 name: "myapex",
4354 key: "myapex.key",
4355 native_shared_libs: ["libfoo"],
4356 }
4357
4358 apex_key {
4359 name: "myapex.key",
4360 public_key: "testkey.avbpubkey",
4361 private_key: "testkey.pem",
4362 }
4363
4364 apex {
4365 name: "otherapex",
4366 key: "otherapex.key",
4367 native_shared_libs: ["libfoo"],
4368 }
4369
4370 apex_key {
4371 name: "otherapex.key",
4372 public_key: "testkey.avbpubkey",
4373 private_key: "testkey.pem",
4374 }
4375
4376 cc_library {
4377 name: "libfoo",
4378 stl: "none",
4379 system_shared_libs: [],
4380 apex_available: ["otherapex"],
4381 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004382}
Jiyong Park127b40b2019-09-30 16:04:35 +09004383
Paul Duffine52e66f2020-03-30 17:54:29 +01004384func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09004385 // libbbaz is an indirect dep
Paul Duffindf915ff2020-03-30 17:58:21 +01004386 testApexError(t, `requires "libbaz" that is not available for the APEX. Dependency path:
Paul Duffinc5192442020-03-31 11:31:36 +01004387.*via tag apex\.dependencyTag.*"sharedLib".*
Paul Duffindf915ff2020-03-30 17:58:21 +01004388.*-> libfoo.*link:shared.*
Paul Duffin65347702020-03-31 15:23:40 +01004389.*via tag cc\.DependencyTag.*"shared".*
Paul Duffindf915ff2020-03-30 17:58:21 +01004390.*-> libbar.*link:shared.*
Paul Duffin65347702020-03-31 15:23:40 +01004391.*via tag cc\.DependencyTag.*"shared".*
4392.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004393 apex {
4394 name: "myapex",
4395 key: "myapex.key",
4396 native_shared_libs: ["libfoo"],
4397 }
4398
4399 apex_key {
4400 name: "myapex.key",
4401 public_key: "testkey.avbpubkey",
4402 private_key: "testkey.pem",
4403 }
4404
Jiyong Park127b40b2019-09-30 16:04:35 +09004405 cc_library {
4406 name: "libfoo",
4407 stl: "none",
4408 shared_libs: ["libbar"],
4409 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09004410 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004411 }
4412
4413 cc_library {
4414 name: "libbar",
4415 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09004416 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004417 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09004418 apex_available: ["myapex"],
4419 }
4420
4421 cc_library {
4422 name: "libbaz",
4423 stl: "none",
4424 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09004425 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004426}
Jiyong Park127b40b2019-09-30 16:04:35 +09004427
Paul Duffine52e66f2020-03-30 17:54:29 +01004428func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09004429 testApexError(t, "\"otherapex\" is not a valid module name", `
4430 apex {
4431 name: "myapex",
4432 key: "myapex.key",
4433 native_shared_libs: ["libfoo"],
4434 }
4435
4436 apex_key {
4437 name: "myapex.key",
4438 public_key: "testkey.avbpubkey",
4439 private_key: "testkey.pem",
4440 }
4441
4442 cc_library {
4443 name: "libfoo",
4444 stl: "none",
4445 system_shared_libs: [],
4446 apex_available: ["otherapex"],
4447 }`)
4448
Paul Duffine52e66f2020-03-30 17:54:29 +01004449 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004450 apex {
4451 name: "myapex",
4452 key: "myapex.key",
4453 native_shared_libs: ["libfoo", "libbar"],
4454 }
4455
4456 apex_key {
4457 name: "myapex.key",
4458 public_key: "testkey.avbpubkey",
4459 private_key: "testkey.pem",
4460 }
4461
4462 cc_library {
4463 name: "libfoo",
4464 stl: "none",
4465 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09004466 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004467 apex_available: ["myapex"],
4468 }
4469
4470 cc_library {
4471 name: "libbar",
4472 stl: "none",
4473 system_shared_libs: [],
4474 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09004475 }
4476
4477 cc_library {
4478 name: "libbaz",
4479 stl: "none",
4480 system_shared_libs: [],
4481 stubs: {
4482 versions: ["10", "20", "30"],
4483 },
Jiyong Park127b40b2019-09-30 16:04:35 +09004484 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01004485}
Jiyong Park127b40b2019-09-30 16:04:35 +09004486
Jiyong Park89e850a2020-04-07 16:37:39 +09004487func TestApexAvailable_CheckForPlatform(t *testing.T) {
Paul Duffine52e66f2020-03-30 17:54:29 +01004488 ctx, _ := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09004489 apex {
4490 name: "myapex",
4491 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09004492 native_shared_libs: ["libbar", "libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004493 }
4494
4495 apex_key {
4496 name: "myapex.key",
4497 public_key: "testkey.avbpubkey",
4498 private_key: "testkey.pem",
4499 }
4500
4501 cc_library {
4502 name: "libfoo",
4503 stl: "none",
4504 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09004505 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09004506 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09004507 }
4508
4509 cc_library {
4510 name: "libfoo2",
4511 stl: "none",
4512 system_shared_libs: [],
4513 shared_libs: ["libbaz"],
4514 apex_available: ["//apex_available:platform"],
4515 }
4516
4517 cc_library {
4518 name: "libbar",
4519 stl: "none",
4520 system_shared_libs: [],
4521 apex_available: ["myapex"],
4522 }
4523
4524 cc_library {
4525 name: "libbaz",
4526 stl: "none",
4527 system_shared_libs: [],
4528 apex_available: ["myapex"],
4529 stubs: {
4530 versions: ["1"],
4531 },
Jiyong Park127b40b2019-09-30 16:04:35 +09004532 }`)
4533
Jiyong Park89e850a2020-04-07 16:37:39 +09004534 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
4535 // because it depends on libbar which isn't available to platform
4536 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4537 if libfoo.NotAvailableForPlatform() != true {
4538 t.Errorf("%q shouldn't be available to platform", libfoo.String())
4539 }
4540
4541 // libfoo2 however can be available to platform because it depends on libbaz which provides
4542 // stubs
4543 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4544 if libfoo2.NotAvailableForPlatform() == true {
4545 t.Errorf("%q should be available to platform", libfoo2.String())
4546 }
Paul Duffine52e66f2020-03-30 17:54:29 +01004547}
Jiyong Parka90ca002019-10-07 15:47:24 +09004548
Paul Duffine52e66f2020-03-30 17:54:29 +01004549func TestApexAvailable_CreatedForApex(t *testing.T) {
Jiyong Park89e850a2020-04-07 16:37:39 +09004550 ctx, _ := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09004551 apex {
4552 name: "myapex",
4553 key: "myapex.key",
4554 native_shared_libs: ["libfoo"],
4555 }
4556
4557 apex_key {
4558 name: "myapex.key",
4559 public_key: "testkey.avbpubkey",
4560 private_key: "testkey.pem",
4561 }
4562
4563 cc_library {
4564 name: "libfoo",
4565 stl: "none",
4566 system_shared_libs: [],
4567 apex_available: ["myapex"],
4568 static: {
4569 apex_available: ["//apex_available:platform"],
4570 },
4571 }`)
4572
Jiyong Park89e850a2020-04-07 16:37:39 +09004573 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
4574 if libfooShared.NotAvailableForPlatform() != true {
4575 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
4576 }
4577 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
4578 if libfooStatic.NotAvailableForPlatform() != false {
4579 t.Errorf("%q should be available to platform", libfooStatic.String())
4580 }
Jiyong Park127b40b2019-09-30 16:04:35 +09004581}
4582
Jiyong Park5d790c32019-11-15 18:40:32 +09004583func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004584 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09004585 apex {
4586 name: "myapex",
4587 key: "myapex.key",
4588 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004589 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09004590 }
4591
4592 override_apex {
4593 name: "override_myapex",
4594 base: "myapex",
4595 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004596 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08004597 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07004598 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09004599 }
4600
4601 apex_key {
4602 name: "myapex.key",
4603 public_key: "testkey.avbpubkey",
4604 private_key: "testkey.pem",
4605 }
4606
4607 android_app {
4608 name: "app",
4609 srcs: ["foo/bar/MyClass.java"],
4610 package_name: "foo",
4611 sdk_version: "none",
4612 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004613 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09004614 }
4615
4616 override_android_app {
4617 name: "override_app",
4618 base: "app",
4619 package_name: "bar",
4620 }
Jiyong Park20bacab2020-03-03 11:45:41 +09004621 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09004622
Jiyong Park317645e2019-12-05 13:20:58 +09004623 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
4624 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
4625 if originalVariant.GetOverriddenBy() != "" {
4626 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
4627 }
4628 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
4629 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
4630 }
4631
Jiyong Park5d790c32019-11-15 18:40:32 +09004632 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
4633 apexRule := module.Rule("apexRule")
4634 copyCmds := apexRule.Args["copy_commands"]
4635
4636 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09004637 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004638
4639 apexBundle := module.Module().(*apexBundle)
4640 name := apexBundle.Name()
4641 if name != "override_myapex" {
4642 t.Errorf("name should be \"override_myapex\", but was %q", name)
4643 }
4644
Baligh Uddin004d7172020-02-19 21:29:28 -08004645 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
4646 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
4647 }
4648
Jiyong Park20bacab2020-03-03 11:45:41 +09004649 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07004650 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09004651
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004652 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
4653 var builder strings.Builder
4654 data.Custom(&builder, name, "TARGET_", "", data)
4655 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09004656 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004657 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
4658 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08004659 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004660 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09004661 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08004662 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
4663 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09004664}
4665
Jooyung Han214bf372019-11-12 13:03:50 +09004666func TestLegacyAndroid10Support(t *testing.T) {
4667 ctx, _ := testApex(t, `
4668 apex {
4669 name: "myapex",
4670 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004671 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09004672 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09004673 }
4674
4675 apex_key {
4676 name: "myapex.key",
4677 public_key: "testkey.avbpubkey",
4678 private_key: "testkey.pem",
4679 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004680
4681 cc_library {
4682 name: "mylib",
4683 srcs: ["mylib.cpp"],
4684 stl: "libc++",
4685 system_shared_libs: [],
4686 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09004687 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004688 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004689 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09004690
4691 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4692 args := module.Rule("apexRule").Args
4693 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00004694 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08004695
4696 // The copies of the libraries in the apex should have one more dependency than
4697 // the ones outside the apex, namely the unwinder. Ideally we should check
4698 // the dependency names directly here but for some reason the names are blank in
4699 // this test.
4700 for _, lib := range []string{"libc++", "mylib"} {
4701 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
4702 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
4703 if len(apexImplicits) != len(nonApexImplicits)+1 {
4704 t.Errorf("%q missing unwinder dep", lib)
4705 }
4706 }
Jooyung Han214bf372019-11-12 13:03:50 +09004707}
4708
Paul Duffin9b879592020-05-26 13:21:35 +01004709var filesForSdkLibrary = map[string][]byte{
4710 "api/current.txt": nil,
4711 "api/removed.txt": nil,
4712 "api/system-current.txt": nil,
4713 "api/system-removed.txt": nil,
4714 "api/test-current.txt": nil,
4715 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01004716
4717 // For java_sdk_library_import
4718 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01004719}
4720
Jooyung Han58f26ab2019-12-18 15:34:32 +09004721func TestJavaSDKLibrary(t *testing.T) {
4722 ctx, _ := testApex(t, `
4723 apex {
4724 name: "myapex",
4725 key: "myapex.key",
4726 java_libs: ["foo"],
4727 }
4728
4729 apex_key {
4730 name: "myapex.key",
4731 public_key: "testkey.avbpubkey",
4732 private_key: "testkey.pem",
4733 }
4734
4735 java_sdk_library {
4736 name: "foo",
4737 srcs: ["a.java"],
4738 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004739 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09004740 }
Paul Duffin9b879592020-05-26 13:21:35 +01004741 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09004742
4743 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00004744 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09004745 "javalib/foo.jar",
4746 "etc/permissions/foo.xml",
4747 })
4748 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09004749 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
4750 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09004751}
4752
Paul Duffin9b879592020-05-26 13:21:35 +01004753func TestJavaSDKLibrary_WithinApex(t *testing.T) {
4754 ctx, _ := testApex(t, `
4755 apex {
4756 name: "myapex",
4757 key: "myapex.key",
4758 java_libs: ["foo", "bar"],
4759 }
4760
4761 apex_key {
4762 name: "myapex.key",
4763 public_key: "testkey.avbpubkey",
4764 private_key: "testkey.pem",
4765 }
4766
4767 java_sdk_library {
4768 name: "foo",
4769 srcs: ["a.java"],
4770 api_packages: ["foo"],
4771 apex_available: ["myapex"],
4772 sdk_version: "none",
4773 system_modules: "none",
4774 }
4775
4776 java_library {
4777 name: "bar",
4778 srcs: ["a.java"],
4779 libs: ["foo"],
4780 apex_available: ["myapex"],
4781 sdk_version: "none",
4782 system_modules: "none",
4783 }
4784 `, withFiles(filesForSdkLibrary))
4785
4786 // java_sdk_library installs both impl jar and permission XML
4787 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4788 "javalib/bar.jar",
4789 "javalib/foo.jar",
4790 "etc/permissions/foo.xml",
4791 })
4792
4793 // The bar library should depend on the implementation jar.
4794 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
4795 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
4796 t.Errorf("expected %q, found %#q", expected, actual)
4797 }
4798}
4799
4800func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
4801 ctx, _ := testApex(t, `
4802 apex {
4803 name: "myapex",
4804 key: "myapex.key",
4805 java_libs: ["foo"],
4806 }
4807
4808 apex_key {
4809 name: "myapex.key",
4810 public_key: "testkey.avbpubkey",
4811 private_key: "testkey.pem",
4812 }
4813
4814 java_sdk_library {
4815 name: "foo",
4816 srcs: ["a.java"],
4817 api_packages: ["foo"],
4818 apex_available: ["myapex"],
4819 sdk_version: "none",
4820 system_modules: "none",
4821 }
4822
4823 java_library {
4824 name: "bar",
4825 srcs: ["a.java"],
4826 libs: ["foo"],
4827 sdk_version: "none",
4828 system_modules: "none",
4829 }
4830 `, withFiles(filesForSdkLibrary))
4831
4832 // java_sdk_library installs both impl jar and permission XML
4833 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4834 "javalib/foo.jar",
4835 "etc/permissions/foo.xml",
4836 })
4837
4838 // The bar library should depend on the stubs jar.
4839 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
4840 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
4841 t.Errorf("expected %q, found %#q", expected, actual)
4842 }
4843}
4844
Paul Duffineedc5d52020-06-12 17:46:39 +01004845func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
4846 ctx, _ := testApex(t, ``,
4847 withFiles(map[string][]byte{
4848 "apex/a.java": nil,
4849 "apex/apex_manifest.json": nil,
4850 "apex/Android.bp": []byte(`
4851 package {
4852 default_visibility: ["//visibility:private"],
4853 }
4854
4855 apex {
4856 name: "myapex",
4857 key: "myapex.key",
4858 java_libs: ["foo", "bar"],
4859 }
4860
4861 apex_key {
4862 name: "myapex.key",
4863 public_key: "testkey.avbpubkey",
4864 private_key: "testkey.pem",
4865 }
4866
4867 java_library {
4868 name: "bar",
4869 srcs: ["a.java"],
4870 libs: ["foo"],
4871 apex_available: ["myapex"],
4872 sdk_version: "none",
4873 system_modules: "none",
4874 }
4875`),
4876 "source/a.java": nil,
4877 "source/api/current.txt": nil,
4878 "source/api/removed.txt": nil,
4879 "source/Android.bp": []byte(`
4880 package {
4881 default_visibility: ["//visibility:private"],
4882 }
4883
4884 java_sdk_library {
4885 name: "foo",
4886 visibility: ["//apex"],
4887 srcs: ["a.java"],
4888 api_packages: ["foo"],
4889 apex_available: ["myapex"],
4890 sdk_version: "none",
4891 system_modules: "none",
4892 public: {
4893 enabled: true,
4894 },
4895 }
4896`),
4897 "prebuilt/a.jar": nil,
4898 "prebuilt/Android.bp": []byte(`
4899 package {
4900 default_visibility: ["//visibility:private"],
4901 }
4902
4903 java_sdk_library_import {
4904 name: "foo",
4905 visibility: ["//apex", "//source"],
4906 apex_available: ["myapex"],
4907 prefer: true,
4908 public: {
4909 jars: ["a.jar"],
4910 },
4911 }
4912`),
4913 }),
4914 )
4915
4916 // java_sdk_library installs both impl jar and permission XML
4917 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4918 "javalib/bar.jar",
4919 "javalib/foo.jar",
4920 "etc/permissions/foo.xml",
4921 })
4922
4923 // The bar library should depend on the implementation jar.
4924 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
4925 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
4926 t.Errorf("expected %q, found %#q", expected, actual)
4927 }
4928}
4929
4930func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
4931 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
4932 apex {
4933 name: "myapex",
4934 key: "myapex.key",
4935 java_libs: ["foo"],
4936 }
4937
4938 apex_key {
4939 name: "myapex.key",
4940 public_key: "testkey.avbpubkey",
4941 private_key: "testkey.pem",
4942 }
4943
4944 java_sdk_library_import {
4945 name: "foo",
4946 apex_available: ["myapex"],
4947 prefer: true,
4948 public: {
4949 jars: ["a.jar"],
4950 },
4951 }
4952
4953 `, withFiles(filesForSdkLibrary))
4954}
4955
atrost6e126252020-01-27 17:01:16 +00004956func TestCompatConfig(t *testing.T) {
4957 ctx, _ := testApex(t, `
4958 apex {
4959 name: "myapex",
4960 key: "myapex.key",
4961 prebuilts: ["myjar-platform-compat-config"],
4962 java_libs: ["myjar"],
4963 }
4964
4965 apex_key {
4966 name: "myapex.key",
4967 public_key: "testkey.avbpubkey",
4968 private_key: "testkey.pem",
4969 }
4970
4971 platform_compat_config {
4972 name: "myjar-platform-compat-config",
4973 src: ":myjar",
4974 }
4975
4976 java_library {
4977 name: "myjar",
4978 srcs: ["foo/bar/MyClass.java"],
4979 sdk_version: "none",
4980 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00004981 apex_available: [ "myapex" ],
4982 }
4983 `)
4984 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
4985 "etc/compatconfig/myjar-platform-compat-config.xml",
4986 "javalib/myjar.jar",
4987 })
4988}
4989
Jiyong Park479321d2019-12-16 11:47:12 +09004990func TestRejectNonInstallableJavaLibrary(t *testing.T) {
4991 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
4992 apex {
4993 name: "myapex",
4994 key: "myapex.key",
4995 java_libs: ["myjar"],
4996 }
4997
4998 apex_key {
4999 name: "myapex.key",
5000 public_key: "testkey.avbpubkey",
5001 private_key: "testkey.pem",
5002 }
5003
5004 java_library {
5005 name: "myjar",
5006 srcs: ["foo/bar/MyClass.java"],
5007 sdk_version: "none",
5008 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005009 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005010 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005011 }
5012 `)
5013}
5014
Jiyong Park7afd1072019-12-30 16:56:33 +09005015func TestCarryRequiredModuleNames(t *testing.T) {
5016 ctx, config := testApex(t, `
5017 apex {
5018 name: "myapex",
5019 key: "myapex.key",
5020 native_shared_libs: ["mylib"],
5021 }
5022
5023 apex_key {
5024 name: "myapex.key",
5025 public_key: "testkey.avbpubkey",
5026 private_key: "testkey.pem",
5027 }
5028
5029 cc_library {
5030 name: "mylib",
5031 srcs: ["mylib.cpp"],
5032 system_shared_libs: [],
5033 stl: "none",
5034 required: ["a", "b"],
5035 host_required: ["c", "d"],
5036 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005037 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005038 }
5039 `)
5040
5041 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
5042 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
5043 name := apexBundle.BaseModuleName()
5044 prefix := "TARGET_"
5045 var builder strings.Builder
5046 data.Custom(&builder, name, prefix, "", data)
5047 androidMk := builder.String()
5048 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
5049 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
5050 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
5051}
5052
Jiyong Park7cd10e32020-01-14 09:22:18 +09005053func TestSymlinksFromApexToSystem(t *testing.T) {
5054 bp := `
5055 apex {
5056 name: "myapex",
5057 key: "myapex.key",
5058 native_shared_libs: ["mylib"],
5059 java_libs: ["myjar"],
5060 }
5061
Jiyong Park9d677202020-02-19 16:29:35 +09005062 apex {
5063 name: "myapex.updatable",
5064 key: "myapex.key",
5065 native_shared_libs: ["mylib"],
5066 java_libs: ["myjar"],
5067 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09005068 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09005069 }
5070
Jiyong Park7cd10e32020-01-14 09:22:18 +09005071 apex_key {
5072 name: "myapex.key",
5073 public_key: "testkey.avbpubkey",
5074 private_key: "testkey.pem",
5075 }
5076
5077 cc_library {
5078 name: "mylib",
5079 srcs: ["mylib.cpp"],
5080 shared_libs: ["myotherlib"],
5081 system_shared_libs: [],
5082 stl: "none",
5083 apex_available: [
5084 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005085 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005086 "//apex_available:platform",
5087 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005088 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005089 }
5090
5091 cc_library {
5092 name: "myotherlib",
5093 srcs: ["mylib.cpp"],
5094 system_shared_libs: [],
5095 stl: "none",
5096 apex_available: [
5097 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005098 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005099 "//apex_available:platform",
5100 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005101 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005102 }
5103
5104 java_library {
5105 name: "myjar",
5106 srcs: ["foo/bar/MyClass.java"],
5107 sdk_version: "none",
5108 system_modules: "none",
5109 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09005110 apex_available: [
5111 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005112 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005113 "//apex_available:platform",
5114 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005115 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005116 }
5117
5118 java_library {
5119 name: "myotherjar",
5120 srcs: ["foo/bar/MyClass.java"],
5121 sdk_version: "none",
5122 system_modules: "none",
5123 apex_available: [
5124 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09005125 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005126 "//apex_available:platform",
5127 ],
Jooyung Han749dc692020-04-15 11:03:39 +09005128 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09005129 }
5130 `
5131
5132 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
5133 for _, f := range files {
5134 if f.path == file {
5135 if f.isLink {
5136 t.Errorf("%q is not a real file", file)
5137 }
5138 return
5139 }
5140 }
5141 t.Errorf("%q is not found", file)
5142 }
5143
5144 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
5145 for _, f := range files {
5146 if f.path == file {
5147 if !f.isLink {
5148 t.Errorf("%q is not a symlink", file)
5149 }
5150 return
5151 }
5152 }
5153 t.Errorf("%q is not found", file)
5154 }
5155
Jiyong Park9d677202020-02-19 16:29:35 +09005156 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
5157 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09005158 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005159 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005160 ensureRealfileExists(t, files, "javalib/myjar.jar")
5161 ensureRealfileExists(t, files, "lib64/mylib.so")
5162 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5163
Jiyong Park9d677202020-02-19 16:29:35 +09005164 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5165 ensureRealfileExists(t, files, "javalib/myjar.jar")
5166 ensureRealfileExists(t, files, "lib64/mylib.so")
5167 ensureRealfileExists(t, files, "lib64/myotherlib.so")
5168
5169 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09005170 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00005171 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09005172 ensureRealfileExists(t, files, "javalib/myjar.jar")
5173 ensureRealfileExists(t, files, "lib64/mylib.so")
5174 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09005175
5176 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
5177 ensureRealfileExists(t, files, "javalib/myjar.jar")
5178 ensureRealfileExists(t, files, "lib64/mylib.so")
5179 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09005180}
5181
Jooyung Han643adc42020-02-27 13:50:06 +09005182func TestApexWithJniLibs(t *testing.T) {
5183 ctx, _ := testApex(t, `
5184 apex {
5185 name: "myapex",
5186 key: "myapex.key",
5187 jni_libs: ["mylib"],
5188 }
5189
5190 apex_key {
5191 name: "myapex.key",
5192 public_key: "testkey.avbpubkey",
5193 private_key: "testkey.pem",
5194 }
5195
5196 cc_library {
5197 name: "mylib",
5198 srcs: ["mylib.cpp"],
5199 shared_libs: ["mylib2"],
5200 system_shared_libs: [],
5201 stl: "none",
5202 apex_available: [ "myapex" ],
5203 }
5204
5205 cc_library {
5206 name: "mylib2",
5207 srcs: ["mylib.cpp"],
5208 system_shared_libs: [],
5209 stl: "none",
5210 apex_available: [ "myapex" ],
5211 }
5212 `)
5213
5214 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
5215 // Notice mylib2.so (transitive dep) is not added as a jni_lib
5216 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
5217 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5218 "lib64/mylib.so",
5219 "lib64/mylib2.so",
5220 })
5221}
5222
Jooyung Han49f67012020-04-17 13:43:10 +09005223func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
5224 ctx, _ := testApex(t, `
5225 apex {
5226 name: "myapex",
5227 key: "myapex.key",
5228 }
5229 apex_key {
5230 name: "myapex.key",
5231 public_key: "testkey.avbpubkey",
5232 private_key: "testkey.pem",
5233 }
5234 `, func(fs map[string][]byte, config android.Config) {
5235 delete(config.Targets, android.Android)
5236 config.AndroidCommonTarget = android.Target{}
5237 })
5238
5239 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
5240 t.Errorf("Expected variants: %v, but got: %v", expected, got)
5241 }
5242}
5243
Jooyung Han643adc42020-02-27 13:50:06 +09005244func TestApexWithJniLibs_Errors(t *testing.T) {
5245 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
5246 apex {
5247 name: "myapex",
5248 key: "myapex.key",
5249 jni_libs: ["xxx"],
5250 }
5251
5252 apex_key {
5253 name: "myapex.key",
5254 public_key: "testkey.avbpubkey",
5255 private_key: "testkey.pem",
5256 }
5257
5258 prebuilt_etc {
5259 name: "xxx",
5260 src: "xxx",
5261 }
5262 `, withFiles(map[string][]byte{
5263 "xxx": nil,
5264 }))
5265}
5266
Jiyong Parkbd159612020-02-28 15:22:21 +09005267func TestAppBundle(t *testing.T) {
5268 ctx, _ := testApex(t, `
5269 apex {
5270 name: "myapex",
5271 key: "myapex.key",
5272 apps: ["AppFoo"],
5273 }
5274
5275 apex_key {
5276 name: "myapex.key",
5277 public_key: "testkey.avbpubkey",
5278 private_key: "testkey.pem",
5279 }
5280
5281 android_app {
5282 name: "AppFoo",
5283 srcs: ["foo/bar/MyClass.java"],
5284 sdk_version: "none",
5285 system_modules: "none",
5286 apex_available: [ "myapex" ],
5287 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09005288 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09005289
5290 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
5291 content := bundleConfigRule.Args["content"]
5292
5293 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09005294 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 +09005295}
5296
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005297func TestAppSetBundle(t *testing.T) {
5298 ctx, _ := testApex(t, `
5299 apex {
5300 name: "myapex",
5301 key: "myapex.key",
5302 apps: ["AppSet"],
5303 }
5304
5305 apex_key {
5306 name: "myapex.key",
5307 public_key: "testkey.avbpubkey",
5308 private_key: "testkey.pem",
5309 }
5310
5311 android_app_set {
5312 name: "AppSet",
5313 set: "AppSet.apks",
5314 }`)
5315 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5316 bundleConfigRule := mod.Description("Bundle Config")
5317 content := bundleConfigRule.Args["content"]
5318 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
5319 s := mod.Rule("apexRule").Args["copy_commands"]
5320 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
5321 if len(copyCmds) != 3 {
5322 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
5323 }
5324 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
5325 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
5326 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
5327}
5328
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005329func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005330 t.Helper()
5331
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005332 bp := `
5333 java_library {
5334 name: "some-updatable-apex-lib",
5335 srcs: ["a.java"],
5336 sdk_version: "current",
5337 apex_available: [
5338 "some-updatable-apex",
5339 ],
5340 }
5341
5342 java_library {
5343 name: "some-non-updatable-apex-lib",
5344 srcs: ["a.java"],
5345 apex_available: [
5346 "some-non-updatable-apex",
5347 ],
5348 }
5349
5350 java_library {
5351 name: "some-platform-lib",
5352 srcs: ["a.java"],
5353 sdk_version: "current",
5354 installable: true,
5355 }
5356
5357 java_library {
5358 name: "some-art-lib",
5359 srcs: ["a.java"],
5360 sdk_version: "current",
5361 apex_available: [
5362 "com.android.art.something",
5363 ],
5364 hostdex: true,
5365 }
5366
5367 apex {
5368 name: "some-updatable-apex",
5369 key: "some-updatable-apex.key",
5370 java_libs: ["some-updatable-apex-lib"],
5371 updatable: true,
5372 min_sdk_version: "current",
5373 }
5374
5375 apex {
5376 name: "some-non-updatable-apex",
5377 key: "some-non-updatable-apex.key",
5378 java_libs: ["some-non-updatable-apex-lib"],
5379 }
5380
5381 apex_key {
5382 name: "some-updatable-apex.key",
5383 }
5384
5385 apex_key {
5386 name: "some-non-updatable-apex.key",
5387 }
5388
5389 apex {
5390 name: "com.android.art.something",
5391 key: "com.android.art.something.key",
5392 java_libs: ["some-art-lib"],
5393 updatable: true,
5394 min_sdk_version: "current",
5395 }
5396
5397 apex_key {
5398 name: "com.android.art.something.key",
5399 }
5400
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005401 filegroup {
5402 name: "some-updatable-apex-file_contexts",
5403 srcs: [
5404 "system/sepolicy/apex/some-updatable-apex-file_contexts",
5405 ],
5406 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005407
5408 filegroup {
5409 name: "some-non-updatable-apex-file_contexts",
5410 srcs: [
5411 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
5412 ],
5413 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005414 `
5415 bp += cc.GatherRequiredDepsForTest(android.Android)
5416 bp += java.GatherRequiredDepsForTest()
5417 bp += dexpreopt.BpToolModulesForTest()
5418
5419 fs := map[string][]byte{
5420 "a.java": nil,
5421 "a.jar": nil,
5422 "build/make/target/product/security": nil,
5423 "apex_manifest.json": nil,
5424 "AndroidManifest.xml": nil,
5425 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005426 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005427 "system/sepolicy/apex/com.android.art.something-file_contexts": nil,
5428 "framework/aidl/a.aidl": nil,
5429 }
5430 cc.GatherRequiredFilesForTest(fs)
5431
5432 ctx := android.NewTestArchContext()
5433 ctx.RegisterModuleType("apex", BundleFactory)
5434 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
5435 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01005436 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005437 cc.RegisterRequiredBuildComponentsForTest(ctx)
5438 java.RegisterJavaBuildComponents(ctx)
5439 java.RegisterSystemModulesBuildComponents(ctx)
5440 java.RegisterAppBuildComponents(ctx)
5441 java.RegisterDexpreoptBootJarsComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005442 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
5443 ctx.PreDepsMutators(RegisterPreDepsMutators)
5444 ctx.PostDepsMutators(RegisterPostDepsMutators)
5445
5446 config := android.TestArchConfig(buildDir, nil, bp, fs)
5447 ctx.Register(config)
5448
5449 _ = dexpreopt.GlobalSoongConfigForTests(config)
5450 dexpreopt.RegisterToolModulesForTest(ctx)
5451 pathCtx := android.PathContextForTesting(config)
5452 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
5453 transformDexpreoptConfig(dexpreoptConfig)
5454 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
5455
5456 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
5457 android.FailIfErrored(t, errs)
5458
5459 _, errs = ctx.PrepareBuildActions(config)
5460 if errmsg == "" {
5461 android.FailIfErrored(t, errs)
5462 } else if len(errs) > 0 {
5463 android.FailIfNoMatchingErrors(t, errmsg, errs)
5464 return
5465 } else {
5466 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
5467 }
5468}
5469
Jooyung Han548640b2020-04-27 12:10:30 +09005470func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
5471 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
5472 apex {
5473 name: "myapex",
5474 key: "myapex.key",
5475 updatable: true,
5476 }
5477
5478 apex_key {
5479 name: "myapex.key",
5480 public_key: "testkey.avbpubkey",
5481 private_key: "testkey.pem",
5482 }
5483 `)
5484}
5485
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005486func TestNoUpdatableJarsInBootImage(t *testing.T) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005487
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005488 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005489 var transform func(*dexpreopt.GlobalConfig)
5490
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005491 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
5492 transform = func(config *dexpreopt.GlobalConfig) {
5493 config.ArtApexJars = []string{"com.android.art.something:some-art-lib"}
5494 }
5495 testNoUpdatableJarsInBootImage(t, "", transform)
5496 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005497
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005498 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005499 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 +01005500 transform = func(config *dexpreopt.GlobalConfig) {
5501 config.BootJars = []string{"com.android.art.something:some-art-lib"}
5502 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005503 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005504 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005505
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005506 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 -07005507 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 +01005508 transform = func(config *dexpreopt.GlobalConfig) {
5509 config.ArtApexJars = []string{"some-updatable-apex:some-updatable-apex-lib"}
5510 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005511 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005512 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005513
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005514 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 -07005515 err = "module 'some-non-updatable-apex-lib' is not allowed in the ART boot image"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005516 transform = func(config *dexpreopt.GlobalConfig) {
5517 config.ArtApexJars = []string{"some-non-updatable-apex:some-non-updatable-apex-lib"}
5518 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005519 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005520 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005521
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005522 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 -07005523 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 +01005524 transform = func(config *dexpreopt.GlobalConfig) {
5525 config.BootJars = []string{"some-updatable-apex:some-updatable-apex-lib"}
5526 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005527 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005528 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005529
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005530 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
5531 transform = func(config *dexpreopt.GlobalConfig) {
5532 config.BootJars = []string{"some-non-updatable-apex:some-non-updatable-apex-lib"}
5533 }
5534 testNoUpdatableJarsInBootImage(t, "", transform)
5535 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01005536
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005537 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005538 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005539 transform = func(config *dexpreopt.GlobalConfig) {
5540 config.ArtApexJars = []string{"platform:nonexistent"}
5541 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005542 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005543 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005544
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005545 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005546 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005547 transform = func(config *dexpreopt.GlobalConfig) {
5548 config.BootJars = []string{"platform:nonexistent"}
5549 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005550 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005551 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005552
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005553 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005554 err = "module 'some-platform-lib' is not allowed in the ART boot image"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005555 transform = func(config *dexpreopt.GlobalConfig) {
5556 config.ArtApexJars = []string{"platform:some-platform-lib"}
5557 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07005558 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005559 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005560
Ulya Trafimovich7caef202020-05-19 12:00:52 +01005561 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
5562 transform = func(config *dexpreopt.GlobalConfig) {
5563 config.BootJars = []string{"platform:some-platform-lib"}
5564 }
5565 testNoUpdatableJarsInBootImage(t, "", transform)
5566 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00005567}
5568
Jiyong Park62304bb2020-04-13 16:19:48 +09005569func TestTestFor(t *testing.T) {
5570 ctx, _ := testApex(t, `
5571 apex {
5572 name: "myapex",
5573 key: "myapex.key",
5574 native_shared_libs: ["mylib", "myprivlib"],
5575 }
5576
5577 apex_key {
5578 name: "myapex.key",
5579 public_key: "testkey.avbpubkey",
5580 private_key: "testkey.pem",
5581 }
5582
5583 cc_library {
5584 name: "mylib",
5585 srcs: ["mylib.cpp"],
5586 system_shared_libs: [],
5587 stl: "none",
5588 stubs: {
5589 versions: ["1"],
5590 },
5591 apex_available: ["myapex"],
5592 }
5593
5594 cc_library {
5595 name: "myprivlib",
5596 srcs: ["mylib.cpp"],
5597 system_shared_libs: [],
5598 stl: "none",
5599 apex_available: ["myapex"],
5600 }
5601
5602
5603 cc_test {
5604 name: "mytest",
5605 gtest: false,
5606 srcs: ["mylib.cpp"],
5607 system_shared_libs: [],
5608 stl: "none",
5609 shared_libs: ["mylib", "myprivlib"],
5610 test_for: ["myapex"]
5611 }
5612 `)
5613
5614 // the test 'mytest' is a test for the apex, therefore is linked to the
5615 // actual implementation of mylib instead of its stub.
5616 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
5617 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
5618 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
5619}
5620
Jaewoong Jungfa00c062020-05-14 14:15:24 -07005621// TODO(jungjw): Move this to proptools
5622func intPtr(i int) *int {
5623 return &i
5624}
5625
5626func TestApexSet(t *testing.T) {
5627 ctx, config := testApex(t, `
5628 apex_set {
5629 name: "myapex",
5630 set: "myapex.apks",
5631 filename: "foo_v2.apex",
5632 overrides: ["foo"],
5633 }
5634 `, func(fs map[string][]byte, config android.Config) {
5635 config.TestProductVariables.Platform_sdk_version = intPtr(30)
5636 config.TestProductVariables.DeviceArch = proptools.StringPtr("arm")
5637 config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm64")
5638 })
5639
5640 m := ctx.ModuleForTests("myapex", "android_common")
5641
5642 // Check extract_apks tool parameters.
5643 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
5644 actual := extractedApex.Args["abis"]
5645 expected := "ARMEABI_V7A,ARM64_V8A"
5646 if actual != expected {
5647 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
5648 }
5649 actual = extractedApex.Args["sdk-version"]
5650 expected = "30"
5651 if actual != expected {
5652 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
5653 }
5654
5655 a := m.Module().(*ApexSet)
5656 expectedOverrides := []string{"foo"}
5657 actualOverrides := android.AndroidMkEntriesForTest(t, config, "", a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
5658 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
5659 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
5660 }
5661}
5662
Jiyong Park7d95a512020-05-10 15:16:24 +09005663func TestNoStaticLinkingToStubsLib(t *testing.T) {
5664 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
5665 apex {
5666 name: "myapex",
5667 key: "myapex.key",
5668 native_shared_libs: ["mylib"],
5669 }
5670
5671 apex_key {
5672 name: "myapex.key",
5673 public_key: "testkey.avbpubkey",
5674 private_key: "testkey.pem",
5675 }
5676
5677 cc_library {
5678 name: "mylib",
5679 srcs: ["mylib.cpp"],
5680 static_libs: ["otherlib"],
5681 system_shared_libs: [],
5682 stl: "none",
5683 apex_available: [ "myapex" ],
5684 }
5685
5686 cc_library {
5687 name: "otherlib",
5688 srcs: ["mylib.cpp"],
5689 system_shared_libs: [],
5690 stl: "none",
5691 stubs: {
5692 versions: ["1", "2", "3"],
5693 },
5694 apex_available: [ "myapex" ],
5695 }
5696 `)
5697}
5698
Jiyong Park8d6c51e2020-06-12 17:26:31 +09005699func TestApexKeysTxt(t *testing.T) {
5700 ctx, _ := testApex(t, `
5701 apex {
5702 name: "myapex",
5703 key: "myapex.key",
5704 }
5705
5706 apex_key {
5707 name: "myapex.key",
5708 public_key: "testkey.avbpubkey",
5709 private_key: "testkey.pem",
5710 }
5711
5712 prebuilt_apex {
5713 name: "myapex",
5714 prefer: true,
5715 arch: {
5716 arm64: {
5717 src: "myapex-arm64.apex",
5718 },
5719 arm: {
5720 src: "myapex-arm.apex",
5721 },
5722 },
5723 }
5724
5725 apex_set {
5726 name: "myapex_set",
5727 set: "myapex.apks",
5728 filename: "myapex_set.apex",
5729 overrides: ["myapex"],
5730 }
5731 `)
5732
5733 apexKeysText := ctx.SingletonForTests("apex_keys_text")
5734 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
5735 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 +09005736 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 +09005737}
5738
Jooyung Han938b5932020-06-20 12:47:47 +09005739func TestAllowedFiles(t *testing.T) {
5740 ctx, _ := testApex(t, `
5741 apex {
5742 name: "myapex",
5743 key: "myapex.key",
5744 apps: ["app"],
5745 allowed_files: "allowed.txt",
5746 }
5747
5748 apex_key {
5749 name: "myapex.key",
5750 public_key: "testkey.avbpubkey",
5751 private_key: "testkey.pem",
5752 }
5753
5754 android_app {
5755 name: "app",
5756 srcs: ["foo/bar/MyClass.java"],
5757 package_name: "foo",
5758 sdk_version: "none",
5759 system_modules: "none",
5760 apex_available: [ "myapex" ],
5761 }
5762 `, withFiles(map[string][]byte{
5763 "sub/Android.bp": []byte(`
5764 override_apex {
5765 name: "override_myapex",
5766 base: "myapex",
5767 apps: ["override_app"],
5768 allowed_files: ":allowed",
5769 }
5770 // Overridable "path" property should be referenced indirectly
5771 filegroup {
5772 name: "allowed",
5773 srcs: ["allowed.txt"],
5774 }
5775 override_android_app {
5776 name: "override_app",
5777 base: "app",
5778 package_name: "bar",
5779 }
5780 `),
5781 }))
5782
5783 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
5784 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
5785 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
5786 }
5787
5788 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
5789 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
5790 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
5791 }
5792}
5793
Martin Stjernholm58c33f02020-07-06 22:56:01 +01005794func TestNonPreferredPrebuiltDependency(t *testing.T) {
5795 _, _ = testApex(t, `
5796 apex {
5797 name: "myapex",
5798 key: "myapex.key",
5799 native_shared_libs: ["mylib"],
5800 }
5801
5802 apex_key {
5803 name: "myapex.key",
5804 public_key: "testkey.avbpubkey",
5805 private_key: "testkey.pem",
5806 }
5807
5808 cc_library {
5809 name: "mylib",
5810 srcs: ["mylib.cpp"],
5811 stubs: {
5812 versions: ["10000"],
5813 },
5814 apex_available: ["myapex"],
5815 }
5816
5817 cc_prebuilt_library_shared {
5818 name: "mylib",
5819 prefer: false,
5820 srcs: ["prebuilt.so"],
5821 stubs: {
5822 versions: ["10000"],
5823 },
5824 apex_available: ["myapex"],
5825 }
5826 `)
5827}
5828
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07005829func TestMain(m *testing.M) {
5830 run := func() int {
5831 setUp()
5832 defer tearDown()
5833
5834 return m.Run()
5835 }
5836
5837 os.Exit(run())
5838}