blob: f6ceab0caa9d425f65e05c62e26cf0b8b3ff0d6f [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Paul Duffin37856732021-02-26 14:24:15 +000021 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070022 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010023 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090024 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090025 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090026 "strings"
27 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090028
29 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Paul Duffin810f33d2021-03-09 14:12:32 +0000116var withBinder32bit = android.FixtureModifyProductVariables(
117 func(variables android.FixtureProductVariables) {
118 variables.Binder32bit = proptools.BoolPtr(true)
119 },
120)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withUnbundledBuild = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Unbundled_build = proptools.BoolPtr(true)
125 },
126)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900127
Paul Duffin284165a2021-03-29 01:50:31 +0100128// Legacy preparer used for running tests within the apex package.
129//
130// This includes everything that was needed to run any test in the apex package prior to the
131// introduction of the test fixtures. Tests that are being converted to use fixtures directly
132// rather than through the testApex...() methods should avoid using this and instead use the
133// various preparers directly, using android.GroupFixturePreparers(...) to group them when
134// necessary.
135//
136// deprecated
137var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000138 // General preparers in alphabetical order as test infrastructure will enforce correct
139 // registration order.
140 android.PrepareForTestWithAndroidBuildComponents,
141 bpf.PrepareForTestWithBpf,
142 cc.PrepareForTestWithCcBuildComponents,
143 java.PrepareForTestWithJavaDefaultModules,
144 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
145 rust.PrepareForTestWithRustDefaultModules,
146 sh.PrepareForTestWithShBuildComponents,
147
148 PrepareForTestWithApexBuildComponents,
149
150 // Additional apex test specific preparers.
151 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
152 filegroup {
153 name: "myapex-file_contexts",
154 srcs: [
155 "apex/myapex-file_contexts",
156 ],
157 }
158 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000159 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000160 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000161 "a.java": nil,
162 "PrebuiltAppFoo.apk": nil,
163 "PrebuiltAppFooPriv.apk": nil,
164 "apex_manifest.json": nil,
165 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
167 "system/sepolicy/apex/myapex2-file_contexts": nil,
168 "system/sepolicy/apex/otherapex-file_contexts": nil,
169 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
170 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
171 "mylib.cpp": nil,
172 "mytest.cpp": nil,
173 "mytest1.cpp": nil,
174 "mytest2.cpp": nil,
175 "mytest3.cpp": nil,
176 "myprebuilt": nil,
177 "my_include": nil,
178 "foo/bar/MyClass.java": nil,
179 "prebuilt.jar": nil,
180 "prebuilt.so": nil,
181 "vendor/foo/devkeys/test.x509.pem": nil,
182 "vendor/foo/devkeys/test.pk8": nil,
183 "testkey.x509.pem": nil,
184 "testkey.pk8": nil,
185 "testkey.override.x509.pem": nil,
186 "testkey.override.pk8": nil,
187 "vendor/foo/devkeys/testkey.avbpubkey": nil,
188 "vendor/foo/devkeys/testkey.pem": nil,
189 "NOTICE": nil,
190 "custom_notice": nil,
191 "custom_notice_for_static_lib": nil,
192 "testkey2.avbpubkey": nil,
193 "testkey2.pem": nil,
194 "myapex-arm64.apex": nil,
195 "myapex-arm.apex": nil,
196 "myapex.apks": nil,
197 "frameworks/base/api/current.txt": nil,
198 "framework/aidl/a.aidl": nil,
199 "build/make/core/proguard.flags": nil,
200 "build/make/core/proguard_basic_keeps.flags": nil,
201 "dummy.txt": nil,
202 "baz": nil,
203 "bar/baz": nil,
204 "testdata/baz": nil,
205 "AppSet.apks": nil,
206 "foo.rs": nil,
207 "libfoo.jar": nil,
208 "libbar.jar": nil,
209 },
210 ),
211
212 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
213 variables.DeviceVndkVersion = proptools.StringPtr("current")
214 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
215 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
216 variables.Platform_sdk_codename = proptools.StringPtr("Q")
217 variables.Platform_sdk_final = proptools.BoolPtr(false)
218 variables.Platform_version_active_codenames = []string{"Q"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900219 variables.Platform_vndk_version = proptools.StringPtr("29")
Paul Duffin37aad602021-03-08 09:47:16 +0000220 }),
221)
222
Paul Duffin52bfaa42021-03-23 23:40:12 +0000223var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
224 "system/sepolicy/apex/myapex-file_contexts": nil,
225})
226
Jooyung Han643adc42020-02-27 13:50:06 +0900227// ensure that 'result' equals 'expected'
228func ensureEquals(t *testing.T, result string, expected string) {
229 t.Helper()
230 if result != expected {
231 t.Errorf("%q != %q", expected, result)
232 }
233}
234
Jiyong Park25fc6a92018-11-18 18:02:45 +0900235// ensure that 'result' contains 'expected'
236func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900237 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238 if !strings.Contains(result, expected) {
239 t.Errorf("%q is not found in %q", expected, result)
240 }
241}
242
Liz Kammer5bd365f2020-05-27 15:15:11 -0700243// ensure that 'result' contains 'expected' exactly one time
244func ensureContainsOnce(t *testing.T, result string, expected string) {
245 t.Helper()
246 count := strings.Count(result, expected)
247 if count != 1 {
248 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
249 }
250}
251
Jiyong Park25fc6a92018-11-18 18:02:45 +0900252// ensures that 'result' does not contain 'notExpected'
253func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900254 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 if strings.Contains(result, notExpected) {
256 t.Errorf("%q is found in %q", notExpected, result)
257 }
258}
259
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700260func ensureMatches(t *testing.T, result string, expectedRex string) {
261 ok, err := regexp.MatchString(expectedRex, result)
262 if err != nil {
263 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
264 return
265 }
266 if !ok {
267 t.Errorf("%s does not match regular expession %s", result, expectedRex)
268 }
269}
270
Jiyong Park25fc6a92018-11-18 18:02:45 +0900271func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900272 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 if !android.InList(expected, result) {
274 t.Errorf("%q is not found in %v", expected, result)
275 }
276}
277
278func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if android.InList(notExpected, result) {
281 t.Errorf("%q is found in %v", notExpected, result)
282 }
283}
284
Jooyung Hane1633032019-08-01 17:41:43 +0900285func ensureListEmpty(t *testing.T, result []string) {
286 t.Helper()
287 if len(result) > 0 {
288 t.Errorf("%q is expected to be empty", result)
289 }
290}
291
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000292func ensureListNotEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) == 0 {
295 t.Errorf("%q is expected to be not empty", result)
296 }
297}
298
Jiyong Park25fc6a92018-11-18 18:02:45 +0900299// Minimal test
300func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800301 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900302 apex_defaults {
303 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900304 manifest: ":myapex.manifest",
305 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900307 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900308 native_shared_libs: [
309 "mylib",
310 "libfoo.ffi",
311 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900312 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800313 multilib: {
314 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900315 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800316 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900317 },
Jiyong Park77acec62020-06-01 21:39:15 +0900318 java_libs: [
319 "myjar",
320 "myjar_dex",
321 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000322 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
324
Jiyong Park30ca9372019-02-07 16:27:23 +0900325 apex {
326 name: "myapex",
327 defaults: ["myapex-defaults"],
328 }
329
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 apex_key {
331 name: "myapex.key",
332 public_key: "testkey.avbpubkey",
333 private_key: "testkey.pem",
334 }
335
Jiyong Park809bb722019-02-13 21:33:49 +0900336 filegroup {
337 name: "myapex.manifest",
338 srcs: ["apex_manifest.json"],
339 }
340
341 filegroup {
342 name: "myapex.androidmanifest",
343 srcs: ["AndroidManifest.xml"],
344 }
345
Jiyong Park25fc6a92018-11-18 18:02:45 +0900346 cc_library {
347 name: "mylib",
348 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900349 shared_libs: [
350 "mylib2",
351 "libbar.ffi",
352 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 system_shared_libs: [],
354 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000355 // TODO: remove //apex_available:platform
356 apex_available: [
357 "//apex_available:platform",
358 "myapex",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 }
361
Alex Light3d673592019-01-18 14:37:31 -0800362 cc_binary {
363 name: "foo",
364 srcs: ["mylib.cpp"],
365 compile_multilib: "both",
366 multilib: {
367 lib32: {
368 suffix: "32",
369 },
370 lib64: {
371 suffix: "64",
372 },
373 },
374 symlinks: ["foo_link_"],
375 symlink_preferred_arch: true,
376 system_shared_libs: [],
377 static_executable: true,
378 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700379 apex_available: [ "myapex", "com.android.gki.*" ],
380 }
381
Jiyong Park99644e92020-11-17 22:21:02 +0900382 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000383 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900384 srcs: ["foo.rs"],
385 rlibs: ["libfoo.rlib.rust"],
386 dylibs: ["libfoo.dylib.rust"],
387 apex_available: ["myapex"],
388 }
389
390 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000391 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900392 srcs: ["foo.rs"],
393 crate_name: "foo",
394 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900395 shared_libs: ["libfoo.shared_from_rust"],
396 }
397
398 cc_library_shared {
399 name: "libfoo.shared_from_rust",
400 srcs: ["mylib.cpp"],
401 system_shared_libs: [],
402 stl: "none",
403 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900404 }
405
406 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000407 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900408 srcs: ["foo.rs"],
409 crate_name: "foo",
410 apex_available: ["myapex"],
411 }
412
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900413 rust_ffi_shared {
414 name: "libfoo.ffi",
415 srcs: ["foo.rs"],
416 crate_name: "foo",
417 apex_available: ["myapex"],
418 }
419
420 rust_ffi_shared {
421 name: "libbar.ffi",
422 srcs: ["foo.rs"],
423 crate_name: "bar",
424 apex_available: ["myapex"],
425 }
426
Yifan Hongd22a84a2020-07-28 17:37:46 -0700427 apex {
428 name: "com.android.gki.fake",
429 binaries: ["foo"],
430 key: "myapex.key",
431 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000432 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800433 }
434
Paul Duffindddd5462020-04-07 15:25:44 +0100435 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900436 name: "mylib2",
437 srcs: ["mylib.cpp"],
438 system_shared_libs: [],
439 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900440 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900441 static_libs: ["libstatic"],
442 // TODO: remove //apex_available:platform
443 apex_available: [
444 "//apex_available:platform",
445 "myapex",
446 ],
447 }
448
Paul Duffindddd5462020-04-07 15:25:44 +0100449 cc_prebuilt_library_shared {
450 name: "mylib2",
451 srcs: ["prebuilt.so"],
452 // TODO: remove //apex_available:platform
453 apex_available: [
454 "//apex_available:platform",
455 "myapex",
456 ],
457 }
458
Jiyong Park9918e1a2020-03-17 19:16:40 +0900459 cc_library_static {
460 name: "libstatic",
461 srcs: ["mylib.cpp"],
462 system_shared_libs: [],
463 stl: "none",
464 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000465 // TODO: remove //apex_available:platform
466 apex_available: [
467 "//apex_available:platform",
468 "myapex",
469 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900470 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900471
472 java_library {
473 name: "myjar",
474 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900475 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900476 sdk_version: "none",
477 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900478 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900479 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000480 // TODO: remove //apex_available:platform
481 apex_available: [
482 "//apex_available:platform",
483 "myapex",
484 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900485 }
486
Jiyong Park77acec62020-06-01 21:39:15 +0900487 dex_import {
488 name: "myjar_dex",
489 jars: ["prebuilt.jar"],
490 apex_available: [
491 "//apex_available:platform",
492 "myapex",
493 ],
494 }
495
Jiyong Park7f7766d2019-07-25 22:02:35 +0900496 java_library {
497 name: "myotherjar",
498 srcs: ["foo/bar/MyClass.java"],
499 sdk_version: "none",
500 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900501 // TODO: remove //apex_available:platform
502 apex_available: [
503 "//apex_available:platform",
504 "myapex",
505 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900506 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900507
508 java_library {
509 name: "mysharedjar",
510 srcs: ["foo/bar/MyClass.java"],
511 sdk_version: "none",
512 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900513 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900514 `)
515
Paul Duffina71a67a2021-03-29 00:42:57 +0100516 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900517
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900518 // Make sure that Android.mk is created
519 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700520 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900521 var builder strings.Builder
522 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
523
524 androidMk := builder.String()
525 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
526 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
527
Jiyong Park42cca6c2019-04-01 11:15:50 +0900528 optFlags := apexRule.Args["opt_flags"]
529 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700530 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100531 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532
Jiyong Park25fc6a92018-11-18 18:02:45 +0900533 copyCmds := apexRule.Args["copy_commands"]
534
535 // Ensure that main rule creates an output
536 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
537
538 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700539 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
540 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
541 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900542 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900543 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900544
545 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700546 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900548 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
549 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900551 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900552
553 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800554 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
555 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900556 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900557 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900558 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900559 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
560 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900561 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900562 // .. but not for java libs
563 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900564 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800565
Colin Cross7113d202019-11-20 16:39:12 -0800566 // Ensure that the platform variant ends with _shared or _common
567 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
568 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900569 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
570 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900571 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
572
573 // Ensure that dynamic dependency to java libs are not included
574 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800575
576 // Ensure that all symlinks are present.
577 found_foo_link_64 := false
578 found_foo := false
579 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900580 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800581 if strings.HasSuffix(cmd, "bin/foo") {
582 found_foo = true
583 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
584 found_foo_link_64 = true
585 }
586 }
587 }
588 good := found_foo && found_foo_link_64
589 if !good {
590 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
591 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900592
Sundong Ahnabb64432019-10-22 13:58:29 +0900593 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700594 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900595 if len(noticeInputs) != 3 {
596 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900597 }
598 ensureListContains(t, noticeInputs, "NOTICE")
599 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900600 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900601
Artur Satayeva8bd1132020-04-27 18:07:06 +0100602 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100603 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
605 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
606 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100607
608 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100609 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100610 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
611 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
612 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800613}
614
Jooyung Hanf21c7972019-12-16 22:32:06 +0900615func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800616 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900617 apex_defaults {
618 name: "myapex-defaults",
619 key: "myapex.key",
620 prebuilts: ["myetc"],
621 native_shared_libs: ["mylib"],
622 java_libs: ["myjar"],
623 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900624 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800625 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000626 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900627 }
628
629 prebuilt_etc {
630 name: "myetc",
631 src: "myprebuilt",
632 }
633
634 apex {
635 name: "myapex",
636 defaults: ["myapex-defaults"],
637 }
638
639 apex_key {
640 name: "myapex.key",
641 public_key: "testkey.avbpubkey",
642 private_key: "testkey.pem",
643 }
644
645 cc_library {
646 name: "mylib",
647 system_shared_libs: [],
648 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900650 }
651
652 java_library {
653 name: "myjar",
654 srcs: ["foo/bar/MyClass.java"],
655 sdk_version: "none",
656 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000657 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900658 }
659
660 android_app {
661 name: "AppFoo",
662 srcs: ["foo/bar/MyClass.java"],
663 sdk_version: "none",
664 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000665 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900666 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900667
668 runtime_resource_overlay {
669 name: "rro",
670 theme: "blue",
671 }
672
markchien2f59ec92020-09-02 16:23:38 +0800673 bpf {
674 name: "bpf",
675 srcs: ["bpf.c", "bpf2.c"],
676 }
677
Jooyung Hanf21c7972019-12-16 22:32:06 +0900678 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000679 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900680 "etc/myetc",
681 "javalib/myjar.jar",
682 "lib64/mylib.so",
683 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900684 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800685 "etc/bpf/bpf.o",
686 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900687 })
688}
689
Jooyung Han01a3ee22019-11-02 02:52:25 +0900690func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800691 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692 apex {
693 name: "myapex",
694 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000695 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900696 }
697
698 apex_key {
699 name: "myapex.key",
700 public_key: "testkey.avbpubkey",
701 private_key: "testkey.pem",
702 }
703 `)
704
705 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900706 args := module.Rule("apexRule").Args
707 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
708 t.Error("manifest should be apex_manifest.pb, but " + manifest)
709 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900710}
711
Liz Kammer4854a7d2021-05-27 14:28:27 -0400712func TestApexManifestMinSdkVersion(t *testing.T) {
713 ctx := testApex(t, `
714 apex_defaults {
715 name: "my_defaults",
716 key: "myapex.key",
717 product_specific: true,
718 file_contexts: ":my-file-contexts",
719 updatable: false,
720 }
721 apex {
722 name: "myapex_30",
723 min_sdk_version: "30",
724 defaults: ["my_defaults"],
725 }
726
727 apex {
728 name: "myapex_current",
729 min_sdk_version: "current",
730 defaults: ["my_defaults"],
731 }
732
733 apex {
734 name: "myapex_none",
735 defaults: ["my_defaults"],
736 }
737
738 apex_key {
739 name: "myapex.key",
740 public_key: "testkey.avbpubkey",
741 private_key: "testkey.pem",
742 }
743
744 filegroup {
745 name: "my-file-contexts",
746 srcs: ["product_specific_file_contexts"],
747 }
748 `, withFiles(map[string][]byte{
749 "product_specific_file_contexts": nil,
750 }), android.FixtureModifyProductVariables(
751 func(variables android.FixtureProductVariables) {
752 variables.Unbundled_build = proptools.BoolPtr(true)
753 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
754 }), android.FixtureMergeEnv(map[string]string{
755 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
756 }))
757
758 testCases := []struct {
759 module string
760 minSdkVersion string
761 }{
762 {
763 module: "myapex_30",
764 minSdkVersion: "30",
765 },
766 {
767 module: "myapex_current",
768 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
769 },
770 {
771 module: "myapex_none",
772 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
773 },
774 }
775 for _, tc := range testCases {
776 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
777 args := module.Rule("apexRule").Args
778 optFlags := args["opt_flags"]
779 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
780 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
781 }
782 }
783}
784
Alex Light5098a612018-11-29 17:12:15 -0800785func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800786 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800787 apex {
788 name: "myapex",
789 key: "myapex.key",
790 payload_type: "zip",
791 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000792 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800793 }
794
795 apex_key {
796 name: "myapex.key",
797 public_key: "testkey.avbpubkey",
798 private_key: "testkey.pem",
799 }
800
801 cc_library {
802 name: "mylib",
803 srcs: ["mylib.cpp"],
804 shared_libs: ["mylib2"],
805 system_shared_libs: [],
806 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000807 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800808 }
809
810 cc_library {
811 name: "mylib2",
812 srcs: ["mylib.cpp"],
813 system_shared_libs: [],
814 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000815 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800816 }
817 `)
818
Sundong Ahnabb64432019-10-22 13:58:29 +0900819 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800820 copyCmds := zipApexRule.Args["copy_commands"]
821
822 // Ensure that main rule creates an output
823 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
824
825 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700826 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800827
828 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700829 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800830
831 // Ensure that both direct and indirect deps are copied into apex
832 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
833 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900834}
835
836func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800837 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900838 apex {
839 name: "myapex",
840 key: "myapex.key",
841 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park7d55b612021-06-11 17:22:09 +0900842 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000843 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900844 }
845
846 apex_key {
847 name: "myapex.key",
848 public_key: "testkey.avbpubkey",
849 private_key: "testkey.pem",
850 }
851
852 cc_library {
853 name: "mylib",
854 srcs: ["mylib.cpp"],
855 shared_libs: ["mylib2", "mylib3"],
856 system_shared_libs: [],
857 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000858 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900859 }
860
861 cc_library {
862 name: "mylib2",
863 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900864 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900865 system_shared_libs: [],
866 stl: "none",
867 stubs: {
868 versions: ["1", "2", "3"],
869 },
870 }
871
872 cc_library {
873 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900874 srcs: ["mylib.cpp"],
875 shared_libs: ["mylib4"],
876 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900877 stl: "none",
878 stubs: {
879 versions: ["10", "11", "12"],
880 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000881 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900882 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900883
884 cc_library {
885 name: "mylib4",
886 srcs: ["mylib.cpp"],
887 system_shared_libs: [],
888 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000889 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900890 }
Jiyong Park7d55b612021-06-11 17:22:09 +0900891
892 rust_binary {
893 name: "foo.rust",
894 srcs: ["foo.rs"],
895 shared_libs: ["libfoo.shared_from_rust"],
896 prefer_rlib: true,
897 apex_available: ["myapex"],
898 }
899
900 cc_library_shared {
901 name: "libfoo.shared_from_rust",
902 srcs: ["mylib.cpp"],
903 system_shared_libs: [],
904 stl: "none",
905 stubs: {
906 versions: ["10", "11", "12"],
907 },
908 }
909
Jiyong Park25fc6a92018-11-18 18:02:45 +0900910 `)
911
Sundong Ahnabb64432019-10-22 13:58:29 +0900912 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900913 copyCmds := apexRule.Args["copy_commands"]
914
915 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800916 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900917
918 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800919 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900920
921 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800922 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900923
Colin Crossaede88c2020-08-11 12:17:01 -0700924 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925
926 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900927 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900929 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900930
931 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700932 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900933 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700934 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900935
936 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900937 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900938 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900939
940 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700941 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900942
Jooyung Hana57af4a2020-01-23 05:36:59 +0000943 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900944 "lib64/mylib.so",
945 "lib64/mylib3.so",
946 "lib64/mylib4.so",
Jiyong Park7d55b612021-06-11 17:22:09 +0900947 "bin/foo.rust",
948 "lib64/libc++.so", // by the implicit dependency from foo.rust
949 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900950 })
Jiyong Park7d55b612021-06-11 17:22:09 +0900951
952 // Ensure that stub dependency from a rust module is not included
953 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
954 // The rust module is linked to the stub cc library
955 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
956 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
957 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900958}
959
Jiyong Park9477c262021-06-22 20:23:05 +0900960func TestApexCanUsePrivateApis(t *testing.T) {
961 ctx := testApex(t, `
962 apex {
963 name: "myapex",
964 key: "myapex.key",
965 native_shared_libs: ["mylib"],
966 binaries: ["foo.rust"],
967 updatable: false,
968 platform_apis: true,
969 }
970
971 apex_key {
972 name: "myapex.key",
973 public_key: "testkey.avbpubkey",
974 private_key: "testkey.pem",
975 }
976
977 cc_library {
978 name: "mylib",
979 srcs: ["mylib.cpp"],
980 shared_libs: ["mylib2"],
981 system_shared_libs: [],
982 stl: "none",
983 apex_available: [ "myapex" ],
984 }
985
986 cc_library {
987 name: "mylib2",
988 srcs: ["mylib.cpp"],
989 cflags: ["-include mylib.h"],
990 system_shared_libs: [],
991 stl: "none",
992 stubs: {
993 versions: ["1", "2", "3"],
994 },
995 }
996
997 rust_binary {
998 name: "foo.rust",
999 srcs: ["foo.rs"],
1000 shared_libs: ["libfoo.shared_from_rust"],
1001 prefer_rlib: true,
1002 apex_available: ["myapex"],
1003 }
1004
1005 cc_library_shared {
1006 name: "libfoo.shared_from_rust",
1007 srcs: ["mylib.cpp"],
1008 system_shared_libs: [],
1009 stl: "none",
1010 stubs: {
1011 versions: ["10", "11", "12"],
1012 },
1013 }
1014 `)
1015
1016 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1017 copyCmds := apexRule.Args["copy_commands"]
1018
1019 // Ensure that indirect stubs dep is not included
1020 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1021 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1022
1023 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1024 // of the platform_apis: true)
1025 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000_private").Rule("ld").Args["libFlags"]
1026 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1027 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1028 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000_private").Rule("rustc").Args["linkFlags"]
1029 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1030 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1031}
1032
Colin Cross7812fd32020-09-25 12:35:10 -07001033func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1034 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001035 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001036 apex {
1037 name: "myapex",
1038 key: "myapex.key",
1039 native_shared_libs: ["mylib", "mylib3"],
1040 min_sdk_version: "29",
1041 }
1042
1043 apex_key {
1044 name: "myapex.key",
1045 public_key: "testkey.avbpubkey",
1046 private_key: "testkey.pem",
1047 }
1048
1049 cc_library {
1050 name: "mylib",
1051 srcs: ["mylib.cpp"],
1052 shared_libs: ["mylib2", "mylib3"],
1053 system_shared_libs: [],
1054 stl: "none",
1055 apex_available: [ "myapex" ],
1056 min_sdk_version: "28",
1057 }
1058
1059 cc_library {
1060 name: "mylib2",
1061 srcs: ["mylib.cpp"],
1062 cflags: ["-include mylib.h"],
1063 system_shared_libs: [],
1064 stl: "none",
1065 stubs: {
1066 versions: ["28", "29", "30", "current"],
1067 },
1068 min_sdk_version: "28",
1069 }
1070
1071 cc_library {
1072 name: "mylib3",
1073 srcs: ["mylib.cpp"],
1074 shared_libs: ["mylib4"],
1075 system_shared_libs: [],
1076 stl: "none",
1077 stubs: {
1078 versions: ["28", "29", "30", "current"],
1079 },
1080 apex_available: [ "myapex" ],
1081 min_sdk_version: "28",
1082 }
1083
1084 cc_library {
1085 name: "mylib4",
1086 srcs: ["mylib.cpp"],
1087 system_shared_libs: [],
1088 stl: "none",
1089 apex_available: [ "myapex" ],
1090 min_sdk_version: "28",
1091 }
1092 `)
1093
1094 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1095 copyCmds := apexRule.Args["copy_commands"]
1096
1097 // Ensure that direct non-stubs dep is always included
1098 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1099
1100 // Ensure that indirect stubs dep is not included
1101 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1102
1103 // Ensure that direct stubs dep is included
1104 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1105
1106 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1107
Jiyong Park55549df2021-02-26 23:57:23 +09001108 // Ensure that mylib is linking with the latest version of stub for mylib2
1109 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001110 // ... and not linking to the non-stub (impl) variant of mylib2
1111 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1112
1113 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1114 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1115 // .. and not linking to the stubs variant of mylib3
1116 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1117
1118 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001119 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001120 ensureNotContains(t, mylib2Cflags, "-include ")
1121
1122 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -07001123 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -07001124
1125 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1126 "lib64/mylib.so",
1127 "lib64/mylib3.so",
1128 "lib64/mylib4.so",
1129 })
1130}
1131
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001132func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1133 t.Parallel()
1134 // myapex (Z)
1135 // mylib -----------------.
1136 // |
1137 // otherapex (29) |
1138 // libstub's versions: 29 Z current
1139 // |
1140 // <platform> |
1141 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001142 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001143 apex {
1144 name: "myapex",
1145 key: "myapex.key",
1146 native_shared_libs: ["mylib"],
1147 min_sdk_version: "Z", // non-final
1148 }
1149
1150 cc_library {
1151 name: "mylib",
1152 srcs: ["mylib.cpp"],
1153 shared_libs: ["libstub"],
1154 apex_available: ["myapex"],
1155 min_sdk_version: "Z",
1156 }
1157
1158 apex_key {
1159 name: "myapex.key",
1160 public_key: "testkey.avbpubkey",
1161 private_key: "testkey.pem",
1162 }
1163
1164 apex {
1165 name: "otherapex",
1166 key: "myapex.key",
1167 native_shared_libs: ["libstub"],
1168 min_sdk_version: "29",
1169 }
1170
1171 cc_library {
1172 name: "libstub",
1173 srcs: ["mylib.cpp"],
1174 stubs: {
1175 versions: ["29", "Z", "current"],
1176 },
1177 apex_available: ["otherapex"],
1178 min_sdk_version: "29",
1179 }
1180
1181 // platform module depending on libstub from otherapex should use the latest stub("current")
1182 cc_library {
1183 name: "libplatform",
1184 srcs: ["mylib.cpp"],
1185 shared_libs: ["libstub"],
1186 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001187 `,
1188 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1189 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1190 variables.Platform_sdk_final = proptools.BoolPtr(false)
1191 variables.Platform_version_active_codenames = []string{"Z"}
1192 }),
1193 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001194
Jiyong Park55549df2021-02-26 23:57:23 +09001195 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001196 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001197 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001198 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001199 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001200
1201 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1202 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1203 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1204 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1205 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1206}
1207
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001208func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001209 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001210 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001211 name: "myapex2",
1212 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001213 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001214 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001215 }
1216
1217 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001218 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001219 public_key: "testkey.avbpubkey",
1220 private_key: "testkey.pem",
1221 }
1222
1223 cc_library {
1224 name: "mylib",
1225 srcs: ["mylib.cpp"],
1226 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001227 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001228 system_shared_libs: [],
1229 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001230 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001231 }
1232
1233 cc_library {
1234 name: "libfoo",
1235 srcs: ["mylib.cpp"],
1236 shared_libs: ["libbar"],
1237 system_shared_libs: [],
1238 stl: "none",
1239 stubs: {
1240 versions: ["10", "20", "30"],
1241 },
1242 }
1243
1244 cc_library {
1245 name: "libbar",
1246 srcs: ["mylib.cpp"],
1247 system_shared_libs: [],
1248 stl: "none",
1249 }
1250
Jiyong Park678c8812020-02-07 17:25:49 +09001251 cc_library_static {
1252 name: "libbaz",
1253 srcs: ["mylib.cpp"],
1254 system_shared_libs: [],
1255 stl: "none",
1256 apex_available: [ "myapex2" ],
1257 }
1258
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001259 `)
1260
Jiyong Park83dc74b2020-01-14 18:38:44 +09001261 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001262 copyCmds := apexRule.Args["copy_commands"]
1263
1264 // Ensure that direct non-stubs dep is always included
1265 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1266
1267 // Ensure that indirect stubs dep is not included
1268 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1269
1270 // Ensure that dependency of stubs is not included
1271 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1272
Colin Crossaede88c2020-08-11 12:17:01 -07001273 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001274
1275 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001276 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001277 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001278 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001279
Jiyong Park3ff16992019-12-27 14:11:47 +09001280 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001281
1282 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1283 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001284
Artur Satayeva8bd1132020-04-27 18:07:06 +01001285 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001286 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001287
Artur Satayeva8bd1132020-04-27 18:07:06 +01001288 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001289 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001290}
1291
Jooyung Hand3639552019-08-09 12:57:43 +09001292func TestApexWithRuntimeLibsDependency(t *testing.T) {
1293 /*
1294 myapex
1295 |
1296 v (runtime_libs)
1297 mylib ------+------> libfoo [provides stub]
1298 |
1299 `------> libbar
1300 */
Colin Cross1c460562021-02-16 17:55:47 -08001301 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001302 apex {
1303 name: "myapex",
1304 key: "myapex.key",
1305 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001306 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001307 }
1308
1309 apex_key {
1310 name: "myapex.key",
1311 public_key: "testkey.avbpubkey",
1312 private_key: "testkey.pem",
1313 }
1314
1315 cc_library {
1316 name: "mylib",
1317 srcs: ["mylib.cpp"],
1318 runtime_libs: ["libfoo", "libbar"],
1319 system_shared_libs: [],
1320 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001321 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001322 }
1323
1324 cc_library {
1325 name: "libfoo",
1326 srcs: ["mylib.cpp"],
1327 system_shared_libs: [],
1328 stl: "none",
1329 stubs: {
1330 versions: ["10", "20", "30"],
1331 },
1332 }
1333
1334 cc_library {
1335 name: "libbar",
1336 srcs: ["mylib.cpp"],
1337 system_shared_libs: [],
1338 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001339 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001340 }
1341
1342 `)
1343
Sundong Ahnabb64432019-10-22 13:58:29 +09001344 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001345 copyCmds := apexRule.Args["copy_commands"]
1346
1347 // Ensure that direct non-stubs dep is always included
1348 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1349
1350 // Ensure that indirect stubs dep is not included
1351 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1352
1353 // Ensure that runtime_libs dep in included
1354 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1355
Sundong Ahnabb64432019-10-22 13:58:29 +09001356 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001357 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1358 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001359
1360}
1361
Paul Duffina02cae32021-03-09 01:44:06 +00001362var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1363 cc.PrepareForTestWithCcBuildComponents,
1364 PrepareForTestWithApexBuildComponents,
1365 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001366 apex {
1367 name: "com.android.runtime",
1368 key: "com.android.runtime.key",
1369 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001370 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001371 }
1372
1373 apex_key {
1374 name: "com.android.runtime.key",
1375 public_key: "testkey.avbpubkey",
1376 private_key: "testkey.pem",
1377 }
Paul Duffina02cae32021-03-09 01:44:06 +00001378 `),
1379 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1380)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001381
Paul Duffina02cae32021-03-09 01:44:06 +00001382func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001383 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001384 cc_library {
1385 name: "libc",
1386 no_libcrt: true,
1387 nocrt: true,
1388 stl: "none",
1389 system_shared_libs: [],
1390 stubs: { versions: ["1"] },
1391 apex_available: ["com.android.runtime"],
1392
1393 sanitize: {
1394 hwaddress: true,
1395 }
1396 }
1397
1398 cc_prebuilt_library_shared {
1399 name: "libclang_rt.hwasan-aarch64-android",
1400 no_libcrt: true,
1401 nocrt: true,
1402 stl: "none",
1403 system_shared_libs: [],
1404 srcs: [""],
1405 stubs: { versions: ["1"] },
1406
1407 sanitize: {
1408 never: true,
1409 },
Paul Duffina02cae32021-03-09 01:44:06 +00001410 } `)
1411 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001412
1413 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1414 "lib64/bionic/libc.so",
1415 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1416 })
1417
1418 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1419
1420 installed := hwasan.Description("install libclang_rt.hwasan")
1421 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1422
1423 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1424 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1425 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1426}
1427
1428func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001429 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001430 prepareForTestOfRuntimeApexWithHwasan,
1431 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1432 variables.SanitizeDevice = []string{"hwaddress"}
1433 }),
1434 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001435 cc_library {
1436 name: "libc",
1437 no_libcrt: true,
1438 nocrt: true,
1439 stl: "none",
1440 system_shared_libs: [],
1441 stubs: { versions: ["1"] },
1442 apex_available: ["com.android.runtime"],
1443 }
1444
1445 cc_prebuilt_library_shared {
1446 name: "libclang_rt.hwasan-aarch64-android",
1447 no_libcrt: true,
1448 nocrt: true,
1449 stl: "none",
1450 system_shared_libs: [],
1451 srcs: [""],
1452 stubs: { versions: ["1"] },
1453
1454 sanitize: {
1455 never: true,
1456 },
1457 }
Paul Duffina02cae32021-03-09 01:44:06 +00001458 `)
1459 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001460
1461 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1462 "lib64/bionic/libc.so",
1463 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1464 })
1465
1466 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1467
1468 installed := hwasan.Description("install libclang_rt.hwasan")
1469 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1470
1471 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1472 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1473 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1474}
1475
Jooyung Han61b66e92020-03-21 14:21:46 +00001476func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1477 testcases := []struct {
1478 name string
1479 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001480 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001481 shouldLink string
1482 shouldNotLink []string
1483 }{
1484 {
Jiyong Park55549df2021-02-26 23:57:23 +09001485 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001486 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001487 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001488 shouldLink: "current",
1489 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001490 },
1491 {
Jiyong Park55549df2021-02-26 23:57:23 +09001492 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001493 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001494 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001495 shouldLink: "current",
1496 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001497 },
1498 }
1499 for _, tc := range testcases {
1500 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001501 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001502 apex {
1503 name: "myapex",
1504 key: "myapex.key",
1505 use_vendor: true,
1506 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001507 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001508 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001509 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001510
Jooyung Han61b66e92020-03-21 14:21:46 +00001511 apex_key {
1512 name: "myapex.key",
1513 public_key: "testkey.avbpubkey",
1514 private_key: "testkey.pem",
1515 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001516
Jooyung Han61b66e92020-03-21 14:21:46 +00001517 cc_library {
1518 name: "mylib",
1519 srcs: ["mylib.cpp"],
1520 vendor_available: true,
1521 shared_libs: ["libbar"],
1522 system_shared_libs: [],
1523 stl: "none",
1524 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001525 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001526 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001527
Jooyung Han61b66e92020-03-21 14:21:46 +00001528 cc_library {
1529 name: "libbar",
1530 srcs: ["mylib.cpp"],
1531 system_shared_libs: [],
1532 stl: "none",
1533 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001534 llndk: {
1535 symbol_file: "libbar.map.txt",
1536 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001537 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001538 `,
1539 setUseVendorAllowListForTest([]string{"myapex"}),
1540 withUnbundledBuild,
1541 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001542
Jooyung Han61b66e92020-03-21 14:21:46 +00001543 // Ensure that LLNDK dep is not included
1544 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1545 "lib64/mylib.so",
1546 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001547
Jooyung Han61b66e92020-03-21 14:21:46 +00001548 // Ensure that LLNDK dep is required
1549 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1550 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1551 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001552
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001553 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1554 ensureContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001555 for _, ver := range tc.shouldNotLink {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001556 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001557 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001558
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001559 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001560 ver := tc.shouldLink
1561 if tc.shouldLink == "current" {
1562 ver = strconv.Itoa(android.FutureApiLevelInt)
1563 }
1564 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001565 })
1566 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001567}
1568
Jiyong Park25fc6a92018-11-18 18:02:45 +09001569func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001570 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001571 apex {
1572 name: "myapex",
1573 key: "myapex.key",
1574 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001575 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001576 }
1577
1578 apex_key {
1579 name: "myapex.key",
1580 public_key: "testkey.avbpubkey",
1581 private_key: "testkey.pem",
1582 }
1583
1584 cc_library {
1585 name: "mylib",
1586 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001587 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001588 shared_libs: ["libdl#27"],
1589 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001590 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001591 }
1592
1593 cc_library_shared {
1594 name: "mylib_shared",
1595 srcs: ["mylib.cpp"],
1596 shared_libs: ["libdl#27"],
1597 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001598 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001599 }
1600
1601 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001602 name: "libBootstrap",
1603 srcs: ["mylib.cpp"],
1604 stl: "none",
1605 bootstrap: true,
1606 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001607 `)
1608
Sundong Ahnabb64432019-10-22 13:58:29 +09001609 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001610 copyCmds := apexRule.Args["copy_commands"]
1611
1612 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001613 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001614 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1615 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001616
1617 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001618 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001619
Colin Crossaede88c2020-08-11 12:17:01 -07001620 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1621 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1622 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001623
1624 // For dependency to libc
1625 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001626 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001627 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001628 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001629 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001630 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1631 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001632
1633 // For dependency to libm
1634 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001635 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001636 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001637 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001638 // ... and is not compiling with the stub
1639 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1640 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1641
1642 // For dependency to libdl
1643 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001644 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001645 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001646 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1647 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001648 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001649 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001650 // ... Cflags from stub is correctly exported to mylib
1651 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1652 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001653
1654 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001655 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1656 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1657 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1658 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001659}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001660
Jooyung Han749dc692020-04-15 11:03:39 +09001661func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001662 // there are three links between liba --> libz.
1663 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001664 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001665 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001666 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001667 apex {
1668 name: "myapex",
1669 key: "myapex.key",
1670 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001671 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001672 }
1673
1674 apex {
1675 name: "otherapex",
1676 key: "myapex.key",
1677 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001678 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001679 }
1680
1681 apex_key {
1682 name: "myapex.key",
1683 public_key: "testkey.avbpubkey",
1684 private_key: "testkey.pem",
1685 }
1686
1687 cc_library {
1688 name: "libx",
1689 shared_libs: ["liba"],
1690 system_shared_libs: [],
1691 stl: "none",
1692 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001693 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001694 }
1695
1696 cc_library {
1697 name: "liby",
1698 shared_libs: ["liba"],
1699 system_shared_libs: [],
1700 stl: "none",
1701 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001702 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001703 }
1704
1705 cc_library {
1706 name: "liba",
1707 shared_libs: ["libz"],
1708 system_shared_libs: [],
1709 stl: "none",
1710 apex_available: [
1711 "//apex_available:anyapex",
1712 "//apex_available:platform",
1713 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001714 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001715 }
1716
1717 cc_library {
1718 name: "libz",
1719 system_shared_libs: [],
1720 stl: "none",
1721 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001722 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001723 },
1724 }
Jooyung Han749dc692020-04-15 11:03:39 +09001725 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001726
1727 expectLink := func(from, from_variant, to, to_variant string) {
1728 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1729 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1730 }
1731 expectNoLink := func(from, from_variant, to, to_variant string) {
1732 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1733 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1734 }
1735 // platform liba is linked to non-stub version
1736 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001737 // liba in myapex is linked to current
1738 expectLink("liba", "shared_apex29", "libz", "shared_current")
1739 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001740 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001741 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001742 // liba in otherapex is linked to current
1743 expectLink("liba", "shared_apex30", "libz", "shared_current")
1744 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001745 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1746 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001747}
1748
Jooyung Hanaed150d2020-04-02 01:41:41 +09001749func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001750 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001751 apex {
1752 name: "myapex",
1753 key: "myapex.key",
1754 native_shared_libs: ["libx"],
1755 min_sdk_version: "R",
1756 }
1757
1758 apex_key {
1759 name: "myapex.key",
1760 public_key: "testkey.avbpubkey",
1761 private_key: "testkey.pem",
1762 }
1763
1764 cc_library {
1765 name: "libx",
1766 shared_libs: ["libz"],
1767 system_shared_libs: [],
1768 stl: "none",
1769 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001770 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001771 }
1772
1773 cc_library {
1774 name: "libz",
1775 system_shared_libs: [],
1776 stl: "none",
1777 stubs: {
1778 versions: ["29", "R"],
1779 },
1780 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001781 `,
1782 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1783 variables.Platform_version_active_codenames = []string{"R"}
1784 }),
1785 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001786
1787 expectLink := func(from, from_variant, to, to_variant string) {
1788 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1789 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1790 }
1791 expectNoLink := func(from, from_variant, to, to_variant string) {
1792 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1793 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1794 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001795 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1796 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001797 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1798 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001799}
1800
Jooyung Han4c4da062021-06-23 10:23:16 +09001801func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1802 testApex(t, `
1803 apex {
1804 name: "myapex",
1805 key: "myapex.key",
1806 java_libs: ["libx"],
1807 min_sdk_version: "S",
1808 }
1809
1810 apex_key {
1811 name: "myapex.key",
1812 public_key: "testkey.avbpubkey",
1813 private_key: "testkey.pem",
1814 }
1815
1816 java_library {
1817 name: "libx",
1818 srcs: ["a.java"],
1819 apex_available: [ "myapex" ],
1820 sdk_version: "current",
1821 min_sdk_version: "S", // should be okay
1822 }
1823 `,
1824 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1825 variables.Platform_version_active_codenames = []string{"S"}
1826 variables.Platform_sdk_codename = proptools.StringPtr("S")
1827 }),
1828 )
1829}
1830
Jooyung Han749dc692020-04-15 11:03:39 +09001831func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001832 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001833 apex {
1834 name: "myapex",
1835 key: "myapex.key",
1836 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001837 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001838 }
1839
1840 apex_key {
1841 name: "myapex.key",
1842 public_key: "testkey.avbpubkey",
1843 private_key: "testkey.pem",
1844 }
1845
1846 cc_library {
1847 name: "libx",
1848 shared_libs: ["libz"],
1849 system_shared_libs: [],
1850 stl: "none",
1851 apex_available: [ "myapex" ],
1852 }
1853
1854 cc_library {
1855 name: "libz",
1856 system_shared_libs: [],
1857 stl: "none",
1858 stubs: {
1859 versions: ["1", "2"],
1860 },
1861 }
1862 `)
1863
1864 expectLink := func(from, from_variant, to, to_variant string) {
1865 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1866 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1867 }
1868 expectNoLink := func(from, from_variant, to, to_variant string) {
1869 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1870 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1871 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001872 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001873 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001874 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001875 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001876}
1877
1878func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001879 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001880 apex {
1881 name: "myapex",
1882 key: "myapex.key",
1883 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001884 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001885 }
1886
1887 apex_key {
1888 name: "myapex.key",
1889 public_key: "testkey.avbpubkey",
1890 private_key: "testkey.pem",
1891 }
1892
1893 cc_library {
1894 name: "libx",
1895 system_shared_libs: [],
1896 stl: "none",
1897 apex_available: [ "myapex" ],
1898 stubs: {
1899 versions: ["1", "2"],
1900 },
1901 }
1902
1903 cc_library {
1904 name: "libz",
1905 shared_libs: ["libx"],
1906 system_shared_libs: [],
1907 stl: "none",
1908 }
1909 `)
1910
1911 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001912 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001913 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1914 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1915 }
1916 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001917 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001918 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1919 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1920 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001921 expectLink("libz", "shared", "libx", "shared_current")
1922 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001923 expectNoLink("libz", "shared", "libz", "shared_1")
1924 expectNoLink("libz", "shared", "libz", "shared")
1925}
1926
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001927var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1928 func(variables android.FixtureProductVariables) {
1929 variables.SanitizeDevice = []string{"hwaddress"}
1930 },
1931)
1932
Jooyung Han75568392020-03-20 04:29:24 +09001933func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001934 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001935 apex {
1936 name: "myapex",
1937 key: "myapex.key",
1938 native_shared_libs: ["libx"],
1939 min_sdk_version: "29",
1940 }
1941
1942 apex_key {
1943 name: "myapex.key",
1944 public_key: "testkey.avbpubkey",
1945 private_key: "testkey.pem",
1946 }
1947
1948 cc_library {
1949 name: "libx",
1950 shared_libs: ["libbar"],
1951 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001952 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001953 }
1954
1955 cc_library {
1956 name: "libbar",
1957 stubs: {
1958 versions: ["29", "30"],
1959 },
1960 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001961 `,
1962 prepareForTestWithSantitizeHwaddress,
1963 )
Jooyung Han03b51852020-02-26 22:45:42 +09001964 expectLink := func(from, from_variant, to, to_variant string) {
1965 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1966 libFlags := ld.Args["libFlags"]
1967 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1968 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001969 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09001970}
1971
Jooyung Han75568392020-03-20 04:29:24 +09001972func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001973 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001974 apex {
1975 name: "myapex",
1976 key: "myapex.key",
1977 native_shared_libs: ["libx"],
1978 min_sdk_version: "29",
1979 }
1980
1981 apex_key {
1982 name: "myapex.key",
1983 public_key: "testkey.avbpubkey",
1984 private_key: "testkey.pem",
1985 }
1986
1987 cc_library {
1988 name: "libx",
1989 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001990 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001991 }
Jooyung Han75568392020-03-20 04:29:24 +09001992 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001993
1994 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001995 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001996 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001997 // note that platform variant is not.
1998 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001999 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002000}
2001
Jooyung Han749dc692020-04-15 11:03:39 +09002002func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2003 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002004 apex {
2005 name: "myapex",
2006 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002007 native_shared_libs: ["mylib"],
2008 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002009 }
2010
2011 apex_key {
2012 name: "myapex.key",
2013 public_key: "testkey.avbpubkey",
2014 private_key: "testkey.pem",
2015 }
Jooyung Han749dc692020-04-15 11:03:39 +09002016
2017 cc_library {
2018 name: "mylib",
2019 srcs: ["mylib.cpp"],
2020 system_shared_libs: [],
2021 stl: "none",
2022 apex_available: [
2023 "myapex",
2024 ],
2025 min_sdk_version: "30",
2026 }
2027 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002028
2029 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2030 apex {
2031 name: "myapex",
2032 key: "myapex.key",
2033 native_shared_libs: ["libfoo.ffi"],
2034 min_sdk_version: "29",
2035 }
2036
2037 apex_key {
2038 name: "myapex.key",
2039 public_key: "testkey.avbpubkey",
2040 private_key: "testkey.pem",
2041 }
2042
2043 rust_ffi_shared {
2044 name: "libfoo.ffi",
2045 srcs: ["foo.rs"],
2046 crate_name: "foo",
2047 apex_available: [
2048 "myapex",
2049 ],
2050 min_sdk_version: "30",
2051 }
2052 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002053
2054 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2055 apex {
2056 name: "myapex",
2057 key: "myapex.key",
2058 java_libs: ["libfoo"],
2059 min_sdk_version: "29",
2060 }
2061
2062 apex_key {
2063 name: "myapex.key",
2064 public_key: "testkey.avbpubkey",
2065 private_key: "testkey.pem",
2066 }
2067
2068 java_import {
2069 name: "libfoo",
2070 jars: ["libfoo.jar"],
2071 apex_available: [
2072 "myapex",
2073 ],
2074 min_sdk_version: "30",
2075 }
2076 `)
Jooyung Han749dc692020-04-15 11:03:39 +09002077}
2078
2079func TestApexMinSdkVersion_Okay(t *testing.T) {
2080 testApex(t, `
2081 apex {
2082 name: "myapex",
2083 key: "myapex.key",
2084 native_shared_libs: ["libfoo"],
2085 java_libs: ["libbar"],
2086 min_sdk_version: "29",
2087 }
2088
2089 apex_key {
2090 name: "myapex.key",
2091 public_key: "testkey.avbpubkey",
2092 private_key: "testkey.pem",
2093 }
2094
2095 cc_library {
2096 name: "libfoo",
2097 srcs: ["mylib.cpp"],
2098 shared_libs: ["libfoo_dep"],
2099 apex_available: ["myapex"],
2100 min_sdk_version: "29",
2101 }
2102
2103 cc_library {
2104 name: "libfoo_dep",
2105 srcs: ["mylib.cpp"],
2106 apex_available: ["myapex"],
2107 min_sdk_version: "29",
2108 }
2109
2110 java_library {
2111 name: "libbar",
2112 sdk_version: "current",
2113 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002114 static_libs: [
2115 "libbar_dep",
2116 "libbar_import_dep",
2117 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002118 apex_available: ["myapex"],
2119 min_sdk_version: "29",
2120 }
2121
2122 java_library {
2123 name: "libbar_dep",
2124 sdk_version: "current",
2125 srcs: ["a.java"],
2126 apex_available: ["myapex"],
2127 min_sdk_version: "29",
2128 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002129
2130 java_import {
2131 name: "libbar_import_dep",
2132 jars: ["libbar.jar"],
2133 apex_available: ["myapex"],
2134 min_sdk_version: "29",
2135 }
Jooyung Han03b51852020-02-26 22:45:42 +09002136 `)
2137}
2138
Artur Satayev8cf899a2020-04-15 17:29:42 +01002139func TestJavaStableSdkVersion(t *testing.T) {
2140 testCases := []struct {
2141 name string
2142 expectedError string
2143 bp string
2144 }{
2145 {
2146 name: "Non-updatable apex with non-stable dep",
2147 bp: `
2148 apex {
2149 name: "myapex",
2150 java_libs: ["myjar"],
2151 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002152 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002153 }
2154 apex_key {
2155 name: "myapex.key",
2156 public_key: "testkey.avbpubkey",
2157 private_key: "testkey.pem",
2158 }
2159 java_library {
2160 name: "myjar",
2161 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002162 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002163 apex_available: ["myapex"],
2164 }
2165 `,
2166 },
2167 {
2168 name: "Updatable apex with stable dep",
2169 bp: `
2170 apex {
2171 name: "myapex",
2172 java_libs: ["myjar"],
2173 key: "myapex.key",
2174 updatable: true,
2175 min_sdk_version: "29",
2176 }
2177 apex_key {
2178 name: "myapex.key",
2179 public_key: "testkey.avbpubkey",
2180 private_key: "testkey.pem",
2181 }
2182 java_library {
2183 name: "myjar",
2184 srcs: ["foo/bar/MyClass.java"],
2185 sdk_version: "current",
2186 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002187 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002188 }
2189 `,
2190 },
2191 {
2192 name: "Updatable apex with non-stable dep",
2193 expectedError: "cannot depend on \"myjar\"",
2194 bp: `
2195 apex {
2196 name: "myapex",
2197 java_libs: ["myjar"],
2198 key: "myapex.key",
2199 updatable: true,
2200 }
2201 apex_key {
2202 name: "myapex.key",
2203 public_key: "testkey.avbpubkey",
2204 private_key: "testkey.pem",
2205 }
2206 java_library {
2207 name: "myjar",
2208 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002209 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002210 apex_available: ["myapex"],
2211 }
2212 `,
2213 },
2214 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002215 name: "Updatable apex with non-stable transitive dep",
2216 // This is not actually detecting that the transitive dependency is unstable, rather it is
2217 // detecting that the transitive dependency is building against a wider API surface than the
2218 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002219 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002220 bp: `
2221 apex {
2222 name: "myapex",
2223 java_libs: ["myjar"],
2224 key: "myapex.key",
2225 updatable: true,
2226 }
2227 apex_key {
2228 name: "myapex.key",
2229 public_key: "testkey.avbpubkey",
2230 private_key: "testkey.pem",
2231 }
2232 java_library {
2233 name: "myjar",
2234 srcs: ["foo/bar/MyClass.java"],
2235 sdk_version: "current",
2236 apex_available: ["myapex"],
2237 static_libs: ["transitive-jar"],
2238 }
2239 java_library {
2240 name: "transitive-jar",
2241 srcs: ["foo/bar/MyClass.java"],
2242 sdk_version: "core_platform",
2243 apex_available: ["myapex"],
2244 }
2245 `,
2246 },
2247 }
2248
2249 for _, test := range testCases {
2250 t.Run(test.name, func(t *testing.T) {
2251 if test.expectedError == "" {
2252 testApex(t, test.bp)
2253 } else {
2254 testApexError(t, test.expectedError, test.bp)
2255 }
2256 })
2257 }
2258}
2259
Jooyung Han749dc692020-04-15 11:03:39 +09002260func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2261 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2262 apex {
2263 name: "myapex",
2264 key: "myapex.key",
2265 native_shared_libs: ["mylib"],
2266 min_sdk_version: "29",
2267 }
2268
2269 apex_key {
2270 name: "myapex.key",
2271 public_key: "testkey.avbpubkey",
2272 private_key: "testkey.pem",
2273 }
2274
2275 cc_library {
2276 name: "mylib",
2277 srcs: ["mylib.cpp"],
2278 shared_libs: ["mylib2"],
2279 system_shared_libs: [],
2280 stl: "none",
2281 apex_available: [
2282 "myapex",
2283 ],
2284 min_sdk_version: "29",
2285 }
2286
2287 // indirect part of the apex
2288 cc_library {
2289 name: "mylib2",
2290 srcs: ["mylib.cpp"],
2291 system_shared_libs: [],
2292 stl: "none",
2293 apex_available: [
2294 "myapex",
2295 ],
2296 min_sdk_version: "30",
2297 }
2298 `)
2299}
2300
2301func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2302 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2303 apex {
2304 name: "myapex",
2305 key: "myapex.key",
2306 apps: ["AppFoo"],
2307 min_sdk_version: "29",
2308 }
2309
2310 apex_key {
2311 name: "myapex.key",
2312 public_key: "testkey.avbpubkey",
2313 private_key: "testkey.pem",
2314 }
2315
2316 android_app {
2317 name: "AppFoo",
2318 srcs: ["foo/bar/MyClass.java"],
2319 sdk_version: "current",
2320 min_sdk_version: "29",
2321 system_modules: "none",
2322 stl: "none",
2323 static_libs: ["bar"],
2324 apex_available: [ "myapex" ],
2325 }
2326
2327 java_library {
2328 name: "bar",
2329 sdk_version: "current",
2330 srcs: ["a.java"],
2331 apex_available: [ "myapex" ],
2332 }
2333 `)
2334}
2335
2336func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002337 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002338 apex {
2339 name: "myapex",
2340 key: "myapex.key",
2341 native_shared_libs: ["mylib"],
2342 min_sdk_version: "29",
2343 }
2344
2345 apex_key {
2346 name: "myapex.key",
2347 public_key: "testkey.avbpubkey",
2348 private_key: "testkey.pem",
2349 }
2350
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002351 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002352 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2353 cc_library {
2354 name: "mylib",
2355 srcs: ["mylib.cpp"],
2356 shared_libs: ["mylib2"],
2357 system_shared_libs: [],
2358 stl: "none",
2359 apex_available: ["myapex", "otherapex"],
2360 min_sdk_version: "29",
2361 }
2362
2363 cc_library {
2364 name: "mylib2",
2365 srcs: ["mylib.cpp"],
2366 system_shared_libs: [],
2367 stl: "none",
2368 apex_available: ["otherapex"],
2369 stubs: { versions: ["29", "30"] },
2370 min_sdk_version: "30",
2371 }
2372
2373 apex {
2374 name: "otherapex",
2375 key: "myapex.key",
2376 native_shared_libs: ["mylib", "mylib2"],
2377 min_sdk_version: "30",
2378 }
2379 `)
2380 expectLink := func(from, from_variant, to, to_variant string) {
2381 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2382 libFlags := ld.Args["libFlags"]
2383 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2384 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002385 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002386 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002387}
2388
Jooyung Haned124c32021-01-26 11:43:46 +09002389func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002390 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2391 func(variables android.FixtureProductVariables) {
2392 variables.Platform_sdk_codename = proptools.StringPtr("S")
2393 variables.Platform_version_active_codenames = []string{"S"}
2394 },
2395 )
Jooyung Haned124c32021-01-26 11:43:46 +09002396 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2397 apex {
2398 name: "myapex",
2399 key: "myapex.key",
2400 native_shared_libs: ["libfoo"],
2401 min_sdk_version: "S",
2402 }
2403 apex_key {
2404 name: "myapex.key",
2405 public_key: "testkey.avbpubkey",
2406 private_key: "testkey.pem",
2407 }
2408 cc_library {
2409 name: "libfoo",
2410 shared_libs: ["libbar"],
2411 apex_available: ["myapex"],
2412 min_sdk_version: "29",
2413 }
2414 cc_library {
2415 name: "libbar",
2416 apex_available: ["myapex"],
2417 }
2418 `, withSAsActiveCodeNames)
2419}
2420
2421func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002422 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2423 variables.Platform_sdk_codename = proptools.StringPtr("S")
2424 variables.Platform_version_active_codenames = []string{"S", "T"}
2425 })
Colin Cross1c460562021-02-16 17:55:47 -08002426 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002427 apex {
2428 name: "myapex",
2429 key: "myapex.key",
2430 native_shared_libs: ["libfoo"],
2431 min_sdk_version: "S",
2432 }
2433 apex_key {
2434 name: "myapex.key",
2435 public_key: "testkey.avbpubkey",
2436 private_key: "testkey.pem",
2437 }
2438 cc_library {
2439 name: "libfoo",
2440 shared_libs: ["libbar"],
2441 apex_available: ["myapex"],
2442 min_sdk_version: "S",
2443 }
2444 cc_library {
2445 name: "libbar",
2446 stubs: {
2447 symbol_file: "libbar.map.txt",
2448 versions: ["30", "S", "T"],
2449 },
2450 }
2451 `, withSAsActiveCodeNames)
2452
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002453 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002454 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2455 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002456 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002457}
2458
Jiyong Park7c2ee712018-12-07 00:42:25 +09002459func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002460 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002461 apex {
2462 name: "myapex",
2463 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002464 native_shared_libs: ["mylib"],
2465 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002466 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002467 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002468 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002469 }
2470
2471 apex_key {
2472 name: "myapex.key",
2473 public_key: "testkey.avbpubkey",
2474 private_key: "testkey.pem",
2475 }
2476
2477 prebuilt_etc {
2478 name: "myetc",
2479 src: "myprebuilt",
2480 sub_dir: "foo/bar",
2481 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002482
2483 cc_library {
2484 name: "mylib",
2485 srcs: ["mylib.cpp"],
2486 relative_install_path: "foo/bar",
2487 system_shared_libs: [],
2488 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002489 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002490 }
2491
2492 cc_binary {
2493 name: "mybin",
2494 srcs: ["mylib.cpp"],
2495 relative_install_path: "foo/bar",
2496 system_shared_libs: [],
2497 static_executable: true,
2498 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002499 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002500 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002501 `)
2502
Sundong Ahnabb64432019-10-22 13:58:29 +09002503 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002504 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2505
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002506 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002507 ensureListContains(t, dirs, "etc")
2508 ensureListContains(t, dirs, "etc/foo")
2509 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002510 ensureListContains(t, dirs, "lib64")
2511 ensureListContains(t, dirs, "lib64/foo")
2512 ensureListContains(t, dirs, "lib64/foo/bar")
2513 ensureListContains(t, dirs, "lib")
2514 ensureListContains(t, dirs, "lib/foo")
2515 ensureListContains(t, dirs, "lib/foo/bar")
2516
Jiyong Parkbd13e442019-03-15 18:10:35 +09002517 ensureListContains(t, dirs, "bin")
2518 ensureListContains(t, dirs, "bin/foo")
2519 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002520}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002521
Jooyung Han35155c42020-02-06 17:33:20 +09002522func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002523 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002524 apex {
2525 name: "myapex",
2526 key: "myapex.key",
2527 multilib: {
2528 both: {
2529 native_shared_libs: ["mylib"],
2530 binaries: ["mybin"],
2531 },
2532 },
2533 compile_multilib: "both",
2534 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002535 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002536 }
2537
2538 apex_key {
2539 name: "myapex.key",
2540 public_key: "testkey.avbpubkey",
2541 private_key: "testkey.pem",
2542 }
2543
2544 cc_library {
2545 name: "mylib",
2546 relative_install_path: "foo/bar",
2547 system_shared_libs: [],
2548 stl: "none",
2549 apex_available: [ "myapex" ],
2550 native_bridge_supported: true,
2551 }
2552
2553 cc_binary {
2554 name: "mybin",
2555 relative_install_path: "foo/bar",
2556 system_shared_libs: [],
2557 static_executable: true,
2558 stl: "none",
2559 apex_available: [ "myapex" ],
2560 native_bridge_supported: true,
2561 compile_multilib: "both", // default is "first" for binary
2562 multilib: {
2563 lib64: {
2564 suffix: "64",
2565 },
2566 },
2567 }
2568 `, withNativeBridgeEnabled)
2569 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2570 "bin/foo/bar/mybin",
2571 "bin/foo/bar/mybin64",
2572 "bin/arm/foo/bar/mybin",
2573 "bin/arm64/foo/bar/mybin64",
2574 "lib/foo/bar/mylib.so",
2575 "lib/arm/foo/bar/mylib.so",
2576 "lib64/foo/bar/mylib.so",
2577 "lib64/arm64/foo/bar/mylib.so",
2578 })
2579}
2580
Jiyong Parkda6eb592018-12-19 17:12:36 +09002581func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002582 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002583 apex {
2584 name: "myapex",
2585 key: "myapex.key",
2586 native_shared_libs: ["mylib"],
2587 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002588 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002589 }
2590
2591 apex_key {
2592 name: "myapex.key",
2593 public_key: "testkey.avbpubkey",
2594 private_key: "testkey.pem",
2595 }
2596
2597 cc_library {
2598 name: "mylib",
2599 srcs: ["mylib.cpp"],
2600 shared_libs: ["mylib2"],
2601 system_shared_libs: [],
2602 vendor_available: true,
2603 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002604 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002605 }
2606
2607 cc_library {
2608 name: "mylib2",
2609 srcs: ["mylib.cpp"],
2610 system_shared_libs: [],
2611 vendor_available: true,
2612 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002613 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002614 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002615 `,
2616 setUseVendorAllowListForTest([]string{"myapex"}),
2617 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002618
2619 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002620 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002621 for _, implicit := range i.Implicits {
2622 inputsList = append(inputsList, implicit.String())
2623 }
2624 }
2625 inputsString := strings.Join(inputsList, " ")
2626
2627 // ensure that the apex includes vendor variants of the direct and indirect deps
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002628 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib.so")
2629 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002630
2631 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002632 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2633 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002634}
Jiyong Park16e91a02018-12-20 18:18:08 +09002635
Jooyung Han85d61762020-06-24 23:50:26 +09002636func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002637 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2638 apex {
2639 name: "myapex",
2640 key: "myapex.key",
2641 use_vendor: true,
2642 }
2643 apex_key {
2644 name: "myapex.key",
2645 public_key: "testkey.avbpubkey",
2646 private_key: "testkey.pem",
2647 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002648 `,
2649 setUseVendorAllowListForTest([]string{""}),
2650 )
Colin Cross440e0d02020-06-11 11:32:11 -07002651 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002652 testApex(t, `
2653 apex {
2654 name: "myapex",
2655 key: "myapex.key",
2656 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002657 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002658 }
2659 apex_key {
2660 name: "myapex.key",
2661 public_key: "testkey.avbpubkey",
2662 private_key: "testkey.pem",
2663 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002664 `,
2665 setUseVendorAllowListForTest([]string{"myapex"}),
2666 )
Jooyung Handc782442019-11-01 03:14:38 +09002667}
2668
Jooyung Han5c998b92019-06-27 11:30:33 +09002669func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2670 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2671 apex {
2672 name: "myapex",
2673 key: "myapex.key",
2674 native_shared_libs: ["mylib"],
2675 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002676 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002677 }
2678
2679 apex_key {
2680 name: "myapex.key",
2681 public_key: "testkey.avbpubkey",
2682 private_key: "testkey.pem",
2683 }
2684
2685 cc_library {
2686 name: "mylib",
2687 srcs: ["mylib.cpp"],
2688 system_shared_libs: [],
2689 stl: "none",
2690 }
2691 `)
2692}
2693
Jooyung Han85d61762020-06-24 23:50:26 +09002694func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002695 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002696 apex {
2697 name: "myapex",
2698 key: "myapex.key",
2699 binaries: ["mybin"],
2700 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002701 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002702 }
2703 apex_key {
2704 name: "myapex.key",
2705 public_key: "testkey.avbpubkey",
2706 private_key: "testkey.pem",
2707 }
2708 cc_binary {
2709 name: "mybin",
2710 vendor: true,
2711 shared_libs: ["libfoo"],
2712 }
2713 cc_library {
2714 name: "libfoo",
2715 proprietary: true,
2716 }
2717 `)
2718
2719 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2720 "bin/mybin",
2721 "lib64/libfoo.so",
2722 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2723 "lib64/libc++.so",
2724 })
2725
2726 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002727 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002728 name := apexBundle.BaseModuleName()
2729 prefix := "TARGET_"
2730 var builder strings.Builder
2731 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002732 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2733 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002734 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002735
2736 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2737 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2738 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002739}
2740
Jooyung Handf78e212020-07-22 15:54:47 +09002741func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002742 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002743 apex {
2744 name: "myapex",
2745 key: "myapex.key",
2746 binaries: ["mybin"],
2747 vendor: true,
2748 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002749 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002750 }
2751 apex_key {
2752 name: "myapex.key",
2753 public_key: "testkey.avbpubkey",
2754 private_key: "testkey.pem",
2755 }
2756 cc_binary {
2757 name: "mybin",
2758 vendor: true,
2759 shared_libs: ["libvndk", "libvendor"],
2760 }
2761 cc_library {
2762 name: "libvndk",
2763 vndk: {
2764 enabled: true,
2765 },
2766 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002767 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002768 }
2769 cc_library {
2770 name: "libvendor",
2771 vendor: true,
2772 }
2773 `)
2774
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002775 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002776
Paul Duffina71a67a2021-03-29 00:42:57 +01002777 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002778 libs := names(ldRule.Args["libFlags"])
2779 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002780 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2781 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002782 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002783 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002784
2785 // VNDK libs are not included when use_vndk_as_stable: true
2786 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2787 "bin/mybin",
2788 "lib64/libvendor.so",
2789 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002790
2791 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2792 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2793 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002794}
2795
Justin Yun13decfb2021-03-08 19:25:55 +09002796func TestProductVariant(t *testing.T) {
2797 ctx := testApex(t, `
2798 apex {
2799 name: "myapex",
2800 key: "myapex.key",
2801 updatable: false,
2802 product_specific: true,
2803 binaries: ["foo"],
2804 }
2805
2806 apex_key {
2807 name: "myapex.key",
2808 public_key: "testkey.avbpubkey",
2809 private_key: "testkey.pem",
2810 }
2811
2812 cc_binary {
2813 name: "foo",
2814 product_available: true,
2815 apex_available: ["myapex"],
2816 srcs: ["foo.cpp"],
2817 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002818 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2819 variables.ProductVndkVersion = proptools.StringPtr("current")
2820 }),
2821 )
Justin Yun13decfb2021-03-08 19:25:55 +09002822
2823 cflags := strings.Fields(
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002824 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002825 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2826 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2827 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2828 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2829}
2830
Jooyung Han8e5685d2020-09-21 11:02:57 +09002831func TestApex_withPrebuiltFirmware(t *testing.T) {
2832 testCases := []struct {
2833 name string
2834 additionalProp string
2835 }{
2836 {"system apex with prebuilt_firmware", ""},
2837 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2838 }
2839 for _, tc := range testCases {
2840 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002841 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002842 apex {
2843 name: "myapex",
2844 key: "myapex.key",
2845 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002846 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002847 `+tc.additionalProp+`
2848 }
2849 apex_key {
2850 name: "myapex.key",
2851 public_key: "testkey.avbpubkey",
2852 private_key: "testkey.pem",
2853 }
2854 prebuilt_firmware {
2855 name: "myfirmware",
2856 src: "myfirmware.bin",
2857 filename_from_src: true,
2858 `+tc.additionalProp+`
2859 }
2860 `)
2861 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2862 "etc/firmware/myfirmware.bin",
2863 })
2864 })
2865 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002866}
2867
Jooyung Hanefb184e2020-06-25 17:14:25 +09002868func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002869 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002870 apex {
2871 name: "myapex",
2872 key: "myapex.key",
2873 use_vendor: true,
2874 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002875 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002876 }
2877
2878 apex_key {
2879 name: "myapex.key",
2880 public_key: "testkey.avbpubkey",
2881 private_key: "testkey.pem",
2882 }
2883
2884 cc_library {
2885 name: "mylib",
2886 vendor_available: true,
2887 apex_available: ["myapex"],
2888 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002889 `,
2890 setUseVendorAllowListForTest([]string{"myapex"}),
2891 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002892
2893 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002894 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002895 name := apexBundle.BaseModuleName()
2896 prefix := "TARGET_"
2897 var builder strings.Builder
2898 data.Custom(&builder, name, prefix, "", data)
2899 androidMk := builder.String()
2900 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2901}
2902
2903func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002904 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002905 apex {
2906 name: "myapex",
2907 key: "myapex.key",
2908 vendor: true,
2909 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002910 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002911 }
2912
2913 apex_key {
2914 name: "myapex.key",
2915 public_key: "testkey.avbpubkey",
2916 private_key: "testkey.pem",
2917 }
2918
2919 cc_library {
2920 name: "mylib",
2921 vendor_available: true,
2922 }
2923 `)
2924
2925 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002926 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002927 name := apexBundle.BaseModuleName()
2928 prefix := "TARGET_"
2929 var builder strings.Builder
2930 data.Custom(&builder, name, prefix, "", data)
2931 androidMk := builder.String()
2932 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2933}
2934
Jooyung Han2ed99d02020-06-24 23:26:26 +09002935func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002936 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002937 apex {
2938 name: "myapex",
2939 key: "myapex.key",
2940 vintf_fragments: ["fragment.xml"],
2941 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002942 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002943 }
2944 apex_key {
2945 name: "myapex.key",
2946 public_key: "testkey.avbpubkey",
2947 private_key: "testkey.pem",
2948 }
2949 cc_binary {
2950 name: "mybin",
2951 }
2952 `)
2953
2954 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002955 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002956 name := apexBundle.BaseModuleName()
2957 prefix := "TARGET_"
2958 var builder strings.Builder
2959 data.Custom(&builder, name, prefix, "", data)
2960 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04002961 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002962 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002963}
2964
Jiyong Park16e91a02018-12-20 18:18:08 +09002965func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002966 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002967 apex {
2968 name: "myapex",
2969 key: "myapex.key",
2970 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002971 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002972 }
2973
2974 apex_key {
2975 name: "myapex.key",
2976 public_key: "testkey.avbpubkey",
2977 private_key: "testkey.pem",
2978 }
2979
2980 cc_library {
2981 name: "mylib",
2982 srcs: ["mylib.cpp"],
2983 system_shared_libs: [],
2984 stl: "none",
2985 stubs: {
2986 versions: ["1", "2", "3"],
2987 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002988 apex_available: [
2989 "//apex_available:platform",
2990 "myapex",
2991 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002992 }
2993
2994 cc_binary {
2995 name: "not_in_apex",
2996 srcs: ["mylib.cpp"],
2997 static_libs: ["mylib"],
2998 static_executable: true,
2999 system_shared_libs: [],
3000 stl: "none",
3001 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003002 `)
3003
Colin Cross7113d202019-11-20 16:39:12 -08003004 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003005
3006 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003007 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003008}
Jiyong Park9335a262018-12-24 11:31:58 +09003009
3010func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003011 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003012 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003013 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003014 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003015 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003016 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003017 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003018 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003019 }
3020
3021 cc_library {
3022 name: "mylib",
3023 srcs: ["mylib.cpp"],
3024 system_shared_libs: [],
3025 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003026 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003027 }
3028
3029 apex_key {
3030 name: "myapex.key",
3031 public_key: "testkey.avbpubkey",
3032 private_key: "testkey.pem",
3033 }
3034
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003035 android_app_certificate {
3036 name: "myapex.certificate",
3037 certificate: "testkey",
3038 }
3039
3040 android_app_certificate {
3041 name: "myapex.certificate.override",
3042 certificate: "testkey.override",
3043 }
3044
Jiyong Park9335a262018-12-24 11:31:58 +09003045 `)
3046
3047 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003048 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003049
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003050 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3051 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003052 "vendor/foo/devkeys/testkey.avbpubkey")
3053 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003054 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3055 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003056 "vendor/foo/devkeys/testkey.pem")
3057 }
3058
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003059 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003060 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003061 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003062 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003063 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003064 }
3065}
Jiyong Park58e364a2019-01-19 19:24:06 +09003066
Jooyung Hanf121a652019-12-17 14:30:11 +09003067func TestCertificate(t *testing.T) {
3068 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003069 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003070 apex {
3071 name: "myapex",
3072 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003073 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003074 }
3075 apex_key {
3076 name: "myapex.key",
3077 public_key: "testkey.avbpubkey",
3078 private_key: "testkey.pem",
3079 }`)
3080 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3081 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3082 if actual := rule.Args["certificates"]; actual != expected {
3083 t.Errorf("certificates should be %q, not %q", expected, actual)
3084 }
3085 })
3086 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003087 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003088 apex {
3089 name: "myapex_keytest",
3090 key: "myapex.key",
3091 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003092 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003093 }
3094 apex_key {
3095 name: "myapex.key",
3096 public_key: "testkey.avbpubkey",
3097 private_key: "testkey.pem",
3098 }
3099 android_app_certificate {
3100 name: "myapex.certificate.override",
3101 certificate: "testkey.override",
3102 }`)
3103 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3104 expected := "testkey.override.x509.pem testkey.override.pk8"
3105 if actual := rule.Args["certificates"]; actual != expected {
3106 t.Errorf("certificates should be %q, not %q", expected, actual)
3107 }
3108 })
3109 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003110 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003111 apex {
3112 name: "myapex",
3113 key: "myapex.key",
3114 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003115 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003116 }
3117 apex_key {
3118 name: "myapex.key",
3119 public_key: "testkey.avbpubkey",
3120 private_key: "testkey.pem",
3121 }
3122 android_app_certificate {
3123 name: "myapex.certificate",
3124 certificate: "testkey",
3125 }`)
3126 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3127 expected := "testkey.x509.pem testkey.pk8"
3128 if actual := rule.Args["certificates"]; actual != expected {
3129 t.Errorf("certificates should be %q, not %q", expected, actual)
3130 }
3131 })
3132 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003133 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003134 apex {
3135 name: "myapex_keytest",
3136 key: "myapex.key",
3137 file_contexts: ":myapex-file_contexts",
3138 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003139 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003140 }
3141 apex_key {
3142 name: "myapex.key",
3143 public_key: "testkey.avbpubkey",
3144 private_key: "testkey.pem",
3145 }
3146 android_app_certificate {
3147 name: "myapex.certificate.override",
3148 certificate: "testkey.override",
3149 }`)
3150 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3151 expected := "testkey.override.x509.pem testkey.override.pk8"
3152 if actual := rule.Args["certificates"]; actual != expected {
3153 t.Errorf("certificates should be %q, not %q", expected, actual)
3154 }
3155 })
3156 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003157 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003158 apex {
3159 name: "myapex",
3160 key: "myapex.key",
3161 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003162 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003163 }
3164 apex_key {
3165 name: "myapex.key",
3166 public_key: "testkey.avbpubkey",
3167 private_key: "testkey.pem",
3168 }`)
3169 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3170 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3171 if actual := rule.Args["certificates"]; actual != expected {
3172 t.Errorf("certificates should be %q, not %q", expected, actual)
3173 }
3174 })
3175 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003176 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003177 apex {
3178 name: "myapex_keytest",
3179 key: "myapex.key",
3180 file_contexts: ":myapex-file_contexts",
3181 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003182 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003183 }
3184 apex_key {
3185 name: "myapex.key",
3186 public_key: "testkey.avbpubkey",
3187 private_key: "testkey.pem",
3188 }
3189 android_app_certificate {
3190 name: "myapex.certificate.override",
3191 certificate: "testkey.override",
3192 }`)
3193 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3194 expected := "testkey.override.x509.pem testkey.override.pk8"
3195 if actual := rule.Args["certificates"]; actual != expected {
3196 t.Errorf("certificates should be %q, not %q", expected, actual)
3197 }
3198 })
3199}
3200
Jiyong Park58e364a2019-01-19 19:24:06 +09003201func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003202 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003203 apex {
3204 name: "myapex",
3205 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003206 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003207 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003208 }
3209
3210 apex {
3211 name: "otherapex",
3212 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003213 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003214 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003215 }
3216
3217 apex_key {
3218 name: "myapex.key",
3219 public_key: "testkey.avbpubkey",
3220 private_key: "testkey.pem",
3221 }
3222
3223 cc_library {
3224 name: "mylib",
3225 srcs: ["mylib.cpp"],
3226 system_shared_libs: [],
3227 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003228 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003229 "myapex",
3230 "otherapex",
3231 ],
Jooyung Han24282772020-03-21 23:20:55 +09003232 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003233 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003234 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003235 cc_library {
3236 name: "mylib2",
3237 srcs: ["mylib.cpp"],
3238 system_shared_libs: [],
3239 stl: "none",
3240 apex_available: [
3241 "myapex",
3242 "otherapex",
3243 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003244 static_libs: ["mylib3"],
3245 recovery_available: true,
3246 min_sdk_version: "29",
3247 }
3248 cc_library {
3249 name: "mylib3",
3250 srcs: ["mylib.cpp"],
3251 system_shared_libs: [],
3252 stl: "none",
3253 apex_available: [
3254 "myapex",
3255 "otherapex",
3256 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09003257 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07003258 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003259 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003260 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003261 `)
3262
Jooyung Hanc87a0592020-03-02 17:44:33 +09003263 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003264 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003265 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003266 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003267
Jooyung Hanccce2f22020-03-07 03:45:53 +09003268 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003269 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003270 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003271 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003272 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003273
Jooyung Hanccce2f22020-03-07 03:45:53 +09003274 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003275 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003276 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003277 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003278 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003279
Colin Crossaede88c2020-08-11 12:17:01 -07003280 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3281 // each variant defines additional macros to distinguish which apex variant it is built for
3282
3283 // non-APEX variant does not have __ANDROID_APEX__ defined
3284 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3285 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3286
3287 // APEX variant has __ANDROID_APEX__ defined
3288 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3289 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3290 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3291 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3292
3293 // APEX variant has __ANDROID_APEX__ defined
3294 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3295 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3296 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3297 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3298
Dan Albertb19953d2020-11-17 15:29:36 -08003299 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003300 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3301 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003302 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003303
3304 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3305 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003306
3307 // non-APEX variant does not have __ANDROID_APEX__ defined
3308 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3309 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3310
3311 // APEX variant has __ANDROID_APEX__ defined
3312 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003313 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003314 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003315 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003316
Jooyung Hanc87a0592020-03-02 17:44:33 +09003317 // APEX variant has __ANDROID_APEX__ defined
3318 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003319 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003320 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003321 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003322
Dan Albertb19953d2020-11-17 15:29:36 -08003323 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003324 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003325 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003326 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003327}
Jiyong Park7e636d02019-01-28 16:16:54 +09003328
3329func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003330 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003331 apex {
3332 name: "myapex",
3333 key: "myapex.key",
3334 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003335 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003336 }
3337
3338 apex_key {
3339 name: "myapex.key",
3340 public_key: "testkey.avbpubkey",
3341 private_key: "testkey.pem",
3342 }
3343
3344 cc_library_headers {
3345 name: "mylib_headers",
3346 export_include_dirs: ["my_include"],
3347 system_shared_libs: [],
3348 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003349 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003350 }
3351
3352 cc_library {
3353 name: "mylib",
3354 srcs: ["mylib.cpp"],
3355 system_shared_libs: [],
3356 stl: "none",
3357 header_libs: ["mylib_headers"],
3358 export_header_lib_headers: ["mylib_headers"],
3359 stubs: {
3360 versions: ["1", "2", "3"],
3361 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003362 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003363 }
3364
3365 cc_library {
3366 name: "otherlib",
3367 srcs: ["mylib.cpp"],
3368 system_shared_libs: [],
3369 stl: "none",
3370 shared_libs: ["mylib"],
3371 }
3372 `)
3373
Colin Cross7113d202019-11-20 16:39:12 -08003374 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003375
3376 // Ensure that the include path of the header lib is exported to 'otherlib'
3377 ensureContains(t, cFlags, "-Imy_include")
3378}
Alex Light9670d332019-01-29 18:07:33 -08003379
Jiyong Park7cd10e32020-01-14 09:22:18 +09003380type fileInApex struct {
3381 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003382 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003383 isLink bool
3384}
3385
Jooyung Hana57af4a2020-01-23 05:36:59 +00003386func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003387 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003388 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003389 copyCmds := apexRule.Args["copy_commands"]
3390 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003391 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003392 for _, cmd := range strings.Split(copyCmds, "&&") {
3393 cmd = strings.TrimSpace(cmd)
3394 if cmd == "" {
3395 continue
3396 }
3397 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003398 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003399 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003400 switch terms[0] {
3401 case "mkdir":
3402 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003403 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003404 t.Fatal("copyCmds contains invalid cp command", cmd)
3405 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003406 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003407 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003408 isLink = false
3409 case "ln":
3410 if len(terms) != 3 && len(terms) != 4 {
3411 // ln LINK TARGET or ln -s LINK TARGET
3412 t.Fatal("copyCmds contains invalid ln command", cmd)
3413 }
3414 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003415 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003416 isLink = true
3417 default:
3418 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3419 }
3420 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003421 index := strings.Index(dst, imageApexDir)
3422 if index == -1 {
3423 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3424 }
3425 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003426 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003427 }
3428 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003429 return ret
3430}
3431
Jooyung Hana57af4a2020-01-23 05:36:59 +00003432func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3433 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003434 var failed bool
3435 var surplus []string
3436 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003437 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003438 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003439 for _, expected := range files {
3440 if matched, _ := path.Match(expected, file.path); matched {
3441 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003442 mactchFound = true
3443 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003444 }
3445 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003446 if !mactchFound {
3447 surplus = append(surplus, file.path)
3448 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003449 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003450
Jooyung Han31c470b2019-10-18 16:26:59 +09003451 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003452 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003453 t.Log("surplus files", surplus)
3454 failed = true
3455 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003456
3457 if len(files) > len(filesMatched) {
3458 var missing []string
3459 for _, expected := range files {
3460 if !filesMatched[expected] {
3461 missing = append(missing, expected)
3462 }
3463 }
3464 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003465 t.Log("missing files", missing)
3466 failed = true
3467 }
3468 if failed {
3469 t.Fail()
3470 }
3471}
3472
Jooyung Han344d5432019-08-23 11:17:39 +09003473func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003474 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003475 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003476 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003477 "etc/llndk.libraries.29.txt",
3478 "etc/vndkcore.libraries.29.txt",
3479 "etc/vndksp.libraries.29.txt",
3480 "etc/vndkprivate.libraries.29.txt",
3481 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003482 }
3483 testCases := []struct {
3484 vndkVersion string
3485 expectedFiles []string
3486 }{
3487 {
3488 vndkVersion: "current",
3489 expectedFiles: append(commonFiles,
3490 "lib/libvndk.so",
3491 "lib/libvndksp.so",
3492 "lib64/libvndk.so",
3493 "lib64/libvndksp.so"),
3494 },
3495 {
3496 vndkVersion: "",
3497 expectedFiles: append(commonFiles,
3498 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3499 "lib/libvndksp.so",
3500 "lib64/libvndksp.so"),
3501 },
3502 }
3503 for _, tc := range testCases {
3504 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3505 ctx := testApex(t, `
3506 apex_vndk {
3507 name: "com.android.vndk.current",
3508 key: "com.android.vndk.current.key",
3509 updatable: false,
3510 }
3511
3512 apex_key {
3513 name: "com.android.vndk.current.key",
3514 public_key: "testkey.avbpubkey",
3515 private_key: "testkey.pem",
3516 }
3517
3518 cc_library {
3519 name: "libvndk",
3520 srcs: ["mylib.cpp"],
3521 vendor_available: true,
3522 product_available: true,
3523 vndk: {
3524 enabled: true,
3525 },
3526 system_shared_libs: [],
3527 stl: "none",
3528 apex_available: [ "com.android.vndk.current" ],
3529 }
3530
3531 cc_library {
3532 name: "libvndksp",
3533 srcs: ["mylib.cpp"],
3534 vendor_available: true,
3535 product_available: true,
3536 vndk: {
3537 enabled: true,
3538 support_system_process: true,
3539 },
3540 system_shared_libs: [],
3541 stl: "none",
3542 apex_available: [ "com.android.vndk.current" ],
3543 }
3544
3545 // VNDK-Ext should not cause any problems
3546
3547 cc_library {
3548 name: "libvndk.ext",
3549 srcs: ["mylib2.cpp"],
3550 vendor: true,
3551 vndk: {
3552 enabled: true,
3553 extends: "libvndk",
3554 },
3555 system_shared_libs: [],
3556 stl: "none",
3557 }
3558
3559 cc_library {
3560 name: "libvndksp.ext",
3561 srcs: ["mylib2.cpp"],
3562 vendor: true,
3563 vndk: {
3564 enabled: true,
3565 support_system_process: true,
3566 extends: "libvndksp",
3567 },
3568 system_shared_libs: [],
3569 stl: "none",
3570 }
3571 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3572 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3573 }))
3574 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3575 })
3576 }
Jooyung Han344d5432019-08-23 11:17:39 +09003577}
3578
3579func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003580 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003581 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003582 name: "com.android.vndk.current",
3583 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003584 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003585 }
3586
3587 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003588 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003589 public_key: "testkey.avbpubkey",
3590 private_key: "testkey.pem",
3591 }
3592
3593 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003594 name: "libvndk",
3595 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003596 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003597 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003598 vndk: {
3599 enabled: true,
3600 },
3601 system_shared_libs: [],
3602 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003603 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003604 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003605
3606 cc_prebuilt_library_shared {
3607 name: "libvndk.arm",
3608 srcs: ["libvndk.arm.so"],
3609 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003610 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003611 vndk: {
3612 enabled: true,
3613 },
3614 enabled: false,
3615 arch: {
3616 arm: {
3617 enabled: true,
3618 },
3619 },
3620 system_shared_libs: [],
3621 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003622 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003623 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003624 `+vndkLibrariesTxtFiles("current"),
3625 withFiles(map[string][]byte{
3626 "libvndk.so": nil,
3627 "libvndk.arm.so": nil,
3628 }))
Colin Cross2807f002021-03-02 10:15:29 -08003629 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003630 "lib/libvndk.so",
3631 "lib/libvndk.arm.so",
3632 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003633 "lib/libc++.so",
3634 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003635 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003636 })
Jooyung Han344d5432019-08-23 11:17:39 +09003637}
3638
Jooyung Han39edb6c2019-11-06 16:53:07 +09003639func vndkLibrariesTxtFiles(vers ...string) (result string) {
3640 for _, v := range vers {
3641 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003642 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003643 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003644 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003645 name: "` + txt + `.libraries.txt",
3646 }
3647 `
3648 }
3649 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003650 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003651 result += `
3652 prebuilt_etc {
3653 name: "` + txt + `.libraries.` + v + `.txt",
3654 src: "dummy.txt",
3655 }
3656 `
3657 }
3658 }
3659 }
3660 return
3661}
3662
Jooyung Han344d5432019-08-23 11:17:39 +09003663func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003664 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003665 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003666 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003667 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003668 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003669 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003670 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003671 }
3672
3673 apex_key {
3674 name: "myapex.key",
3675 public_key: "testkey.avbpubkey",
3676 private_key: "testkey.pem",
3677 }
3678
Jooyung Han31c470b2019-10-18 16:26:59 +09003679 vndk_prebuilt_shared {
3680 name: "libvndk27",
3681 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003682 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003683 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003684 vndk: {
3685 enabled: true,
3686 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003687 target_arch: "arm64",
3688 arch: {
3689 arm: {
3690 srcs: ["libvndk27_arm.so"],
3691 },
3692 arm64: {
3693 srcs: ["libvndk27_arm64.so"],
3694 },
3695 },
Colin Cross2807f002021-03-02 10:15:29 -08003696 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003697 }
3698
3699 vndk_prebuilt_shared {
3700 name: "libvndk27",
3701 version: "27",
3702 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003703 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003704 vndk: {
3705 enabled: true,
3706 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003707 target_arch: "x86_64",
3708 arch: {
3709 x86: {
3710 srcs: ["libvndk27_x86.so"],
3711 },
3712 x86_64: {
3713 srcs: ["libvndk27_x86_64.so"],
3714 },
3715 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003716 }
3717 `+vndkLibrariesTxtFiles("27"),
3718 withFiles(map[string][]byte{
3719 "libvndk27_arm.so": nil,
3720 "libvndk27_arm64.so": nil,
3721 "libvndk27_x86.so": nil,
3722 "libvndk27_x86_64.so": nil,
3723 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003724
Colin Cross2807f002021-03-02 10:15:29 -08003725 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003726 "lib/libvndk27_arm.so",
3727 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003728 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003729 })
Jooyung Han344d5432019-08-23 11:17:39 +09003730}
3731
Jooyung Han90eee022019-10-01 20:02:42 +09003732func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003733 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003734 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003735 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003736 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003737 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003738 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003739 }
3740 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003741 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003742 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003743 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003744 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003745 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003746 }
3747 apex_key {
3748 name: "myapex.key",
3749 public_key: "testkey.avbpubkey",
3750 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003751 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003752
3753 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003754 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003755 actual := proptools.String(bundle.properties.Apex_name)
3756 if !reflect.DeepEqual(actual, expected) {
3757 t.Errorf("Got '%v', expected '%v'", actual, expected)
3758 }
3759 }
3760
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003761 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003762 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003763}
3764
Jooyung Han344d5432019-08-23 11:17:39 +09003765func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003766 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003767 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003768 name: "com.android.vndk.current",
3769 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003770 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003771 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003772 }
3773
3774 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003775 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003776 public_key: "testkey.avbpubkey",
3777 private_key: "testkey.pem",
3778 }
3779
3780 cc_library {
3781 name: "libvndk",
3782 srcs: ["mylib.cpp"],
3783 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003784 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003785 native_bridge_supported: true,
3786 host_supported: true,
3787 vndk: {
3788 enabled: true,
3789 },
3790 system_shared_libs: [],
3791 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003792 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003793 }
Colin Cross2807f002021-03-02 10:15:29 -08003794 `+vndkLibrariesTxtFiles("current"),
3795 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003796
Colin Cross2807f002021-03-02 10:15:29 -08003797 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003798 "lib/libvndk.so",
3799 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003800 "lib/libc++.so",
3801 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003802 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003803 })
Jooyung Han344d5432019-08-23 11:17:39 +09003804}
3805
3806func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003807 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003808 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003809 name: "com.android.vndk.current",
3810 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003811 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003812 native_bridge_supported: true,
3813 }
3814
3815 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003816 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003817 public_key: "testkey.avbpubkey",
3818 private_key: "testkey.pem",
3819 }
3820
3821 cc_library {
3822 name: "libvndk",
3823 srcs: ["mylib.cpp"],
3824 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003825 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003826 native_bridge_supported: true,
3827 host_supported: true,
3828 vndk: {
3829 enabled: true,
3830 },
3831 system_shared_libs: [],
3832 stl: "none",
3833 }
3834 `)
3835}
3836
Jooyung Han31c470b2019-10-18 16:26:59 +09003837func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003838 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003839 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003840 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003841 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003842 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003843 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003844 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003845 }
3846
3847 apex_key {
3848 name: "myapex.key",
3849 public_key: "testkey.avbpubkey",
3850 private_key: "testkey.pem",
3851 }
3852
3853 vndk_prebuilt_shared {
3854 name: "libvndk27",
3855 version: "27",
3856 target_arch: "arm",
3857 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003858 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003859 vndk: {
3860 enabled: true,
3861 },
3862 arch: {
3863 arm: {
3864 srcs: ["libvndk27.so"],
3865 }
3866 },
3867 }
3868
3869 vndk_prebuilt_shared {
3870 name: "libvndk27",
3871 version: "27",
3872 target_arch: "arm",
3873 binder32bit: true,
3874 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003875 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003876 vndk: {
3877 enabled: true,
3878 },
3879 arch: {
3880 arm: {
3881 srcs: ["libvndk27binder32.so"],
3882 }
3883 },
Colin Cross2807f002021-03-02 10:15:29 -08003884 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003885 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003886 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003887 withFiles(map[string][]byte{
3888 "libvndk27.so": nil,
3889 "libvndk27binder32.so": nil,
3890 }),
3891 withBinder32bit,
3892 withTargets(map[android.OsType][]android.Target{
3893 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003894 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3895 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003896 },
3897 }),
3898 )
3899
Colin Cross2807f002021-03-02 10:15:29 -08003900 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003901 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003902 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003903 })
3904}
3905
Jooyung Han45a96772020-06-15 14:59:42 +09003906func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003907 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003908 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003909 name: "com.android.vndk.current",
3910 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003911 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003912 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003913 }
3914
3915 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003916 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003917 public_key: "testkey.avbpubkey",
3918 private_key: "testkey.pem",
3919 }
3920
3921 cc_library {
3922 name: "libz",
3923 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003924 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003925 vndk: {
3926 enabled: true,
3927 },
3928 stubs: {
3929 symbol_file: "libz.map.txt",
3930 versions: ["30"],
3931 }
3932 }
3933 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3934 "libz.map.txt": nil,
3935 }))
3936
Colin Cross2807f002021-03-02 10:15:29 -08003937 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003938 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3939 ensureListEmpty(t, provideNativeLibs)
3940}
3941
Jooyung Hane1633032019-08-01 17:41:43 +09003942func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003943 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003944 apex {
3945 name: "myapex_nodep",
3946 key: "myapex.key",
3947 native_shared_libs: ["lib_nodep"],
3948 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003949 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003950 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003951 }
3952
3953 apex {
3954 name: "myapex_dep",
3955 key: "myapex.key",
3956 native_shared_libs: ["lib_dep"],
3957 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003958 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003959 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003960 }
3961
3962 apex {
3963 name: "myapex_provider",
3964 key: "myapex.key",
3965 native_shared_libs: ["libfoo"],
3966 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003967 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003968 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003969 }
3970
3971 apex {
3972 name: "myapex_selfcontained",
3973 key: "myapex.key",
3974 native_shared_libs: ["lib_dep", "libfoo"],
3975 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003976 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003977 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003978 }
3979
3980 apex_key {
3981 name: "myapex.key",
3982 public_key: "testkey.avbpubkey",
3983 private_key: "testkey.pem",
3984 }
3985
3986 cc_library {
3987 name: "lib_nodep",
3988 srcs: ["mylib.cpp"],
3989 system_shared_libs: [],
3990 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003991 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003992 }
3993
3994 cc_library {
3995 name: "lib_dep",
3996 srcs: ["mylib.cpp"],
3997 shared_libs: ["libfoo"],
3998 system_shared_libs: [],
3999 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004000 apex_available: [
4001 "myapex_dep",
4002 "myapex_provider",
4003 "myapex_selfcontained",
4004 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004005 }
4006
4007 cc_library {
4008 name: "libfoo",
4009 srcs: ["mytest.cpp"],
4010 stubs: {
4011 versions: ["1"],
4012 },
4013 system_shared_libs: [],
4014 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004015 apex_available: [
4016 "myapex_provider",
4017 "myapex_selfcontained",
4018 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004019 }
4020 `)
4021
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004022 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004023 var provideNativeLibs, requireNativeLibs []string
4024
Sundong Ahnabb64432019-10-22 13:58:29 +09004025 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004026 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4027 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004028 ensureListEmpty(t, provideNativeLibs)
4029 ensureListEmpty(t, requireNativeLibs)
4030
Sundong Ahnabb64432019-10-22 13:58:29 +09004031 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004032 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4033 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004034 ensureListEmpty(t, provideNativeLibs)
4035 ensureListContains(t, requireNativeLibs, "libfoo.so")
4036
Sundong Ahnabb64432019-10-22 13:58:29 +09004037 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004038 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4039 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004040 ensureListContains(t, provideNativeLibs, "libfoo.so")
4041 ensureListEmpty(t, requireNativeLibs)
4042
Sundong Ahnabb64432019-10-22 13:58:29 +09004043 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004044 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4045 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004046 ensureListContains(t, provideNativeLibs, "libfoo.so")
4047 ensureListEmpty(t, requireNativeLibs)
4048}
4049
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004050func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004051 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004052 apex {
4053 name: "myapex",
4054 key: "myapex.key",
4055 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09004056 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004057 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004058 }
4059
4060 apex_key {
4061 name: "myapex.key",
4062 public_key: "testkey.avbpubkey",
4063 private_key: "testkey.pem",
4064 }
Jiyong Parkdb334862020-02-05 17:19:28 +09004065
4066 cc_library {
4067 name: "mylib",
4068 srcs: ["mylib.cpp"],
4069 system_shared_libs: [],
4070 stl: "none",
4071 apex_available: [
4072 "//apex_available:platform",
4073 "myapex",
4074 ],
4075 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004076 `)
4077
Sundong Ahnabb64432019-10-22 13:58:29 +09004078 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004079 apexManifestRule := module.Rule("apexManifestRule")
4080 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
4081 apexRule := module.Rule("apexRule")
4082 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09004083
4084 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004085 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09004086 name := apexBundle.BaseModuleName()
4087 prefix := "TARGET_"
4088 var builder strings.Builder
4089 data.Custom(&builder, name, prefix, "", data)
4090 androidMk := builder.String()
4091 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
4092 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004093}
4094
Alex Light0851b882019-02-07 13:20:53 -08004095func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004096 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004097 apex {
4098 name: "myapex",
4099 key: "myapex.key",
4100 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004101 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004102 }
4103
4104 apex_key {
4105 name: "myapex.key",
4106 public_key: "testkey.avbpubkey",
4107 private_key: "testkey.pem",
4108 }
4109
4110 cc_library {
4111 name: "mylib_common",
4112 srcs: ["mylib.cpp"],
4113 system_shared_libs: [],
4114 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004115 apex_available: [
4116 "//apex_available:platform",
4117 "myapex",
4118 ],
Alex Light0851b882019-02-07 13:20:53 -08004119 }
4120 `)
4121
Sundong Ahnabb64432019-10-22 13:58:29 +09004122 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004123 apexRule := module.Rule("apexRule")
4124 copyCmds := apexRule.Args["copy_commands"]
4125
4126 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4127 t.Log("Apex was a test apex!")
4128 t.Fail()
4129 }
4130 // Ensure that main rule creates an output
4131 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4132
4133 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004134 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004135
4136 // Ensure that both direct and indirect deps are copied into apex
4137 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4138
Colin Cross7113d202019-11-20 16:39:12 -08004139 // Ensure that the platform variant ends with _shared
4140 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004141
Colin Cross56a83212020-09-15 18:30:11 -07004142 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004143 t.Log("Found mylib_common not in any apex!")
4144 t.Fail()
4145 }
4146}
4147
4148func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004149 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004150 apex_test {
4151 name: "myapex",
4152 key: "myapex.key",
4153 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004154 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004155 }
4156
4157 apex_key {
4158 name: "myapex.key",
4159 public_key: "testkey.avbpubkey",
4160 private_key: "testkey.pem",
4161 }
4162
4163 cc_library {
4164 name: "mylib_common_test",
4165 srcs: ["mylib.cpp"],
4166 system_shared_libs: [],
4167 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004168 // TODO: remove //apex_available:platform
4169 apex_available: [
4170 "//apex_available:platform",
4171 "myapex",
4172 ],
Alex Light0851b882019-02-07 13:20:53 -08004173 }
4174 `)
4175
Sundong Ahnabb64432019-10-22 13:58:29 +09004176 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004177 apexRule := module.Rule("apexRule")
4178 copyCmds := apexRule.Args["copy_commands"]
4179
4180 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4181 t.Log("Apex was not a test apex!")
4182 t.Fail()
4183 }
4184 // Ensure that main rule creates an output
4185 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4186
4187 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004188 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004189
4190 // Ensure that both direct and indirect deps are copied into apex
4191 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4192
Colin Cross7113d202019-11-20 16:39:12 -08004193 // Ensure that the platform variant ends with _shared
4194 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004195}
4196
Alex Light9670d332019-01-29 18:07:33 -08004197func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004198 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004199 apex {
4200 name: "myapex",
4201 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004202 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004203 multilib: {
4204 first: {
4205 native_shared_libs: ["mylib_common"],
4206 }
4207 },
4208 target: {
4209 android: {
4210 multilib: {
4211 first: {
4212 native_shared_libs: ["mylib"],
4213 }
4214 }
4215 },
4216 host: {
4217 multilib: {
4218 first: {
4219 native_shared_libs: ["mylib2"],
4220 }
4221 }
4222 }
4223 }
4224 }
4225
4226 apex_key {
4227 name: "myapex.key",
4228 public_key: "testkey.avbpubkey",
4229 private_key: "testkey.pem",
4230 }
4231
4232 cc_library {
4233 name: "mylib",
4234 srcs: ["mylib.cpp"],
4235 system_shared_libs: [],
4236 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004237 // TODO: remove //apex_available:platform
4238 apex_available: [
4239 "//apex_available:platform",
4240 "myapex",
4241 ],
Alex Light9670d332019-01-29 18:07:33 -08004242 }
4243
4244 cc_library {
4245 name: "mylib_common",
4246 srcs: ["mylib.cpp"],
4247 system_shared_libs: [],
4248 stl: "none",
4249 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004250 // TODO: remove //apex_available:platform
4251 apex_available: [
4252 "//apex_available:platform",
4253 "myapex",
4254 ],
Alex Light9670d332019-01-29 18:07:33 -08004255 }
4256
4257 cc_library {
4258 name: "mylib2",
4259 srcs: ["mylib.cpp"],
4260 system_shared_libs: [],
4261 stl: "none",
4262 compile_multilib: "first",
4263 }
4264 `)
4265
Sundong Ahnabb64432019-10-22 13:58:29 +09004266 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004267 copyCmds := apexRule.Args["copy_commands"]
4268
4269 // Ensure that main rule creates an output
4270 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4271
4272 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004273 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4274 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4275 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004276
4277 // Ensure that both direct and indirect deps are copied into apex
4278 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4279 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4280 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4281
Colin Cross7113d202019-11-20 16:39:12 -08004282 // Ensure that the platform variant ends with _shared
4283 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4284 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4285 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004286}
Jiyong Park04480cf2019-02-06 00:16:29 +09004287
Jiyong Park59140302020-12-14 18:44:04 +09004288func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004289 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004290 apex {
4291 name: "myapex",
4292 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004293 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004294 arch: {
4295 arm64: {
4296 native_shared_libs: ["mylib.arm64"],
4297 },
4298 x86_64: {
4299 native_shared_libs: ["mylib.x64"],
4300 },
4301 }
4302 }
4303
4304 apex_key {
4305 name: "myapex.key",
4306 public_key: "testkey.avbpubkey",
4307 private_key: "testkey.pem",
4308 }
4309
4310 cc_library {
4311 name: "mylib.arm64",
4312 srcs: ["mylib.cpp"],
4313 system_shared_libs: [],
4314 stl: "none",
4315 // TODO: remove //apex_available:platform
4316 apex_available: [
4317 "//apex_available:platform",
4318 "myapex",
4319 ],
4320 }
4321
4322 cc_library {
4323 name: "mylib.x64",
4324 srcs: ["mylib.cpp"],
4325 system_shared_libs: [],
4326 stl: "none",
4327 // TODO: remove //apex_available:platform
4328 apex_available: [
4329 "//apex_available:platform",
4330 "myapex",
4331 ],
4332 }
4333 `)
4334
4335 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4336 copyCmds := apexRule.Args["copy_commands"]
4337
4338 // Ensure that apex variant is created for the direct dep
4339 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4340 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4341
4342 // Ensure that both direct and indirect deps are copied into apex
4343 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4344 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4345}
4346
Jiyong Park04480cf2019-02-06 00:16:29 +09004347func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004348 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004349 apex {
4350 name: "myapex",
4351 key: "myapex.key",
4352 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004353 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004354 }
4355
4356 apex_key {
4357 name: "myapex.key",
4358 public_key: "testkey.avbpubkey",
4359 private_key: "testkey.pem",
4360 }
4361
4362 sh_binary {
4363 name: "myscript",
4364 src: "mylib.cpp",
4365 filename: "myscript.sh",
4366 sub_dir: "script",
4367 }
4368 `)
4369
Sundong Ahnabb64432019-10-22 13:58:29 +09004370 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004371 copyCmds := apexRule.Args["copy_commands"]
4372
4373 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4374}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004375
Jooyung Han91df2082019-11-20 01:49:42 +09004376func TestApexInVariousPartition(t *testing.T) {
4377 testcases := []struct {
4378 propName, parition, flattenedPartition string
4379 }{
4380 {"", "system", "system_ext"},
4381 {"product_specific: true", "product", "product"},
4382 {"soc_specific: true", "vendor", "vendor"},
4383 {"proprietary: true", "vendor", "vendor"},
4384 {"vendor: true", "vendor", "vendor"},
4385 {"system_ext_specific: true", "system_ext", "system_ext"},
4386 }
4387 for _, tc := range testcases {
4388 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004389 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004390 apex {
4391 name: "myapex",
4392 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004393 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004394 `+tc.propName+`
4395 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004396
Jooyung Han91df2082019-11-20 01:49:42 +09004397 apex_key {
4398 name: "myapex.key",
4399 public_key: "testkey.avbpubkey",
4400 private_key: "testkey.pem",
4401 }
4402 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004403
Jooyung Han91df2082019-11-20 01:49:42 +09004404 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004405 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4406 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004407 if actual != expected {
4408 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4409 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004410
Jooyung Han91df2082019-11-20 01:49:42 +09004411 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004412 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4413 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004414 if actual != expected {
4415 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4416 }
4417 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004418 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004419}
Jiyong Park67882562019-03-21 01:11:21 +09004420
Jooyung Han580eb4f2020-06-24 19:33:06 +09004421func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004422 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004423 apex {
4424 name: "myapex",
4425 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004426 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004427 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004428
Jooyung Han580eb4f2020-06-24 19:33:06 +09004429 apex_key {
4430 name: "myapex.key",
4431 public_key: "testkey.avbpubkey",
4432 private_key: "testkey.pem",
4433 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004434 `)
4435 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004436 rule := module.Output("file_contexts")
4437 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4438}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004439
Jooyung Han580eb4f2020-06-24 19:33:06 +09004440func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004441 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004442 apex {
4443 name: "myapex",
4444 key: "myapex.key",
4445 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004446 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004447 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004448
Jooyung Han580eb4f2020-06-24 19:33:06 +09004449 apex_key {
4450 name: "myapex.key",
4451 public_key: "testkey.avbpubkey",
4452 private_key: "testkey.pem",
4453 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004454 `, withFiles(map[string][]byte{
4455 "my_own_file_contexts": nil,
4456 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004457}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004458
Jooyung Han580eb4f2020-06-24 19:33:06 +09004459func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004460 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004461 apex {
4462 name: "myapex",
4463 key: "myapex.key",
4464 product_specific: true,
4465 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004466 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004467 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004468
Jooyung Han580eb4f2020-06-24 19:33:06 +09004469 apex_key {
4470 name: "myapex.key",
4471 public_key: "testkey.avbpubkey",
4472 private_key: "testkey.pem",
4473 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004474 `)
4475
Colin Cross1c460562021-02-16 17:55:47 -08004476 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004477 apex {
4478 name: "myapex",
4479 key: "myapex.key",
4480 product_specific: true,
4481 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004482 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004483 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004484
Jooyung Han580eb4f2020-06-24 19:33:06 +09004485 apex_key {
4486 name: "myapex.key",
4487 public_key: "testkey.avbpubkey",
4488 private_key: "testkey.pem",
4489 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004490 `, withFiles(map[string][]byte{
4491 "product_specific_file_contexts": nil,
4492 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004493 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4494 rule := module.Output("file_contexts")
4495 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4496}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004497
Jooyung Han580eb4f2020-06-24 19:33:06 +09004498func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004499 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004500 apex {
4501 name: "myapex",
4502 key: "myapex.key",
4503 product_specific: true,
4504 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004505 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004506 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004507
Jooyung Han580eb4f2020-06-24 19:33:06 +09004508 apex_key {
4509 name: "myapex.key",
4510 public_key: "testkey.avbpubkey",
4511 private_key: "testkey.pem",
4512 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004513
Jooyung Han580eb4f2020-06-24 19:33:06 +09004514 filegroup {
4515 name: "my-file-contexts",
4516 srcs: ["product_specific_file_contexts"],
4517 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004518 `, withFiles(map[string][]byte{
4519 "product_specific_file_contexts": nil,
4520 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004521 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4522 rule := module.Output("file_contexts")
4523 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004524}
4525
Jiyong Park67882562019-03-21 01:11:21 +09004526func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004527 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004528 apex_key {
4529 name: "myapex.key",
4530 public_key: ":my.avbpubkey",
4531 private_key: ":my.pem",
4532 product_specific: true,
4533 }
4534
4535 filegroup {
4536 name: "my.avbpubkey",
4537 srcs: ["testkey2.avbpubkey"],
4538 }
4539
4540 filegroup {
4541 name: "my.pem",
4542 srcs: ["testkey2.pem"],
4543 }
4544 `)
4545
4546 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4547 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004548 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004549 if actual_pubkey != expected_pubkey {
4550 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4551 }
4552 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004553 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004554 if actual_privkey != expected_privkey {
4555 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4556 }
4557}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004558
4559func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004560 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004561 prebuilt_apex {
4562 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004563 arch: {
4564 arm64: {
4565 src: "myapex-arm64.apex",
4566 },
4567 arm: {
4568 src: "myapex-arm.apex",
4569 },
4570 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004571 }
4572 `)
4573
Paul Duffin6717d882021-06-15 19:09:41 +01004574 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004575
Jiyong Parkc95714e2019-03-29 14:23:10 +09004576 expectedInput := "myapex-arm64.apex"
4577 if prebuilt.inputApex.String() != expectedInput {
4578 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4579 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004580}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004581
Paul Duffinc0609c62021-03-01 17:27:16 +00004582func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004583 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004584 prebuilt_apex {
4585 name: "myapex",
4586 }
4587 `)
4588}
4589
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004590func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004591 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004592 prebuilt_apex {
4593 name: "myapex",
4594 src: "myapex-arm.apex",
4595 filename: "notmyapex.apex",
4596 }
4597 `)
4598
Paul Duffin6717d882021-06-15 19:09:41 +01004599 p := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004600
4601 expected := "notmyapex.apex"
4602 if p.installFilename != expected {
4603 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4604 }
4605}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004606
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004607func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004608 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004609 prebuilt_apex {
4610 name: "myapex.prebuilt",
4611 src: "myapex-arm.apex",
4612 overrides: [
4613 "myapex",
4614 ],
4615 }
4616 `)
4617
Paul Duffin6717d882021-06-15 19:09:41 +01004618 p := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt").Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004619
4620 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004621 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004622 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004623 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004624 }
4625}
4626
Paul Duffin092153d2021-01-26 11:42:39 +00004627// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4628// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004629func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004630 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004631
Paul Duffin89886cb2021-02-05 16:44:03 +00004632 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004633 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004634 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004635 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004636 stem := android.RemoveOptionalPrebuiltPrefix(name)
4637 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004638 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4639 }
4640 }
4641
Paul Duffin39853512021-02-26 11:09:39 +00004642 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004643 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004644 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004645 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4646 }
4647 }
4648
4649 t.Run("prebuilt only", func(t *testing.T) {
4650 bp := `
4651 prebuilt_apex {
4652 name: "myapex",
4653 arch: {
4654 arm64: {
4655 src: "myapex-arm64.apex",
4656 },
4657 arm: {
4658 src: "myapex-arm.apex",
4659 },
4660 },
Paul Duffin39853512021-02-26 11:09:39 +00004661 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004662 }
4663
4664 java_import {
4665 name: "libfoo",
4666 jars: ["libfoo.jar"],
4667 }
Paul Duffin39853512021-02-26 11:09:39 +00004668
4669 java_sdk_library_import {
4670 name: "libbar",
4671 public: {
4672 jars: ["libbar.jar"],
4673 },
4674 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004675 `
4676
4677 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4678 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4679
Paul Duffinf6932af2021-02-26 18:21:56 +00004680 // Make sure that the deapexer has the correct input APEX.
4681 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4682 rule := deapexer.Rule("deapexer")
4683 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4684 t.Errorf("expected: %q, found: %q", expected, actual)
4685 }
4686
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004687 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004688 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004689 rule = prebuiltApex.Rule("android/soong/android.Cp")
4690 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4691 t.Errorf("expected: %q, found: %q", expected, actual)
4692 }
4693
Paul Duffin89886cb2021-02-05 16:44:03 +00004694 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004695
4696 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004697 })
4698
4699 t.Run("prebuilt with source preferred", func(t *testing.T) {
4700
4701 bp := `
4702 prebuilt_apex {
4703 name: "myapex",
4704 arch: {
4705 arm64: {
4706 src: "myapex-arm64.apex",
4707 },
4708 arm: {
4709 src: "myapex-arm.apex",
4710 },
4711 },
Paul Duffin39853512021-02-26 11:09:39 +00004712 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004713 }
4714
4715 java_import {
4716 name: "libfoo",
4717 jars: ["libfoo.jar"],
4718 }
4719
4720 java_library {
4721 name: "libfoo",
4722 }
Paul Duffin39853512021-02-26 11:09:39 +00004723
4724 java_sdk_library_import {
4725 name: "libbar",
4726 public: {
4727 jars: ["libbar.jar"],
4728 },
4729 }
4730
4731 java_sdk_library {
4732 name: "libbar",
4733 srcs: ["foo/bar/MyClass.java"],
4734 unsafe_ignore_missing_latest_api: true,
4735 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004736 `
4737
4738 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4739 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4740
Paul Duffin89886cb2021-02-05 16:44:03 +00004741 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004742 ensureNoSourceVariant(t, ctx, "libfoo")
4743
4744 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4745 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004746 })
4747
4748 t.Run("prebuilt preferred with source", func(t *testing.T) {
4749 bp := `
4750 prebuilt_apex {
4751 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004752 arch: {
4753 arm64: {
4754 src: "myapex-arm64.apex",
4755 },
4756 arm: {
4757 src: "myapex-arm.apex",
4758 },
4759 },
Paul Duffin39853512021-02-26 11:09:39 +00004760 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004761 }
4762
4763 java_import {
4764 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004765 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004766 jars: ["libfoo.jar"],
4767 }
4768
4769 java_library {
4770 name: "libfoo",
4771 }
Paul Duffin39853512021-02-26 11:09:39 +00004772
4773 java_sdk_library_import {
4774 name: "libbar",
4775 prefer: true,
4776 public: {
4777 jars: ["libbar.jar"],
4778 },
4779 }
4780
4781 java_sdk_library {
4782 name: "libbar",
4783 srcs: ["foo/bar/MyClass.java"],
4784 unsafe_ignore_missing_latest_api: true,
4785 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004786 `
4787
4788 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4789 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4790
Paul Duffin89886cb2021-02-05 16:44:03 +00004791 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004792 ensureNoSourceVariant(t, ctx, "libfoo")
4793
4794 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4795 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004796 })
4797}
4798
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004799func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004800 preparer := android.GroupFixturePreparers(
4801 java.FixtureConfigureBootJars("myapex:libfoo", "myapex:libbar"),
4802 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4803 // is disabled.
4804 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4805 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004806
Paul Duffin37856732021-02-26 14:24:15 +00004807 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4808 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004809 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004810 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004811 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004812 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004813 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004814 foundLibfooJar = true
4815 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004816 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004817 }
4818 }
4819 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004820 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().BuildDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004821 }
4822 }
4823
Paul Duffind061d402021-06-07 21:36:01 +01004824 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004825 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004826 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01004827 var rule android.TestingBuildParams
4828
4829 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
4830 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004831 }
4832
Paul Duffin89f570a2021-06-16 01:42:33 +01004833 fragment := java.ApexVariantReference{
4834 Apex: proptools.StringPtr("myapex"),
4835 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4836 }
4837
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004838 t.Run("prebuilt only", func(t *testing.T) {
4839 bp := `
4840 prebuilt_apex {
4841 name: "myapex",
4842 arch: {
4843 arm64: {
4844 src: "myapex-arm64.apex",
4845 },
4846 arm: {
4847 src: "myapex-arm.apex",
4848 },
4849 },
Paul Duffin89f570a2021-06-16 01:42:33 +01004850 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4851 }
4852
4853 prebuilt_bootclasspath_fragment {
4854 name: "my-bootclasspath-fragment",
4855 contents: ["libfoo", "libbar"],
4856 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004857 }
4858
4859 java_import {
4860 name: "libfoo",
4861 jars: ["libfoo.jar"],
4862 apex_available: ["myapex"],
4863 }
Paul Duffin37856732021-02-26 14:24:15 +00004864
4865 java_sdk_library_import {
4866 name: "libbar",
4867 public: {
4868 jars: ["libbar.jar"],
4869 },
4870 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01004871 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004872 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004873 `
4874
Paul Duffin89f570a2021-06-16 01:42:33 +01004875 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004876 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4877 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004878
Paul Duffin537ea3d2021-05-14 10:38:00 +01004879 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004880 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin51d7da22021-06-16 02:04:13 +01004881 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4882 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4883 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004884 })
4885
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004886 t.Run("apex_set only", func(t *testing.T) {
4887 bp := `
4888 apex_set {
4889 name: "myapex",
4890 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01004891 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4892 }
4893
4894 prebuilt_bootclasspath_fragment {
4895 name: "my-bootclasspath-fragment",
4896 contents: ["libfoo", "libbar"],
4897 apex_available: ["myapex"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004898 }
4899
4900 java_import {
4901 name: "libfoo",
4902 jars: ["libfoo.jar"],
4903 apex_available: ["myapex"],
4904 }
4905
4906 java_sdk_library_import {
4907 name: "libbar",
4908 public: {
4909 jars: ["libbar.jar"],
4910 },
4911 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01004912 shared_library: false,
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004913 }
4914 `
4915
Paul Duffin89f570a2021-06-16 01:42:33 +01004916 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004917 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4918 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
4919
Paul Duffin537ea3d2021-05-14 10:38:00 +01004920 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004921 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin51d7da22021-06-16 02:04:13 +01004922 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4923 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4924 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004925 })
4926
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004927 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4928 bp := `
4929 prebuilt_apex {
4930 name: "myapex",
4931 arch: {
4932 arm64: {
4933 src: "myapex-arm64.apex",
4934 },
4935 arm: {
4936 src: "myapex-arm.apex",
4937 },
4938 },
Paul Duffin89f570a2021-06-16 01:42:33 +01004939 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4940 }
4941
4942 prebuilt_bootclasspath_fragment {
4943 name: "my-bootclasspath-fragment",
4944 contents: ["libfoo", "libbar"],
4945 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004946 }
4947
4948 java_import {
4949 name: "libfoo",
4950 jars: ["libfoo.jar"],
4951 apex_available: ["myapex"],
4952 }
4953
4954 java_library {
4955 name: "libfoo",
4956 srcs: ["foo/bar/MyClass.java"],
4957 apex_available: ["myapex"],
4958 }
Paul Duffin37856732021-02-26 14:24:15 +00004959
4960 java_sdk_library_import {
4961 name: "libbar",
4962 public: {
4963 jars: ["libbar.jar"],
4964 },
4965 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01004966 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004967 }
4968
4969 java_sdk_library {
4970 name: "libbar",
4971 srcs: ["foo/bar/MyClass.java"],
4972 unsafe_ignore_missing_latest_api: true,
4973 apex_available: ["myapex"],
4974 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004975 `
4976
4977 // In this test the source (java_library) libfoo is active since the
4978 // prebuilt (java_import) defaults to prefer:false. However the
4979 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4980 // find the dex boot jar in it. We either need to disable the source libfoo
4981 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01004982 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004983 })
4984
4985 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4986 bp := `
4987 prebuilt_apex {
4988 name: "myapex",
4989 arch: {
4990 arm64: {
4991 src: "myapex-arm64.apex",
4992 },
4993 arm: {
4994 src: "myapex-arm.apex",
4995 },
4996 },
Paul Duffin89f570a2021-06-16 01:42:33 +01004997 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4998 }
4999
5000 prebuilt_bootclasspath_fragment {
5001 name: "my-bootclasspath-fragment",
5002 contents: ["libfoo", "libbar"],
5003 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005004 }
5005
5006 java_import {
5007 name: "libfoo",
5008 prefer: true,
5009 jars: ["libfoo.jar"],
5010 apex_available: ["myapex"],
5011 }
5012
5013 java_library {
5014 name: "libfoo",
5015 srcs: ["foo/bar/MyClass.java"],
5016 apex_available: ["myapex"],
5017 }
Paul Duffin37856732021-02-26 14:24:15 +00005018
5019 java_sdk_library_import {
5020 name: "libbar",
5021 prefer: true,
5022 public: {
5023 jars: ["libbar.jar"],
5024 },
5025 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005026 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005027 }
5028
5029 java_sdk_library {
5030 name: "libbar",
5031 srcs: ["foo/bar/MyClass.java"],
5032 unsafe_ignore_missing_latest_api: true,
5033 apex_available: ["myapex"],
5034 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005035 `
5036
Paul Duffin89f570a2021-06-16 01:42:33 +01005037 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005038 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5039 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005040
Paul Duffin537ea3d2021-05-14 10:38:00 +01005041 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00005042 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin51d7da22021-06-16 02:04:13 +01005043 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
5044 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
5045 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005046 })
5047
5048 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5049 bp := `
5050 apex {
5051 name: "myapex",
5052 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005053 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005054 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005055 }
5056
5057 apex_key {
5058 name: "myapex.key",
5059 public_key: "testkey.avbpubkey",
5060 private_key: "testkey.pem",
5061 }
5062
5063 prebuilt_apex {
5064 name: "myapex",
5065 arch: {
5066 arm64: {
5067 src: "myapex-arm64.apex",
5068 },
5069 arm: {
5070 src: "myapex-arm.apex",
5071 },
5072 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005073 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5074 }
5075
5076 prebuilt_bootclasspath_fragment {
5077 name: "my-bootclasspath-fragment",
5078 contents: ["libfoo", "libbar"],
5079 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005080 }
5081
5082 java_import {
5083 name: "libfoo",
5084 jars: ["libfoo.jar"],
5085 apex_available: ["myapex"],
5086 }
5087
5088 java_library {
5089 name: "libfoo",
5090 srcs: ["foo/bar/MyClass.java"],
5091 apex_available: ["myapex"],
5092 }
Paul Duffin37856732021-02-26 14:24:15 +00005093
5094 java_sdk_library_import {
5095 name: "libbar",
5096 public: {
5097 jars: ["libbar.jar"],
5098 },
5099 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005100 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005101 }
5102
5103 java_sdk_library {
5104 name: "libbar",
5105 srcs: ["foo/bar/MyClass.java"],
5106 unsafe_ignore_missing_latest_api: true,
5107 apex_available: ["myapex"],
5108 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005109 `
5110
Paul Duffin89f570a2021-06-16 01:42:33 +01005111 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005112 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5113 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005114
Paul Duffin537ea3d2021-05-14 10:38:00 +01005115 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin4fd997b2021-02-03 20:06:33 +00005116 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin51d7da22021-06-16 02:04:13 +01005117 out/soong/.intermediates/libbar/android_common_myapex/javac/libbar.jar
5118 out/soong/.intermediates/libfoo/android_common_apex10000/javac/libfoo.jar
5119 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005120 })
5121
5122 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5123 bp := `
5124 apex {
5125 name: "myapex",
5126 enabled: false,
5127 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005128 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005129 }
5130
5131 apex_key {
5132 name: "myapex.key",
5133 public_key: "testkey.avbpubkey",
5134 private_key: "testkey.pem",
5135 }
5136
5137 prebuilt_apex {
5138 name: "myapex",
5139 arch: {
5140 arm64: {
5141 src: "myapex-arm64.apex",
5142 },
5143 arm: {
5144 src: "myapex-arm.apex",
5145 },
5146 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005147 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5148 }
5149
5150 prebuilt_bootclasspath_fragment {
5151 name: "my-bootclasspath-fragment",
5152 contents: ["libfoo", "libbar"],
5153 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005154 }
5155
5156 java_import {
5157 name: "libfoo",
5158 prefer: true,
5159 jars: ["libfoo.jar"],
5160 apex_available: ["myapex"],
5161 }
5162
5163 java_library {
5164 name: "libfoo",
5165 srcs: ["foo/bar/MyClass.java"],
5166 apex_available: ["myapex"],
5167 }
Paul Duffin37856732021-02-26 14:24:15 +00005168
5169 java_sdk_library_import {
5170 name: "libbar",
5171 prefer: true,
5172 public: {
5173 jars: ["libbar.jar"],
5174 },
5175 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005176 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005177 }
5178
5179 java_sdk_library {
5180 name: "libbar",
5181 srcs: ["foo/bar/MyClass.java"],
5182 unsafe_ignore_missing_latest_api: true,
5183 apex_available: ["myapex"],
5184 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005185 `
5186
Paul Duffin89f570a2021-06-16 01:42:33 +01005187 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005188 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5189 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005190
Paul Duffin537ea3d2021-05-14 10:38:00 +01005191 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00005192 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin51d7da22021-06-16 02:04:13 +01005193 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
5194 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
5195 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005196 })
5197}
5198
Roland Levillain630846d2019-06-26 12:48:34 +01005199func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005200 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005201 apex_test {
5202 name: "myapex",
5203 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005204 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005205 tests: [
5206 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005207 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005208 ],
5209 }
5210
5211 apex_key {
5212 name: "myapex.key",
5213 public_key: "testkey.avbpubkey",
5214 private_key: "testkey.pem",
5215 }
5216
Liz Kammer1c14a212020-05-12 15:26:55 -07005217 filegroup {
5218 name: "fg",
5219 srcs: [
5220 "baz",
5221 "bar/baz"
5222 ],
5223 }
5224
Roland Levillain630846d2019-06-26 12:48:34 +01005225 cc_test {
5226 name: "mytest",
5227 gtest: false,
5228 srcs: ["mytest.cpp"],
5229 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005230 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005231 system_shared_libs: [],
5232 static_executable: true,
5233 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005234 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005235 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005236
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005237 cc_library {
5238 name: "mylib",
5239 srcs: ["mylib.cpp"],
5240 system_shared_libs: [],
5241 stl: "none",
5242 }
5243
Liz Kammer5bd365f2020-05-27 15:15:11 -07005244 filegroup {
5245 name: "fg2",
5246 srcs: [
5247 "testdata/baz"
5248 ],
5249 }
5250
Roland Levillain9b5fde92019-06-28 15:41:19 +01005251 cc_test {
5252 name: "mytests",
5253 gtest: false,
5254 srcs: [
5255 "mytest1.cpp",
5256 "mytest2.cpp",
5257 "mytest3.cpp",
5258 ],
5259 test_per_src: true,
5260 relative_install_path: "test",
5261 system_shared_libs: [],
5262 static_executable: true,
5263 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005264 data: [
5265 ":fg",
5266 ":fg2",
5267 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005268 }
Roland Levillain630846d2019-06-26 12:48:34 +01005269 `)
5270
Sundong Ahnabb64432019-10-22 13:58:29 +09005271 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005272 copyCmds := apexRule.Args["copy_commands"]
5273
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005274 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005275 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005276 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005277
Liz Kammer1c14a212020-05-12 15:26:55 -07005278 //Ensure that test data are copied into apex.
5279 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5280 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5281
Roland Levillain9b5fde92019-06-28 15:41:19 +01005282 // Ensure that test deps built with `test_per_src` are copied into apex.
5283 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5284 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5285 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005286
5287 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005288 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005289 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005290 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005291 prefix := "TARGET_"
5292 var builder strings.Builder
5293 data.Custom(&builder, name, prefix, "", data)
5294 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09005295 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5296 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5297 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5298 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09005299 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09005300 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005301 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005302
5303 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005304 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005305 data.Custom(&builder, name, prefix, "", data)
5306 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005307 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5308 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005309}
5310
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005311func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005312 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005313 apex {
5314 name: "myapex",
5315 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005316 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005317 }
5318 apex_key {
5319 name: "myapex.key",
5320 public_key: "testkey.avbpubkey",
5321 private_key: "testkey.pem",
5322 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005323 `,
5324 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5325 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5326 }),
5327 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005328 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09005329 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005330 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005331 var builder strings.Builder
5332 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5333 androidMk := builder.String()
5334 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
5335}
5336
Jooyung Hand48f3c32019-08-23 11:18:57 +09005337func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5338 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5339 apex {
5340 name: "myapex",
5341 key: "myapex.key",
5342 native_shared_libs: ["libfoo"],
5343 }
5344
5345 apex_key {
5346 name: "myapex.key",
5347 public_key: "testkey.avbpubkey",
5348 private_key: "testkey.pem",
5349 }
5350
5351 cc_library {
5352 name: "libfoo",
5353 stl: "none",
5354 system_shared_libs: [],
5355 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005356 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005357 }
5358 `)
5359 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5360 apex {
5361 name: "myapex",
5362 key: "myapex.key",
5363 java_libs: ["myjar"],
5364 }
5365
5366 apex_key {
5367 name: "myapex.key",
5368 public_key: "testkey.avbpubkey",
5369 private_key: "testkey.pem",
5370 }
5371
5372 java_library {
5373 name: "myjar",
5374 srcs: ["foo/bar/MyClass.java"],
5375 sdk_version: "none",
5376 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005377 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005378 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005379 }
5380 `)
5381}
5382
Bill Peckhama41a6962021-01-11 10:58:54 -08005383func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005384 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005385 apex {
5386 name: "myapex",
5387 key: "myapex.key",
5388 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005389 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005390 }
5391
5392 apex_key {
5393 name: "myapex.key",
5394 public_key: "testkey.avbpubkey",
5395 private_key: "testkey.pem",
5396 }
5397
5398 java_import {
5399 name: "myjavaimport",
5400 apex_available: ["myapex"],
5401 jars: ["my.jar"],
5402 compile_dex: true,
5403 }
5404 `)
5405
5406 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5407 apexRule := module.Rule("apexRule")
5408 copyCmds := apexRule.Args["copy_commands"]
5409 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5410}
5411
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005412func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005413 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005414 apex {
5415 name: "myapex",
5416 key: "myapex.key",
5417 apps: [
5418 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005419 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005420 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005421 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005422 }
5423
5424 apex_key {
5425 name: "myapex.key",
5426 public_key: "testkey.avbpubkey",
5427 private_key: "testkey.pem",
5428 }
5429
5430 android_app {
5431 name: "AppFoo",
5432 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005433 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005434 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005435 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005436 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005437 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005438 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005439
5440 android_app {
5441 name: "AppFooPriv",
5442 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005443 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005444 system_modules: "none",
5445 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005446 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005447 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005448 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005449
5450 cc_library_shared {
5451 name: "libjni",
5452 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005453 shared_libs: ["libfoo"],
5454 stl: "none",
5455 system_shared_libs: [],
5456 apex_available: [ "myapex" ],
5457 sdk_version: "current",
5458 }
5459
5460 cc_library_shared {
5461 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005462 stl: "none",
5463 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005464 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005465 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005466 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005467 `)
5468
Sundong Ahnabb64432019-10-22 13:58:29 +09005469 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005470 apexRule := module.Rule("apexRule")
5471 copyCmds := apexRule.Args["copy_commands"]
5472
5473 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005474 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005475
Colin Crossaede88c2020-08-11 12:17:01 -07005476 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005477 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005478 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005479 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005480 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005481 // JNI libraries including transitive deps are
5482 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005483 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005484 // ... embedded inside APK (jnilibs.zip)
5485 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5486 // ... and not directly inside the APEX
5487 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5488 }
Dario Frenicde2a032019-10-27 00:29:22 +01005489}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005490
Dario Frenicde2a032019-10-27 00:29:22 +01005491func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005492 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005493 apex {
5494 name: "myapex",
5495 key: "myapex.key",
5496 apps: [
5497 "AppFooPrebuilt",
5498 "AppFooPrivPrebuilt",
5499 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005500 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005501 }
5502
5503 apex_key {
5504 name: "myapex.key",
5505 public_key: "testkey.avbpubkey",
5506 private_key: "testkey.pem",
5507 }
5508
5509 android_app_import {
5510 name: "AppFooPrebuilt",
5511 apk: "PrebuiltAppFoo.apk",
5512 presigned: true,
5513 dex_preopt: {
5514 enabled: false,
5515 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005516 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005517 }
5518
5519 android_app_import {
5520 name: "AppFooPrivPrebuilt",
5521 apk: "PrebuiltAppFooPriv.apk",
5522 privileged: true,
5523 presigned: true,
5524 dex_preopt: {
5525 enabled: false,
5526 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005527 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005528 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005529 }
5530 `)
5531
Sundong Ahnabb64432019-10-22 13:58:29 +09005532 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005533 apexRule := module.Rule("apexRule")
5534 copyCmds := apexRule.Args["copy_commands"]
5535
5536 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005537 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5538}
5539
5540func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005541 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005542 apex {
5543 name: "myapex",
5544 key: "myapex.key",
5545 apps: [
5546 "AppFoo",
5547 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005548 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005549 }
5550
5551 apex_key {
5552 name: "myapex.key",
5553 public_key: "testkey.avbpubkey",
5554 private_key: "testkey.pem",
5555 }
5556
5557 android_app {
5558 name: "AppFoo",
5559 srcs: ["foo/bar/MyClass.java"],
5560 sdk_version: "none",
5561 system_modules: "none",
5562 apex_available: [ "myapex" ],
5563 }
5564
5565 android_app_import {
5566 name: "AppFoo",
5567 apk: "AppFooPrebuilt.apk",
5568 filename: "AppFooPrebuilt.apk",
5569 presigned: true,
5570 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005571 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005572 }
5573 `, withFiles(map[string][]byte{
5574 "AppFooPrebuilt.apk": nil,
5575 }))
5576
5577 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005578 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005579 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005580}
5581
Dario Freni6f3937c2019-12-20 22:58:03 +00005582func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005583 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005584 apex {
5585 name: "myapex",
5586 key: "myapex.key",
5587 apps: [
5588 "TesterHelpAppFoo",
5589 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005590 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005591 }
5592
5593 apex_key {
5594 name: "myapex.key",
5595 public_key: "testkey.avbpubkey",
5596 private_key: "testkey.pem",
5597 }
5598
5599 android_test_helper_app {
5600 name: "TesterHelpAppFoo",
5601 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005602 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005603 }
5604
5605 `)
5606
5607 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5608 apexRule := module.Rule("apexRule")
5609 copyCmds := apexRule.Args["copy_commands"]
5610
5611 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5612}
5613
Jooyung Han18020ea2019-11-13 10:50:48 +09005614func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5615 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005616 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005617 apex {
5618 name: "myapex",
5619 key: "myapex.key",
5620 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005621 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005622 }
5623
5624 apex_key {
5625 name: "myapex.key",
5626 public_key: "testkey.avbpubkey",
5627 private_key: "testkey.pem",
5628 }
5629
5630 apex {
5631 name: "otherapex",
5632 key: "myapex.key",
5633 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005634 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005635 }
5636
5637 cc_defaults {
5638 name: "libfoo-defaults",
5639 apex_available: ["otherapex"],
5640 }
5641
5642 cc_library {
5643 name: "libfoo",
5644 defaults: ["libfoo-defaults"],
5645 stl: "none",
5646 system_shared_libs: [],
5647 }`)
5648}
5649
Paul Duffine52e66f2020-03-30 17:54:29 +01005650func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005651 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005652 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005653 apex {
5654 name: "myapex",
5655 key: "myapex.key",
5656 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005657 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005658 }
5659
5660 apex_key {
5661 name: "myapex.key",
5662 public_key: "testkey.avbpubkey",
5663 private_key: "testkey.pem",
5664 }
5665
5666 apex {
5667 name: "otherapex",
5668 key: "otherapex.key",
5669 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005670 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005671 }
5672
5673 apex_key {
5674 name: "otherapex.key",
5675 public_key: "testkey.avbpubkey",
5676 private_key: "testkey.pem",
5677 }
5678
5679 cc_library {
5680 name: "libfoo",
5681 stl: "none",
5682 system_shared_libs: [],
5683 apex_available: ["otherapex"],
5684 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005685}
Jiyong Park127b40b2019-09-30 16:04:35 +09005686
Paul Duffine52e66f2020-03-30 17:54:29 +01005687func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005688 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005689 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005690.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005691.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005692.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005693.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005694.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005695.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005696 apex {
5697 name: "myapex",
5698 key: "myapex.key",
5699 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005700 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005701 }
5702
5703 apex_key {
5704 name: "myapex.key",
5705 public_key: "testkey.avbpubkey",
5706 private_key: "testkey.pem",
5707 }
5708
Jiyong Park127b40b2019-09-30 16:04:35 +09005709 cc_library {
5710 name: "libfoo",
5711 stl: "none",
5712 shared_libs: ["libbar"],
5713 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005714 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005715 }
5716
5717 cc_library {
5718 name: "libbar",
5719 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005720 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005721 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005722 apex_available: ["myapex"],
5723 }
5724
5725 cc_library {
5726 name: "libbaz",
5727 stl: "none",
5728 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005729 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005730}
Jiyong Park127b40b2019-09-30 16:04:35 +09005731
Paul Duffine52e66f2020-03-30 17:54:29 +01005732func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005733 testApexError(t, "\"otherapex\" is not a valid module name", `
5734 apex {
5735 name: "myapex",
5736 key: "myapex.key",
5737 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005738 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005739 }
5740
5741 apex_key {
5742 name: "myapex.key",
5743 public_key: "testkey.avbpubkey",
5744 private_key: "testkey.pem",
5745 }
5746
5747 cc_library {
5748 name: "libfoo",
5749 stl: "none",
5750 system_shared_libs: [],
5751 apex_available: ["otherapex"],
5752 }`)
5753
Paul Duffine52e66f2020-03-30 17:54:29 +01005754 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005755 apex {
5756 name: "myapex",
5757 key: "myapex.key",
5758 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005759 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005760 }
5761
5762 apex_key {
5763 name: "myapex.key",
5764 public_key: "testkey.avbpubkey",
5765 private_key: "testkey.pem",
5766 }
5767
5768 cc_library {
5769 name: "libfoo",
5770 stl: "none",
5771 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005772 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005773 apex_available: ["myapex"],
5774 }
5775
5776 cc_library {
5777 name: "libbar",
5778 stl: "none",
5779 system_shared_libs: [],
5780 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005781 }
5782
5783 cc_library {
5784 name: "libbaz",
5785 stl: "none",
5786 system_shared_libs: [],
5787 stubs: {
5788 versions: ["10", "20", "30"],
5789 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005790 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005791}
Jiyong Park127b40b2019-09-30 16:04:35 +09005792
Jiyong Park89e850a2020-04-07 16:37:39 +09005793func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005794 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005795 apex {
5796 name: "myapex",
5797 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005798 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005799 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005800 }
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: "libfoo",
5810 stl: "none",
5811 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005812 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005813 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005814 }
5815
5816 cc_library {
5817 name: "libfoo2",
5818 stl: "none",
5819 system_shared_libs: [],
5820 shared_libs: ["libbaz"],
5821 apex_available: ["//apex_available:platform"],
5822 }
5823
5824 cc_library {
5825 name: "libbar",
5826 stl: "none",
5827 system_shared_libs: [],
5828 apex_available: ["myapex"],
5829 }
5830
5831 cc_library {
5832 name: "libbaz",
5833 stl: "none",
5834 system_shared_libs: [],
5835 apex_available: ["myapex"],
5836 stubs: {
5837 versions: ["1"],
5838 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005839 }`)
5840
Jiyong Park89e850a2020-04-07 16:37:39 +09005841 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5842 // because it depends on libbar which isn't available to platform
5843 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5844 if libfoo.NotAvailableForPlatform() != true {
5845 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5846 }
5847
5848 // libfoo2 however can be available to platform because it depends on libbaz which provides
5849 // stubs
5850 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5851 if libfoo2.NotAvailableForPlatform() == true {
5852 t.Errorf("%q should be available to platform", libfoo2.String())
5853 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005854}
Jiyong Parka90ca002019-10-07 15:47:24 +09005855
Paul Duffine52e66f2020-03-30 17:54:29 +01005856func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005857 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005858 apex {
5859 name: "myapex",
5860 key: "myapex.key",
5861 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005862 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005863 }
5864
5865 apex_key {
5866 name: "myapex.key",
5867 public_key: "testkey.avbpubkey",
5868 private_key: "testkey.pem",
5869 }
5870
5871 cc_library {
5872 name: "libfoo",
5873 stl: "none",
5874 system_shared_libs: [],
5875 apex_available: ["myapex"],
5876 static: {
5877 apex_available: ["//apex_available:platform"],
5878 },
5879 }`)
5880
Jiyong Park89e850a2020-04-07 16:37:39 +09005881 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5882 if libfooShared.NotAvailableForPlatform() != true {
5883 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5884 }
5885 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5886 if libfooStatic.NotAvailableForPlatform() != false {
5887 t.Errorf("%q should be available to platform", libfooStatic.String())
5888 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005889}
5890
Jiyong Park5d790c32019-11-15 18:40:32 +09005891func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005892 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005893 apex {
5894 name: "myapex",
5895 key: "myapex.key",
5896 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005897 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005898 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005899 }
5900
5901 override_apex {
5902 name: "override_myapex",
5903 base: "myapex",
5904 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005905 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005906 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005907 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005908 key: "mynewapex.key",
5909 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09005910 }
5911
5912 apex_key {
5913 name: "myapex.key",
5914 public_key: "testkey.avbpubkey",
5915 private_key: "testkey.pem",
5916 }
5917
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005918 apex_key {
5919 name: "mynewapex.key",
5920 public_key: "testkey2.avbpubkey",
5921 private_key: "testkey2.pem",
5922 }
5923
5924 android_app_certificate {
5925 name: "myapex.certificate",
5926 certificate: "testkey",
5927 }
5928
Jiyong Park5d790c32019-11-15 18:40:32 +09005929 android_app {
5930 name: "app",
5931 srcs: ["foo/bar/MyClass.java"],
5932 package_name: "foo",
5933 sdk_version: "none",
5934 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005935 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005936 }
5937
5938 override_android_app {
5939 name: "override_app",
5940 base: "app",
5941 package_name: "bar",
5942 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005943 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005944
Jiyong Park317645e2019-12-05 13:20:58 +09005945 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5946 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5947 if originalVariant.GetOverriddenBy() != "" {
5948 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5949 }
5950 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5951 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5952 }
5953
Jiyong Park5d790c32019-11-15 18:40:32 +09005954 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5955 apexRule := module.Rule("apexRule")
5956 copyCmds := apexRule.Args["copy_commands"]
5957
5958 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005959 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005960
5961 apexBundle := module.Module().(*apexBundle)
5962 name := apexBundle.Name()
5963 if name != "override_myapex" {
5964 t.Errorf("name should be \"override_myapex\", but was %q", name)
5965 }
5966
Baligh Uddin004d7172020-02-19 21:29:28 -08005967 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5968 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5969 }
5970
Jiyong Park20bacab2020-03-03 11:45:41 +09005971 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005972 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005973 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
5974
5975 signApkRule := module.Rule("signapk")
5976 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09005977
Colin Crossaa255532020-07-03 13:18:24 -07005978 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005979 var builder strings.Builder
5980 data.Custom(&builder, name, "TARGET_", "", data)
5981 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005982 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005983 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5984 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005985 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005986 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005987 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005988 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5989 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005990}
5991
Jooyung Han214bf372019-11-12 13:03:50 +09005992func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005993 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005994 apex {
5995 name: "myapex",
5996 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005997 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005998 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005999 }
6000
6001 apex_key {
6002 name: "myapex.key",
6003 public_key: "testkey.avbpubkey",
6004 private_key: "testkey.pem",
6005 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006006
6007 cc_library {
6008 name: "mylib",
6009 srcs: ["mylib.cpp"],
6010 stl: "libc++",
6011 system_shared_libs: [],
6012 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006013 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006014 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006015 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006016
6017 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6018 args := module.Rule("apexRule").Args
6019 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006020 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006021
6022 // The copies of the libraries in the apex should have one more dependency than
6023 // the ones outside the apex, namely the unwinder. Ideally we should check
6024 // the dependency names directly here but for some reason the names are blank in
6025 // this test.
6026 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006027 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006028 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6029 if len(apexImplicits) != len(nonApexImplicits)+1 {
6030 t.Errorf("%q missing unwinder dep", lib)
6031 }
6032 }
Jooyung Han214bf372019-11-12 13:03:50 +09006033}
6034
Paul Duffine05480a2021-03-08 15:07:14 +00006035var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006036 "api/current.txt": nil,
6037 "api/removed.txt": nil,
6038 "api/system-current.txt": nil,
6039 "api/system-removed.txt": nil,
6040 "api/test-current.txt": nil,
6041 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006042
Anton Hanssondff2c782020-12-21 17:10:01 +00006043 "100/public/api/foo.txt": nil,
6044 "100/public/api/foo-removed.txt": nil,
6045 "100/system/api/foo.txt": nil,
6046 "100/system/api/foo-removed.txt": nil,
6047
Paul Duffineedc5d52020-06-12 17:46:39 +01006048 // For java_sdk_library_import
6049 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006050}
6051
Jooyung Han58f26ab2019-12-18 15:34:32 +09006052func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006053 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006054 apex {
6055 name: "myapex",
6056 key: "myapex.key",
6057 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006058 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006059 }
6060
6061 apex_key {
6062 name: "myapex.key",
6063 public_key: "testkey.avbpubkey",
6064 private_key: "testkey.pem",
6065 }
6066
6067 java_sdk_library {
6068 name: "foo",
6069 srcs: ["a.java"],
6070 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006071 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006072 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006073
6074 prebuilt_apis {
6075 name: "sdk",
6076 api_dirs: ["100"],
6077 }
Paul Duffin9b879592020-05-26 13:21:35 +01006078 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006079
6080 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006081 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006082 "javalib/foo.jar",
6083 "etc/permissions/foo.xml",
6084 })
6085 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006086 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
6087 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09006088}
6089
Paul Duffin9b879592020-05-26 13:21:35 +01006090func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006091 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006092 apex {
6093 name: "myapex",
6094 key: "myapex.key",
6095 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006096 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006097 }
6098
6099 apex_key {
6100 name: "myapex.key",
6101 public_key: "testkey.avbpubkey",
6102 private_key: "testkey.pem",
6103 }
6104
6105 java_sdk_library {
6106 name: "foo",
6107 srcs: ["a.java"],
6108 api_packages: ["foo"],
6109 apex_available: ["myapex"],
6110 sdk_version: "none",
6111 system_modules: "none",
6112 }
6113
6114 java_library {
6115 name: "bar",
6116 srcs: ["a.java"],
6117 libs: ["foo"],
6118 apex_available: ["myapex"],
6119 sdk_version: "none",
6120 system_modules: "none",
6121 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006122
6123 prebuilt_apis {
6124 name: "sdk",
6125 api_dirs: ["100"],
6126 }
Paul Duffin9b879592020-05-26 13:21:35 +01006127 `, withFiles(filesForSdkLibrary))
6128
6129 // java_sdk_library installs both impl jar and permission XML
6130 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6131 "javalib/bar.jar",
6132 "javalib/foo.jar",
6133 "etc/permissions/foo.xml",
6134 })
6135
6136 // The bar library should depend on the implementation jar.
6137 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006138 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006139 t.Errorf("expected %q, found %#q", expected, actual)
6140 }
6141}
6142
6143func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006144 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006145 apex {
6146 name: "myapex",
6147 key: "myapex.key",
6148 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006149 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006150 }
6151
6152 apex_key {
6153 name: "myapex.key",
6154 public_key: "testkey.avbpubkey",
6155 private_key: "testkey.pem",
6156 }
6157
6158 java_sdk_library {
6159 name: "foo",
6160 srcs: ["a.java"],
6161 api_packages: ["foo"],
6162 apex_available: ["myapex"],
6163 sdk_version: "none",
6164 system_modules: "none",
6165 }
6166
6167 java_library {
6168 name: "bar",
6169 srcs: ["a.java"],
6170 libs: ["foo"],
6171 sdk_version: "none",
6172 system_modules: "none",
6173 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006174
6175 prebuilt_apis {
6176 name: "sdk",
6177 api_dirs: ["100"],
6178 }
Paul Duffin9b879592020-05-26 13:21:35 +01006179 `, withFiles(filesForSdkLibrary))
6180
6181 // java_sdk_library installs both impl jar and permission XML
6182 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6183 "javalib/foo.jar",
6184 "etc/permissions/foo.xml",
6185 })
6186
6187 // The bar library should depend on the stubs jar.
6188 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006189 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006190 t.Errorf("expected %q, found %#q", expected, actual)
6191 }
6192}
6193
Paul Duffineedc5d52020-06-12 17:46:39 +01006194func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006195 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006196 prebuilt_apis {
6197 name: "sdk",
6198 api_dirs: ["100"],
6199 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006200 withFiles(map[string][]byte{
6201 "apex/a.java": nil,
6202 "apex/apex_manifest.json": nil,
6203 "apex/Android.bp": []byte(`
6204 package {
6205 default_visibility: ["//visibility:private"],
6206 }
6207
6208 apex {
6209 name: "myapex",
6210 key: "myapex.key",
6211 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006212 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006213 }
6214
6215 apex_key {
6216 name: "myapex.key",
6217 public_key: "testkey.avbpubkey",
6218 private_key: "testkey.pem",
6219 }
6220
6221 java_library {
6222 name: "bar",
6223 srcs: ["a.java"],
6224 libs: ["foo"],
6225 apex_available: ["myapex"],
6226 sdk_version: "none",
6227 system_modules: "none",
6228 }
6229`),
6230 "source/a.java": nil,
6231 "source/api/current.txt": nil,
6232 "source/api/removed.txt": nil,
6233 "source/Android.bp": []byte(`
6234 package {
6235 default_visibility: ["//visibility:private"],
6236 }
6237
6238 java_sdk_library {
6239 name: "foo",
6240 visibility: ["//apex"],
6241 srcs: ["a.java"],
6242 api_packages: ["foo"],
6243 apex_available: ["myapex"],
6244 sdk_version: "none",
6245 system_modules: "none",
6246 public: {
6247 enabled: true,
6248 },
6249 }
6250`),
6251 "prebuilt/a.jar": nil,
6252 "prebuilt/Android.bp": []byte(`
6253 package {
6254 default_visibility: ["//visibility:private"],
6255 }
6256
6257 java_sdk_library_import {
6258 name: "foo",
6259 visibility: ["//apex", "//source"],
6260 apex_available: ["myapex"],
6261 prefer: true,
6262 public: {
6263 jars: ["a.jar"],
6264 },
6265 }
6266`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006267 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006268 )
6269
6270 // java_sdk_library installs both impl jar and permission XML
6271 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6272 "javalib/bar.jar",
6273 "javalib/foo.jar",
6274 "etc/permissions/foo.xml",
6275 })
6276
6277 // The bar library should depend on the implementation jar.
6278 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006279 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006280 t.Errorf("expected %q, found %#q", expected, actual)
6281 }
6282}
6283
6284func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6285 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6286 apex {
6287 name: "myapex",
6288 key: "myapex.key",
6289 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006290 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006291 }
6292
6293 apex_key {
6294 name: "myapex.key",
6295 public_key: "testkey.avbpubkey",
6296 private_key: "testkey.pem",
6297 }
6298
6299 java_sdk_library_import {
6300 name: "foo",
6301 apex_available: ["myapex"],
6302 prefer: true,
6303 public: {
6304 jars: ["a.jar"],
6305 },
6306 }
6307
6308 `, withFiles(filesForSdkLibrary))
6309}
6310
atrost6e126252020-01-27 17:01:16 +00006311func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006312 result := android.GroupFixturePreparers(
6313 prepareForApexTest,
6314 java.PrepareForTestWithPlatformCompatConfig,
6315 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006316 apex {
6317 name: "myapex",
6318 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006319 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006320 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006321 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006322 }
6323
6324 apex_key {
6325 name: "myapex.key",
6326 public_key: "testkey.avbpubkey",
6327 private_key: "testkey.pem",
6328 }
6329
6330 platform_compat_config {
6331 name: "myjar-platform-compat-config",
6332 src: ":myjar",
6333 }
6334
6335 java_library {
6336 name: "myjar",
6337 srcs: ["foo/bar/MyClass.java"],
6338 sdk_version: "none",
6339 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006340 apex_available: [ "myapex" ],
6341 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006342
6343 // Make sure that a preferred prebuilt does not affect the apex contents.
6344 prebuilt_platform_compat_config {
6345 name: "myjar-platform-compat-config",
6346 metadata: "compat-config/metadata.xml",
6347 prefer: true,
6348 }
atrost6e126252020-01-27 17:01:16 +00006349 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006350 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006351 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6352 "etc/compatconfig/myjar-platform-compat-config.xml",
6353 "javalib/myjar.jar",
6354 })
6355}
6356
Jiyong Park479321d2019-12-16 11:47:12 +09006357func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6358 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6359 apex {
6360 name: "myapex",
6361 key: "myapex.key",
6362 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006363 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006364 }
6365
6366 apex_key {
6367 name: "myapex.key",
6368 public_key: "testkey.avbpubkey",
6369 private_key: "testkey.pem",
6370 }
6371
6372 java_library {
6373 name: "myjar",
6374 srcs: ["foo/bar/MyClass.java"],
6375 sdk_version: "none",
6376 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006377 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006378 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006379 }
6380 `)
6381}
6382
Jiyong Park7afd1072019-12-30 16:56:33 +09006383func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006384 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006385 apex {
6386 name: "myapex",
6387 key: "myapex.key",
6388 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006389 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006390 }
6391
6392 apex_key {
6393 name: "myapex.key",
6394 public_key: "testkey.avbpubkey",
6395 private_key: "testkey.pem",
6396 }
6397
6398 cc_library {
6399 name: "mylib",
6400 srcs: ["mylib.cpp"],
6401 system_shared_libs: [],
6402 stl: "none",
6403 required: ["a", "b"],
6404 host_required: ["c", "d"],
6405 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006406 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006407 }
6408 `)
6409
6410 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006411 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006412 name := apexBundle.BaseModuleName()
6413 prefix := "TARGET_"
6414 var builder strings.Builder
6415 data.Custom(&builder, name, prefix, "", data)
6416 androidMk := builder.String()
6417 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6418 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6419 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6420}
6421
Jiyong Park7cd10e32020-01-14 09:22:18 +09006422func TestSymlinksFromApexToSystem(t *testing.T) {
6423 bp := `
6424 apex {
6425 name: "myapex",
6426 key: "myapex.key",
6427 native_shared_libs: ["mylib"],
6428 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006429 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006430 }
6431
Jiyong Park9d677202020-02-19 16:29:35 +09006432 apex {
6433 name: "myapex.updatable",
6434 key: "myapex.key",
6435 native_shared_libs: ["mylib"],
6436 java_libs: ["myjar"],
6437 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006438 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006439 }
6440
Jiyong Park7cd10e32020-01-14 09:22:18 +09006441 apex_key {
6442 name: "myapex.key",
6443 public_key: "testkey.avbpubkey",
6444 private_key: "testkey.pem",
6445 }
6446
6447 cc_library {
6448 name: "mylib",
6449 srcs: ["mylib.cpp"],
6450 shared_libs: ["myotherlib"],
6451 system_shared_libs: [],
6452 stl: "none",
6453 apex_available: [
6454 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006455 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006456 "//apex_available:platform",
6457 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006458 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006459 }
6460
6461 cc_library {
6462 name: "myotherlib",
6463 srcs: ["mylib.cpp"],
6464 system_shared_libs: [],
6465 stl: "none",
6466 apex_available: [
6467 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006468 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006469 "//apex_available:platform",
6470 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006471 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006472 }
6473
6474 java_library {
6475 name: "myjar",
6476 srcs: ["foo/bar/MyClass.java"],
6477 sdk_version: "none",
6478 system_modules: "none",
6479 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006480 apex_available: [
6481 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006482 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006483 "//apex_available:platform",
6484 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006485 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006486 }
6487
6488 java_library {
6489 name: "myotherjar",
6490 srcs: ["foo/bar/MyClass.java"],
6491 sdk_version: "none",
6492 system_modules: "none",
6493 apex_available: [
6494 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006495 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006496 "//apex_available:platform",
6497 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006498 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006499 }
6500 `
6501
6502 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6503 for _, f := range files {
6504 if f.path == file {
6505 if f.isLink {
6506 t.Errorf("%q is not a real file", file)
6507 }
6508 return
6509 }
6510 }
6511 t.Errorf("%q is not found", file)
6512 }
6513
6514 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6515 for _, f := range files {
6516 if f.path == file {
6517 if !f.isLink {
6518 t.Errorf("%q is not a symlink", file)
6519 }
6520 return
6521 }
6522 }
6523 t.Errorf("%q is not found", file)
6524 }
6525
Jiyong Park9d677202020-02-19 16:29:35 +09006526 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6527 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006528 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006529 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006530 ensureRealfileExists(t, files, "javalib/myjar.jar")
6531 ensureRealfileExists(t, files, "lib64/mylib.so")
6532 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6533
Jiyong Park9d677202020-02-19 16:29:35 +09006534 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6535 ensureRealfileExists(t, files, "javalib/myjar.jar")
6536 ensureRealfileExists(t, files, "lib64/mylib.so")
6537 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6538
6539 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006540 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006541 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006542 ensureRealfileExists(t, files, "javalib/myjar.jar")
6543 ensureRealfileExists(t, files, "lib64/mylib.so")
6544 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006545
6546 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6547 ensureRealfileExists(t, files, "javalib/myjar.jar")
6548 ensureRealfileExists(t, files, "lib64/mylib.so")
6549 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006550}
6551
Yo Chiange8128052020-07-23 20:09:18 +08006552func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006553 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006554 apex {
6555 name: "myapex",
6556 key: "myapex.key",
6557 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006558 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006559 }
6560
6561 apex_key {
6562 name: "myapex.key",
6563 public_key: "testkey.avbpubkey",
6564 private_key: "testkey.pem",
6565 }
6566
6567 cc_library_shared {
6568 name: "mylib",
6569 srcs: ["mylib.cpp"],
6570 shared_libs: ["myotherlib"],
6571 system_shared_libs: [],
6572 stl: "none",
6573 apex_available: [
6574 "myapex",
6575 "//apex_available:platform",
6576 ],
6577 }
6578
6579 cc_prebuilt_library_shared {
6580 name: "myotherlib",
6581 srcs: ["prebuilt.so"],
6582 system_shared_libs: [],
6583 stl: "none",
6584 apex_available: [
6585 "myapex",
6586 "//apex_available:platform",
6587 ],
6588 }
6589 `)
6590
6591 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006592 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006593 var builder strings.Builder
6594 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6595 androidMk := builder.String()
6596 // `myotherlib` is added to `myapex` as symlink
6597 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6598 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6599 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6600 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006601 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += mylib.myapex:64 myotherlib:64 apex_manifest.pb.myapex apex_pubkey.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08006602}
6603
Jooyung Han643adc42020-02-27 13:50:06 +09006604func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006605 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006606 apex {
6607 name: "myapex",
6608 key: "myapex.key",
6609 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006610 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006611 }
6612
6613 apex_key {
6614 name: "myapex.key",
6615 public_key: "testkey.avbpubkey",
6616 private_key: "testkey.pem",
6617 }
6618
6619 cc_library {
6620 name: "mylib",
6621 srcs: ["mylib.cpp"],
6622 shared_libs: ["mylib2"],
6623 system_shared_libs: [],
6624 stl: "none",
6625 apex_available: [ "myapex" ],
6626 }
6627
6628 cc_library {
6629 name: "mylib2",
6630 srcs: ["mylib.cpp"],
6631 system_shared_libs: [],
6632 stl: "none",
6633 apex_available: [ "myapex" ],
6634 }
6635 `)
6636
6637 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6638 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6639 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6640 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6641 "lib64/mylib.so",
6642 "lib64/mylib2.so",
6643 })
6644}
6645
Jooyung Han49f67012020-04-17 13:43:10 +09006646func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006647 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006648 apex {
6649 name: "myapex",
6650 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006651 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006652 }
6653 apex_key {
6654 name: "myapex.key",
6655 public_key: "testkey.avbpubkey",
6656 private_key: "testkey.pem",
6657 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006658 `,
6659 android.FixtureModifyConfig(func(config android.Config) {
6660 delete(config.Targets, android.Android)
6661 config.AndroidCommonTarget = android.Target{}
6662 }),
6663 )
Jooyung Han49f67012020-04-17 13:43:10 +09006664
6665 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6666 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6667 }
6668}
6669
Jiyong Parkbd159612020-02-28 15:22:21 +09006670func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006671 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006672 apex {
6673 name: "myapex",
6674 key: "myapex.key",
6675 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006676 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006677 }
6678
6679 apex_key {
6680 name: "myapex.key",
6681 public_key: "testkey.avbpubkey",
6682 private_key: "testkey.pem",
6683 }
6684
6685 android_app {
6686 name: "AppFoo",
6687 srcs: ["foo/bar/MyClass.java"],
6688 sdk_version: "none",
6689 system_modules: "none",
6690 apex_available: [ "myapex" ],
6691 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006692 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006693
Colin Crosscf371cc2020-11-13 11:48:42 -08006694 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006695 content := bundleConfigRule.Args["content"]
6696
6697 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006698 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 +09006699}
6700
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006701func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006702 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006703 apex {
6704 name: "myapex",
6705 key: "myapex.key",
6706 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006707 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006708 }
6709
6710 apex_key {
6711 name: "myapex.key",
6712 public_key: "testkey.avbpubkey",
6713 private_key: "testkey.pem",
6714 }
6715
6716 android_app_set {
6717 name: "AppSet",
6718 set: "AppSet.apks",
6719 }`)
6720 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006721 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006722 content := bundleConfigRule.Args["content"]
6723 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6724 s := mod.Rule("apexRule").Args["copy_commands"]
6725 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6726 if len(copyCmds) != 3 {
6727 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6728 }
6729 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6730 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6731 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6732}
6733
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006734func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01006735 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006736 apex_set {
6737 name: "myapex",
6738 filename: "foo_v2.apex",
6739 sanitized: {
6740 none: { set: "myapex.apks", },
6741 hwaddress: { set: "myapex.hwasan.apks", },
6742 },
Paul Duffin24704672021-04-06 16:09:30 +01006743 }
6744 `
6745 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006746
Paul Duffin24704672021-04-06 16:09:30 +01006747 // Check that the extractor produces the correct output file from the correct input file.
6748 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006749
Paul Duffin24704672021-04-06 16:09:30 +01006750 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
6751 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006752
Paul Duffin24704672021-04-06 16:09:30 +01006753 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
6754
6755 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01006756 m = ctx.ModuleForTests("myapex", "android_common_myapex")
6757 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01006758
6759 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006760}
6761
Paul Duffin89f570a2021-06-16 01:42:33 +01006762func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006763 t.Helper()
6764
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006765 bp := `
6766 java_library {
6767 name: "some-updatable-apex-lib",
6768 srcs: ["a.java"],
6769 sdk_version: "current",
6770 apex_available: [
6771 "some-updatable-apex",
6772 ],
6773 }
6774
6775 java_library {
6776 name: "some-non-updatable-apex-lib",
6777 srcs: ["a.java"],
6778 apex_available: [
6779 "some-non-updatable-apex",
6780 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01006781 compile_dex: true,
6782 }
6783
6784 bootclasspath_fragment {
6785 name: "some-non-updatable-fragment",
6786 contents: ["some-non-updatable-apex-lib"],
6787 apex_available: [
6788 "some-non-updatable-apex",
6789 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006790 }
6791
6792 java_library {
6793 name: "some-platform-lib",
6794 srcs: ["a.java"],
6795 sdk_version: "current",
6796 installable: true,
6797 }
6798
6799 java_library {
6800 name: "some-art-lib",
6801 srcs: ["a.java"],
6802 sdk_version: "current",
6803 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006804 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006805 ],
6806 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01006807 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006808 }
6809
6810 apex {
6811 name: "some-updatable-apex",
6812 key: "some-updatable-apex.key",
6813 java_libs: ["some-updatable-apex-lib"],
6814 updatable: true,
6815 min_sdk_version: "current",
6816 }
6817
6818 apex {
6819 name: "some-non-updatable-apex",
6820 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01006821 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006822 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006823 }
6824
6825 apex_key {
6826 name: "some-updatable-apex.key",
6827 }
6828
6829 apex_key {
6830 name: "some-non-updatable-apex.key",
6831 }
6832
6833 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006834 name: "com.android.art.debug",
6835 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01006836 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006837 updatable: true,
6838 min_sdk_version: "current",
6839 }
6840
Paul Duffinf23bc472021-04-27 12:42:20 +01006841 bootclasspath_fragment {
6842 name: "art-bootclasspath-fragment",
6843 image_name: "art",
6844 contents: ["some-art-lib"],
6845 apex_available: [
6846 "com.android.art.debug",
6847 ],
6848 }
6849
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006850 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006851 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006852 }
6853
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006854 filegroup {
6855 name: "some-updatable-apex-file_contexts",
6856 srcs: [
6857 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6858 ],
6859 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006860
6861 filegroup {
6862 name: "some-non-updatable-apex-file_contexts",
6863 srcs: [
6864 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6865 ],
6866 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006867 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006868
Paul Duffin89f570a2021-06-16 01:42:33 +01006869 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00006870}
6871
Paul Duffin89f570a2021-06-16 01:42:33 +01006872func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006873 t.Helper()
6874
Paul Duffin55607122021-03-30 23:32:51 +01006875 fs := android.MockFS{
6876 "a.java": nil,
6877 "a.jar": nil,
6878 "apex_manifest.json": nil,
6879 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006880 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006881 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6882 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6883 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006884 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006885 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006886
Paul Duffin55607122021-03-30 23:32:51 +01006887 errorHandler := android.FixtureExpectsNoErrors
6888 if errmsg != "" {
6889 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006890 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006891
Paul Duffin55607122021-03-30 23:32:51 +01006892 result := android.GroupFixturePreparers(
6893 cc.PrepareForTestWithCcDefaultModules,
6894 java.PrepareForTestWithHiddenApiBuildComponents,
6895 java.PrepareForTestWithJavaDefaultModules,
6896 java.PrepareForTestWithJavaSdkLibraryFiles,
6897 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01006898 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01006899 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01006900 android.FixtureModifyMockFS(func(fs android.MockFS) {
6901 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
6902 insert := ""
6903 for _, fragment := range fragments {
6904 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
6905 }
6906 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
6907 platform_bootclasspath {
6908 name: "platform-bootclasspath",
6909 fragments: [
6910 %s
6911 ],
6912 }
6913 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01006914 }
Paul Duffin89f570a2021-06-16 01:42:33 +01006915 }),
Paul Duffin55607122021-03-30 23:32:51 +01006916 ).
6917 ExtendWithErrorHandler(errorHandler).
6918 RunTestWithBp(t, bp)
6919
6920 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006921}
6922
Jooyung Han548640b2020-04-27 12:10:30 +09006923func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6924 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6925 apex {
6926 name: "myapex",
6927 key: "myapex.key",
6928 updatable: true,
6929 }
6930
6931 apex_key {
6932 name: "myapex.key",
6933 public_key: "testkey.avbpubkey",
6934 private_key: "testkey.pem",
6935 }
6936 `)
6937}
6938
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006939func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6940 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6941 apex {
6942 name: "myapex",
6943 key: "myapex.key",
6944 }
6945
6946 apex_key {
6947 name: "myapex.key",
6948 public_key: "testkey.avbpubkey",
6949 private_key: "testkey.pem",
6950 }
6951 `)
6952}
6953
satayevb98371c2021-06-15 16:49:50 +01006954func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
6955 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
6956 apex {
6957 name: "myapex",
6958 key: "myapex.key",
6959 systemserverclasspath_fragments: [
6960 "mysystemserverclasspathfragment",
6961 ],
6962 min_sdk_version: "29",
6963 updatable: true,
6964 }
6965
6966 apex_key {
6967 name: "myapex.key",
6968 public_key: "testkey.avbpubkey",
6969 private_key: "testkey.pem",
6970 }
6971
6972 java_library {
6973 name: "foo",
6974 srcs: ["b.java"],
6975 min_sdk_version: "29",
6976 installable: true,
6977 apex_available: [
6978 "myapex",
6979 ],
6980 }
6981
6982 systemserverclasspath_fragment {
6983 name: "mysystemserverclasspathfragment",
6984 generate_classpaths_proto: false,
6985 contents: [
6986 "foo",
6987 ],
6988 apex_available: [
6989 "myapex",
6990 ],
6991 }
6992 `)
6993}
6994
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006995func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006996 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
6997 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
6998 // modules to be included in the BootJars.
6999 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7000 return android.GroupFixturePreparers(
7001 dexpreopt.FixtureSetBootJars(bootJars...),
7002 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7003 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7004 }),
7005 )
7006 }
7007
7008 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7009 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7010 // specified in the ArtApexJars configuration.
7011 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7012 return android.GroupFixturePreparers(
7013 dexpreopt.FixtureSetArtBootJars(bootJars...),
7014 dexpreopt.FixtureSetBootJars(bootJars...),
7015 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7016 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7017 }),
7018 )
7019 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007020
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007021 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007022 preparer := java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007023 fragment := java.ApexVariantReference{
7024 Apex: proptools.StringPtr("com.android.art.debug"),
7025 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7026 }
7027 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007028 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007029
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007030 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007031 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7032 // Update the dexpreopt BootJars directly.
7033 preparer := prepareSetBootJars("com.android.art.debug:some-art-lib")
7034 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007035 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007036
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007037 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007038 err := `ArtApexJars expects this to be in apex "some-updatable-apex" but this is only in apexes.*"com.android.art.debug"`
Paul Duffin60264a02021-04-12 20:02:36 +01007039 // Update the dexpreopt ArtApexJars directly.
7040 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7041 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007042 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007043
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007044 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007045 err := `ArtApexJars expects this to be in apex "some-non-updatable-apex" but this is only in apexes.*"com.android.art.debug"`
Paul Duffin60264a02021-04-12 20:02:36 +01007046 // Update the dexpreopt ArtApexJars directly.
7047 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
7048 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007049 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007050
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007051 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007052 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
7053 preparer := java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib")
7054 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007055 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007056
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007057 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007058 preparer := java.FixtureConfigureBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007059 fragment := java.ApexVariantReference{
7060 Apex: proptools.StringPtr("some-non-updatable-apex"),
7061 Module: proptools.StringPtr("some-non-updatable-fragment"),
7062 }
7063 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007064 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007065
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007066 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007067 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007068 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7069 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007070 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007071
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007072 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007073 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007074 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7075 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007076 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007077
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007078 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007079 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01007080 // Update the dexpreopt ArtApexJars directly.
7081 preparer := prepareSetArtJars("platform:some-platform-lib")
7082 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007083 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007084
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007085 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007086 preparer := java.FixtureConfigureBootJars("platform:some-platform-lib")
7087 testNoUpdatableJarsInBootImage(t, "", preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007088 })
Paul Duffin064b70c2020-11-02 17:32:38 +00007089}
7090
7091func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007092 preparer := java.FixtureConfigureBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00007093 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01007094 fragment := java.ApexVariantReference{
7095 Apex: proptools.StringPtr("myapex"),
7096 Module: proptools.StringPtr("my-bootclasspath-fragment"),
7097 }
7098
Paul Duffin064b70c2020-11-02 17:32:38 +00007099 testDexpreoptWithApexes(t, `
7100 prebuilt_apex {
7101 name: "myapex" ,
7102 arch: {
7103 arm64: {
7104 src: "myapex-arm64.apex",
7105 },
7106 arm: {
7107 src: "myapex-arm.apex",
7108 },
7109 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007110 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7111 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007112
Paul Duffin89f570a2021-06-16 01:42:33 +01007113 prebuilt_bootclasspath_fragment {
7114 name: "my-bootclasspath-fragment",
7115 contents: ["libfoo"],
7116 apex_available: ["myapex"],
7117 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007118
Paul Duffin89f570a2021-06-16 01:42:33 +01007119 java_import {
7120 name: "libfoo",
7121 jars: ["libfoo.jar"],
7122 apex_available: ["myapex"],
7123 }
7124 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00007125 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007126}
7127
Andrei Onea115e7e72020-06-05 21:14:03 +01007128func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
7129 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01007130 bp += `
7131 apex_key {
7132 name: "myapex.key",
7133 public_key: "testkey.avbpubkey",
7134 private_key: "testkey.pem",
7135 }`
Paul Duffin45338f02021-03-30 23:07:52 +01007136 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01007137 "lib1/src/A.java": nil,
7138 "lib2/src/B.java": nil,
7139 "system/sepolicy/apex/myapex-file_contexts": nil,
7140 }
7141
Paul Duffin45338f02021-03-30 23:07:52 +01007142 errorHandler := android.FixtureExpectsNoErrors
7143 if errmsg != "" {
7144 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07007145 }
Colin Crossae8600b2020-10-29 17:09:13 -07007146
Paul Duffin45338f02021-03-30 23:07:52 +01007147 android.GroupFixturePreparers(
7148 android.PrepareForTestWithAndroidBuildComponents,
7149 java.PrepareForTestWithJavaBuildComponents,
7150 PrepareForTestWithApexBuildComponents,
7151 android.PrepareForTestWithNeverallowRules(rules),
7152 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7153 updatableBootJars := make([]string, 0, len(apexBootJars))
7154 for _, apexBootJar := range apexBootJars {
7155 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
7156 }
7157 variables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
7158 }),
7159 fs.AddToFixture(),
7160 ).
7161 ExtendWithErrorHandler(errorHandler).
7162 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01007163}
7164
7165func TestApexPermittedPackagesRules(t *testing.T) {
7166 testcases := []struct {
7167 name string
7168 expectedError string
7169 bp string
7170 bootJars []string
7171 modulesPackages map[string][]string
7172 }{
7173
7174 {
7175 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
7176 expectedError: "",
7177 bp: `
7178 java_library {
7179 name: "bcp_lib1",
7180 srcs: ["lib1/src/*.java"],
7181 permitted_packages: ["foo.bar"],
7182 apex_available: ["myapex"],
7183 sdk_version: "none",
7184 system_modules: "none",
7185 }
7186 java_library {
7187 name: "nonbcp_lib2",
7188 srcs: ["lib2/src/*.java"],
7189 apex_available: ["myapex"],
7190 permitted_packages: ["a.b"],
7191 sdk_version: "none",
7192 system_modules: "none",
7193 }
7194 apex {
7195 name: "myapex",
7196 key: "myapex.key",
7197 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007198 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007199 }`,
7200 bootJars: []string{"bcp_lib1"},
7201 modulesPackages: map[string][]string{
7202 "myapex": []string{
7203 "foo.bar",
7204 },
7205 },
7206 },
7207 {
7208 name: "Bootclasspath apex jar not satisfying allowed module packages.",
7209 expectedError: `module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only allow these packages: foo.bar. Please jarjar or move code around.`,
7210 bp: `
7211 java_library {
7212 name: "bcp_lib1",
7213 srcs: ["lib1/src/*.java"],
7214 apex_available: ["myapex"],
7215 permitted_packages: ["foo.bar"],
7216 sdk_version: "none",
7217 system_modules: "none",
7218 }
7219 java_library {
7220 name: "bcp_lib2",
7221 srcs: ["lib2/src/*.java"],
7222 apex_available: ["myapex"],
7223 permitted_packages: ["foo.bar", "bar.baz"],
7224 sdk_version: "none",
7225 system_modules: "none",
7226 }
7227 apex {
7228 name: "myapex",
7229 key: "myapex.key",
7230 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007231 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007232 }
7233 `,
7234 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7235 modulesPackages: map[string][]string{
7236 "myapex": []string{
7237 "foo.bar",
7238 },
7239 },
7240 },
7241 }
7242 for _, tc := range testcases {
7243 t.Run(tc.name, func(t *testing.T) {
7244 rules := createApexPermittedPackagesRules(tc.modulesPackages)
7245 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
7246 })
7247 }
7248}
7249
Jiyong Park62304bb2020-04-13 16:19:48 +09007250func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007251 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09007252 apex {
7253 name: "myapex",
7254 key: "myapex.key",
7255 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007256 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09007257 }
7258
7259 apex_key {
7260 name: "myapex.key",
7261 public_key: "testkey.avbpubkey",
7262 private_key: "testkey.pem",
7263 }
7264
7265 cc_library {
7266 name: "mylib",
7267 srcs: ["mylib.cpp"],
7268 system_shared_libs: [],
7269 stl: "none",
7270 stubs: {
7271 versions: ["1"],
7272 },
7273 apex_available: ["myapex"],
7274 }
7275
7276 cc_library {
7277 name: "myprivlib",
7278 srcs: ["mylib.cpp"],
7279 system_shared_libs: [],
7280 stl: "none",
7281 apex_available: ["myapex"],
7282 }
7283
7284
7285 cc_test {
7286 name: "mytest",
7287 gtest: false,
7288 srcs: ["mylib.cpp"],
7289 system_shared_libs: [],
7290 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09007291 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09007292 test_for: ["myapex"]
7293 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007294
7295 cc_library {
7296 name: "mytestlib",
7297 srcs: ["mylib.cpp"],
7298 system_shared_libs: [],
7299 shared_libs: ["mylib", "myprivlib"],
7300 stl: "none",
7301 test_for: ["myapex"],
7302 }
7303
7304 cc_benchmark {
7305 name: "mybench",
7306 srcs: ["mylib.cpp"],
7307 system_shared_libs: [],
7308 shared_libs: ["mylib", "myprivlib"],
7309 stl: "none",
7310 test_for: ["myapex"],
7311 }
Jiyong Park62304bb2020-04-13 16:19:48 +09007312 `)
7313
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007314 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007315 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007316 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7317 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7318 }
7319
7320 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09007321 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007322 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7323 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7324 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7325}
Jiyong Park46a512f2020-12-04 18:02:13 +09007326
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007327func TestIndirectTestFor(t *testing.T) {
7328 ctx := testApex(t, `
7329 apex {
7330 name: "myapex",
7331 key: "myapex.key",
7332 native_shared_libs: ["mylib", "myprivlib"],
7333 updatable: false,
7334 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007335
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007336 apex_key {
7337 name: "myapex.key",
7338 public_key: "testkey.avbpubkey",
7339 private_key: "testkey.pem",
7340 }
7341
7342 cc_library {
7343 name: "mylib",
7344 srcs: ["mylib.cpp"],
7345 system_shared_libs: [],
7346 stl: "none",
7347 stubs: {
7348 versions: ["1"],
7349 },
7350 apex_available: ["myapex"],
7351 }
7352
7353 cc_library {
7354 name: "myprivlib",
7355 srcs: ["mylib.cpp"],
7356 system_shared_libs: [],
7357 stl: "none",
7358 shared_libs: ["mylib"],
7359 apex_available: ["myapex"],
7360 }
7361
7362 cc_library {
7363 name: "mytestlib",
7364 srcs: ["mylib.cpp"],
7365 system_shared_libs: [],
7366 shared_libs: ["myprivlib"],
7367 stl: "none",
7368 test_for: ["myapex"],
7369 }
7370 `)
7371
7372 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007373 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007374 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7375 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7376 }
7377
7378 // The platform variant of mytestlib links to the platform variant of the
7379 // internal myprivlib.
7380 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
7381
7382 // The platform variant of myprivlib links to the platform variant of mylib
7383 // and bypasses its stubs.
7384 ensureLinkedLibIs("myprivlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09007385}
7386
Martin Stjernholmec009002021-03-27 15:18:31 +00007387func TestTestForForLibInOtherApex(t *testing.T) {
7388 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
7389 _ = testApex(t, `
7390 apex {
7391 name: "com.android.art",
7392 key: "myapex.key",
7393 native_shared_libs: ["mylib"],
7394 updatable: false,
7395 }
7396
7397 apex {
7398 name: "com.android.art.debug",
7399 key: "myapex.key",
7400 native_shared_libs: ["mylib", "mytestlib"],
7401 updatable: false,
7402 }
7403
7404 apex_key {
7405 name: "myapex.key",
7406 public_key: "testkey.avbpubkey",
7407 private_key: "testkey.pem",
7408 }
7409
7410 cc_library {
7411 name: "mylib",
7412 srcs: ["mylib.cpp"],
7413 system_shared_libs: [],
7414 stl: "none",
7415 stubs: {
7416 versions: ["1"],
7417 },
7418 apex_available: ["com.android.art", "com.android.art.debug"],
7419 }
7420
7421 cc_library {
7422 name: "mytestlib",
7423 srcs: ["mylib.cpp"],
7424 system_shared_libs: [],
7425 shared_libs: ["mylib"],
7426 stl: "none",
7427 apex_available: ["com.android.art.debug"],
7428 test_for: ["com.android.art"],
7429 }
7430 `,
7431 android.MockFS{
7432 "system/sepolicy/apex/com.android.art-file_contexts": nil,
7433 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
7434 }.AddToFixture())
7435}
7436
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007437// TODO(jungjw): Move this to proptools
7438func intPtr(i int) *int {
7439 return &i
7440}
7441
7442func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007443 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007444 apex_set {
7445 name: "myapex",
7446 set: "myapex.apks",
7447 filename: "foo_v2.apex",
7448 overrides: ["foo"],
7449 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007450 `,
7451 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7452 variables.Platform_sdk_version = intPtr(30)
7453 }),
7454 android.FixtureModifyConfig(func(config android.Config) {
7455 config.Targets[android.Android] = []android.Target{
7456 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7457 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7458 }
7459 }),
7460 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007461
Paul Duffin24704672021-04-06 16:09:30 +01007462 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007463
7464 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01007465 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007466 actual := extractedApex.Args["abis"]
7467 expected := "ARMEABI_V7A,ARM64_V8A"
7468 if actual != expected {
7469 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7470 }
7471 actual = extractedApex.Args["sdk-version"]
7472 expected = "30"
7473 if actual != expected {
7474 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7475 }
7476
Paul Duffin6717d882021-06-15 19:09:41 +01007477 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007478 a := m.Module().(*ApexSet)
7479 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007480 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007481 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7482 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7483 }
7484}
7485
Jiyong Park7d95a512020-05-10 15:16:24 +09007486func TestNoStaticLinkingToStubsLib(t *testing.T) {
7487 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7488 apex {
7489 name: "myapex",
7490 key: "myapex.key",
7491 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007492 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007493 }
7494
7495 apex_key {
7496 name: "myapex.key",
7497 public_key: "testkey.avbpubkey",
7498 private_key: "testkey.pem",
7499 }
7500
7501 cc_library {
7502 name: "mylib",
7503 srcs: ["mylib.cpp"],
7504 static_libs: ["otherlib"],
7505 system_shared_libs: [],
7506 stl: "none",
7507 apex_available: [ "myapex" ],
7508 }
7509
7510 cc_library {
7511 name: "otherlib",
7512 srcs: ["mylib.cpp"],
7513 system_shared_libs: [],
7514 stl: "none",
7515 stubs: {
7516 versions: ["1", "2", "3"],
7517 },
7518 apex_available: [ "myapex" ],
7519 }
7520 `)
7521}
7522
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007523func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007524 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007525 apex {
7526 name: "myapex",
7527 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007528 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007529 }
7530
7531 apex_key {
7532 name: "myapex.key",
7533 public_key: "testkey.avbpubkey",
7534 private_key: "testkey.pem",
7535 }
7536
7537 prebuilt_apex {
7538 name: "myapex",
7539 prefer: true,
7540 arch: {
7541 arm64: {
7542 src: "myapex-arm64.apex",
7543 },
7544 arm: {
7545 src: "myapex-arm.apex",
7546 },
7547 },
7548 }
7549
7550 apex_set {
7551 name: "myapex_set",
7552 set: "myapex.apks",
7553 filename: "myapex_set.apex",
7554 overrides: ["myapex"],
7555 }
7556 `)
7557
7558 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7559 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7560 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 +09007561 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 +09007562}
7563
Jooyung Han938b5932020-06-20 12:47:47 +09007564func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007565 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007566 apex {
7567 name: "myapex",
7568 key: "myapex.key",
7569 apps: ["app"],
7570 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007571 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007572 }
7573
7574 apex_key {
7575 name: "myapex.key",
7576 public_key: "testkey.avbpubkey",
7577 private_key: "testkey.pem",
7578 }
7579
7580 android_app {
7581 name: "app",
7582 srcs: ["foo/bar/MyClass.java"],
7583 package_name: "foo",
7584 sdk_version: "none",
7585 system_modules: "none",
7586 apex_available: [ "myapex" ],
7587 }
7588 `, withFiles(map[string][]byte{
7589 "sub/Android.bp": []byte(`
7590 override_apex {
7591 name: "override_myapex",
7592 base: "myapex",
7593 apps: ["override_app"],
7594 allowed_files: ":allowed",
7595 }
7596 // Overridable "path" property should be referenced indirectly
7597 filegroup {
7598 name: "allowed",
7599 srcs: ["allowed.txt"],
7600 }
7601 override_android_app {
7602 name: "override_app",
7603 base: "app",
7604 package_name: "bar",
7605 }
7606 `),
7607 }))
7608
7609 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7610 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7611 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7612 }
7613
7614 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7615 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7616 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7617 }
7618}
7619
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007620func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007621 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007622 apex {
7623 name: "myapex",
7624 key: "myapex.key",
7625 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007626 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007627 }
7628
7629 apex_key {
7630 name: "myapex.key",
7631 public_key: "testkey.avbpubkey",
7632 private_key: "testkey.pem",
7633 }
7634
7635 cc_library {
7636 name: "mylib",
7637 srcs: ["mylib.cpp"],
7638 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007639 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007640 },
7641 apex_available: ["myapex"],
7642 }
7643
7644 cc_prebuilt_library_shared {
7645 name: "mylib",
7646 prefer: false,
7647 srcs: ["prebuilt.so"],
7648 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007649 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007650 },
7651 apex_available: ["myapex"],
7652 }
7653 `)
7654}
7655
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007656func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007657 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007658 apex {
7659 name: "myapex",
7660 key: "myapex.key",
7661 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007662 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007663 }
7664 apex_key {
7665 name: "myapex.key",
7666 public_key: "testkey.avbpubkey",
7667 private_key: "testkey.pem",
7668 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007669 `,
7670 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7671 variables.CompressedApex = proptools.BoolPtr(true)
7672 }),
7673 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007674
7675 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7676 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7677
7678 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7679 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7680
7681 // Make sure output of bundle is .capex
7682 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7683 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7684
7685 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007686 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007687 var builder strings.Builder
7688 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7689 androidMk := builder.String()
7690 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7691}
7692
Martin Stjernholm2856c662020-12-02 15:03:42 +00007693func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007694 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007695 apex {
7696 name: "myapex",
7697 key: "myapex.key",
7698 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007699 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007700 }
7701
7702 apex_key {
7703 name: "myapex.key",
7704 public_key: "testkey.avbpubkey",
7705 private_key: "testkey.pem",
7706 }
7707
7708 cc_library {
7709 name: "mylib",
7710 srcs: ["mylib.cpp"],
7711 apex_available: ["myapex"],
7712 shared_libs: ["otherlib"],
7713 system_shared_libs: [],
7714 }
7715
7716 cc_library {
7717 name: "otherlib",
7718 srcs: ["mylib.cpp"],
7719 stubs: {
7720 versions: ["current"],
7721 },
7722 }
7723
7724 cc_prebuilt_library_shared {
7725 name: "otherlib",
7726 prefer: true,
7727 srcs: ["prebuilt.so"],
7728 stubs: {
7729 versions: ["current"],
7730 },
7731 }
7732 `)
7733
7734 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007735 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007736 var builder strings.Builder
7737 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7738 androidMk := builder.String()
7739
7740 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7741 // a thing there.
7742 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7743}
7744
Jiyong Parke3867542020-12-03 17:28:25 +09007745func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007746 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007747 apex {
7748 name: "myapex",
7749 key: "myapex.key",
7750 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007751 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007752 }
7753
7754 apex_key {
7755 name: "myapex.key",
7756 public_key: "testkey.avbpubkey",
7757 private_key: "testkey.pem",
7758 }
7759
7760 cc_library {
7761 name: "mylib",
7762 srcs: ["mylib.cpp"],
7763 system_shared_libs: [],
7764 stl: "none",
7765 apex_available: ["myapex"],
7766 shared_libs: ["mylib2"],
7767 target: {
7768 apex: {
7769 exclude_shared_libs: ["mylib2"],
7770 },
7771 },
7772 }
7773
7774 cc_library {
7775 name: "mylib2",
7776 srcs: ["mylib.cpp"],
7777 system_shared_libs: [],
7778 stl: "none",
7779 }
7780 `)
7781
7782 // Check if mylib is linked to mylib2 for the non-apex target
7783 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7784 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7785
7786 // Make sure that the link doesn't occur for the apex target
7787 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7788 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7789
7790 // It shouldn't appear in the copy cmd as well.
7791 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7792 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7793}
7794
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007795func TestPrebuiltStubLibDep(t *testing.T) {
7796 bpBase := `
7797 apex {
7798 name: "myapex",
7799 key: "myapex.key",
7800 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007801 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007802 }
7803 apex_key {
7804 name: "myapex.key",
7805 public_key: "testkey.avbpubkey",
7806 private_key: "testkey.pem",
7807 }
7808 cc_library {
7809 name: "mylib",
7810 srcs: ["mylib.cpp"],
7811 apex_available: ["myapex"],
7812 shared_libs: ["stublib"],
7813 system_shared_libs: [],
7814 }
7815 apex {
7816 name: "otherapex",
7817 enabled: %s,
7818 key: "myapex.key",
7819 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007820 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007821 }
7822 `
7823
7824 stublibSourceBp := `
7825 cc_library {
7826 name: "stublib",
7827 srcs: ["mylib.cpp"],
7828 apex_available: ["otherapex"],
7829 system_shared_libs: [],
7830 stl: "none",
7831 stubs: {
7832 versions: ["1"],
7833 },
7834 }
7835 `
7836
7837 stublibPrebuiltBp := `
7838 cc_prebuilt_library_shared {
7839 name: "stublib",
7840 srcs: ["prebuilt.so"],
7841 apex_available: ["otherapex"],
7842 stubs: {
7843 versions: ["1"],
7844 },
7845 %s
7846 }
7847 `
7848
7849 tests := []struct {
7850 name string
7851 stublibBp string
7852 usePrebuilt bool
7853 modNames []string // Modules to collect AndroidMkEntries for
7854 otherApexEnabled []string
7855 }{
7856 {
7857 name: "only_source",
7858 stublibBp: stublibSourceBp,
7859 usePrebuilt: false,
7860 modNames: []string{"stublib"},
7861 otherApexEnabled: []string{"true", "false"},
7862 },
7863 {
7864 name: "source_preferred",
7865 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7866 usePrebuilt: false,
7867 modNames: []string{"stublib", "prebuilt_stublib"},
7868 otherApexEnabled: []string{"true", "false"},
7869 },
7870 {
7871 name: "prebuilt_preferred",
7872 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7873 usePrebuilt: true,
7874 modNames: []string{"stublib", "prebuilt_stublib"},
7875 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7876 },
7877 {
7878 name: "only_prebuilt",
7879 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7880 usePrebuilt: true,
7881 modNames: []string{"stublib"},
7882 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7883 },
7884 }
7885
7886 for _, test := range tests {
7887 t.Run(test.name, func(t *testing.T) {
7888 for _, otherApexEnabled := range test.otherApexEnabled {
7889 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007890 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007891
7892 type modAndMkEntries struct {
7893 mod *cc.Module
7894 mkEntries android.AndroidMkEntries
7895 }
7896 entries := []*modAndMkEntries{}
7897
7898 // Gather shared lib modules that are installable
7899 for _, modName := range test.modNames {
7900 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7901 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7902 continue
7903 }
7904 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007905 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007906 continue
7907 }
Colin Crossaa255532020-07-03 13:18:24 -07007908 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007909 if ent.Disabled {
7910 continue
7911 }
7912 entries = append(entries, &modAndMkEntries{
7913 mod: mod,
7914 mkEntries: ent,
7915 })
7916 }
7917 }
7918 }
7919
7920 var entry *modAndMkEntries = nil
7921 for _, ent := range entries {
7922 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7923 if entry != nil {
7924 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7925 } else {
7926 entry = ent
7927 }
7928 }
7929 }
7930
7931 if entry == nil {
7932 t.Errorf("AndroidMk entry for \"stublib\" missing")
7933 } else {
7934 isPrebuilt := entry.mod.Prebuilt() != nil
7935 if isPrebuilt != test.usePrebuilt {
7936 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7937 }
7938 if !entry.mod.IsStubs() {
7939 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7940 }
7941 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7942 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7943 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007944 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09007945 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09007946 if !android.InList(expected, cflags) {
7947 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7948 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007949 }
7950 })
7951 }
7952 })
7953 }
7954}
7955
Martin Stjernholmdf298b32021-05-21 20:57:29 +01007956func TestHostApexInHostOnlyBuild(t *testing.T) {
7957 testApex(t, `
7958 apex {
7959 name: "myapex",
7960 host_supported: true,
7961 key: "myapex.key",
7962 updatable: false,
7963 payload_type: "zip",
7964 }
7965 apex_key {
7966 name: "myapex.key",
7967 public_key: "testkey.avbpubkey",
7968 private_key: "testkey.pem",
7969 }
7970 `,
7971 android.FixtureModifyConfig(func(config android.Config) {
7972 // We may not have device targets in all builds, e.g. in
7973 // prebuilts/build-tools/build-prebuilts.sh
7974 config.Targets[android.Android] = []android.Target{}
7975 }))
7976}
7977
Colin Crossc33e5212021-05-25 18:16:02 -07007978func TestApexJavaCoverage(t *testing.T) {
7979 bp := `
7980 apex {
7981 name: "myapex",
7982 key: "myapex.key",
7983 java_libs: ["mylib"],
7984 bootclasspath_fragments: ["mybootclasspathfragment"],
7985 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
7986 updatable: false,
7987 }
7988
7989 apex_key {
7990 name: "myapex.key",
7991 public_key: "testkey.avbpubkey",
7992 private_key: "testkey.pem",
7993 }
7994
7995 java_library {
7996 name: "mylib",
7997 srcs: ["mylib.java"],
7998 apex_available: ["myapex"],
7999 compile_dex: true,
8000 }
8001
8002 bootclasspath_fragment {
8003 name: "mybootclasspathfragment",
8004 contents: ["mybootclasspathlib"],
8005 apex_available: ["myapex"],
8006 }
8007
8008 java_library {
8009 name: "mybootclasspathlib",
8010 srcs: ["mybootclasspathlib.java"],
8011 apex_available: ["myapex"],
8012 compile_dex: true,
8013 }
8014
8015 systemserverclasspath_fragment {
8016 name: "mysystemserverclasspathfragment",
8017 contents: ["mysystemserverclasspathlib"],
8018 apex_available: ["myapex"],
8019 }
8020
8021 java_library {
8022 name: "mysystemserverclasspathlib",
8023 srcs: ["mysystemserverclasspathlib.java"],
8024 apex_available: ["myapex"],
8025 compile_dex: true,
8026 }
8027 `
8028
8029 result := android.GroupFixturePreparers(
8030 PrepareForTestWithApexBuildComponents,
8031 prepareForTestWithMyapex,
8032 java.PrepareForTestWithJavaDefaultModules,
8033 android.PrepareForTestWithAndroidBuildComponents,
8034 android.FixtureWithRootAndroidBp(bp),
8035 android.FixtureMergeEnv(map[string]string{
8036 "EMMA_INSTRUMENT": "true",
8037 }),
8038 ).RunTest(t)
8039
8040 // Make sure jacoco ran on both mylib and mybootclasspathlib
8041 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8042 t.Errorf("Failed to find jacoco rule for mylib")
8043 }
8044 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8045 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
8046 }
8047 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8048 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
8049 }
8050}
8051
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008052func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01008053 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008054}