blob: 600f6c21706edd46ab146da0766ea6446cb93d58 [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: [],
Alex Light3d673592019-01-18 14:37:31 -0800377 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700378 apex_available: [ "myapex", "com.android.gki.*" ],
379 }
380
Jiyong Park99644e92020-11-17 22:21:02 +0900381 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000382 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900383 srcs: ["foo.rs"],
384 rlibs: ["libfoo.rlib.rust"],
385 dylibs: ["libfoo.dylib.rust"],
386 apex_available: ["myapex"],
387 }
388
389 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000390 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900391 srcs: ["foo.rs"],
392 crate_name: "foo",
393 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900394 shared_libs: ["libfoo.shared_from_rust"],
395 }
396
397 cc_library_shared {
398 name: "libfoo.shared_from_rust",
399 srcs: ["mylib.cpp"],
400 system_shared_libs: [],
401 stl: "none",
402 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900403 }
404
405 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000406 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900407 srcs: ["foo.rs"],
408 crate_name: "foo",
409 apex_available: ["myapex"],
410 }
411
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900412 rust_ffi_shared {
413 name: "libfoo.ffi",
414 srcs: ["foo.rs"],
415 crate_name: "foo",
416 apex_available: ["myapex"],
417 }
418
419 rust_ffi_shared {
420 name: "libbar.ffi",
421 srcs: ["foo.rs"],
422 crate_name: "bar",
423 apex_available: ["myapex"],
424 }
425
Yifan Hongd22a84a2020-07-28 17:37:46 -0700426 apex {
427 name: "com.android.gki.fake",
428 binaries: ["foo"],
429 key: "myapex.key",
430 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000431 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800432 }
433
Paul Duffindddd5462020-04-07 15:25:44 +0100434 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900435 name: "mylib2",
436 srcs: ["mylib.cpp"],
437 system_shared_libs: [],
438 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900439 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900440 static_libs: ["libstatic"],
441 // TODO: remove //apex_available:platform
442 apex_available: [
443 "//apex_available:platform",
444 "myapex",
445 ],
446 }
447
Paul Duffindddd5462020-04-07 15:25:44 +0100448 cc_prebuilt_library_shared {
449 name: "mylib2",
450 srcs: ["prebuilt.so"],
451 // TODO: remove //apex_available:platform
452 apex_available: [
453 "//apex_available:platform",
454 "myapex",
455 ],
456 }
457
Jiyong Park9918e1a2020-03-17 19:16:40 +0900458 cc_library_static {
459 name: "libstatic",
460 srcs: ["mylib.cpp"],
461 system_shared_libs: [],
462 stl: "none",
463 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000464 // TODO: remove //apex_available:platform
465 apex_available: [
466 "//apex_available:platform",
467 "myapex",
468 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900469 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900470
471 java_library {
472 name: "myjar",
473 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900474 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475 sdk_version: "none",
476 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900478 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000479 // TODO: remove //apex_available:platform
480 apex_available: [
481 "//apex_available:platform",
482 "myapex",
483 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 }
485
Jiyong Park77acec62020-06-01 21:39:15 +0900486 dex_import {
487 name: "myjar_dex",
488 jars: ["prebuilt.jar"],
489 apex_available: [
490 "//apex_available:platform",
491 "myapex",
492 ],
493 }
494
Jiyong Park7f7766d2019-07-25 22:02:35 +0900495 java_library {
496 name: "myotherjar",
497 srcs: ["foo/bar/MyClass.java"],
498 sdk_version: "none",
499 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900500 // TODO: remove //apex_available:platform
501 apex_available: [
502 "//apex_available:platform",
503 "myapex",
504 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900505 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900506
507 java_library {
508 name: "mysharedjar",
509 srcs: ["foo/bar/MyClass.java"],
510 sdk_version: "none",
511 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900512 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900513 `)
514
Paul Duffina71a67a2021-03-29 00:42:57 +0100515 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900516
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900517 // Make sure that Android.mk is created
518 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700519 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900520 var builder strings.Builder
521 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
522
523 androidMk := builder.String()
524 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
525 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
526
Jiyong Park42cca6c2019-04-01 11:15:50 +0900527 optFlags := apexRule.Args["opt_flags"]
528 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700529 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100530 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900531
Jiyong Park25fc6a92018-11-18 18:02:45 +0900532 copyCmds := apexRule.Args["copy_commands"]
533
534 // Ensure that main rule creates an output
535 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
536
537 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700538 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
539 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
540 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900541 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900542 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900543
544 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700545 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
546 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900547 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
548 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900551
552 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800553 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
554 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900555 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900556 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900557 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900558 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
559 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900560 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900561 // .. but not for java libs
562 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900563 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800564
Colin Cross7113d202019-11-20 16:39:12 -0800565 // Ensure that the platform variant ends with _shared or _common
566 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
567 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900568 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
569 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900570 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
571
572 // Ensure that dynamic dependency to java libs are not included
573 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800574
575 // Ensure that all symlinks are present.
576 found_foo_link_64 := false
577 found_foo := false
578 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900579 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800580 if strings.HasSuffix(cmd, "bin/foo") {
581 found_foo = true
582 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
583 found_foo_link_64 = true
584 }
585 }
586 }
587 good := found_foo && found_foo_link_64
588 if !good {
589 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
590 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900591
Sundong Ahnabb64432019-10-22 13:58:29 +0900592 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700593 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900594 if len(noticeInputs) != 3 {
595 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900596 }
597 ensureListContains(t, noticeInputs, "NOTICE")
598 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900599 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900600
Artur Satayeva8bd1132020-04-27 18:07:06 +0100601 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100602 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100603 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
604 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
605 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100606
607 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100608 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100609 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
610 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
611 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800612}
613
Jooyung Hanf21c7972019-12-16 22:32:06 +0900614func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800615 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900616 apex_defaults {
617 name: "myapex-defaults",
618 key: "myapex.key",
619 prebuilts: ["myetc"],
620 native_shared_libs: ["mylib"],
621 java_libs: ["myjar"],
622 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900623 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800624 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000625 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900626 }
627
628 prebuilt_etc {
629 name: "myetc",
630 src: "myprebuilt",
631 }
632
633 apex {
634 name: "myapex",
635 defaults: ["myapex-defaults"],
636 }
637
638 apex_key {
639 name: "myapex.key",
640 public_key: "testkey.avbpubkey",
641 private_key: "testkey.pem",
642 }
643
644 cc_library {
645 name: "mylib",
646 system_shared_libs: [],
647 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000648 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900649 }
650
651 java_library {
652 name: "myjar",
653 srcs: ["foo/bar/MyClass.java"],
654 sdk_version: "none",
655 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000656 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900657 }
658
659 android_app {
660 name: "AppFoo",
661 srcs: ["foo/bar/MyClass.java"],
662 sdk_version: "none",
663 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000664 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900665 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900666
667 runtime_resource_overlay {
668 name: "rro",
669 theme: "blue",
670 }
671
markchien2f59ec92020-09-02 16:23:38 +0800672 bpf {
673 name: "bpf",
674 srcs: ["bpf.c", "bpf2.c"],
675 }
676
Jooyung Hanf21c7972019-12-16 22:32:06 +0900677 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000678 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 "etc/myetc",
680 "javalib/myjar.jar",
681 "lib64/mylib.so",
682 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900683 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800684 "etc/bpf/bpf.o",
685 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900686 })
687}
688
Jooyung Han01a3ee22019-11-02 02:52:25 +0900689func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800690 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900691 apex {
692 name: "myapex",
693 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000694 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900695 }
696
697 apex_key {
698 name: "myapex.key",
699 public_key: "testkey.avbpubkey",
700 private_key: "testkey.pem",
701 }
702 `)
703
704 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900705 args := module.Rule("apexRule").Args
706 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
707 t.Error("manifest should be apex_manifest.pb, but " + manifest)
708 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900709}
710
Liz Kammer4854a7d2021-05-27 14:28:27 -0400711func TestApexManifestMinSdkVersion(t *testing.T) {
712 ctx := testApex(t, `
713 apex_defaults {
714 name: "my_defaults",
715 key: "myapex.key",
716 product_specific: true,
717 file_contexts: ":my-file-contexts",
718 updatable: false,
719 }
720 apex {
721 name: "myapex_30",
722 min_sdk_version: "30",
723 defaults: ["my_defaults"],
724 }
725
726 apex {
727 name: "myapex_current",
728 min_sdk_version: "current",
729 defaults: ["my_defaults"],
730 }
731
732 apex {
733 name: "myapex_none",
734 defaults: ["my_defaults"],
735 }
736
737 apex_key {
738 name: "myapex.key",
739 public_key: "testkey.avbpubkey",
740 private_key: "testkey.pem",
741 }
742
743 filegroup {
744 name: "my-file-contexts",
745 srcs: ["product_specific_file_contexts"],
746 }
747 `, withFiles(map[string][]byte{
748 "product_specific_file_contexts": nil,
749 }), android.FixtureModifyProductVariables(
750 func(variables android.FixtureProductVariables) {
751 variables.Unbundled_build = proptools.BoolPtr(true)
752 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
753 }), android.FixtureMergeEnv(map[string]string{
754 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
755 }))
756
757 testCases := []struct {
758 module string
759 minSdkVersion string
760 }{
761 {
762 module: "myapex_30",
763 minSdkVersion: "30",
764 },
765 {
766 module: "myapex_current",
767 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
768 },
769 {
770 module: "myapex_none",
771 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
772 },
773 }
774 for _, tc := range testCases {
775 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
776 args := module.Rule("apexRule").Args
777 optFlags := args["opt_flags"]
778 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
779 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
780 }
781 }
782}
783
Alex Light5098a612018-11-29 17:12:15 -0800784func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800785 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800786 apex {
787 name: "myapex",
788 key: "myapex.key",
789 payload_type: "zip",
790 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000791 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800792 }
793
794 apex_key {
795 name: "myapex.key",
796 public_key: "testkey.avbpubkey",
797 private_key: "testkey.pem",
798 }
799
800 cc_library {
801 name: "mylib",
802 srcs: ["mylib.cpp"],
803 shared_libs: ["mylib2"],
804 system_shared_libs: [],
805 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000806 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800807 }
808
809 cc_library {
810 name: "mylib2",
811 srcs: ["mylib.cpp"],
812 system_shared_libs: [],
813 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000814 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800815 }
816 `)
817
Sundong Ahnabb64432019-10-22 13:58:29 +0900818 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800819 copyCmds := zipApexRule.Args["copy_commands"]
820
821 // Ensure that main rule creates an output
822 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
823
824 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700825 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800826
827 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700828 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800829
830 // Ensure that both direct and indirect deps are copied into apex
831 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
832 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900833}
834
835func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800836 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900837 apex {
838 name: "myapex",
839 key: "myapex.key",
840 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park7d55b612021-06-11 17:22:09 +0900841 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000842 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900843 }
844
845 apex_key {
846 name: "myapex.key",
847 public_key: "testkey.avbpubkey",
848 private_key: "testkey.pem",
849 }
850
851 cc_library {
852 name: "mylib",
853 srcs: ["mylib.cpp"],
854 shared_libs: ["mylib2", "mylib3"],
855 system_shared_libs: [],
856 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000857 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900858 }
859
860 cc_library {
861 name: "mylib2",
862 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900863 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900864 system_shared_libs: [],
865 stl: "none",
866 stubs: {
867 versions: ["1", "2", "3"],
868 },
869 }
870
871 cc_library {
872 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900873 srcs: ["mylib.cpp"],
874 shared_libs: ["mylib4"],
875 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900876 stl: "none",
877 stubs: {
878 versions: ["10", "11", "12"],
879 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000880 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900881 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900882
883 cc_library {
884 name: "mylib4",
885 srcs: ["mylib.cpp"],
886 system_shared_libs: [],
887 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000888 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900889 }
Jiyong Park7d55b612021-06-11 17:22:09 +0900890
891 rust_binary {
892 name: "foo.rust",
893 srcs: ["foo.rs"],
894 shared_libs: ["libfoo.shared_from_rust"],
895 prefer_rlib: true,
896 apex_available: ["myapex"],
897 }
898
899 cc_library_shared {
900 name: "libfoo.shared_from_rust",
901 srcs: ["mylib.cpp"],
902 system_shared_libs: [],
903 stl: "none",
904 stubs: {
905 versions: ["10", "11", "12"],
906 },
907 }
908
Jiyong Park25fc6a92018-11-18 18:02:45 +0900909 `)
910
Sundong Ahnabb64432019-10-22 13:58:29 +0900911 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900912 copyCmds := apexRule.Args["copy_commands"]
913
914 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800915 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900916
917 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800918 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900919
920 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800921 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900922
Colin Crossaede88c2020-08-11 12:17:01 -0700923 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900924
925 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900926 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900928 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900929
930 // 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 -0700931 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900932 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700933 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900934
935 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900936 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900937 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900938
939 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700940 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900941
Jooyung Hana57af4a2020-01-23 05:36:59 +0000942 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900943 "lib64/mylib.so",
944 "lib64/mylib3.so",
945 "lib64/mylib4.so",
Jiyong Park7d55b612021-06-11 17:22:09 +0900946 "bin/foo.rust",
947 "lib64/libc++.so", // by the implicit dependency from foo.rust
948 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900949 })
Jiyong Park7d55b612021-06-11 17:22:09 +0900950
951 // Ensure that stub dependency from a rust module is not included
952 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
953 // The rust module is linked to the stub cc library
954 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
955 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
956 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900957}
958
Jiyong Park9477c262021-06-22 20:23:05 +0900959func TestApexCanUsePrivateApis(t *testing.T) {
960 ctx := testApex(t, `
961 apex {
962 name: "myapex",
963 key: "myapex.key",
964 native_shared_libs: ["mylib"],
965 binaries: ["foo.rust"],
966 updatable: false,
967 platform_apis: true,
968 }
969
970 apex_key {
971 name: "myapex.key",
972 public_key: "testkey.avbpubkey",
973 private_key: "testkey.pem",
974 }
975
976 cc_library {
977 name: "mylib",
978 srcs: ["mylib.cpp"],
979 shared_libs: ["mylib2"],
980 system_shared_libs: [],
981 stl: "none",
982 apex_available: [ "myapex" ],
983 }
984
985 cc_library {
986 name: "mylib2",
987 srcs: ["mylib.cpp"],
988 cflags: ["-include mylib.h"],
989 system_shared_libs: [],
990 stl: "none",
991 stubs: {
992 versions: ["1", "2", "3"],
993 },
994 }
995
996 rust_binary {
997 name: "foo.rust",
998 srcs: ["foo.rs"],
999 shared_libs: ["libfoo.shared_from_rust"],
1000 prefer_rlib: true,
1001 apex_available: ["myapex"],
1002 }
1003
1004 cc_library_shared {
1005 name: "libfoo.shared_from_rust",
1006 srcs: ["mylib.cpp"],
1007 system_shared_libs: [],
1008 stl: "none",
1009 stubs: {
1010 versions: ["10", "11", "12"],
1011 },
1012 }
1013 `)
1014
1015 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1016 copyCmds := apexRule.Args["copy_commands"]
1017
1018 // Ensure that indirect stubs dep is not included
1019 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1020 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1021
1022 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1023 // of the platform_apis: true)
1024 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000_private").Rule("ld").Args["libFlags"]
1025 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1026 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1027 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000_private").Rule("rustc").Args["linkFlags"]
1028 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1029 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1030}
1031
Colin Cross7812fd32020-09-25 12:35:10 -07001032func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1033 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001034 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001035 apex {
1036 name: "myapex",
1037 key: "myapex.key",
1038 native_shared_libs: ["mylib", "mylib3"],
1039 min_sdk_version: "29",
1040 }
1041
1042 apex_key {
1043 name: "myapex.key",
1044 public_key: "testkey.avbpubkey",
1045 private_key: "testkey.pem",
1046 }
1047
1048 cc_library {
1049 name: "mylib",
1050 srcs: ["mylib.cpp"],
1051 shared_libs: ["mylib2", "mylib3"],
1052 system_shared_libs: [],
1053 stl: "none",
1054 apex_available: [ "myapex" ],
1055 min_sdk_version: "28",
1056 }
1057
1058 cc_library {
1059 name: "mylib2",
1060 srcs: ["mylib.cpp"],
1061 cflags: ["-include mylib.h"],
1062 system_shared_libs: [],
1063 stl: "none",
1064 stubs: {
1065 versions: ["28", "29", "30", "current"],
1066 },
1067 min_sdk_version: "28",
1068 }
1069
1070 cc_library {
1071 name: "mylib3",
1072 srcs: ["mylib.cpp"],
1073 shared_libs: ["mylib4"],
1074 system_shared_libs: [],
1075 stl: "none",
1076 stubs: {
1077 versions: ["28", "29", "30", "current"],
1078 },
1079 apex_available: [ "myapex" ],
1080 min_sdk_version: "28",
1081 }
1082
1083 cc_library {
1084 name: "mylib4",
1085 srcs: ["mylib.cpp"],
1086 system_shared_libs: [],
1087 stl: "none",
1088 apex_available: [ "myapex" ],
1089 min_sdk_version: "28",
1090 }
1091 `)
1092
1093 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1094 copyCmds := apexRule.Args["copy_commands"]
1095
1096 // Ensure that direct non-stubs dep is always included
1097 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1098
1099 // Ensure that indirect stubs dep is not included
1100 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1101
1102 // Ensure that direct stubs dep is included
1103 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1104
1105 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1106
Jiyong Park55549df2021-02-26 23:57:23 +09001107 // Ensure that mylib is linking with the latest version of stub for mylib2
1108 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001109 // ... and not linking to the non-stub (impl) variant of mylib2
1110 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1111
1112 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1113 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1114 // .. and not linking to the stubs variant of mylib3
1115 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1116
1117 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001118 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001119 ensureNotContains(t, mylib2Cflags, "-include ")
1120
1121 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -07001122 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -07001123
1124 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1125 "lib64/mylib.so",
1126 "lib64/mylib3.so",
1127 "lib64/mylib4.so",
1128 })
1129}
1130
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001131func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1132 t.Parallel()
1133 // myapex (Z)
1134 // mylib -----------------.
1135 // |
1136 // otherapex (29) |
1137 // libstub's versions: 29 Z current
1138 // |
1139 // <platform> |
1140 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001141 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001142 apex {
1143 name: "myapex",
1144 key: "myapex.key",
1145 native_shared_libs: ["mylib"],
1146 min_sdk_version: "Z", // non-final
1147 }
1148
1149 cc_library {
1150 name: "mylib",
1151 srcs: ["mylib.cpp"],
1152 shared_libs: ["libstub"],
1153 apex_available: ["myapex"],
1154 min_sdk_version: "Z",
1155 }
1156
1157 apex_key {
1158 name: "myapex.key",
1159 public_key: "testkey.avbpubkey",
1160 private_key: "testkey.pem",
1161 }
1162
1163 apex {
1164 name: "otherapex",
1165 key: "myapex.key",
1166 native_shared_libs: ["libstub"],
1167 min_sdk_version: "29",
1168 }
1169
1170 cc_library {
1171 name: "libstub",
1172 srcs: ["mylib.cpp"],
1173 stubs: {
1174 versions: ["29", "Z", "current"],
1175 },
1176 apex_available: ["otherapex"],
1177 min_sdk_version: "29",
1178 }
1179
1180 // platform module depending on libstub from otherapex should use the latest stub("current")
1181 cc_library {
1182 name: "libplatform",
1183 srcs: ["mylib.cpp"],
1184 shared_libs: ["libstub"],
1185 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001186 `,
1187 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1188 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1189 variables.Platform_sdk_final = proptools.BoolPtr(false)
1190 variables.Platform_version_active_codenames = []string{"Z"}
1191 }),
1192 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001193
Jiyong Park55549df2021-02-26 23:57:23 +09001194 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001195 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001196 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001197 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001198 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001199
1200 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1201 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1202 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1203 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1204 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1205}
1206
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001207func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001208 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001209 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001210 name: "myapex2",
1211 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001212 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001213 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001214 }
1215
1216 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001217 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001218 public_key: "testkey.avbpubkey",
1219 private_key: "testkey.pem",
1220 }
1221
1222 cc_library {
1223 name: "mylib",
1224 srcs: ["mylib.cpp"],
1225 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001226 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001227 system_shared_libs: [],
1228 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001229 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001230 }
1231
1232 cc_library {
1233 name: "libfoo",
1234 srcs: ["mylib.cpp"],
1235 shared_libs: ["libbar"],
1236 system_shared_libs: [],
1237 stl: "none",
1238 stubs: {
1239 versions: ["10", "20", "30"],
1240 },
1241 }
1242
1243 cc_library {
1244 name: "libbar",
1245 srcs: ["mylib.cpp"],
1246 system_shared_libs: [],
1247 stl: "none",
1248 }
1249
Jiyong Park678c8812020-02-07 17:25:49 +09001250 cc_library_static {
1251 name: "libbaz",
1252 srcs: ["mylib.cpp"],
1253 system_shared_libs: [],
1254 stl: "none",
1255 apex_available: [ "myapex2" ],
1256 }
1257
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001258 `)
1259
Jiyong Park83dc74b2020-01-14 18:38:44 +09001260 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001261 copyCmds := apexRule.Args["copy_commands"]
1262
1263 // Ensure that direct non-stubs dep is always included
1264 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1265
1266 // Ensure that indirect stubs dep is not included
1267 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1268
1269 // Ensure that dependency of stubs is not included
1270 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1271
Colin Crossaede88c2020-08-11 12:17:01 -07001272 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001273
1274 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001275 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001276 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001277 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001278
Jiyong Park3ff16992019-12-27 14:11:47 +09001279 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001280
1281 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1282 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001283
Artur Satayeva8bd1132020-04-27 18:07:06 +01001284 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001285 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001286
Artur Satayeva8bd1132020-04-27 18:07:06 +01001287 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001288 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001289}
1290
Jooyung Hand3639552019-08-09 12:57:43 +09001291func TestApexWithRuntimeLibsDependency(t *testing.T) {
1292 /*
1293 myapex
1294 |
1295 v (runtime_libs)
1296 mylib ------+------> libfoo [provides stub]
1297 |
1298 `------> libbar
1299 */
Colin Cross1c460562021-02-16 17:55:47 -08001300 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001301 apex {
1302 name: "myapex",
1303 key: "myapex.key",
1304 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001305 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001306 }
1307
1308 apex_key {
1309 name: "myapex.key",
1310 public_key: "testkey.avbpubkey",
1311 private_key: "testkey.pem",
1312 }
1313
1314 cc_library {
1315 name: "mylib",
1316 srcs: ["mylib.cpp"],
1317 runtime_libs: ["libfoo", "libbar"],
1318 system_shared_libs: [],
1319 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001320 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001321 }
1322
1323 cc_library {
1324 name: "libfoo",
1325 srcs: ["mylib.cpp"],
1326 system_shared_libs: [],
1327 stl: "none",
1328 stubs: {
1329 versions: ["10", "20", "30"],
1330 },
1331 }
1332
1333 cc_library {
1334 name: "libbar",
1335 srcs: ["mylib.cpp"],
1336 system_shared_libs: [],
1337 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001338 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001339 }
1340
1341 `)
1342
Sundong Ahnabb64432019-10-22 13:58:29 +09001343 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001344 copyCmds := apexRule.Args["copy_commands"]
1345
1346 // Ensure that direct non-stubs dep is always included
1347 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1348
1349 // Ensure that indirect stubs dep is not included
1350 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1351
1352 // Ensure that runtime_libs dep in included
1353 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1354
Sundong Ahnabb64432019-10-22 13:58:29 +09001355 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001356 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1357 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001358
1359}
1360
Paul Duffina02cae32021-03-09 01:44:06 +00001361var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1362 cc.PrepareForTestWithCcBuildComponents,
1363 PrepareForTestWithApexBuildComponents,
1364 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001365 apex {
1366 name: "com.android.runtime",
1367 key: "com.android.runtime.key",
1368 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001369 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001370 }
1371
1372 apex_key {
1373 name: "com.android.runtime.key",
1374 public_key: "testkey.avbpubkey",
1375 private_key: "testkey.pem",
1376 }
Paul Duffina02cae32021-03-09 01:44:06 +00001377 `),
1378 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1379)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001380
Paul Duffina02cae32021-03-09 01:44:06 +00001381func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001382 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001383 cc_library {
1384 name: "libc",
1385 no_libcrt: true,
1386 nocrt: true,
1387 stl: "none",
1388 system_shared_libs: [],
1389 stubs: { versions: ["1"] },
1390 apex_available: ["com.android.runtime"],
1391
1392 sanitize: {
1393 hwaddress: true,
1394 }
1395 }
1396
1397 cc_prebuilt_library_shared {
1398 name: "libclang_rt.hwasan-aarch64-android",
1399 no_libcrt: true,
1400 nocrt: true,
1401 stl: "none",
1402 system_shared_libs: [],
1403 srcs: [""],
1404 stubs: { versions: ["1"] },
1405
1406 sanitize: {
1407 never: true,
1408 },
Paul Duffina02cae32021-03-09 01:44:06 +00001409 } `)
1410 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001411
1412 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1413 "lib64/bionic/libc.so",
1414 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1415 })
1416
1417 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1418
1419 installed := hwasan.Description("install libclang_rt.hwasan")
1420 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1421
1422 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1423 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1424 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1425}
1426
1427func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001428 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001429 prepareForTestOfRuntimeApexWithHwasan,
1430 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1431 variables.SanitizeDevice = []string{"hwaddress"}
1432 }),
1433 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001434 cc_library {
1435 name: "libc",
1436 no_libcrt: true,
1437 nocrt: true,
1438 stl: "none",
1439 system_shared_libs: [],
1440 stubs: { versions: ["1"] },
1441 apex_available: ["com.android.runtime"],
1442 }
1443
1444 cc_prebuilt_library_shared {
1445 name: "libclang_rt.hwasan-aarch64-android",
1446 no_libcrt: true,
1447 nocrt: true,
1448 stl: "none",
1449 system_shared_libs: [],
1450 srcs: [""],
1451 stubs: { versions: ["1"] },
1452
1453 sanitize: {
1454 never: true,
1455 },
1456 }
Paul Duffina02cae32021-03-09 01:44:06 +00001457 `)
1458 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001459
1460 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1461 "lib64/bionic/libc.so",
1462 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1463 })
1464
1465 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1466
1467 installed := hwasan.Description("install libclang_rt.hwasan")
1468 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1469
1470 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1471 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1472 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1473}
1474
Jooyung Han61b66e92020-03-21 14:21:46 +00001475func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1476 testcases := []struct {
1477 name string
1478 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001479 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001480 shouldLink string
1481 shouldNotLink []string
1482 }{
1483 {
Jiyong Park55549df2021-02-26 23:57:23 +09001484 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001485 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001486 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001487 shouldLink: "current",
1488 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001489 },
1490 {
Jiyong Park55549df2021-02-26 23:57:23 +09001491 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001492 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001493 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001494 shouldLink: "current",
1495 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001496 },
1497 }
1498 for _, tc := range testcases {
1499 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001500 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001501 apex {
1502 name: "myapex",
1503 key: "myapex.key",
1504 use_vendor: true,
1505 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001506 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001507 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001508 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001509
Jooyung Han61b66e92020-03-21 14:21:46 +00001510 apex_key {
1511 name: "myapex.key",
1512 public_key: "testkey.avbpubkey",
1513 private_key: "testkey.pem",
1514 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001515
Jooyung Han61b66e92020-03-21 14:21:46 +00001516 cc_library {
1517 name: "mylib",
1518 srcs: ["mylib.cpp"],
1519 vendor_available: true,
1520 shared_libs: ["libbar"],
1521 system_shared_libs: [],
1522 stl: "none",
1523 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001524 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001525 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001526
Jooyung Han61b66e92020-03-21 14:21:46 +00001527 cc_library {
1528 name: "libbar",
1529 srcs: ["mylib.cpp"],
1530 system_shared_libs: [],
1531 stl: "none",
1532 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001533 llndk: {
1534 symbol_file: "libbar.map.txt",
1535 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001536 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001537 `,
1538 setUseVendorAllowListForTest([]string{"myapex"}),
1539 withUnbundledBuild,
1540 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001541
Jooyung Han61b66e92020-03-21 14:21:46 +00001542 // Ensure that LLNDK dep is not included
1543 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1544 "lib64/mylib.so",
1545 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001546
Jooyung Han61b66e92020-03-21 14:21:46 +00001547 // Ensure that LLNDK dep is required
1548 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1549 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1550 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001551
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001552 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1553 ensureContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001554 for _, ver := range tc.shouldNotLink {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001555 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001556 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001557
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001558 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001559 ver := tc.shouldLink
1560 if tc.shouldLink == "current" {
1561 ver = strconv.Itoa(android.FutureApiLevelInt)
1562 }
1563 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001564 })
1565 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001566}
1567
Jiyong Park25fc6a92018-11-18 18:02:45 +09001568func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001569 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001570 apex {
1571 name: "myapex",
1572 key: "myapex.key",
1573 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001574 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001575 }
1576
1577 apex_key {
1578 name: "myapex.key",
1579 public_key: "testkey.avbpubkey",
1580 private_key: "testkey.pem",
1581 }
1582
1583 cc_library {
1584 name: "mylib",
1585 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001586 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001587 shared_libs: ["libdl#27"],
1588 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001589 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001590 }
1591
1592 cc_library_shared {
1593 name: "mylib_shared",
1594 srcs: ["mylib.cpp"],
1595 shared_libs: ["libdl#27"],
1596 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001597 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001598 }
1599
1600 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001601 name: "libBootstrap",
1602 srcs: ["mylib.cpp"],
1603 stl: "none",
1604 bootstrap: true,
1605 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001606 `)
1607
Sundong Ahnabb64432019-10-22 13:58:29 +09001608 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001609 copyCmds := apexRule.Args["copy_commands"]
1610
1611 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001612 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001613 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1614 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001615
1616 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001617 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001618
Colin Crossaede88c2020-08-11 12:17:01 -07001619 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1620 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1621 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001622
1623 // For dependency to libc
1624 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001625 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001626 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001627 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001628 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001629 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1630 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001631
1632 // For dependency to libm
1633 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001634 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001635 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001636 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001637 // ... and is not compiling with the stub
1638 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1639 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1640
1641 // For dependency to libdl
1642 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001643 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001644 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001645 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1646 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001647 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001648 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001649 // ... Cflags from stub is correctly exported to mylib
1650 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1651 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001652
1653 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001654 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1655 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1656 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1657 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001658}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001659
Jooyung Han749dc692020-04-15 11:03:39 +09001660func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001661 // there are three links between liba --> libz.
1662 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001663 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001664 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001665 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001666 apex {
1667 name: "myapex",
1668 key: "myapex.key",
1669 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001670 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001671 }
1672
1673 apex {
1674 name: "otherapex",
1675 key: "myapex.key",
1676 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001677 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001678 }
1679
1680 apex_key {
1681 name: "myapex.key",
1682 public_key: "testkey.avbpubkey",
1683 private_key: "testkey.pem",
1684 }
1685
1686 cc_library {
1687 name: "libx",
1688 shared_libs: ["liba"],
1689 system_shared_libs: [],
1690 stl: "none",
1691 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001692 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001693 }
1694
1695 cc_library {
1696 name: "liby",
1697 shared_libs: ["liba"],
1698 system_shared_libs: [],
1699 stl: "none",
1700 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001701 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001702 }
1703
1704 cc_library {
1705 name: "liba",
1706 shared_libs: ["libz"],
1707 system_shared_libs: [],
1708 stl: "none",
1709 apex_available: [
1710 "//apex_available:anyapex",
1711 "//apex_available:platform",
1712 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001713 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001714 }
1715
1716 cc_library {
1717 name: "libz",
1718 system_shared_libs: [],
1719 stl: "none",
1720 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001721 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001722 },
1723 }
Jooyung Han749dc692020-04-15 11:03:39 +09001724 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001725
1726 expectLink := func(from, from_variant, to, to_variant string) {
1727 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1728 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1729 }
1730 expectNoLink := func(from, from_variant, to, to_variant string) {
1731 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1732 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1733 }
1734 // platform liba is linked to non-stub version
1735 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001736 // liba in myapex is linked to current
1737 expectLink("liba", "shared_apex29", "libz", "shared_current")
1738 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001739 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001740 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001741 // liba in otherapex is linked to current
1742 expectLink("liba", "shared_apex30", "libz", "shared_current")
1743 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001744 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1745 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001746}
1747
Jooyung Hanaed150d2020-04-02 01:41:41 +09001748func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001749 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001750 apex {
1751 name: "myapex",
1752 key: "myapex.key",
1753 native_shared_libs: ["libx"],
1754 min_sdk_version: "R",
1755 }
1756
1757 apex_key {
1758 name: "myapex.key",
1759 public_key: "testkey.avbpubkey",
1760 private_key: "testkey.pem",
1761 }
1762
1763 cc_library {
1764 name: "libx",
1765 shared_libs: ["libz"],
1766 system_shared_libs: [],
1767 stl: "none",
1768 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001769 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001770 }
1771
1772 cc_library {
1773 name: "libz",
1774 system_shared_libs: [],
1775 stl: "none",
1776 stubs: {
1777 versions: ["29", "R"],
1778 },
1779 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001780 `,
1781 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1782 variables.Platform_version_active_codenames = []string{"R"}
1783 }),
1784 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001785
1786 expectLink := func(from, from_variant, to, to_variant string) {
1787 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1788 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1789 }
1790 expectNoLink := func(from, from_variant, to, to_variant string) {
1791 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1792 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1793 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001794 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1795 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001796 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1797 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001798}
1799
Jooyung Han4c4da062021-06-23 10:23:16 +09001800func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1801 testApex(t, `
1802 apex {
1803 name: "myapex",
1804 key: "myapex.key",
1805 java_libs: ["libx"],
1806 min_sdk_version: "S",
1807 }
1808
1809 apex_key {
1810 name: "myapex.key",
1811 public_key: "testkey.avbpubkey",
1812 private_key: "testkey.pem",
1813 }
1814
1815 java_library {
1816 name: "libx",
1817 srcs: ["a.java"],
1818 apex_available: [ "myapex" ],
1819 sdk_version: "current",
1820 min_sdk_version: "S", // should be okay
1821 }
1822 `,
1823 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1824 variables.Platform_version_active_codenames = []string{"S"}
1825 variables.Platform_sdk_codename = proptools.StringPtr("S")
1826 }),
1827 )
1828}
1829
Jooyung Han749dc692020-04-15 11:03:39 +09001830func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001831 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001832 apex {
1833 name: "myapex",
1834 key: "myapex.key",
1835 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001836 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001837 }
1838
1839 apex_key {
1840 name: "myapex.key",
1841 public_key: "testkey.avbpubkey",
1842 private_key: "testkey.pem",
1843 }
1844
1845 cc_library {
1846 name: "libx",
1847 shared_libs: ["libz"],
1848 system_shared_libs: [],
1849 stl: "none",
1850 apex_available: [ "myapex" ],
1851 }
1852
1853 cc_library {
1854 name: "libz",
1855 system_shared_libs: [],
1856 stl: "none",
1857 stubs: {
1858 versions: ["1", "2"],
1859 },
1860 }
1861 `)
1862
1863 expectLink := func(from, from_variant, to, to_variant string) {
1864 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1865 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1866 }
1867 expectNoLink := func(from, from_variant, to, to_variant string) {
1868 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1869 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1870 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001871 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001872 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001873 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001874 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001875}
1876
Jiyong Park5df7bd32021-08-25 16:18:46 +09001877func TestApexMinSdkVersion_crtobjectInVendorApex(t *testing.T) {
1878 ctx := testApex(t, `
1879 apex {
1880 name: "myapex",
1881 key: "myapex.key",
1882 native_shared_libs: ["mylib"],
1883 updatable: false,
1884 vendor: true,
1885 min_sdk_version: "29",
1886 }
1887
1888 apex_key {
1889 name: "myapex.key",
1890 public_key: "testkey.avbpubkey",
1891 private_key: "testkey.pem",
1892 }
1893
1894 cc_library {
1895 name: "mylib",
1896 vendor_available: true,
1897 system_shared_libs: [],
1898 stl: "none",
1899 apex_available: [ "myapex" ],
1900 min_sdk_version: "29",
1901 }
1902 `)
1903
1904 vendorVariant := "android_vendor.29_arm64_armv8-a"
1905
1906 // First check that the correct variant of crtbegin_so is used.
1907 ldRule := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29").Rule("ld")
1908 crtBegin := names(ldRule.Args["crtBegin"])
1909 ensureListContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
1910
1911 // Ensure that the crtbegin_so used by the APEX is targeting 29
1912 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1913 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1914}
1915
Jooyung Han03b51852020-02-26 22:45:42 +09001916func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001917 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001918 apex {
1919 name: "myapex",
1920 key: "myapex.key",
1921 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001922 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001923 }
1924
1925 apex_key {
1926 name: "myapex.key",
1927 public_key: "testkey.avbpubkey",
1928 private_key: "testkey.pem",
1929 }
1930
1931 cc_library {
1932 name: "libx",
1933 system_shared_libs: [],
1934 stl: "none",
1935 apex_available: [ "myapex" ],
1936 stubs: {
1937 versions: ["1", "2"],
1938 },
1939 }
1940
1941 cc_library {
1942 name: "libz",
1943 shared_libs: ["libx"],
1944 system_shared_libs: [],
1945 stl: "none",
1946 }
1947 `)
1948
1949 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001950 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001951 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1952 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1953 }
1954 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001955 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001956 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1957 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1958 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001959 expectLink("libz", "shared", "libx", "shared_current")
1960 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001961 expectNoLink("libz", "shared", "libz", "shared_1")
1962 expectNoLink("libz", "shared", "libz", "shared")
1963}
1964
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001965var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1966 func(variables android.FixtureProductVariables) {
1967 variables.SanitizeDevice = []string{"hwaddress"}
1968 },
1969)
1970
Jooyung Han75568392020-03-20 04:29:24 +09001971func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001972 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001973 apex {
1974 name: "myapex",
1975 key: "myapex.key",
1976 native_shared_libs: ["libx"],
1977 min_sdk_version: "29",
1978 }
1979
1980 apex_key {
1981 name: "myapex.key",
1982 public_key: "testkey.avbpubkey",
1983 private_key: "testkey.pem",
1984 }
1985
1986 cc_library {
1987 name: "libx",
1988 shared_libs: ["libbar"],
1989 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001990 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001991 }
1992
1993 cc_library {
1994 name: "libbar",
1995 stubs: {
1996 versions: ["29", "30"],
1997 },
1998 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001999 `,
2000 prepareForTestWithSantitizeHwaddress,
2001 )
Jooyung Han03b51852020-02-26 22:45:42 +09002002 expectLink := func(from, from_variant, to, to_variant string) {
2003 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2004 libFlags := ld.Args["libFlags"]
2005 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2006 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002007 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002008}
2009
Jooyung Han75568392020-03-20 04:29:24 +09002010func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002011 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002012 apex {
2013 name: "myapex",
2014 key: "myapex.key",
2015 native_shared_libs: ["libx"],
2016 min_sdk_version: "29",
2017 }
2018
2019 apex_key {
2020 name: "myapex.key",
2021 public_key: "testkey.avbpubkey",
2022 private_key: "testkey.pem",
2023 }
2024
2025 cc_library {
2026 name: "libx",
2027 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002028 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002029 }
Jooyung Han75568392020-03-20 04:29:24 +09002030 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002031
2032 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002033 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002034 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002035 // note that platform variant is not.
2036 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002037 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002038}
2039
Jooyung Han749dc692020-04-15 11:03:39 +09002040func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2041 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002042 apex {
2043 name: "myapex",
2044 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002045 native_shared_libs: ["mylib"],
2046 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002047 }
2048
2049 apex_key {
2050 name: "myapex.key",
2051 public_key: "testkey.avbpubkey",
2052 private_key: "testkey.pem",
2053 }
Jooyung Han749dc692020-04-15 11:03:39 +09002054
2055 cc_library {
2056 name: "mylib",
2057 srcs: ["mylib.cpp"],
2058 system_shared_libs: [],
2059 stl: "none",
2060 apex_available: [
2061 "myapex",
2062 ],
2063 min_sdk_version: "30",
2064 }
2065 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002066
2067 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2068 apex {
2069 name: "myapex",
2070 key: "myapex.key",
2071 native_shared_libs: ["libfoo.ffi"],
2072 min_sdk_version: "29",
2073 }
2074
2075 apex_key {
2076 name: "myapex.key",
2077 public_key: "testkey.avbpubkey",
2078 private_key: "testkey.pem",
2079 }
2080
2081 rust_ffi_shared {
2082 name: "libfoo.ffi",
2083 srcs: ["foo.rs"],
2084 crate_name: "foo",
2085 apex_available: [
2086 "myapex",
2087 ],
2088 min_sdk_version: "30",
2089 }
2090 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002091
2092 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2093 apex {
2094 name: "myapex",
2095 key: "myapex.key",
2096 java_libs: ["libfoo"],
2097 min_sdk_version: "29",
2098 }
2099
2100 apex_key {
2101 name: "myapex.key",
2102 public_key: "testkey.avbpubkey",
2103 private_key: "testkey.pem",
2104 }
2105
2106 java_import {
2107 name: "libfoo",
2108 jars: ["libfoo.jar"],
2109 apex_available: [
2110 "myapex",
2111 ],
2112 min_sdk_version: "30",
2113 }
2114 `)
Jooyung Han749dc692020-04-15 11:03:39 +09002115}
2116
2117func TestApexMinSdkVersion_Okay(t *testing.T) {
2118 testApex(t, `
2119 apex {
2120 name: "myapex",
2121 key: "myapex.key",
2122 native_shared_libs: ["libfoo"],
2123 java_libs: ["libbar"],
2124 min_sdk_version: "29",
2125 }
2126
2127 apex_key {
2128 name: "myapex.key",
2129 public_key: "testkey.avbpubkey",
2130 private_key: "testkey.pem",
2131 }
2132
2133 cc_library {
2134 name: "libfoo",
2135 srcs: ["mylib.cpp"],
2136 shared_libs: ["libfoo_dep"],
2137 apex_available: ["myapex"],
2138 min_sdk_version: "29",
2139 }
2140
2141 cc_library {
2142 name: "libfoo_dep",
2143 srcs: ["mylib.cpp"],
2144 apex_available: ["myapex"],
2145 min_sdk_version: "29",
2146 }
2147
2148 java_library {
2149 name: "libbar",
2150 sdk_version: "current",
2151 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002152 static_libs: [
2153 "libbar_dep",
2154 "libbar_import_dep",
2155 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002156 apex_available: ["myapex"],
2157 min_sdk_version: "29",
2158 }
2159
2160 java_library {
2161 name: "libbar_dep",
2162 sdk_version: "current",
2163 srcs: ["a.java"],
2164 apex_available: ["myapex"],
2165 min_sdk_version: "29",
2166 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002167
2168 java_import {
2169 name: "libbar_import_dep",
2170 jars: ["libbar.jar"],
2171 apex_available: ["myapex"],
2172 min_sdk_version: "29",
2173 }
Jooyung Han03b51852020-02-26 22:45:42 +09002174 `)
2175}
2176
Artur Satayev8cf899a2020-04-15 17:29:42 +01002177func TestJavaStableSdkVersion(t *testing.T) {
2178 testCases := []struct {
2179 name string
2180 expectedError string
2181 bp string
2182 }{
2183 {
2184 name: "Non-updatable apex with non-stable dep",
2185 bp: `
2186 apex {
2187 name: "myapex",
2188 java_libs: ["myjar"],
2189 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002190 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002191 }
2192 apex_key {
2193 name: "myapex.key",
2194 public_key: "testkey.avbpubkey",
2195 private_key: "testkey.pem",
2196 }
2197 java_library {
2198 name: "myjar",
2199 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002200 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002201 apex_available: ["myapex"],
2202 }
2203 `,
2204 },
2205 {
2206 name: "Updatable apex with stable dep",
2207 bp: `
2208 apex {
2209 name: "myapex",
2210 java_libs: ["myjar"],
2211 key: "myapex.key",
2212 updatable: true,
2213 min_sdk_version: "29",
2214 }
2215 apex_key {
2216 name: "myapex.key",
2217 public_key: "testkey.avbpubkey",
2218 private_key: "testkey.pem",
2219 }
2220 java_library {
2221 name: "myjar",
2222 srcs: ["foo/bar/MyClass.java"],
2223 sdk_version: "current",
2224 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002225 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002226 }
2227 `,
2228 },
2229 {
2230 name: "Updatable apex with non-stable dep",
2231 expectedError: "cannot depend on \"myjar\"",
2232 bp: `
2233 apex {
2234 name: "myapex",
2235 java_libs: ["myjar"],
2236 key: "myapex.key",
2237 updatable: true,
2238 }
2239 apex_key {
2240 name: "myapex.key",
2241 public_key: "testkey.avbpubkey",
2242 private_key: "testkey.pem",
2243 }
2244 java_library {
2245 name: "myjar",
2246 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002247 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002248 apex_available: ["myapex"],
2249 }
2250 `,
2251 },
2252 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002253 name: "Updatable apex with non-stable transitive dep",
2254 // This is not actually detecting that the transitive dependency is unstable, rather it is
2255 // detecting that the transitive dependency is building against a wider API surface than the
2256 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002257 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002258 bp: `
2259 apex {
2260 name: "myapex",
2261 java_libs: ["myjar"],
2262 key: "myapex.key",
2263 updatable: true,
2264 }
2265 apex_key {
2266 name: "myapex.key",
2267 public_key: "testkey.avbpubkey",
2268 private_key: "testkey.pem",
2269 }
2270 java_library {
2271 name: "myjar",
2272 srcs: ["foo/bar/MyClass.java"],
2273 sdk_version: "current",
2274 apex_available: ["myapex"],
2275 static_libs: ["transitive-jar"],
2276 }
2277 java_library {
2278 name: "transitive-jar",
2279 srcs: ["foo/bar/MyClass.java"],
2280 sdk_version: "core_platform",
2281 apex_available: ["myapex"],
2282 }
2283 `,
2284 },
2285 }
2286
2287 for _, test := range testCases {
2288 t.Run(test.name, func(t *testing.T) {
2289 if test.expectedError == "" {
2290 testApex(t, test.bp)
2291 } else {
2292 testApexError(t, test.expectedError, test.bp)
2293 }
2294 })
2295 }
2296}
2297
Jooyung Han749dc692020-04-15 11:03:39 +09002298func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2299 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2300 apex {
2301 name: "myapex",
2302 key: "myapex.key",
2303 native_shared_libs: ["mylib"],
2304 min_sdk_version: "29",
2305 }
2306
2307 apex_key {
2308 name: "myapex.key",
2309 public_key: "testkey.avbpubkey",
2310 private_key: "testkey.pem",
2311 }
2312
2313 cc_library {
2314 name: "mylib",
2315 srcs: ["mylib.cpp"],
2316 shared_libs: ["mylib2"],
2317 system_shared_libs: [],
2318 stl: "none",
2319 apex_available: [
2320 "myapex",
2321 ],
2322 min_sdk_version: "29",
2323 }
2324
2325 // indirect part of the apex
2326 cc_library {
2327 name: "mylib2",
2328 srcs: ["mylib.cpp"],
2329 system_shared_libs: [],
2330 stl: "none",
2331 apex_available: [
2332 "myapex",
2333 ],
2334 min_sdk_version: "30",
2335 }
2336 `)
2337}
2338
2339func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2340 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2341 apex {
2342 name: "myapex",
2343 key: "myapex.key",
2344 apps: ["AppFoo"],
2345 min_sdk_version: "29",
2346 }
2347
2348 apex_key {
2349 name: "myapex.key",
2350 public_key: "testkey.avbpubkey",
2351 private_key: "testkey.pem",
2352 }
2353
2354 android_app {
2355 name: "AppFoo",
2356 srcs: ["foo/bar/MyClass.java"],
2357 sdk_version: "current",
2358 min_sdk_version: "29",
2359 system_modules: "none",
2360 stl: "none",
2361 static_libs: ["bar"],
2362 apex_available: [ "myapex" ],
2363 }
2364
2365 java_library {
2366 name: "bar",
2367 sdk_version: "current",
2368 srcs: ["a.java"],
2369 apex_available: [ "myapex" ],
2370 }
2371 `)
2372}
2373
2374func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002375 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002376 apex {
2377 name: "myapex",
2378 key: "myapex.key",
2379 native_shared_libs: ["mylib"],
2380 min_sdk_version: "29",
2381 }
2382
2383 apex_key {
2384 name: "myapex.key",
2385 public_key: "testkey.avbpubkey",
2386 private_key: "testkey.pem",
2387 }
2388
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002389 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002390 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2391 cc_library {
2392 name: "mylib",
2393 srcs: ["mylib.cpp"],
2394 shared_libs: ["mylib2"],
2395 system_shared_libs: [],
2396 stl: "none",
2397 apex_available: ["myapex", "otherapex"],
2398 min_sdk_version: "29",
2399 }
2400
2401 cc_library {
2402 name: "mylib2",
2403 srcs: ["mylib.cpp"],
2404 system_shared_libs: [],
2405 stl: "none",
2406 apex_available: ["otherapex"],
2407 stubs: { versions: ["29", "30"] },
2408 min_sdk_version: "30",
2409 }
2410
2411 apex {
2412 name: "otherapex",
2413 key: "myapex.key",
2414 native_shared_libs: ["mylib", "mylib2"],
2415 min_sdk_version: "30",
2416 }
2417 `)
2418 expectLink := func(from, from_variant, to, to_variant string) {
2419 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2420 libFlags := ld.Args["libFlags"]
2421 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2422 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002423 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002424 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002425}
2426
Jooyung Haned124c32021-01-26 11:43:46 +09002427func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002428 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2429 func(variables android.FixtureProductVariables) {
2430 variables.Platform_sdk_codename = proptools.StringPtr("S")
2431 variables.Platform_version_active_codenames = []string{"S"}
2432 },
2433 )
Jooyung Haned124c32021-01-26 11:43:46 +09002434 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2435 apex {
2436 name: "myapex",
2437 key: "myapex.key",
2438 native_shared_libs: ["libfoo"],
2439 min_sdk_version: "S",
2440 }
2441 apex_key {
2442 name: "myapex.key",
2443 public_key: "testkey.avbpubkey",
2444 private_key: "testkey.pem",
2445 }
2446 cc_library {
2447 name: "libfoo",
2448 shared_libs: ["libbar"],
2449 apex_available: ["myapex"],
2450 min_sdk_version: "29",
2451 }
2452 cc_library {
2453 name: "libbar",
2454 apex_available: ["myapex"],
2455 }
2456 `, withSAsActiveCodeNames)
2457}
2458
2459func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002460 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2461 variables.Platform_sdk_codename = proptools.StringPtr("S")
2462 variables.Platform_version_active_codenames = []string{"S", "T"}
2463 })
Colin Cross1c460562021-02-16 17:55:47 -08002464 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002465 apex {
2466 name: "myapex",
2467 key: "myapex.key",
2468 native_shared_libs: ["libfoo"],
2469 min_sdk_version: "S",
2470 }
2471 apex_key {
2472 name: "myapex.key",
2473 public_key: "testkey.avbpubkey",
2474 private_key: "testkey.pem",
2475 }
2476 cc_library {
2477 name: "libfoo",
2478 shared_libs: ["libbar"],
2479 apex_available: ["myapex"],
2480 min_sdk_version: "S",
2481 }
2482 cc_library {
2483 name: "libbar",
2484 stubs: {
2485 symbol_file: "libbar.map.txt",
2486 versions: ["30", "S", "T"],
2487 },
2488 }
2489 `, withSAsActiveCodeNames)
2490
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002491 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002492 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2493 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002494 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002495}
2496
Jiyong Park7c2ee712018-12-07 00:42:25 +09002497func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002498 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002499 apex {
2500 name: "myapex",
2501 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002502 native_shared_libs: ["mylib"],
2503 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002504 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002505 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002506 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002507 }
2508
2509 apex_key {
2510 name: "myapex.key",
2511 public_key: "testkey.avbpubkey",
2512 private_key: "testkey.pem",
2513 }
2514
2515 prebuilt_etc {
2516 name: "myetc",
2517 src: "myprebuilt",
2518 sub_dir: "foo/bar",
2519 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002520
2521 cc_library {
2522 name: "mylib",
2523 srcs: ["mylib.cpp"],
2524 relative_install_path: "foo/bar",
2525 system_shared_libs: [],
2526 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002527 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002528 }
2529
2530 cc_binary {
2531 name: "mybin",
2532 srcs: ["mylib.cpp"],
2533 relative_install_path: "foo/bar",
2534 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002535 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002536 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002537 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002538 `)
2539
Sundong Ahnabb64432019-10-22 13:58:29 +09002540 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002541 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2542
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002543 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002544 ensureListContains(t, dirs, "etc")
2545 ensureListContains(t, dirs, "etc/foo")
2546 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002547 ensureListContains(t, dirs, "lib64")
2548 ensureListContains(t, dirs, "lib64/foo")
2549 ensureListContains(t, dirs, "lib64/foo/bar")
2550 ensureListContains(t, dirs, "lib")
2551 ensureListContains(t, dirs, "lib/foo")
2552 ensureListContains(t, dirs, "lib/foo/bar")
2553
Jiyong Parkbd13e442019-03-15 18:10:35 +09002554 ensureListContains(t, dirs, "bin")
2555 ensureListContains(t, dirs, "bin/foo")
2556 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002557}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002558
Jooyung Han35155c42020-02-06 17:33:20 +09002559func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002560 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002561 apex {
2562 name: "myapex",
2563 key: "myapex.key",
2564 multilib: {
2565 both: {
2566 native_shared_libs: ["mylib"],
2567 binaries: ["mybin"],
2568 },
2569 },
2570 compile_multilib: "both",
2571 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002572 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002573 }
2574
2575 apex_key {
2576 name: "myapex.key",
2577 public_key: "testkey.avbpubkey",
2578 private_key: "testkey.pem",
2579 }
2580
2581 cc_library {
2582 name: "mylib",
2583 relative_install_path: "foo/bar",
2584 system_shared_libs: [],
2585 stl: "none",
2586 apex_available: [ "myapex" ],
2587 native_bridge_supported: true,
2588 }
2589
2590 cc_binary {
2591 name: "mybin",
2592 relative_install_path: "foo/bar",
2593 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002594 stl: "none",
2595 apex_available: [ "myapex" ],
2596 native_bridge_supported: true,
2597 compile_multilib: "both", // default is "first" for binary
2598 multilib: {
2599 lib64: {
2600 suffix: "64",
2601 },
2602 },
2603 }
2604 `, withNativeBridgeEnabled)
2605 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2606 "bin/foo/bar/mybin",
2607 "bin/foo/bar/mybin64",
2608 "bin/arm/foo/bar/mybin",
2609 "bin/arm64/foo/bar/mybin64",
2610 "lib/foo/bar/mylib.so",
2611 "lib/arm/foo/bar/mylib.so",
2612 "lib64/foo/bar/mylib.so",
2613 "lib64/arm64/foo/bar/mylib.so",
2614 })
2615}
2616
Jiyong Parkda6eb592018-12-19 17:12:36 +09002617func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002618 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002619 apex {
2620 name: "myapex",
2621 key: "myapex.key",
2622 native_shared_libs: ["mylib"],
2623 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002624 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002625 }
2626
2627 apex_key {
2628 name: "myapex.key",
2629 public_key: "testkey.avbpubkey",
2630 private_key: "testkey.pem",
2631 }
2632
2633 cc_library {
2634 name: "mylib",
2635 srcs: ["mylib.cpp"],
2636 shared_libs: ["mylib2"],
2637 system_shared_libs: [],
2638 vendor_available: true,
2639 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002640 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002641 }
2642
2643 cc_library {
2644 name: "mylib2",
2645 srcs: ["mylib.cpp"],
2646 system_shared_libs: [],
2647 vendor_available: true,
2648 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002649 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002650 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002651 `,
2652 setUseVendorAllowListForTest([]string{"myapex"}),
2653 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002654
2655 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002656 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002657 for _, implicit := range i.Implicits {
2658 inputsList = append(inputsList, implicit.String())
2659 }
2660 }
2661 inputsString := strings.Join(inputsList, " ")
2662
2663 // ensure that the apex includes vendor variants of the direct and indirect deps
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002664 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib.so")
2665 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002666
2667 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002668 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2669 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002670}
Jiyong Park16e91a02018-12-20 18:18:08 +09002671
Jooyung Han85d61762020-06-24 23:50:26 +09002672func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002673 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2674 apex {
2675 name: "myapex",
2676 key: "myapex.key",
2677 use_vendor: true,
2678 }
2679 apex_key {
2680 name: "myapex.key",
2681 public_key: "testkey.avbpubkey",
2682 private_key: "testkey.pem",
2683 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002684 `,
2685 setUseVendorAllowListForTest([]string{""}),
2686 )
Colin Cross440e0d02020-06-11 11:32:11 -07002687 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002688 testApex(t, `
2689 apex {
2690 name: "myapex",
2691 key: "myapex.key",
2692 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002693 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002694 }
2695 apex_key {
2696 name: "myapex.key",
2697 public_key: "testkey.avbpubkey",
2698 private_key: "testkey.pem",
2699 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002700 `,
2701 setUseVendorAllowListForTest([]string{"myapex"}),
2702 )
Jooyung Handc782442019-11-01 03:14:38 +09002703}
2704
Jooyung Han5c998b92019-06-27 11:30:33 +09002705func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2706 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2707 apex {
2708 name: "myapex",
2709 key: "myapex.key",
2710 native_shared_libs: ["mylib"],
2711 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002712 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002713 }
2714
2715 apex_key {
2716 name: "myapex.key",
2717 public_key: "testkey.avbpubkey",
2718 private_key: "testkey.pem",
2719 }
2720
2721 cc_library {
2722 name: "mylib",
2723 srcs: ["mylib.cpp"],
2724 system_shared_libs: [],
2725 stl: "none",
2726 }
2727 `)
2728}
2729
Jooyung Han85d61762020-06-24 23:50:26 +09002730func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002731 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002732 apex {
2733 name: "myapex",
2734 key: "myapex.key",
2735 binaries: ["mybin"],
2736 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002737 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002738 }
2739 apex_key {
2740 name: "myapex.key",
2741 public_key: "testkey.avbpubkey",
2742 private_key: "testkey.pem",
2743 }
2744 cc_binary {
2745 name: "mybin",
2746 vendor: true,
2747 shared_libs: ["libfoo"],
2748 }
2749 cc_library {
2750 name: "libfoo",
2751 proprietary: true,
2752 }
2753 `)
2754
2755 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2756 "bin/mybin",
2757 "lib64/libfoo.so",
2758 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2759 "lib64/libc++.so",
2760 })
2761
2762 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002763 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002764 name := apexBundle.BaseModuleName()
2765 prefix := "TARGET_"
2766 var builder strings.Builder
2767 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002768 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2769 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002770 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002771
2772 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2773 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2774 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002775}
2776
Jooyung Handf78e212020-07-22 15:54:47 +09002777func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002778 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002779 apex {
2780 name: "myapex",
2781 key: "myapex.key",
2782 binaries: ["mybin"],
2783 vendor: true,
2784 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002785 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002786 }
2787 apex_key {
2788 name: "myapex.key",
2789 public_key: "testkey.avbpubkey",
2790 private_key: "testkey.pem",
2791 }
2792 cc_binary {
2793 name: "mybin",
2794 vendor: true,
2795 shared_libs: ["libvndk", "libvendor"],
2796 }
2797 cc_library {
2798 name: "libvndk",
2799 vndk: {
2800 enabled: true,
2801 },
2802 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002803 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002804 }
2805 cc_library {
2806 name: "libvendor",
2807 vendor: true,
2808 }
2809 `)
2810
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002811 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002812
Paul Duffina71a67a2021-03-29 00:42:57 +01002813 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002814 libs := names(ldRule.Args["libFlags"])
2815 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002816 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2817 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002818 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002819 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002820
2821 // VNDK libs are not included when use_vndk_as_stable: true
2822 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2823 "bin/mybin",
2824 "lib64/libvendor.so",
2825 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002826
2827 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2828 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2829 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002830}
2831
Justin Yun13decfb2021-03-08 19:25:55 +09002832func TestProductVariant(t *testing.T) {
2833 ctx := testApex(t, `
2834 apex {
2835 name: "myapex",
2836 key: "myapex.key",
2837 updatable: false,
2838 product_specific: true,
2839 binaries: ["foo"],
2840 }
2841
2842 apex_key {
2843 name: "myapex.key",
2844 public_key: "testkey.avbpubkey",
2845 private_key: "testkey.pem",
2846 }
2847
2848 cc_binary {
2849 name: "foo",
2850 product_available: true,
2851 apex_available: ["myapex"],
2852 srcs: ["foo.cpp"],
2853 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002854 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2855 variables.ProductVndkVersion = proptools.StringPtr("current")
2856 }),
2857 )
Justin Yun13decfb2021-03-08 19:25:55 +09002858
2859 cflags := strings.Fields(
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002860 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002861 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2862 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2863 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2864 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2865}
2866
Jooyung Han8e5685d2020-09-21 11:02:57 +09002867func TestApex_withPrebuiltFirmware(t *testing.T) {
2868 testCases := []struct {
2869 name string
2870 additionalProp string
2871 }{
2872 {"system apex with prebuilt_firmware", ""},
2873 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2874 }
2875 for _, tc := range testCases {
2876 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002877 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002878 apex {
2879 name: "myapex",
2880 key: "myapex.key",
2881 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002882 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002883 `+tc.additionalProp+`
2884 }
2885 apex_key {
2886 name: "myapex.key",
2887 public_key: "testkey.avbpubkey",
2888 private_key: "testkey.pem",
2889 }
2890 prebuilt_firmware {
2891 name: "myfirmware",
2892 src: "myfirmware.bin",
2893 filename_from_src: true,
2894 `+tc.additionalProp+`
2895 }
2896 `)
2897 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2898 "etc/firmware/myfirmware.bin",
2899 })
2900 })
2901 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002902}
2903
Jooyung Hanefb184e2020-06-25 17:14:25 +09002904func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002905 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002906 apex {
2907 name: "myapex",
2908 key: "myapex.key",
2909 use_vendor: true,
2910 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002911 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002912 }
2913
2914 apex_key {
2915 name: "myapex.key",
2916 public_key: "testkey.avbpubkey",
2917 private_key: "testkey.pem",
2918 }
2919
2920 cc_library {
2921 name: "mylib",
2922 vendor_available: true,
2923 apex_available: ["myapex"],
2924 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002925 `,
2926 setUseVendorAllowListForTest([]string{"myapex"}),
2927 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002928
2929 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002930 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002931 name := apexBundle.BaseModuleName()
2932 prefix := "TARGET_"
2933 var builder strings.Builder
2934 data.Custom(&builder, name, prefix, "", data)
2935 androidMk := builder.String()
2936 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2937}
2938
2939func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002940 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002941 apex {
2942 name: "myapex",
2943 key: "myapex.key",
2944 vendor: true,
2945 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002946 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002947 }
2948
2949 apex_key {
2950 name: "myapex.key",
2951 public_key: "testkey.avbpubkey",
2952 private_key: "testkey.pem",
2953 }
2954
2955 cc_library {
2956 name: "mylib",
2957 vendor_available: true,
2958 }
2959 `)
2960
2961 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002962 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002963 name := apexBundle.BaseModuleName()
2964 prefix := "TARGET_"
2965 var builder strings.Builder
2966 data.Custom(&builder, name, prefix, "", data)
2967 androidMk := builder.String()
2968 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2969}
2970
Jooyung Han2ed99d02020-06-24 23:26:26 +09002971func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002972 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002973 apex {
2974 name: "myapex",
2975 key: "myapex.key",
2976 vintf_fragments: ["fragment.xml"],
2977 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002978 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002979 }
2980 apex_key {
2981 name: "myapex.key",
2982 public_key: "testkey.avbpubkey",
2983 private_key: "testkey.pem",
2984 }
2985 cc_binary {
2986 name: "mybin",
2987 }
2988 `)
2989
2990 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002991 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002992 name := apexBundle.BaseModuleName()
2993 prefix := "TARGET_"
2994 var builder strings.Builder
2995 data.Custom(&builder, name, prefix, "", data)
2996 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04002997 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002998 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002999}
3000
Jiyong Park16e91a02018-12-20 18:18:08 +09003001func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003002 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003003 apex {
3004 name: "myapex",
3005 key: "myapex.key",
3006 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003007 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003008 }
3009
3010 apex_key {
3011 name: "myapex.key",
3012 public_key: "testkey.avbpubkey",
3013 private_key: "testkey.pem",
3014 }
3015
3016 cc_library {
3017 name: "mylib",
3018 srcs: ["mylib.cpp"],
3019 system_shared_libs: [],
3020 stl: "none",
3021 stubs: {
3022 versions: ["1", "2", "3"],
3023 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003024 apex_available: [
3025 "//apex_available:platform",
3026 "myapex",
3027 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003028 }
3029
3030 cc_binary {
3031 name: "not_in_apex",
3032 srcs: ["mylib.cpp"],
3033 static_libs: ["mylib"],
3034 static_executable: true,
3035 system_shared_libs: [],
3036 stl: "none",
3037 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003038 `)
3039
Colin Cross7113d202019-11-20 16:39:12 -08003040 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003041
3042 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003043 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003044}
Jiyong Park9335a262018-12-24 11:31:58 +09003045
3046func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003047 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003048 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003049 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003050 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003051 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003052 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003053 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003054 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003055 }
3056
3057 cc_library {
3058 name: "mylib",
3059 srcs: ["mylib.cpp"],
3060 system_shared_libs: [],
3061 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003062 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003063 }
3064
3065 apex_key {
3066 name: "myapex.key",
3067 public_key: "testkey.avbpubkey",
3068 private_key: "testkey.pem",
3069 }
3070
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003071 android_app_certificate {
3072 name: "myapex.certificate",
3073 certificate: "testkey",
3074 }
3075
3076 android_app_certificate {
3077 name: "myapex.certificate.override",
3078 certificate: "testkey.override",
3079 }
3080
Jiyong Park9335a262018-12-24 11:31:58 +09003081 `)
3082
3083 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003084 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003085
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003086 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3087 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003088 "vendor/foo/devkeys/testkey.avbpubkey")
3089 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003090 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3091 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003092 "vendor/foo/devkeys/testkey.pem")
3093 }
3094
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003095 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003096 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003097 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003098 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003099 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003100 }
3101}
Jiyong Park58e364a2019-01-19 19:24:06 +09003102
Jooyung Hanf121a652019-12-17 14:30:11 +09003103func TestCertificate(t *testing.T) {
3104 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003105 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003106 apex {
3107 name: "myapex",
3108 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003109 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003110 }
3111 apex_key {
3112 name: "myapex.key",
3113 public_key: "testkey.avbpubkey",
3114 private_key: "testkey.pem",
3115 }`)
3116 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3117 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3118 if actual := rule.Args["certificates"]; actual != expected {
3119 t.Errorf("certificates should be %q, not %q", expected, actual)
3120 }
3121 })
3122 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003123 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003124 apex {
3125 name: "myapex_keytest",
3126 key: "myapex.key",
3127 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003128 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003129 }
3130 apex_key {
3131 name: "myapex.key",
3132 public_key: "testkey.avbpubkey",
3133 private_key: "testkey.pem",
3134 }
3135 android_app_certificate {
3136 name: "myapex.certificate.override",
3137 certificate: "testkey.override",
3138 }`)
3139 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3140 expected := "testkey.override.x509.pem testkey.override.pk8"
3141 if actual := rule.Args["certificates"]; actual != expected {
3142 t.Errorf("certificates should be %q, not %q", expected, actual)
3143 }
3144 })
3145 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003146 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003147 apex {
3148 name: "myapex",
3149 key: "myapex.key",
3150 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003151 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003152 }
3153 apex_key {
3154 name: "myapex.key",
3155 public_key: "testkey.avbpubkey",
3156 private_key: "testkey.pem",
3157 }
3158 android_app_certificate {
3159 name: "myapex.certificate",
3160 certificate: "testkey",
3161 }`)
3162 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3163 expected := "testkey.x509.pem testkey.pk8"
3164 if actual := rule.Args["certificates"]; actual != expected {
3165 t.Errorf("certificates should be %q, not %q", expected, actual)
3166 }
3167 })
3168 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003169 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003170 apex {
3171 name: "myapex_keytest",
3172 key: "myapex.key",
3173 file_contexts: ":myapex-file_contexts",
3174 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003175 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003176 }
3177 apex_key {
3178 name: "myapex.key",
3179 public_key: "testkey.avbpubkey",
3180 private_key: "testkey.pem",
3181 }
3182 android_app_certificate {
3183 name: "myapex.certificate.override",
3184 certificate: "testkey.override",
3185 }`)
3186 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3187 expected := "testkey.override.x509.pem testkey.override.pk8"
3188 if actual := rule.Args["certificates"]; actual != expected {
3189 t.Errorf("certificates should be %q, not %q", expected, actual)
3190 }
3191 })
3192 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003193 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003194 apex {
3195 name: "myapex",
3196 key: "myapex.key",
3197 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003198 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003199 }
3200 apex_key {
3201 name: "myapex.key",
3202 public_key: "testkey.avbpubkey",
3203 private_key: "testkey.pem",
3204 }`)
3205 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3206 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3207 if actual := rule.Args["certificates"]; actual != expected {
3208 t.Errorf("certificates should be %q, not %q", expected, actual)
3209 }
3210 })
3211 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003212 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003213 apex {
3214 name: "myapex_keytest",
3215 key: "myapex.key",
3216 file_contexts: ":myapex-file_contexts",
3217 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003218 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003219 }
3220 apex_key {
3221 name: "myapex.key",
3222 public_key: "testkey.avbpubkey",
3223 private_key: "testkey.pem",
3224 }
3225 android_app_certificate {
3226 name: "myapex.certificate.override",
3227 certificate: "testkey.override",
3228 }`)
3229 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3230 expected := "testkey.override.x509.pem testkey.override.pk8"
3231 if actual := rule.Args["certificates"]; actual != expected {
3232 t.Errorf("certificates should be %q, not %q", expected, actual)
3233 }
3234 })
3235}
3236
Jiyong Park58e364a2019-01-19 19:24:06 +09003237func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003238 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003239 apex {
3240 name: "myapex",
3241 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003242 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003243 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003244 }
3245
3246 apex {
3247 name: "otherapex",
3248 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003249 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003250 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003251 }
3252
3253 apex_key {
3254 name: "myapex.key",
3255 public_key: "testkey.avbpubkey",
3256 private_key: "testkey.pem",
3257 }
3258
3259 cc_library {
3260 name: "mylib",
3261 srcs: ["mylib.cpp"],
3262 system_shared_libs: [],
3263 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003264 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003265 "myapex",
3266 "otherapex",
3267 ],
Jooyung Han24282772020-03-21 23:20:55 +09003268 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003269 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003270 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003271 cc_library {
3272 name: "mylib2",
3273 srcs: ["mylib.cpp"],
3274 system_shared_libs: [],
3275 stl: "none",
3276 apex_available: [
3277 "myapex",
3278 "otherapex",
3279 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003280 static_libs: ["mylib3"],
3281 recovery_available: true,
3282 min_sdk_version: "29",
3283 }
3284 cc_library {
3285 name: "mylib3",
3286 srcs: ["mylib.cpp"],
3287 system_shared_libs: [],
3288 stl: "none",
3289 apex_available: [
3290 "myapex",
3291 "otherapex",
3292 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09003293 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07003294 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003295 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003296 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003297 `)
3298
Jooyung Hanc87a0592020-03-02 17:44:33 +09003299 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003300 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003301 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003302 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003303
Jooyung Hanccce2f22020-03-07 03:45:53 +09003304 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003305 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003306 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003307 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003308 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003309
Jooyung Hanccce2f22020-03-07 03:45:53 +09003310 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003311 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003312 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003313 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003314 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003315
Colin Crossaede88c2020-08-11 12:17:01 -07003316 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3317 // each variant defines additional macros to distinguish which apex variant it is built for
3318
3319 // non-APEX variant does not have __ANDROID_APEX__ defined
3320 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3321 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3322
3323 // APEX variant has __ANDROID_APEX__ defined
3324 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3325 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3326 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3327 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3328
3329 // APEX variant has __ANDROID_APEX__ defined
3330 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3331 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3332 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3333 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3334
Dan Albertb19953d2020-11-17 15:29:36 -08003335 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003336 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3337 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003338 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003339
3340 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3341 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003342
3343 // non-APEX variant does not have __ANDROID_APEX__ defined
3344 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3345 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3346
3347 // APEX variant has __ANDROID_APEX__ defined
3348 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003349 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003350 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003351 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003352
Jooyung Hanc87a0592020-03-02 17:44:33 +09003353 // APEX variant has __ANDROID_APEX__ defined
3354 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003355 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003356 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003357 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003358
Dan Albertb19953d2020-11-17 15:29:36 -08003359 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003360 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003361 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003362 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003363}
Jiyong Park7e636d02019-01-28 16:16:54 +09003364
3365func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003366 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003367 apex {
3368 name: "myapex",
3369 key: "myapex.key",
3370 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003371 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003372 }
3373
3374 apex_key {
3375 name: "myapex.key",
3376 public_key: "testkey.avbpubkey",
3377 private_key: "testkey.pem",
3378 }
3379
3380 cc_library_headers {
3381 name: "mylib_headers",
3382 export_include_dirs: ["my_include"],
3383 system_shared_libs: [],
3384 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003385 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003386 }
3387
3388 cc_library {
3389 name: "mylib",
3390 srcs: ["mylib.cpp"],
3391 system_shared_libs: [],
3392 stl: "none",
3393 header_libs: ["mylib_headers"],
3394 export_header_lib_headers: ["mylib_headers"],
3395 stubs: {
3396 versions: ["1", "2", "3"],
3397 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003398 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003399 }
3400
3401 cc_library {
3402 name: "otherlib",
3403 srcs: ["mylib.cpp"],
3404 system_shared_libs: [],
3405 stl: "none",
3406 shared_libs: ["mylib"],
3407 }
3408 `)
3409
Colin Cross7113d202019-11-20 16:39:12 -08003410 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003411
3412 // Ensure that the include path of the header lib is exported to 'otherlib'
3413 ensureContains(t, cFlags, "-Imy_include")
3414}
Alex Light9670d332019-01-29 18:07:33 -08003415
Jiyong Park7cd10e32020-01-14 09:22:18 +09003416type fileInApex struct {
3417 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003418 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003419 isLink bool
3420}
3421
Jooyung Hana57af4a2020-01-23 05:36:59 +00003422func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003423 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003424 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003425 copyCmds := apexRule.Args["copy_commands"]
3426 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003427 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003428 for _, cmd := range strings.Split(copyCmds, "&&") {
3429 cmd = strings.TrimSpace(cmd)
3430 if cmd == "" {
3431 continue
3432 }
3433 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003434 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003435 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003436 switch terms[0] {
3437 case "mkdir":
3438 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003439 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003440 t.Fatal("copyCmds contains invalid cp command", cmd)
3441 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003442 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003443 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003444 isLink = false
3445 case "ln":
3446 if len(terms) != 3 && len(terms) != 4 {
3447 // ln LINK TARGET or ln -s LINK TARGET
3448 t.Fatal("copyCmds contains invalid ln command", cmd)
3449 }
3450 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003451 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003452 isLink = true
3453 default:
3454 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3455 }
3456 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003457 index := strings.Index(dst, imageApexDir)
3458 if index == -1 {
3459 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3460 }
3461 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003462 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003463 }
3464 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003465 return ret
3466}
3467
Jooyung Hana57af4a2020-01-23 05:36:59 +00003468func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3469 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003470 var failed bool
3471 var surplus []string
3472 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003473 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003474 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003475 for _, expected := range files {
3476 if matched, _ := path.Match(expected, file.path); matched {
3477 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003478 mactchFound = true
3479 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003480 }
3481 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003482 if !mactchFound {
3483 surplus = append(surplus, file.path)
3484 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003485 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003486
Jooyung Han31c470b2019-10-18 16:26:59 +09003487 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003488 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003489 t.Log("surplus files", surplus)
3490 failed = true
3491 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003492
3493 if len(files) > len(filesMatched) {
3494 var missing []string
3495 for _, expected := range files {
3496 if !filesMatched[expected] {
3497 missing = append(missing, expected)
3498 }
3499 }
3500 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003501 t.Log("missing files", missing)
3502 failed = true
3503 }
3504 if failed {
3505 t.Fail()
3506 }
3507}
3508
Jooyung Han344d5432019-08-23 11:17:39 +09003509func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003510 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003511 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003512 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003513 "etc/llndk.libraries.29.txt",
3514 "etc/vndkcore.libraries.29.txt",
3515 "etc/vndksp.libraries.29.txt",
3516 "etc/vndkprivate.libraries.29.txt",
3517 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003518 }
3519 testCases := []struct {
3520 vndkVersion string
3521 expectedFiles []string
3522 }{
3523 {
3524 vndkVersion: "current",
3525 expectedFiles: append(commonFiles,
3526 "lib/libvndk.so",
3527 "lib/libvndksp.so",
3528 "lib64/libvndk.so",
3529 "lib64/libvndksp.so"),
3530 },
3531 {
3532 vndkVersion: "",
3533 expectedFiles: append(commonFiles,
3534 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3535 "lib/libvndksp.so",
3536 "lib64/libvndksp.so"),
3537 },
3538 }
3539 for _, tc := range testCases {
3540 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3541 ctx := testApex(t, `
3542 apex_vndk {
3543 name: "com.android.vndk.current",
3544 key: "com.android.vndk.current.key",
3545 updatable: false,
3546 }
3547
3548 apex_key {
3549 name: "com.android.vndk.current.key",
3550 public_key: "testkey.avbpubkey",
3551 private_key: "testkey.pem",
3552 }
3553
3554 cc_library {
3555 name: "libvndk",
3556 srcs: ["mylib.cpp"],
3557 vendor_available: true,
3558 product_available: true,
3559 vndk: {
3560 enabled: true,
3561 },
3562 system_shared_libs: [],
3563 stl: "none",
3564 apex_available: [ "com.android.vndk.current" ],
3565 }
3566
3567 cc_library {
3568 name: "libvndksp",
3569 srcs: ["mylib.cpp"],
3570 vendor_available: true,
3571 product_available: true,
3572 vndk: {
3573 enabled: true,
3574 support_system_process: true,
3575 },
3576 system_shared_libs: [],
3577 stl: "none",
3578 apex_available: [ "com.android.vndk.current" ],
3579 }
3580
3581 // VNDK-Ext should not cause any problems
3582
3583 cc_library {
3584 name: "libvndk.ext",
3585 srcs: ["mylib2.cpp"],
3586 vendor: true,
3587 vndk: {
3588 enabled: true,
3589 extends: "libvndk",
3590 },
3591 system_shared_libs: [],
3592 stl: "none",
3593 }
3594
3595 cc_library {
3596 name: "libvndksp.ext",
3597 srcs: ["mylib2.cpp"],
3598 vendor: true,
3599 vndk: {
3600 enabled: true,
3601 support_system_process: true,
3602 extends: "libvndksp",
3603 },
3604 system_shared_libs: [],
3605 stl: "none",
3606 }
3607 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3608 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3609 }))
3610 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3611 })
3612 }
Jooyung Han344d5432019-08-23 11:17:39 +09003613}
3614
3615func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003616 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003617 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003618 name: "com.android.vndk.current",
3619 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003620 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003621 }
3622
3623 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003624 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003625 public_key: "testkey.avbpubkey",
3626 private_key: "testkey.pem",
3627 }
3628
3629 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003630 name: "libvndk",
3631 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003632 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003633 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003634 vndk: {
3635 enabled: true,
3636 },
3637 system_shared_libs: [],
3638 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003639 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003640 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003641
3642 cc_prebuilt_library_shared {
3643 name: "libvndk.arm",
3644 srcs: ["libvndk.arm.so"],
3645 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003646 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003647 vndk: {
3648 enabled: true,
3649 },
3650 enabled: false,
3651 arch: {
3652 arm: {
3653 enabled: true,
3654 },
3655 },
3656 system_shared_libs: [],
3657 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003658 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003659 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003660 `+vndkLibrariesTxtFiles("current"),
3661 withFiles(map[string][]byte{
3662 "libvndk.so": nil,
3663 "libvndk.arm.so": nil,
3664 }))
Colin Cross2807f002021-03-02 10:15:29 -08003665 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003666 "lib/libvndk.so",
3667 "lib/libvndk.arm.so",
3668 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003669 "lib/libc++.so",
3670 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003671 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003672 })
Jooyung Han344d5432019-08-23 11:17:39 +09003673}
3674
Jooyung Han39edb6c2019-11-06 16:53:07 +09003675func vndkLibrariesTxtFiles(vers ...string) (result string) {
3676 for _, v := range vers {
3677 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003678 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003679 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003680 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003681 name: "` + txt + `.libraries.txt",
3682 }
3683 `
3684 }
3685 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003686 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003687 result += `
3688 prebuilt_etc {
3689 name: "` + txt + `.libraries.` + v + `.txt",
3690 src: "dummy.txt",
3691 }
3692 `
3693 }
3694 }
3695 }
3696 return
3697}
3698
Jooyung Han344d5432019-08-23 11:17:39 +09003699func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003700 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003701 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003702 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003703 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003704 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003705 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003706 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003707 }
3708
3709 apex_key {
3710 name: "myapex.key",
3711 public_key: "testkey.avbpubkey",
3712 private_key: "testkey.pem",
3713 }
3714
Jooyung Han31c470b2019-10-18 16:26:59 +09003715 vndk_prebuilt_shared {
3716 name: "libvndk27",
3717 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003718 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003719 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003720 vndk: {
3721 enabled: true,
3722 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003723 target_arch: "arm64",
3724 arch: {
3725 arm: {
3726 srcs: ["libvndk27_arm.so"],
3727 },
3728 arm64: {
3729 srcs: ["libvndk27_arm64.so"],
3730 },
3731 },
Colin Cross2807f002021-03-02 10:15:29 -08003732 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003733 }
3734
3735 vndk_prebuilt_shared {
3736 name: "libvndk27",
3737 version: "27",
3738 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003739 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003740 vndk: {
3741 enabled: true,
3742 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003743 target_arch: "x86_64",
3744 arch: {
3745 x86: {
3746 srcs: ["libvndk27_x86.so"],
3747 },
3748 x86_64: {
3749 srcs: ["libvndk27_x86_64.so"],
3750 },
3751 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003752 }
3753 `+vndkLibrariesTxtFiles("27"),
3754 withFiles(map[string][]byte{
3755 "libvndk27_arm.so": nil,
3756 "libvndk27_arm64.so": nil,
3757 "libvndk27_x86.so": nil,
3758 "libvndk27_x86_64.so": nil,
3759 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003760
Colin Cross2807f002021-03-02 10:15:29 -08003761 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003762 "lib/libvndk27_arm.so",
3763 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003764 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003765 })
Jooyung Han344d5432019-08-23 11:17:39 +09003766}
3767
Jooyung Han90eee022019-10-01 20:02:42 +09003768func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003769 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003770 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003771 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003772 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003773 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003774 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003775 }
3776 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003777 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003778 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003779 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003780 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003781 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003782 }
3783 apex_key {
3784 name: "myapex.key",
3785 public_key: "testkey.avbpubkey",
3786 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003787 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003788
3789 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003790 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003791 actual := proptools.String(bundle.properties.Apex_name)
3792 if !reflect.DeepEqual(actual, expected) {
3793 t.Errorf("Got '%v', expected '%v'", actual, expected)
3794 }
3795 }
3796
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003797 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003798 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003799}
3800
Jooyung Han344d5432019-08-23 11:17:39 +09003801func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003802 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003803 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003804 name: "com.android.vndk.current",
3805 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003806 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003807 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003808 }
3809
3810 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003811 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003812 public_key: "testkey.avbpubkey",
3813 private_key: "testkey.pem",
3814 }
3815
3816 cc_library {
3817 name: "libvndk",
3818 srcs: ["mylib.cpp"],
3819 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003820 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003821 native_bridge_supported: true,
3822 host_supported: true,
3823 vndk: {
3824 enabled: true,
3825 },
3826 system_shared_libs: [],
3827 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003828 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003829 }
Colin Cross2807f002021-03-02 10:15:29 -08003830 `+vndkLibrariesTxtFiles("current"),
3831 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003832
Colin Cross2807f002021-03-02 10:15:29 -08003833 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003834 "lib/libvndk.so",
3835 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003836 "lib/libc++.so",
3837 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003838 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003839 })
Jooyung Han344d5432019-08-23 11:17:39 +09003840}
3841
3842func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003843 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003844 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003845 name: "com.android.vndk.current",
3846 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003847 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003848 native_bridge_supported: true,
3849 }
3850
3851 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003852 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003853 public_key: "testkey.avbpubkey",
3854 private_key: "testkey.pem",
3855 }
3856
3857 cc_library {
3858 name: "libvndk",
3859 srcs: ["mylib.cpp"],
3860 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003861 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003862 native_bridge_supported: true,
3863 host_supported: true,
3864 vndk: {
3865 enabled: true,
3866 },
3867 system_shared_libs: [],
3868 stl: "none",
3869 }
3870 `)
3871}
3872
Jooyung Han31c470b2019-10-18 16:26:59 +09003873func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003874 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003875 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003876 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003877 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003878 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003879 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003880 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003881 }
3882
3883 apex_key {
3884 name: "myapex.key",
3885 public_key: "testkey.avbpubkey",
3886 private_key: "testkey.pem",
3887 }
3888
3889 vndk_prebuilt_shared {
3890 name: "libvndk27",
3891 version: "27",
3892 target_arch: "arm",
3893 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003894 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003895 vndk: {
3896 enabled: true,
3897 },
3898 arch: {
3899 arm: {
3900 srcs: ["libvndk27.so"],
3901 }
3902 },
3903 }
3904
3905 vndk_prebuilt_shared {
3906 name: "libvndk27",
3907 version: "27",
3908 target_arch: "arm",
3909 binder32bit: true,
3910 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003911 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003912 vndk: {
3913 enabled: true,
3914 },
3915 arch: {
3916 arm: {
3917 srcs: ["libvndk27binder32.so"],
3918 }
3919 },
Colin Cross2807f002021-03-02 10:15:29 -08003920 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003921 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003922 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003923 withFiles(map[string][]byte{
3924 "libvndk27.so": nil,
3925 "libvndk27binder32.so": nil,
3926 }),
3927 withBinder32bit,
3928 withTargets(map[android.OsType][]android.Target{
3929 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003930 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3931 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003932 },
3933 }),
3934 )
3935
Colin Cross2807f002021-03-02 10:15:29 -08003936 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003937 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003938 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003939 })
3940}
3941
Jooyung Han45a96772020-06-15 14:59:42 +09003942func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003943 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003944 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003945 name: "com.android.vndk.current",
3946 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003947 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003948 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003949 }
3950
3951 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003952 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003953 public_key: "testkey.avbpubkey",
3954 private_key: "testkey.pem",
3955 }
3956
3957 cc_library {
3958 name: "libz",
3959 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003960 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003961 vndk: {
3962 enabled: true,
3963 },
3964 stubs: {
3965 symbol_file: "libz.map.txt",
3966 versions: ["30"],
3967 }
3968 }
3969 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3970 "libz.map.txt": nil,
3971 }))
3972
Colin Cross2807f002021-03-02 10:15:29 -08003973 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003974 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3975 ensureListEmpty(t, provideNativeLibs)
3976}
3977
Jooyung Hane1633032019-08-01 17:41:43 +09003978func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003979 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003980 apex {
3981 name: "myapex_nodep",
3982 key: "myapex.key",
3983 native_shared_libs: ["lib_nodep"],
3984 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003985 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003986 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003987 }
3988
3989 apex {
3990 name: "myapex_dep",
3991 key: "myapex.key",
3992 native_shared_libs: ["lib_dep"],
3993 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003994 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003995 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003996 }
3997
3998 apex {
3999 name: "myapex_provider",
4000 key: "myapex.key",
4001 native_shared_libs: ["libfoo"],
4002 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004003 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004004 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004005 }
4006
4007 apex {
4008 name: "myapex_selfcontained",
4009 key: "myapex.key",
4010 native_shared_libs: ["lib_dep", "libfoo"],
4011 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004012 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004013 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004014 }
4015
4016 apex_key {
4017 name: "myapex.key",
4018 public_key: "testkey.avbpubkey",
4019 private_key: "testkey.pem",
4020 }
4021
4022 cc_library {
4023 name: "lib_nodep",
4024 srcs: ["mylib.cpp"],
4025 system_shared_libs: [],
4026 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004027 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004028 }
4029
4030 cc_library {
4031 name: "lib_dep",
4032 srcs: ["mylib.cpp"],
4033 shared_libs: ["libfoo"],
4034 system_shared_libs: [],
4035 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004036 apex_available: [
4037 "myapex_dep",
4038 "myapex_provider",
4039 "myapex_selfcontained",
4040 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004041 }
4042
4043 cc_library {
4044 name: "libfoo",
4045 srcs: ["mytest.cpp"],
4046 stubs: {
4047 versions: ["1"],
4048 },
4049 system_shared_libs: [],
4050 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004051 apex_available: [
4052 "myapex_provider",
4053 "myapex_selfcontained",
4054 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004055 }
4056 `)
4057
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004058 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004059 var provideNativeLibs, requireNativeLibs []string
4060
Sundong Ahnabb64432019-10-22 13:58:29 +09004061 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004062 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4063 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004064 ensureListEmpty(t, provideNativeLibs)
4065 ensureListEmpty(t, requireNativeLibs)
4066
Sundong Ahnabb64432019-10-22 13:58:29 +09004067 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004068 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4069 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004070 ensureListEmpty(t, provideNativeLibs)
4071 ensureListContains(t, requireNativeLibs, "libfoo.so")
4072
Sundong Ahnabb64432019-10-22 13:58:29 +09004073 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004074 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4075 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004076 ensureListContains(t, provideNativeLibs, "libfoo.so")
4077 ensureListEmpty(t, requireNativeLibs)
4078
Sundong Ahnabb64432019-10-22 13:58:29 +09004079 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004080 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4081 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004082 ensureListContains(t, provideNativeLibs, "libfoo.so")
4083 ensureListEmpty(t, requireNativeLibs)
4084}
4085
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004086func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004087 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004088 apex {
4089 name: "myapex",
4090 key: "myapex.key",
4091 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09004092 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004093 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004094 }
4095
4096 apex_key {
4097 name: "myapex.key",
4098 public_key: "testkey.avbpubkey",
4099 private_key: "testkey.pem",
4100 }
Jiyong Parkdb334862020-02-05 17:19:28 +09004101
4102 cc_library {
4103 name: "mylib",
4104 srcs: ["mylib.cpp"],
4105 system_shared_libs: [],
4106 stl: "none",
4107 apex_available: [
4108 "//apex_available:platform",
4109 "myapex",
4110 ],
4111 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004112 `)
4113
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004114 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004115 apexManifestRule := module.Rule("apexManifestRule")
4116 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
4117 apexRule := module.Rule("apexRule")
4118 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09004119
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004120 apexBundle := module.Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004121 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09004122 name := apexBundle.BaseModuleName()
4123 prefix := "TARGET_"
4124 var builder strings.Builder
4125 data.Custom(&builder, name, prefix, "", data)
4126 androidMk := builder.String()
4127 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
4128 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004129}
4130
Alex Light0851b882019-02-07 13:20:53 -08004131func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004132 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004133 apex {
4134 name: "myapex",
4135 key: "myapex.key",
4136 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004137 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004138 }
4139
4140 apex_key {
4141 name: "myapex.key",
4142 public_key: "testkey.avbpubkey",
4143 private_key: "testkey.pem",
4144 }
4145
4146 cc_library {
4147 name: "mylib_common",
4148 srcs: ["mylib.cpp"],
4149 system_shared_libs: [],
4150 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004151 apex_available: [
4152 "//apex_available:platform",
4153 "myapex",
4154 ],
Alex Light0851b882019-02-07 13:20:53 -08004155 }
4156 `)
4157
Sundong Ahnabb64432019-10-22 13:58:29 +09004158 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004159 apexRule := module.Rule("apexRule")
4160 copyCmds := apexRule.Args["copy_commands"]
4161
4162 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4163 t.Log("Apex was a test apex!")
4164 t.Fail()
4165 }
4166 // Ensure that main rule creates an output
4167 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4168
4169 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004170 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004171
4172 // Ensure that both direct and indirect deps are copied into apex
4173 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4174
Colin Cross7113d202019-11-20 16:39:12 -08004175 // Ensure that the platform variant ends with _shared
4176 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004177
Colin Cross56a83212020-09-15 18:30:11 -07004178 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004179 t.Log("Found mylib_common not in any apex!")
4180 t.Fail()
4181 }
4182}
4183
4184func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004185 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004186 apex_test {
4187 name: "myapex",
4188 key: "myapex.key",
4189 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004190 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004191 }
4192
4193 apex_key {
4194 name: "myapex.key",
4195 public_key: "testkey.avbpubkey",
4196 private_key: "testkey.pem",
4197 }
4198
4199 cc_library {
4200 name: "mylib_common_test",
4201 srcs: ["mylib.cpp"],
4202 system_shared_libs: [],
4203 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004204 // TODO: remove //apex_available:platform
4205 apex_available: [
4206 "//apex_available:platform",
4207 "myapex",
4208 ],
Alex Light0851b882019-02-07 13:20:53 -08004209 }
4210 `)
4211
Sundong Ahnabb64432019-10-22 13:58:29 +09004212 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004213 apexRule := module.Rule("apexRule")
4214 copyCmds := apexRule.Args["copy_commands"]
4215
4216 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4217 t.Log("Apex was not a test apex!")
4218 t.Fail()
4219 }
4220 // Ensure that main rule creates an output
4221 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4222
4223 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004224 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004225
4226 // Ensure that both direct and indirect deps are copied into apex
4227 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4228
Colin Cross7113d202019-11-20 16:39:12 -08004229 // Ensure that the platform variant ends with _shared
4230 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004231}
4232
Alex Light9670d332019-01-29 18:07:33 -08004233func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004234 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004235 apex {
4236 name: "myapex",
4237 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004238 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004239 multilib: {
4240 first: {
4241 native_shared_libs: ["mylib_common"],
4242 }
4243 },
4244 target: {
4245 android: {
4246 multilib: {
4247 first: {
4248 native_shared_libs: ["mylib"],
4249 }
4250 }
4251 },
4252 host: {
4253 multilib: {
4254 first: {
4255 native_shared_libs: ["mylib2"],
4256 }
4257 }
4258 }
4259 }
4260 }
4261
4262 apex_key {
4263 name: "myapex.key",
4264 public_key: "testkey.avbpubkey",
4265 private_key: "testkey.pem",
4266 }
4267
4268 cc_library {
4269 name: "mylib",
4270 srcs: ["mylib.cpp"],
4271 system_shared_libs: [],
4272 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004273 // TODO: remove //apex_available:platform
4274 apex_available: [
4275 "//apex_available:platform",
4276 "myapex",
4277 ],
Alex Light9670d332019-01-29 18:07:33 -08004278 }
4279
4280 cc_library {
4281 name: "mylib_common",
4282 srcs: ["mylib.cpp"],
4283 system_shared_libs: [],
4284 stl: "none",
4285 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004286 // TODO: remove //apex_available:platform
4287 apex_available: [
4288 "//apex_available:platform",
4289 "myapex",
4290 ],
Alex Light9670d332019-01-29 18:07:33 -08004291 }
4292
4293 cc_library {
4294 name: "mylib2",
4295 srcs: ["mylib.cpp"],
4296 system_shared_libs: [],
4297 stl: "none",
4298 compile_multilib: "first",
4299 }
4300 `)
4301
Sundong Ahnabb64432019-10-22 13:58:29 +09004302 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004303 copyCmds := apexRule.Args["copy_commands"]
4304
4305 // Ensure that main rule creates an output
4306 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4307
4308 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004309 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4310 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4311 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004312
4313 // Ensure that both direct and indirect deps are copied into apex
4314 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4315 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4316 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4317
Colin Cross7113d202019-11-20 16:39:12 -08004318 // Ensure that the platform variant ends with _shared
4319 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4320 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4321 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004322}
Jiyong Park04480cf2019-02-06 00:16:29 +09004323
Jiyong Park59140302020-12-14 18:44:04 +09004324func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004325 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004326 apex {
4327 name: "myapex",
4328 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004329 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004330 arch: {
4331 arm64: {
4332 native_shared_libs: ["mylib.arm64"],
4333 },
4334 x86_64: {
4335 native_shared_libs: ["mylib.x64"],
4336 },
4337 }
4338 }
4339
4340 apex_key {
4341 name: "myapex.key",
4342 public_key: "testkey.avbpubkey",
4343 private_key: "testkey.pem",
4344 }
4345
4346 cc_library {
4347 name: "mylib.arm64",
4348 srcs: ["mylib.cpp"],
4349 system_shared_libs: [],
4350 stl: "none",
4351 // TODO: remove //apex_available:platform
4352 apex_available: [
4353 "//apex_available:platform",
4354 "myapex",
4355 ],
4356 }
4357
4358 cc_library {
4359 name: "mylib.x64",
4360 srcs: ["mylib.cpp"],
4361 system_shared_libs: [],
4362 stl: "none",
4363 // TODO: remove //apex_available:platform
4364 apex_available: [
4365 "//apex_available:platform",
4366 "myapex",
4367 ],
4368 }
4369 `)
4370
4371 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4372 copyCmds := apexRule.Args["copy_commands"]
4373
4374 // Ensure that apex variant is created for the direct dep
4375 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4376 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4377
4378 // Ensure that both direct and indirect deps are copied into apex
4379 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4380 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4381}
4382
Jiyong Park04480cf2019-02-06 00:16:29 +09004383func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004384 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004385 apex {
4386 name: "myapex",
4387 key: "myapex.key",
4388 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004389 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004390 }
4391
4392 apex_key {
4393 name: "myapex.key",
4394 public_key: "testkey.avbpubkey",
4395 private_key: "testkey.pem",
4396 }
4397
4398 sh_binary {
4399 name: "myscript",
4400 src: "mylib.cpp",
4401 filename: "myscript.sh",
4402 sub_dir: "script",
4403 }
4404 `)
4405
Sundong Ahnabb64432019-10-22 13:58:29 +09004406 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004407 copyCmds := apexRule.Args["copy_commands"]
4408
4409 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4410}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004411
Jooyung Han91df2082019-11-20 01:49:42 +09004412func TestApexInVariousPartition(t *testing.T) {
4413 testcases := []struct {
4414 propName, parition, flattenedPartition string
4415 }{
4416 {"", "system", "system_ext"},
4417 {"product_specific: true", "product", "product"},
4418 {"soc_specific: true", "vendor", "vendor"},
4419 {"proprietary: true", "vendor", "vendor"},
4420 {"vendor: true", "vendor", "vendor"},
4421 {"system_ext_specific: true", "system_ext", "system_ext"},
4422 }
4423 for _, tc := range testcases {
4424 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004425 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004426 apex {
4427 name: "myapex",
4428 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004429 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004430 `+tc.propName+`
4431 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004432
Jooyung Han91df2082019-11-20 01:49:42 +09004433 apex_key {
4434 name: "myapex.key",
4435 public_key: "testkey.avbpubkey",
4436 private_key: "testkey.pem",
4437 }
4438 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004439
Jooyung Han91df2082019-11-20 01:49:42 +09004440 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004441 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4442 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004443 if actual != expected {
4444 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4445 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004446
Jooyung Han91df2082019-11-20 01:49:42 +09004447 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004448 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4449 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004450 if actual != expected {
4451 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4452 }
4453 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004454 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004455}
Jiyong Park67882562019-03-21 01:11:21 +09004456
Jooyung Han580eb4f2020-06-24 19:33:06 +09004457func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004458 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004459 apex {
4460 name: "myapex",
4461 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004462 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004463 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004464
Jooyung Han580eb4f2020-06-24 19:33:06 +09004465 apex_key {
4466 name: "myapex.key",
4467 public_key: "testkey.avbpubkey",
4468 private_key: "testkey.pem",
4469 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004470 `)
4471 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004472 rule := module.Output("file_contexts")
4473 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4474}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004475
Jooyung Han580eb4f2020-06-24 19:33:06 +09004476func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004477 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004478 apex {
4479 name: "myapex",
4480 key: "myapex.key",
4481 file_contexts: "my_own_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 "my_own_file_contexts": nil,
4492 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004493}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004494
Jooyung Han580eb4f2020-06-24 19:33:06 +09004495func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004496 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004497 apex {
4498 name: "myapex",
4499 key: "myapex.key",
4500 product_specific: true,
4501 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004502 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004503 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004504
Jooyung Han580eb4f2020-06-24 19:33:06 +09004505 apex_key {
4506 name: "myapex.key",
4507 public_key: "testkey.avbpubkey",
4508 private_key: "testkey.pem",
4509 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004510 `)
4511
Colin Cross1c460562021-02-16 17:55:47 -08004512 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004513 apex {
4514 name: "myapex",
4515 key: "myapex.key",
4516 product_specific: true,
4517 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004518 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004519 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004520
Jooyung Han580eb4f2020-06-24 19:33:06 +09004521 apex_key {
4522 name: "myapex.key",
4523 public_key: "testkey.avbpubkey",
4524 private_key: "testkey.pem",
4525 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004526 `, withFiles(map[string][]byte{
4527 "product_specific_file_contexts": nil,
4528 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004529 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4530 rule := module.Output("file_contexts")
4531 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4532}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004533
Jooyung Han580eb4f2020-06-24 19:33:06 +09004534func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004535 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004536 apex {
4537 name: "myapex",
4538 key: "myapex.key",
4539 product_specific: true,
4540 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004541 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004542 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004543
Jooyung Han580eb4f2020-06-24 19:33:06 +09004544 apex_key {
4545 name: "myapex.key",
4546 public_key: "testkey.avbpubkey",
4547 private_key: "testkey.pem",
4548 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004549
Jooyung Han580eb4f2020-06-24 19:33:06 +09004550 filegroup {
4551 name: "my-file-contexts",
4552 srcs: ["product_specific_file_contexts"],
4553 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004554 `, withFiles(map[string][]byte{
4555 "product_specific_file_contexts": nil,
4556 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004557 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4558 rule := module.Output("file_contexts")
4559 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004560}
4561
Jiyong Park67882562019-03-21 01:11:21 +09004562func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004563 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004564 apex_key {
4565 name: "myapex.key",
4566 public_key: ":my.avbpubkey",
4567 private_key: ":my.pem",
4568 product_specific: true,
4569 }
4570
4571 filegroup {
4572 name: "my.avbpubkey",
4573 srcs: ["testkey2.avbpubkey"],
4574 }
4575
4576 filegroup {
4577 name: "my.pem",
4578 srcs: ["testkey2.pem"],
4579 }
4580 `)
4581
4582 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4583 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004584 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004585 if actual_pubkey != expected_pubkey {
4586 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4587 }
4588 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004589 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004590 if actual_privkey != expected_privkey {
4591 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4592 }
4593}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004594
4595func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004596 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004597 prebuilt_apex {
4598 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004599 arch: {
4600 arm64: {
4601 src: "myapex-arm64.apex",
4602 },
4603 arm: {
4604 src: "myapex-arm.apex",
4605 },
4606 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004607 }
4608 `)
4609
Paul Duffin6717d882021-06-15 19:09:41 +01004610 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004611
Jiyong Parkc95714e2019-03-29 14:23:10 +09004612 expectedInput := "myapex-arm64.apex"
4613 if prebuilt.inputApex.String() != expectedInput {
4614 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4615 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004616}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004617
Paul Duffinc0609c62021-03-01 17:27:16 +00004618func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004619 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004620 prebuilt_apex {
4621 name: "myapex",
4622 }
4623 `)
4624}
4625
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004626func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004627 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004628 prebuilt_apex {
4629 name: "myapex",
4630 src: "myapex-arm.apex",
4631 filename: "notmyapex.apex",
4632 }
4633 `)
4634
Paul Duffin6717d882021-06-15 19:09:41 +01004635 p := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004636
4637 expected := "notmyapex.apex"
4638 if p.installFilename != expected {
4639 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4640 }
4641}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004642
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004643func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004644 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004645 prebuilt_apex {
4646 name: "myapex.prebuilt",
4647 src: "myapex-arm.apex",
4648 overrides: [
4649 "myapex",
4650 ],
4651 }
4652 `)
4653
Paul Duffin6717d882021-06-15 19:09:41 +01004654 p := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt").Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004655
4656 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004657 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004658 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004659 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004660 }
4661}
4662
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004663func TestPrebuiltApexName(t *testing.T) {
4664 testApex(t, `
4665 prebuilt_apex {
4666 name: "com.company.android.myapex",
4667 apex_name: "com.android.myapex",
4668 src: "company-myapex-arm.apex",
4669 }
4670 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4671
4672 testApex(t, `
4673 apex_set {
4674 name: "com.company.android.myapex",
4675 apex_name: "com.android.myapex",
4676 set: "company-myapex.apks",
4677 }
4678 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4679}
4680
4681func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4682 _ = android.GroupFixturePreparers(
4683 java.PrepareForTestWithJavaDefaultModules,
4684 PrepareForTestWithApexBuildComponents,
4685 android.FixtureWithRootAndroidBp(`
4686 platform_bootclasspath {
4687 name: "platform-bootclasspath",
4688 fragments: [
4689 {
4690 apex: "com.android.art",
4691 module: "art-bootclasspath-fragment",
4692 },
4693 ],
4694 }
4695
4696 prebuilt_apex {
4697 name: "com.company.android.art",
4698 apex_name: "com.android.art",
4699 src: "com.company.android.art-arm.apex",
4700 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4701 }
4702
4703 prebuilt_bootclasspath_fragment {
4704 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004705 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004706 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004707 hidden_api: {
4708 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4709 metadata: "my-bootclasspath-fragment/metadata.csv",
4710 index: "my-bootclasspath-fragment/index.csv",
4711 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4712 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4713 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004714 }
4715
4716 java_import {
4717 name: "core-oj",
4718 jars: ["prebuilt.jar"],
4719 }
4720 `),
4721 ).RunTest(t)
4722}
4723
Paul Duffin092153d2021-01-26 11:42:39 +00004724// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4725// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004726func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004727 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004728
Paul Duffin89886cb2021-02-05 16:44:03 +00004729 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004730 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004731 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004732 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004733 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004734 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4735 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4736 android.NormalizePathForTesting(dexJarBuildPath))
4737 }
4738
4739 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
4740 // Make sure the import has been given the correct path to the dex jar.
4741 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4742 dexJarBuildPath := p.DexJarInstallPath()
4743 stem := android.RemoveOptionalPrebuiltPrefix(name)
4744 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4745 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4746 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004747 }
4748
Paul Duffin39853512021-02-26 11:09:39 +00004749 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004750 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004751 android.AssertArrayString(t, "Check if there is no source variant",
4752 []string{"android_common"},
4753 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004754 }
4755
4756 t.Run("prebuilt only", func(t *testing.T) {
4757 bp := `
4758 prebuilt_apex {
4759 name: "myapex",
4760 arch: {
4761 arm64: {
4762 src: "myapex-arm64.apex",
4763 },
4764 arm: {
4765 src: "myapex-arm.apex",
4766 },
4767 },
Paul Duffin39853512021-02-26 11:09:39 +00004768 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004769 }
4770
4771 java_import {
4772 name: "libfoo",
4773 jars: ["libfoo.jar"],
4774 }
Paul Duffin39853512021-02-26 11:09:39 +00004775
4776 java_sdk_library_import {
4777 name: "libbar",
4778 public: {
4779 jars: ["libbar.jar"],
4780 },
4781 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004782 `
4783
4784 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4785 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4786
Paul Duffinf6932af2021-02-26 18:21:56 +00004787 // Make sure that the deapexer has the correct input APEX.
4788 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4789 rule := deapexer.Rule("deapexer")
4790 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4791 t.Errorf("expected: %q, found: %q", expected, actual)
4792 }
4793
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004794 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004795 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004796 rule = prebuiltApex.Rule("android/soong/android.Cp")
4797 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4798 t.Errorf("expected: %q, found: %q", expected, actual)
4799 }
4800
Paul Duffin89886cb2021-02-05 16:44:03 +00004801 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004802 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004803
4804 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004805 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004806 })
4807
4808 t.Run("prebuilt with source preferred", func(t *testing.T) {
4809
4810 bp := `
4811 prebuilt_apex {
4812 name: "myapex",
4813 arch: {
4814 arm64: {
4815 src: "myapex-arm64.apex",
4816 },
4817 arm: {
4818 src: "myapex-arm.apex",
4819 },
4820 },
Paul Duffin39853512021-02-26 11:09:39 +00004821 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004822 }
4823
4824 java_import {
4825 name: "libfoo",
4826 jars: ["libfoo.jar"],
4827 }
4828
4829 java_library {
4830 name: "libfoo",
4831 }
Paul Duffin39853512021-02-26 11:09:39 +00004832
4833 java_sdk_library_import {
4834 name: "libbar",
4835 public: {
4836 jars: ["libbar.jar"],
4837 },
4838 }
4839
4840 java_sdk_library {
4841 name: "libbar",
4842 srcs: ["foo/bar/MyClass.java"],
4843 unsafe_ignore_missing_latest_api: true,
4844 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004845 `
4846
4847 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4848 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4849
Paul Duffin89886cb2021-02-05 16:44:03 +00004850 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004851 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004852 ensureNoSourceVariant(t, ctx, "libfoo")
4853
4854 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004855 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004856 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004857 })
4858
4859 t.Run("prebuilt preferred with source", func(t *testing.T) {
4860 bp := `
4861 prebuilt_apex {
4862 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004863 arch: {
4864 arm64: {
4865 src: "myapex-arm64.apex",
4866 },
4867 arm: {
4868 src: "myapex-arm.apex",
4869 },
4870 },
Paul Duffin39853512021-02-26 11:09:39 +00004871 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004872 }
4873
4874 java_import {
4875 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004876 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004877 jars: ["libfoo.jar"],
4878 }
4879
4880 java_library {
4881 name: "libfoo",
4882 }
Paul Duffin39853512021-02-26 11:09:39 +00004883
4884 java_sdk_library_import {
4885 name: "libbar",
4886 prefer: true,
4887 public: {
4888 jars: ["libbar.jar"],
4889 },
4890 }
4891
4892 java_sdk_library {
4893 name: "libbar",
4894 srcs: ["foo/bar/MyClass.java"],
4895 unsafe_ignore_missing_latest_api: true,
4896 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004897 `
4898
4899 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4900 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4901
Paul Duffin89886cb2021-02-05 16:44:03 +00004902 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004903 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004904 ensureNoSourceVariant(t, ctx, "libfoo")
4905
4906 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004907 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004908 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004909 })
4910}
4911
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004912func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004913 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01004914 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01004915 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4916 // is disabled.
4917 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4918 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004919
Paul Duffin37856732021-02-26 14:24:15 +00004920 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4921 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004922 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004923 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004924 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004925 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004926 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004927 foundLibfooJar = true
4928 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004929 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004930 }
4931 }
4932 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004933 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 +00004934 }
4935 }
4936
Paul Duffin40a3f652021-07-19 13:11:24 +01004937 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004938 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004939 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01004940 var rule android.TestingBuildParams
4941
4942 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
4943 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004944 }
4945
Paul Duffin40a3f652021-07-19 13:11:24 +01004946 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
4947 t.Helper()
4948 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
4949 var rule android.TestingBuildParams
4950
4951 rule = platformBootclasspath.Output("hiddenapi-index.csv")
4952 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
4953 }
4954
Paul Duffin89f570a2021-06-16 01:42:33 +01004955 fragment := java.ApexVariantReference{
4956 Apex: proptools.StringPtr("myapex"),
4957 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4958 }
4959
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004960 t.Run("prebuilt only", func(t *testing.T) {
4961 bp := `
4962 prebuilt_apex {
4963 name: "myapex",
4964 arch: {
4965 arm64: {
4966 src: "myapex-arm64.apex",
4967 },
4968 arm: {
4969 src: "myapex-arm.apex",
4970 },
4971 },
Paul Duffin89f570a2021-06-16 01:42:33 +01004972 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4973 }
4974
4975 prebuilt_bootclasspath_fragment {
4976 name: "my-bootclasspath-fragment",
4977 contents: ["libfoo", "libbar"],
4978 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01004979 hidden_api: {
4980 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4981 metadata: "my-bootclasspath-fragment/metadata.csv",
4982 index: "my-bootclasspath-fragment/index.csv",
4983 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4984 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4985 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004986 }
4987
4988 java_import {
4989 name: "libfoo",
4990 jars: ["libfoo.jar"],
4991 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01004992 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004993 }
Paul Duffin37856732021-02-26 14:24:15 +00004994
4995 java_sdk_library_import {
4996 name: "libbar",
4997 public: {
4998 jars: ["libbar.jar"],
4999 },
5000 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005001 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005002 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005003 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005004 `
5005
Paul Duffin89f570a2021-06-16 01:42:33 +01005006 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005007 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5008 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005009
Paul Duffin537ea3d2021-05-14 10:38:00 +01005010 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005011 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005012 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005013 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005014 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5015 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005016 })
5017
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005018 t.Run("apex_set only", func(t *testing.T) {
5019 bp := `
5020 apex_set {
5021 name: "myapex",
5022 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005023 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5024 }
5025
5026 prebuilt_bootclasspath_fragment {
5027 name: "my-bootclasspath-fragment",
5028 contents: ["libfoo", "libbar"],
5029 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005030 hidden_api: {
5031 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5032 metadata: "my-bootclasspath-fragment/metadata.csv",
5033 index: "my-bootclasspath-fragment/index.csv",
5034 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5035 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5036 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005037 }
5038
5039 java_import {
5040 name: "libfoo",
5041 jars: ["libfoo.jar"],
5042 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005043 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005044 }
5045
5046 java_sdk_library_import {
5047 name: "libbar",
5048 public: {
5049 jars: ["libbar.jar"],
5050 },
5051 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005052 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005053 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005054 }
5055 `
5056
Paul Duffin89f570a2021-06-16 01:42:33 +01005057 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005058 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5059 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5060
Paul Duffin537ea3d2021-05-14 10:38:00 +01005061 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005062 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005063 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005064 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005065 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5066 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005067 })
5068
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005069 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5070 bp := `
5071 prebuilt_apex {
5072 name: "myapex",
5073 arch: {
5074 arm64: {
5075 src: "myapex-arm64.apex",
5076 },
5077 arm: {
5078 src: "myapex-arm.apex",
5079 },
5080 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005081 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5082 }
5083
5084 prebuilt_bootclasspath_fragment {
5085 name: "my-bootclasspath-fragment",
5086 contents: ["libfoo", "libbar"],
5087 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005088 hidden_api: {
5089 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5090 metadata: "my-bootclasspath-fragment/metadata.csv",
5091 index: "my-bootclasspath-fragment/index.csv",
5092 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5093 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5094 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005095 }
5096
5097 java_import {
5098 name: "libfoo",
5099 jars: ["libfoo.jar"],
5100 apex_available: ["myapex"],
5101 }
5102
5103 java_library {
5104 name: "libfoo",
5105 srcs: ["foo/bar/MyClass.java"],
5106 apex_available: ["myapex"],
5107 }
Paul Duffin37856732021-02-26 14:24:15 +00005108
5109 java_sdk_library_import {
5110 name: "libbar",
5111 public: {
5112 jars: ["libbar.jar"],
5113 },
5114 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005115 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005116 }
5117
5118 java_sdk_library {
5119 name: "libbar",
5120 srcs: ["foo/bar/MyClass.java"],
5121 unsafe_ignore_missing_latest_api: true,
5122 apex_available: ["myapex"],
5123 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005124 `
5125
5126 // In this test the source (java_library) libfoo is active since the
5127 // prebuilt (java_import) defaults to prefer:false. However the
5128 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5129 // find the dex boot jar in it. We either need to disable the source libfoo
5130 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005131 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005132 })
5133
5134 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5135 bp := `
5136 prebuilt_apex {
5137 name: "myapex",
5138 arch: {
5139 arm64: {
5140 src: "myapex-arm64.apex",
5141 },
5142 arm: {
5143 src: "myapex-arm.apex",
5144 },
5145 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005146 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5147 }
5148
5149 prebuilt_bootclasspath_fragment {
5150 name: "my-bootclasspath-fragment",
5151 contents: ["libfoo", "libbar"],
5152 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005153 hidden_api: {
5154 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5155 metadata: "my-bootclasspath-fragment/metadata.csv",
5156 index: "my-bootclasspath-fragment/index.csv",
5157 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5158 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5159 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005160 }
5161
5162 java_import {
5163 name: "libfoo",
5164 prefer: true,
5165 jars: ["libfoo.jar"],
5166 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005167 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005168 }
5169
5170 java_library {
5171 name: "libfoo",
5172 srcs: ["foo/bar/MyClass.java"],
5173 apex_available: ["myapex"],
5174 }
Paul Duffin37856732021-02-26 14:24:15 +00005175
5176 java_sdk_library_import {
5177 name: "libbar",
5178 prefer: true,
5179 public: {
5180 jars: ["libbar.jar"],
5181 },
5182 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005183 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005184 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005185 }
5186
5187 java_sdk_library {
5188 name: "libbar",
5189 srcs: ["foo/bar/MyClass.java"],
5190 unsafe_ignore_missing_latest_api: true,
5191 apex_available: ["myapex"],
5192 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005193 `
5194
Paul Duffin89f570a2021-06-16 01:42:33 +01005195 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005196 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5197 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005198
Paul Duffin537ea3d2021-05-14 10:38:00 +01005199 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005200 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005201 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005202 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005203 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5204 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005205 })
5206
5207 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5208 bp := `
5209 apex {
5210 name: "myapex",
5211 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005212 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005213 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005214 }
5215
5216 apex_key {
5217 name: "myapex.key",
5218 public_key: "testkey.avbpubkey",
5219 private_key: "testkey.pem",
5220 }
5221
5222 prebuilt_apex {
5223 name: "myapex",
5224 arch: {
5225 arm64: {
5226 src: "myapex-arm64.apex",
5227 },
5228 arm: {
5229 src: "myapex-arm.apex",
5230 },
5231 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005232 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5233 }
5234
5235 prebuilt_bootclasspath_fragment {
5236 name: "my-bootclasspath-fragment",
5237 contents: ["libfoo", "libbar"],
5238 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005239 hidden_api: {
5240 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5241 metadata: "my-bootclasspath-fragment/metadata.csv",
5242 index: "my-bootclasspath-fragment/index.csv",
5243 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5244 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5245 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005246 }
5247
5248 java_import {
5249 name: "libfoo",
5250 jars: ["libfoo.jar"],
5251 apex_available: ["myapex"],
5252 }
5253
5254 java_library {
5255 name: "libfoo",
5256 srcs: ["foo/bar/MyClass.java"],
5257 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005258 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005259 }
Paul Duffin37856732021-02-26 14:24:15 +00005260
5261 java_sdk_library_import {
5262 name: "libbar",
5263 public: {
5264 jars: ["libbar.jar"],
5265 },
5266 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005267 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005268 }
5269
5270 java_sdk_library {
5271 name: "libbar",
5272 srcs: ["foo/bar/MyClass.java"],
5273 unsafe_ignore_missing_latest_api: true,
5274 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005275 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005276 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005277 `
5278
Paul Duffin89f570a2021-06-16 01:42:33 +01005279 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005280 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5281 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005282
Paul Duffin537ea3d2021-05-14 10:38:00 +01005283 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005284 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005285 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005286 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005287 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5288 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005289 })
5290
5291 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5292 bp := `
5293 apex {
5294 name: "myapex",
5295 enabled: false,
5296 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005297 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005298 }
5299
5300 apex_key {
5301 name: "myapex.key",
5302 public_key: "testkey.avbpubkey",
5303 private_key: "testkey.pem",
5304 }
5305
5306 prebuilt_apex {
5307 name: "myapex",
5308 arch: {
5309 arm64: {
5310 src: "myapex-arm64.apex",
5311 },
5312 arm: {
5313 src: "myapex-arm.apex",
5314 },
5315 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005316 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5317 }
5318
5319 prebuilt_bootclasspath_fragment {
5320 name: "my-bootclasspath-fragment",
5321 contents: ["libfoo", "libbar"],
5322 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005323 hidden_api: {
5324 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5325 metadata: "my-bootclasspath-fragment/metadata.csv",
5326 index: "my-bootclasspath-fragment/index.csv",
5327 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5328 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5329 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005330 }
5331
5332 java_import {
5333 name: "libfoo",
5334 prefer: true,
5335 jars: ["libfoo.jar"],
5336 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005337 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005338 }
5339
5340 java_library {
5341 name: "libfoo",
5342 srcs: ["foo/bar/MyClass.java"],
5343 apex_available: ["myapex"],
5344 }
Paul Duffin37856732021-02-26 14:24:15 +00005345
5346 java_sdk_library_import {
5347 name: "libbar",
5348 prefer: true,
5349 public: {
5350 jars: ["libbar.jar"],
5351 },
5352 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005353 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005354 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005355 }
5356
5357 java_sdk_library {
5358 name: "libbar",
5359 srcs: ["foo/bar/MyClass.java"],
5360 unsafe_ignore_missing_latest_api: true,
5361 apex_available: ["myapex"],
5362 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005363 `
5364
Paul Duffin89f570a2021-06-16 01:42:33 +01005365 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005366 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5367 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005368
Paul Duffin537ea3d2021-05-14 10:38:00 +01005369 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005370 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005371 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005372 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005373 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5374 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005375 })
5376}
5377
Roland Levillain630846d2019-06-26 12:48:34 +01005378func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005379 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005380 apex_test {
5381 name: "myapex",
5382 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005383 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005384 tests: [
5385 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005386 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005387 ],
5388 }
5389
5390 apex_key {
5391 name: "myapex.key",
5392 public_key: "testkey.avbpubkey",
5393 private_key: "testkey.pem",
5394 }
5395
Liz Kammer1c14a212020-05-12 15:26:55 -07005396 filegroup {
5397 name: "fg",
5398 srcs: [
5399 "baz",
5400 "bar/baz"
5401 ],
5402 }
5403
Roland Levillain630846d2019-06-26 12:48:34 +01005404 cc_test {
5405 name: "mytest",
5406 gtest: false,
5407 srcs: ["mytest.cpp"],
5408 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005409 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005410 system_shared_libs: [],
5411 static_executable: true,
5412 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005413 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005414 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005415
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005416 cc_library {
5417 name: "mylib",
5418 srcs: ["mylib.cpp"],
5419 system_shared_libs: [],
5420 stl: "none",
5421 }
5422
Liz Kammer5bd365f2020-05-27 15:15:11 -07005423 filegroup {
5424 name: "fg2",
5425 srcs: [
5426 "testdata/baz"
5427 ],
5428 }
5429
Roland Levillain9b5fde92019-06-28 15:41:19 +01005430 cc_test {
5431 name: "mytests",
5432 gtest: false,
5433 srcs: [
5434 "mytest1.cpp",
5435 "mytest2.cpp",
5436 "mytest3.cpp",
5437 ],
5438 test_per_src: true,
5439 relative_install_path: "test",
5440 system_shared_libs: [],
5441 static_executable: true,
5442 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005443 data: [
5444 ":fg",
5445 ":fg2",
5446 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005447 }
Roland Levillain630846d2019-06-26 12:48:34 +01005448 `)
5449
Sundong Ahnabb64432019-10-22 13:58:29 +09005450 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005451 copyCmds := apexRule.Args["copy_commands"]
5452
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005453 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005454 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005455 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005456
Liz Kammer1c14a212020-05-12 15:26:55 -07005457 //Ensure that test data are copied into apex.
5458 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5459 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5460
Roland Levillain9b5fde92019-06-28 15:41:19 +01005461 // Ensure that test deps built with `test_per_src` are copied into apex.
5462 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5463 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5464 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005465
5466 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005467 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005468 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005469 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005470 prefix := "TARGET_"
5471 var builder strings.Builder
5472 data.Custom(&builder, name, prefix, "", data)
5473 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09005474 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5475 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5476 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5477 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09005478 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09005479 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005480 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005481
5482 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005483 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005484 data.Custom(&builder, name, prefix, "", data)
5485 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005486 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5487 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005488}
5489
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005490func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005491 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005492 apex {
5493 name: "myapex",
5494 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005495 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005496 }
5497 apex_key {
5498 name: "myapex.key",
5499 public_key: "testkey.avbpubkey",
5500 private_key: "testkey.pem",
5501 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005502 `,
5503 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5504 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5505 }),
5506 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005507 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09005508 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005509 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005510 var builder strings.Builder
5511 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5512 androidMk := builder.String()
5513 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
5514}
5515
Jooyung Hand48f3c32019-08-23 11:18:57 +09005516func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5517 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5518 apex {
5519 name: "myapex",
5520 key: "myapex.key",
5521 native_shared_libs: ["libfoo"],
5522 }
5523
5524 apex_key {
5525 name: "myapex.key",
5526 public_key: "testkey.avbpubkey",
5527 private_key: "testkey.pem",
5528 }
5529
5530 cc_library {
5531 name: "libfoo",
5532 stl: "none",
5533 system_shared_libs: [],
5534 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005535 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005536 }
5537 `)
5538 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5539 apex {
5540 name: "myapex",
5541 key: "myapex.key",
5542 java_libs: ["myjar"],
5543 }
5544
5545 apex_key {
5546 name: "myapex.key",
5547 public_key: "testkey.avbpubkey",
5548 private_key: "testkey.pem",
5549 }
5550
5551 java_library {
5552 name: "myjar",
5553 srcs: ["foo/bar/MyClass.java"],
5554 sdk_version: "none",
5555 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005556 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005557 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005558 }
5559 `)
5560}
5561
Bill Peckhama41a6962021-01-11 10:58:54 -08005562func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005563 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005564 apex {
5565 name: "myapex",
5566 key: "myapex.key",
5567 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005568 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005569 }
5570
5571 apex_key {
5572 name: "myapex.key",
5573 public_key: "testkey.avbpubkey",
5574 private_key: "testkey.pem",
5575 }
5576
5577 java_import {
5578 name: "myjavaimport",
5579 apex_available: ["myapex"],
5580 jars: ["my.jar"],
5581 compile_dex: true,
5582 }
5583 `)
5584
5585 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5586 apexRule := module.Rule("apexRule")
5587 copyCmds := apexRule.Args["copy_commands"]
5588 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5589}
5590
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005591func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005592 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005593 apex {
5594 name: "myapex",
5595 key: "myapex.key",
5596 apps: [
5597 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005598 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005599 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005600 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005601 }
5602
5603 apex_key {
5604 name: "myapex.key",
5605 public_key: "testkey.avbpubkey",
5606 private_key: "testkey.pem",
5607 }
5608
5609 android_app {
5610 name: "AppFoo",
5611 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005612 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005613 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005614 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005615 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005616 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005617 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005618
5619 android_app {
5620 name: "AppFooPriv",
5621 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005622 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005623 system_modules: "none",
5624 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005625 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005626 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005627 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005628
5629 cc_library_shared {
5630 name: "libjni",
5631 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005632 shared_libs: ["libfoo"],
5633 stl: "none",
5634 system_shared_libs: [],
5635 apex_available: [ "myapex" ],
5636 sdk_version: "current",
5637 }
5638
5639 cc_library_shared {
5640 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005641 stl: "none",
5642 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005643 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005644 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005645 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005646 `)
5647
Sundong Ahnabb64432019-10-22 13:58:29 +09005648 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005649 apexRule := module.Rule("apexRule")
5650 copyCmds := apexRule.Args["copy_commands"]
5651
5652 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005653 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005654
Colin Crossaede88c2020-08-11 12:17:01 -07005655 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005656 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005657 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005658 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005659 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005660 // JNI libraries including transitive deps are
5661 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005662 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005663 // ... embedded inside APK (jnilibs.zip)
5664 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5665 // ... and not directly inside the APEX
5666 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5667 }
Dario Frenicde2a032019-10-27 00:29:22 +01005668}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005669
Dario Frenicde2a032019-10-27 00:29:22 +01005670func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005671 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005672 apex {
5673 name: "myapex",
5674 key: "myapex.key",
5675 apps: [
5676 "AppFooPrebuilt",
5677 "AppFooPrivPrebuilt",
5678 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005679 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005680 }
5681
5682 apex_key {
5683 name: "myapex.key",
5684 public_key: "testkey.avbpubkey",
5685 private_key: "testkey.pem",
5686 }
5687
5688 android_app_import {
5689 name: "AppFooPrebuilt",
5690 apk: "PrebuiltAppFoo.apk",
5691 presigned: true,
5692 dex_preopt: {
5693 enabled: false,
5694 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005695 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005696 }
5697
5698 android_app_import {
5699 name: "AppFooPrivPrebuilt",
5700 apk: "PrebuiltAppFooPriv.apk",
5701 privileged: true,
5702 presigned: true,
5703 dex_preopt: {
5704 enabled: false,
5705 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005706 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005707 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005708 }
5709 `)
5710
Sundong Ahnabb64432019-10-22 13:58:29 +09005711 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005712 apexRule := module.Rule("apexRule")
5713 copyCmds := apexRule.Args["copy_commands"]
5714
5715 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005716 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5717}
5718
5719func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005720 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005721 apex {
5722 name: "myapex",
5723 key: "myapex.key",
5724 apps: [
5725 "AppFoo",
5726 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005727 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005728 }
5729
5730 apex_key {
5731 name: "myapex.key",
5732 public_key: "testkey.avbpubkey",
5733 private_key: "testkey.pem",
5734 }
5735
5736 android_app {
5737 name: "AppFoo",
5738 srcs: ["foo/bar/MyClass.java"],
5739 sdk_version: "none",
5740 system_modules: "none",
5741 apex_available: [ "myapex" ],
5742 }
5743
5744 android_app_import {
5745 name: "AppFoo",
5746 apk: "AppFooPrebuilt.apk",
5747 filename: "AppFooPrebuilt.apk",
5748 presigned: true,
5749 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005750 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005751 }
5752 `, withFiles(map[string][]byte{
5753 "AppFooPrebuilt.apk": nil,
5754 }))
5755
5756 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005757 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005758 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005759}
5760
Dario Freni6f3937c2019-12-20 22:58:03 +00005761func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005762 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005763 apex {
5764 name: "myapex",
5765 key: "myapex.key",
5766 apps: [
5767 "TesterHelpAppFoo",
5768 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005769 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005770 }
5771
5772 apex_key {
5773 name: "myapex.key",
5774 public_key: "testkey.avbpubkey",
5775 private_key: "testkey.pem",
5776 }
5777
5778 android_test_helper_app {
5779 name: "TesterHelpAppFoo",
5780 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005781 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005782 }
5783
5784 `)
5785
5786 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5787 apexRule := module.Rule("apexRule")
5788 copyCmds := apexRule.Args["copy_commands"]
5789
5790 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5791}
5792
Jooyung Han18020ea2019-11-13 10:50:48 +09005793func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5794 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005795 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005796 apex {
5797 name: "myapex",
5798 key: "myapex.key",
5799 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005800 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005801 }
5802
5803 apex_key {
5804 name: "myapex.key",
5805 public_key: "testkey.avbpubkey",
5806 private_key: "testkey.pem",
5807 }
5808
5809 apex {
5810 name: "otherapex",
5811 key: "myapex.key",
5812 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005813 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005814 }
5815
5816 cc_defaults {
5817 name: "libfoo-defaults",
5818 apex_available: ["otherapex"],
5819 }
5820
5821 cc_library {
5822 name: "libfoo",
5823 defaults: ["libfoo-defaults"],
5824 stl: "none",
5825 system_shared_libs: [],
5826 }`)
5827}
5828
Paul Duffine52e66f2020-03-30 17:54:29 +01005829func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005830 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005831 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005832 apex {
5833 name: "myapex",
5834 key: "myapex.key",
5835 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005836 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005837 }
5838
5839 apex_key {
5840 name: "myapex.key",
5841 public_key: "testkey.avbpubkey",
5842 private_key: "testkey.pem",
5843 }
5844
5845 apex {
5846 name: "otherapex",
5847 key: "otherapex.key",
5848 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005849 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005850 }
5851
5852 apex_key {
5853 name: "otherapex.key",
5854 public_key: "testkey.avbpubkey",
5855 private_key: "testkey.pem",
5856 }
5857
5858 cc_library {
5859 name: "libfoo",
5860 stl: "none",
5861 system_shared_libs: [],
5862 apex_available: ["otherapex"],
5863 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005864}
Jiyong Park127b40b2019-09-30 16:04:35 +09005865
Paul Duffine52e66f2020-03-30 17:54:29 +01005866func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005867 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005868 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005869.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005870.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005871.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005872.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005873.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005874.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005875 apex {
5876 name: "myapex",
5877 key: "myapex.key",
5878 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005879 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005880 }
5881
5882 apex_key {
5883 name: "myapex.key",
5884 public_key: "testkey.avbpubkey",
5885 private_key: "testkey.pem",
5886 }
5887
Jiyong Park127b40b2019-09-30 16:04:35 +09005888 cc_library {
5889 name: "libfoo",
5890 stl: "none",
5891 shared_libs: ["libbar"],
5892 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005893 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005894 }
5895
5896 cc_library {
5897 name: "libbar",
5898 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005899 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005900 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005901 apex_available: ["myapex"],
5902 }
5903
5904 cc_library {
5905 name: "libbaz",
5906 stl: "none",
5907 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005908 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005909}
Jiyong Park127b40b2019-09-30 16:04:35 +09005910
Paul Duffine52e66f2020-03-30 17:54:29 +01005911func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005912 testApexError(t, "\"otherapex\" is not a valid module name", `
5913 apex {
5914 name: "myapex",
5915 key: "myapex.key",
5916 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005917 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005918 }
5919
5920 apex_key {
5921 name: "myapex.key",
5922 public_key: "testkey.avbpubkey",
5923 private_key: "testkey.pem",
5924 }
5925
5926 cc_library {
5927 name: "libfoo",
5928 stl: "none",
5929 system_shared_libs: [],
5930 apex_available: ["otherapex"],
5931 }`)
5932
Paul Duffine52e66f2020-03-30 17:54:29 +01005933 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005934 apex {
5935 name: "myapex",
5936 key: "myapex.key",
5937 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005938 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005939 }
5940
5941 apex_key {
5942 name: "myapex.key",
5943 public_key: "testkey.avbpubkey",
5944 private_key: "testkey.pem",
5945 }
5946
5947 cc_library {
5948 name: "libfoo",
5949 stl: "none",
5950 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005951 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005952 apex_available: ["myapex"],
5953 }
5954
5955 cc_library {
5956 name: "libbar",
5957 stl: "none",
5958 system_shared_libs: [],
5959 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005960 }
5961
5962 cc_library {
5963 name: "libbaz",
5964 stl: "none",
5965 system_shared_libs: [],
5966 stubs: {
5967 versions: ["10", "20", "30"],
5968 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005969 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005970}
Jiyong Park127b40b2019-09-30 16:04:35 +09005971
Jiyong Park89e850a2020-04-07 16:37:39 +09005972func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005973 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005974 apex {
5975 name: "myapex",
5976 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005977 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005978 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005979 }
5980
5981 apex_key {
5982 name: "myapex.key",
5983 public_key: "testkey.avbpubkey",
5984 private_key: "testkey.pem",
5985 }
5986
5987 cc_library {
5988 name: "libfoo",
5989 stl: "none",
5990 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005991 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005992 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005993 }
5994
5995 cc_library {
5996 name: "libfoo2",
5997 stl: "none",
5998 system_shared_libs: [],
5999 shared_libs: ["libbaz"],
6000 apex_available: ["//apex_available:platform"],
6001 }
6002
6003 cc_library {
6004 name: "libbar",
6005 stl: "none",
6006 system_shared_libs: [],
6007 apex_available: ["myapex"],
6008 }
6009
6010 cc_library {
6011 name: "libbaz",
6012 stl: "none",
6013 system_shared_libs: [],
6014 apex_available: ["myapex"],
6015 stubs: {
6016 versions: ["1"],
6017 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006018 }`)
6019
Jiyong Park89e850a2020-04-07 16:37:39 +09006020 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6021 // because it depends on libbar which isn't available to platform
6022 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6023 if libfoo.NotAvailableForPlatform() != true {
6024 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6025 }
6026
6027 // libfoo2 however can be available to platform because it depends on libbaz which provides
6028 // stubs
6029 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6030 if libfoo2.NotAvailableForPlatform() == true {
6031 t.Errorf("%q should be available to platform", libfoo2.String())
6032 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006033}
Jiyong Parka90ca002019-10-07 15:47:24 +09006034
Paul Duffine52e66f2020-03-30 17:54:29 +01006035func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006036 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006037 apex {
6038 name: "myapex",
6039 key: "myapex.key",
6040 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006041 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006042 }
6043
6044 apex_key {
6045 name: "myapex.key",
6046 public_key: "testkey.avbpubkey",
6047 private_key: "testkey.pem",
6048 }
6049
6050 cc_library {
6051 name: "libfoo",
6052 stl: "none",
6053 system_shared_libs: [],
6054 apex_available: ["myapex"],
6055 static: {
6056 apex_available: ["//apex_available:platform"],
6057 },
6058 }`)
6059
Jiyong Park89e850a2020-04-07 16:37:39 +09006060 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6061 if libfooShared.NotAvailableForPlatform() != true {
6062 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6063 }
6064 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6065 if libfooStatic.NotAvailableForPlatform() != false {
6066 t.Errorf("%q should be available to platform", libfooStatic.String())
6067 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006068}
6069
Jiyong Park5d790c32019-11-15 18:40:32 +09006070func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006071 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006072 apex {
6073 name: "myapex",
6074 key: "myapex.key",
6075 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006076 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006077 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006078 }
6079
6080 override_apex {
6081 name: "override_myapex",
6082 base: "myapex",
6083 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006084 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006085 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006086 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006087 key: "mynewapex.key",
6088 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006089 }
6090
6091 apex_key {
6092 name: "myapex.key",
6093 public_key: "testkey.avbpubkey",
6094 private_key: "testkey.pem",
6095 }
6096
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006097 apex_key {
6098 name: "mynewapex.key",
6099 public_key: "testkey2.avbpubkey",
6100 private_key: "testkey2.pem",
6101 }
6102
6103 android_app_certificate {
6104 name: "myapex.certificate",
6105 certificate: "testkey",
6106 }
6107
Jiyong Park5d790c32019-11-15 18:40:32 +09006108 android_app {
6109 name: "app",
6110 srcs: ["foo/bar/MyClass.java"],
6111 package_name: "foo",
6112 sdk_version: "none",
6113 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006114 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006115 }
6116
6117 override_android_app {
6118 name: "override_app",
6119 base: "app",
6120 package_name: "bar",
6121 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006122 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006123
Jiyong Park317645e2019-12-05 13:20:58 +09006124 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6125 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6126 if originalVariant.GetOverriddenBy() != "" {
6127 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6128 }
6129 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6130 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6131 }
6132
Jiyong Park5d790c32019-11-15 18:40:32 +09006133 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6134 apexRule := module.Rule("apexRule")
6135 copyCmds := apexRule.Args["copy_commands"]
6136
6137 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006138 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006139
6140 apexBundle := module.Module().(*apexBundle)
6141 name := apexBundle.Name()
6142 if name != "override_myapex" {
6143 t.Errorf("name should be \"override_myapex\", but was %q", name)
6144 }
6145
Baligh Uddin004d7172020-02-19 21:29:28 -08006146 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6147 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6148 }
6149
Jiyong Park20bacab2020-03-03 11:45:41 +09006150 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006151 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006152 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6153
6154 signApkRule := module.Rule("signapk")
6155 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006156
Colin Crossaa255532020-07-03 13:18:24 -07006157 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006158 var builder strings.Builder
6159 data.Custom(&builder, name, "TARGET_", "", data)
6160 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09006161 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006162 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
6163 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006164 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006165 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006166 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006167 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
6168 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006169}
6170
Jooyung Han214bf372019-11-12 13:03:50 +09006171func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006172 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006173 apex {
6174 name: "myapex",
6175 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006176 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006177 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006178 }
6179
6180 apex_key {
6181 name: "myapex.key",
6182 public_key: "testkey.avbpubkey",
6183 private_key: "testkey.pem",
6184 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006185
6186 cc_library {
6187 name: "mylib",
6188 srcs: ["mylib.cpp"],
6189 stl: "libc++",
6190 system_shared_libs: [],
6191 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006192 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006193 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006194 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006195
6196 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6197 args := module.Rule("apexRule").Args
6198 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006199 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006200
6201 // The copies of the libraries in the apex should have one more dependency than
6202 // the ones outside the apex, namely the unwinder. Ideally we should check
6203 // the dependency names directly here but for some reason the names are blank in
6204 // this test.
6205 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006206 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006207 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6208 if len(apexImplicits) != len(nonApexImplicits)+1 {
6209 t.Errorf("%q missing unwinder dep", lib)
6210 }
6211 }
Jooyung Han214bf372019-11-12 13:03:50 +09006212}
6213
Paul Duffine05480a2021-03-08 15:07:14 +00006214var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006215 "api/current.txt": nil,
6216 "api/removed.txt": nil,
6217 "api/system-current.txt": nil,
6218 "api/system-removed.txt": nil,
6219 "api/test-current.txt": nil,
6220 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006221
Anton Hanssondff2c782020-12-21 17:10:01 +00006222 "100/public/api/foo.txt": nil,
6223 "100/public/api/foo-removed.txt": nil,
6224 "100/system/api/foo.txt": nil,
6225 "100/system/api/foo-removed.txt": nil,
6226
Paul Duffineedc5d52020-06-12 17:46:39 +01006227 // For java_sdk_library_import
6228 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006229}
6230
Jooyung Han58f26ab2019-12-18 15:34:32 +09006231func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006232 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006233 apex {
6234 name: "myapex",
6235 key: "myapex.key",
6236 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006237 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006238 }
6239
6240 apex_key {
6241 name: "myapex.key",
6242 public_key: "testkey.avbpubkey",
6243 private_key: "testkey.pem",
6244 }
6245
6246 java_sdk_library {
6247 name: "foo",
6248 srcs: ["a.java"],
6249 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006250 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006251 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006252
6253 prebuilt_apis {
6254 name: "sdk",
6255 api_dirs: ["100"],
6256 }
Paul Duffin9b879592020-05-26 13:21:35 +01006257 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006258
6259 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006260 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006261 "javalib/foo.jar",
6262 "etc/permissions/foo.xml",
6263 })
6264 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006265 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
6266 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09006267}
6268
Paul Duffin9b879592020-05-26 13:21:35 +01006269func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006270 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006271 apex {
6272 name: "myapex",
6273 key: "myapex.key",
6274 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006275 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006276 }
6277
6278 apex_key {
6279 name: "myapex.key",
6280 public_key: "testkey.avbpubkey",
6281 private_key: "testkey.pem",
6282 }
6283
6284 java_sdk_library {
6285 name: "foo",
6286 srcs: ["a.java"],
6287 api_packages: ["foo"],
6288 apex_available: ["myapex"],
6289 sdk_version: "none",
6290 system_modules: "none",
6291 }
6292
6293 java_library {
6294 name: "bar",
6295 srcs: ["a.java"],
6296 libs: ["foo"],
6297 apex_available: ["myapex"],
6298 sdk_version: "none",
6299 system_modules: "none",
6300 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006301
6302 prebuilt_apis {
6303 name: "sdk",
6304 api_dirs: ["100"],
6305 }
Paul Duffin9b879592020-05-26 13:21:35 +01006306 `, withFiles(filesForSdkLibrary))
6307
6308 // java_sdk_library installs both impl jar and permission XML
6309 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6310 "javalib/bar.jar",
6311 "javalib/foo.jar",
6312 "etc/permissions/foo.xml",
6313 })
6314
6315 // The bar library should depend on the implementation jar.
6316 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006317 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006318 t.Errorf("expected %q, found %#q", expected, actual)
6319 }
6320}
6321
6322func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006323 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006324 apex {
6325 name: "myapex",
6326 key: "myapex.key",
6327 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006328 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006329 }
6330
6331 apex_key {
6332 name: "myapex.key",
6333 public_key: "testkey.avbpubkey",
6334 private_key: "testkey.pem",
6335 }
6336
6337 java_sdk_library {
6338 name: "foo",
6339 srcs: ["a.java"],
6340 api_packages: ["foo"],
6341 apex_available: ["myapex"],
6342 sdk_version: "none",
6343 system_modules: "none",
6344 }
6345
6346 java_library {
6347 name: "bar",
6348 srcs: ["a.java"],
6349 libs: ["foo"],
6350 sdk_version: "none",
6351 system_modules: "none",
6352 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006353
6354 prebuilt_apis {
6355 name: "sdk",
6356 api_dirs: ["100"],
6357 }
Paul Duffin9b879592020-05-26 13:21:35 +01006358 `, withFiles(filesForSdkLibrary))
6359
6360 // java_sdk_library installs both impl jar and permission XML
6361 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6362 "javalib/foo.jar",
6363 "etc/permissions/foo.xml",
6364 })
6365
6366 // The bar library should depend on the stubs jar.
6367 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006368 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006369 t.Errorf("expected %q, found %#q", expected, actual)
6370 }
6371}
6372
Paul Duffineedc5d52020-06-12 17:46:39 +01006373func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006374 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006375 prebuilt_apis {
6376 name: "sdk",
6377 api_dirs: ["100"],
6378 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006379 withFiles(map[string][]byte{
6380 "apex/a.java": nil,
6381 "apex/apex_manifest.json": nil,
6382 "apex/Android.bp": []byte(`
6383 package {
6384 default_visibility: ["//visibility:private"],
6385 }
6386
6387 apex {
6388 name: "myapex",
6389 key: "myapex.key",
6390 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006391 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006392 }
6393
6394 apex_key {
6395 name: "myapex.key",
6396 public_key: "testkey.avbpubkey",
6397 private_key: "testkey.pem",
6398 }
6399
6400 java_library {
6401 name: "bar",
6402 srcs: ["a.java"],
6403 libs: ["foo"],
6404 apex_available: ["myapex"],
6405 sdk_version: "none",
6406 system_modules: "none",
6407 }
6408`),
6409 "source/a.java": nil,
6410 "source/api/current.txt": nil,
6411 "source/api/removed.txt": nil,
6412 "source/Android.bp": []byte(`
6413 package {
6414 default_visibility: ["//visibility:private"],
6415 }
6416
6417 java_sdk_library {
6418 name: "foo",
6419 visibility: ["//apex"],
6420 srcs: ["a.java"],
6421 api_packages: ["foo"],
6422 apex_available: ["myapex"],
6423 sdk_version: "none",
6424 system_modules: "none",
6425 public: {
6426 enabled: true,
6427 },
6428 }
6429`),
6430 "prebuilt/a.jar": nil,
6431 "prebuilt/Android.bp": []byte(`
6432 package {
6433 default_visibility: ["//visibility:private"],
6434 }
6435
6436 java_sdk_library_import {
6437 name: "foo",
6438 visibility: ["//apex", "//source"],
6439 apex_available: ["myapex"],
6440 prefer: true,
6441 public: {
6442 jars: ["a.jar"],
6443 },
6444 }
6445`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006446 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006447 )
6448
6449 // java_sdk_library installs both impl jar and permission XML
6450 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6451 "javalib/bar.jar",
6452 "javalib/foo.jar",
6453 "etc/permissions/foo.xml",
6454 })
6455
6456 // The bar library should depend on the implementation jar.
6457 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006458 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006459 t.Errorf("expected %q, found %#q", expected, actual)
6460 }
6461}
6462
6463func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6464 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6465 apex {
6466 name: "myapex",
6467 key: "myapex.key",
6468 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006469 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006470 }
6471
6472 apex_key {
6473 name: "myapex.key",
6474 public_key: "testkey.avbpubkey",
6475 private_key: "testkey.pem",
6476 }
6477
6478 java_sdk_library_import {
6479 name: "foo",
6480 apex_available: ["myapex"],
6481 prefer: true,
6482 public: {
6483 jars: ["a.jar"],
6484 },
6485 }
6486
6487 `, withFiles(filesForSdkLibrary))
6488}
6489
atrost6e126252020-01-27 17:01:16 +00006490func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006491 result := android.GroupFixturePreparers(
6492 prepareForApexTest,
6493 java.PrepareForTestWithPlatformCompatConfig,
6494 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006495 apex {
6496 name: "myapex",
6497 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006498 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006499 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006500 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006501 }
6502
6503 apex_key {
6504 name: "myapex.key",
6505 public_key: "testkey.avbpubkey",
6506 private_key: "testkey.pem",
6507 }
6508
6509 platform_compat_config {
6510 name: "myjar-platform-compat-config",
6511 src: ":myjar",
6512 }
6513
6514 java_library {
6515 name: "myjar",
6516 srcs: ["foo/bar/MyClass.java"],
6517 sdk_version: "none",
6518 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006519 apex_available: [ "myapex" ],
6520 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006521
6522 // Make sure that a preferred prebuilt does not affect the apex contents.
6523 prebuilt_platform_compat_config {
6524 name: "myjar-platform-compat-config",
6525 metadata: "compat-config/metadata.xml",
6526 prefer: true,
6527 }
atrost6e126252020-01-27 17:01:16 +00006528 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006529 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006530 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6531 "etc/compatconfig/myjar-platform-compat-config.xml",
6532 "javalib/myjar.jar",
6533 })
6534}
6535
Jiyong Park479321d2019-12-16 11:47:12 +09006536func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6537 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6538 apex {
6539 name: "myapex",
6540 key: "myapex.key",
6541 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006542 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006543 }
6544
6545 apex_key {
6546 name: "myapex.key",
6547 public_key: "testkey.avbpubkey",
6548 private_key: "testkey.pem",
6549 }
6550
6551 java_library {
6552 name: "myjar",
6553 srcs: ["foo/bar/MyClass.java"],
6554 sdk_version: "none",
6555 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006556 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006557 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006558 }
6559 `)
6560}
6561
Jiyong Park7afd1072019-12-30 16:56:33 +09006562func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006563 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006564 apex {
6565 name: "myapex",
6566 key: "myapex.key",
6567 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006568 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006569 }
6570
6571 apex_key {
6572 name: "myapex.key",
6573 public_key: "testkey.avbpubkey",
6574 private_key: "testkey.pem",
6575 }
6576
6577 cc_library {
6578 name: "mylib",
6579 srcs: ["mylib.cpp"],
6580 system_shared_libs: [],
6581 stl: "none",
6582 required: ["a", "b"],
6583 host_required: ["c", "d"],
6584 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006585 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006586 }
6587 `)
6588
6589 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006590 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006591 name := apexBundle.BaseModuleName()
6592 prefix := "TARGET_"
6593 var builder strings.Builder
6594 data.Custom(&builder, name, prefix, "", data)
6595 androidMk := builder.String()
6596 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6597 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6598 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6599}
6600
Jiyong Park7cd10e32020-01-14 09:22:18 +09006601func TestSymlinksFromApexToSystem(t *testing.T) {
6602 bp := `
6603 apex {
6604 name: "myapex",
6605 key: "myapex.key",
6606 native_shared_libs: ["mylib"],
6607 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006608 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006609 }
6610
Jiyong Park9d677202020-02-19 16:29:35 +09006611 apex {
6612 name: "myapex.updatable",
6613 key: "myapex.key",
6614 native_shared_libs: ["mylib"],
6615 java_libs: ["myjar"],
6616 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006617 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006618 }
6619
Jiyong Park7cd10e32020-01-14 09:22:18 +09006620 apex_key {
6621 name: "myapex.key",
6622 public_key: "testkey.avbpubkey",
6623 private_key: "testkey.pem",
6624 }
6625
6626 cc_library {
6627 name: "mylib",
6628 srcs: ["mylib.cpp"],
6629 shared_libs: ["myotherlib"],
6630 system_shared_libs: [],
6631 stl: "none",
6632 apex_available: [
6633 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006634 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006635 "//apex_available:platform",
6636 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006637 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006638 }
6639
6640 cc_library {
6641 name: "myotherlib",
6642 srcs: ["mylib.cpp"],
6643 system_shared_libs: [],
6644 stl: "none",
6645 apex_available: [
6646 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006647 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006648 "//apex_available:platform",
6649 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006650 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006651 }
6652
6653 java_library {
6654 name: "myjar",
6655 srcs: ["foo/bar/MyClass.java"],
6656 sdk_version: "none",
6657 system_modules: "none",
6658 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006659 apex_available: [
6660 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006661 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006662 "//apex_available:platform",
6663 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006664 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006665 }
6666
6667 java_library {
6668 name: "myotherjar",
6669 srcs: ["foo/bar/MyClass.java"],
6670 sdk_version: "none",
6671 system_modules: "none",
6672 apex_available: [
6673 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006674 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006675 "//apex_available:platform",
6676 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006677 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006678 }
6679 `
6680
6681 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6682 for _, f := range files {
6683 if f.path == file {
6684 if f.isLink {
6685 t.Errorf("%q is not a real file", file)
6686 }
6687 return
6688 }
6689 }
6690 t.Errorf("%q is not found", file)
6691 }
6692
6693 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6694 for _, f := range files {
6695 if f.path == file {
6696 if !f.isLink {
6697 t.Errorf("%q is not a symlink", file)
6698 }
6699 return
6700 }
6701 }
6702 t.Errorf("%q is not found", file)
6703 }
6704
Jiyong Park9d677202020-02-19 16:29:35 +09006705 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6706 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006707 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006708 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006709 ensureRealfileExists(t, files, "javalib/myjar.jar")
6710 ensureRealfileExists(t, files, "lib64/mylib.so")
6711 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6712
Jiyong Park9d677202020-02-19 16:29:35 +09006713 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6714 ensureRealfileExists(t, files, "javalib/myjar.jar")
6715 ensureRealfileExists(t, files, "lib64/mylib.so")
6716 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6717
6718 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006719 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006720 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006721 ensureRealfileExists(t, files, "javalib/myjar.jar")
6722 ensureRealfileExists(t, files, "lib64/mylib.so")
6723 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006724
6725 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6726 ensureRealfileExists(t, files, "javalib/myjar.jar")
6727 ensureRealfileExists(t, files, "lib64/mylib.so")
6728 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006729}
6730
Yo Chiange8128052020-07-23 20:09:18 +08006731func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006732 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006733 apex {
6734 name: "myapex",
6735 key: "myapex.key",
6736 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006737 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006738 }
6739
6740 apex_key {
6741 name: "myapex.key",
6742 public_key: "testkey.avbpubkey",
6743 private_key: "testkey.pem",
6744 }
6745
6746 cc_library_shared {
6747 name: "mylib",
6748 srcs: ["mylib.cpp"],
6749 shared_libs: ["myotherlib"],
6750 system_shared_libs: [],
6751 stl: "none",
6752 apex_available: [
6753 "myapex",
6754 "//apex_available:platform",
6755 ],
6756 }
6757
6758 cc_prebuilt_library_shared {
6759 name: "myotherlib",
6760 srcs: ["prebuilt.so"],
6761 system_shared_libs: [],
6762 stl: "none",
6763 apex_available: [
6764 "myapex",
6765 "//apex_available:platform",
6766 ],
6767 }
6768 `)
6769
6770 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006771 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006772 var builder strings.Builder
6773 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6774 androidMk := builder.String()
6775 // `myotherlib` is added to `myapex` as symlink
6776 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6777 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6778 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6779 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006780 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 +08006781}
6782
Jooyung Han643adc42020-02-27 13:50:06 +09006783func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006784 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006785 apex {
6786 name: "myapex",
6787 key: "myapex.key",
6788 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006789 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006790 }
6791
6792 apex_key {
6793 name: "myapex.key",
6794 public_key: "testkey.avbpubkey",
6795 private_key: "testkey.pem",
6796 }
6797
6798 cc_library {
6799 name: "mylib",
6800 srcs: ["mylib.cpp"],
6801 shared_libs: ["mylib2"],
6802 system_shared_libs: [],
6803 stl: "none",
6804 apex_available: [ "myapex" ],
6805 }
6806
6807 cc_library {
6808 name: "mylib2",
6809 srcs: ["mylib.cpp"],
6810 system_shared_libs: [],
6811 stl: "none",
6812 apex_available: [ "myapex" ],
6813 }
6814 `)
6815
6816 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6817 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6818 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6819 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6820 "lib64/mylib.so",
6821 "lib64/mylib2.so",
6822 })
6823}
6824
Jooyung Han49f67012020-04-17 13:43:10 +09006825func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006826 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006827 apex {
6828 name: "myapex",
6829 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006830 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006831 }
6832 apex_key {
6833 name: "myapex.key",
6834 public_key: "testkey.avbpubkey",
6835 private_key: "testkey.pem",
6836 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006837 `,
6838 android.FixtureModifyConfig(func(config android.Config) {
6839 delete(config.Targets, android.Android)
6840 config.AndroidCommonTarget = android.Target{}
6841 }),
6842 )
Jooyung Han49f67012020-04-17 13:43:10 +09006843
6844 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6845 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6846 }
6847}
6848
Jiyong Parkbd159612020-02-28 15:22:21 +09006849func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006850 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006851 apex {
6852 name: "myapex",
6853 key: "myapex.key",
6854 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006855 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006856 }
6857
6858 apex_key {
6859 name: "myapex.key",
6860 public_key: "testkey.avbpubkey",
6861 private_key: "testkey.pem",
6862 }
6863
6864 android_app {
6865 name: "AppFoo",
6866 srcs: ["foo/bar/MyClass.java"],
6867 sdk_version: "none",
6868 system_modules: "none",
6869 apex_available: [ "myapex" ],
6870 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006871 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006872
Colin Crosscf371cc2020-11-13 11:48:42 -08006873 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006874 content := bundleConfigRule.Args["content"]
6875
6876 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006877 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 +09006878}
6879
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006880func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006881 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006882 apex {
6883 name: "myapex",
6884 key: "myapex.key",
6885 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006886 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006887 }
6888
6889 apex_key {
6890 name: "myapex.key",
6891 public_key: "testkey.avbpubkey",
6892 private_key: "testkey.pem",
6893 }
6894
6895 android_app_set {
6896 name: "AppSet",
6897 set: "AppSet.apks",
6898 }`)
6899 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006900 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006901 content := bundleConfigRule.Args["content"]
6902 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6903 s := mod.Rule("apexRule").Args["copy_commands"]
6904 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6905 if len(copyCmds) != 3 {
6906 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6907 }
6908 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6909 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6910 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6911}
6912
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006913func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01006914 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006915 apex_set {
6916 name: "myapex",
6917 filename: "foo_v2.apex",
6918 sanitized: {
6919 none: { set: "myapex.apks", },
6920 hwaddress: { set: "myapex.hwasan.apks", },
6921 },
Paul Duffin24704672021-04-06 16:09:30 +01006922 }
6923 `
6924 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006925
Paul Duffin24704672021-04-06 16:09:30 +01006926 // Check that the extractor produces the correct output file from the correct input file.
6927 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006928
Paul Duffin24704672021-04-06 16:09:30 +01006929 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
6930 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006931
Paul Duffin24704672021-04-06 16:09:30 +01006932 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
6933
6934 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01006935 m = ctx.ModuleForTests("myapex", "android_common_myapex")
6936 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01006937
6938 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006939}
6940
Paul Duffin89f570a2021-06-16 01:42:33 +01006941func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006942 t.Helper()
6943
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006944 bp := `
6945 java_library {
6946 name: "some-updatable-apex-lib",
6947 srcs: ["a.java"],
6948 sdk_version: "current",
6949 apex_available: [
6950 "some-updatable-apex",
6951 ],
satayevabcd5972021-08-06 17:49:46 +01006952 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006953 }
6954
6955 java_library {
6956 name: "some-non-updatable-apex-lib",
6957 srcs: ["a.java"],
6958 apex_available: [
6959 "some-non-updatable-apex",
6960 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01006961 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01006962 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01006963 }
6964
6965 bootclasspath_fragment {
6966 name: "some-non-updatable-fragment",
6967 contents: ["some-non-updatable-apex-lib"],
6968 apex_available: [
6969 "some-non-updatable-apex",
6970 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006971 }
6972
6973 java_library {
6974 name: "some-platform-lib",
6975 srcs: ["a.java"],
6976 sdk_version: "current",
6977 installable: true,
6978 }
6979
6980 java_library {
6981 name: "some-art-lib",
6982 srcs: ["a.java"],
6983 sdk_version: "current",
6984 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006985 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006986 ],
6987 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01006988 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006989 }
6990
6991 apex {
6992 name: "some-updatable-apex",
6993 key: "some-updatable-apex.key",
6994 java_libs: ["some-updatable-apex-lib"],
6995 updatable: true,
6996 min_sdk_version: "current",
6997 }
6998
6999 apex {
7000 name: "some-non-updatable-apex",
7001 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007002 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007003 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007004 }
7005
7006 apex_key {
7007 name: "some-updatable-apex.key",
7008 }
7009
7010 apex_key {
7011 name: "some-non-updatable-apex.key",
7012 }
7013
7014 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007015 name: "com.android.art.debug",
7016 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007017 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007018 updatable: true,
7019 min_sdk_version: "current",
7020 }
7021
Paul Duffinf23bc472021-04-27 12:42:20 +01007022 bootclasspath_fragment {
7023 name: "art-bootclasspath-fragment",
7024 image_name: "art",
7025 contents: ["some-art-lib"],
7026 apex_available: [
7027 "com.android.art.debug",
7028 ],
7029 }
7030
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007031 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007032 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007033 }
7034
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007035 filegroup {
7036 name: "some-updatable-apex-file_contexts",
7037 srcs: [
7038 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7039 ],
7040 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007041
7042 filegroup {
7043 name: "some-non-updatable-apex-file_contexts",
7044 srcs: [
7045 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7046 ],
7047 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007048 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007049
Paul Duffin89f570a2021-06-16 01:42:33 +01007050 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007051}
7052
Paul Duffin89f570a2021-06-16 01:42:33 +01007053func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007054 t.Helper()
7055
Paul Duffin55607122021-03-30 23:32:51 +01007056 fs := android.MockFS{
7057 "a.java": nil,
7058 "a.jar": nil,
7059 "apex_manifest.json": nil,
7060 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007061 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007062 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7063 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7064 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007065 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007066 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007067
Paul Duffin55607122021-03-30 23:32:51 +01007068 errorHandler := android.FixtureExpectsNoErrors
7069 if errmsg != "" {
7070 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007071 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007072
Paul Duffin55607122021-03-30 23:32:51 +01007073 result := android.GroupFixturePreparers(
7074 cc.PrepareForTestWithCcDefaultModules,
7075 java.PrepareForTestWithHiddenApiBuildComponents,
7076 java.PrepareForTestWithJavaDefaultModules,
7077 java.PrepareForTestWithJavaSdkLibraryFiles,
7078 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007079 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007080 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007081 android.FixtureModifyMockFS(func(fs android.MockFS) {
7082 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7083 insert := ""
7084 for _, fragment := range fragments {
7085 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7086 }
7087 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7088 platform_bootclasspath {
7089 name: "platform-bootclasspath",
7090 fragments: [
7091 %s
7092 ],
7093 }
7094 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007095 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007096 }),
Paul Duffin55607122021-03-30 23:32:51 +01007097 ).
7098 ExtendWithErrorHandler(errorHandler).
7099 RunTestWithBp(t, bp)
7100
7101 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007102}
7103
Jooyung Han548640b2020-04-27 12:10:30 +09007104func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7105 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7106 apex {
7107 name: "myapex",
7108 key: "myapex.key",
7109 updatable: true,
7110 }
7111
7112 apex_key {
7113 name: "myapex.key",
7114 public_key: "testkey.avbpubkey",
7115 private_key: "testkey.pem",
7116 }
7117 `)
7118}
7119
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007120func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7121 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7122 apex {
7123 name: "myapex",
7124 key: "myapex.key",
7125 }
7126
7127 apex_key {
7128 name: "myapex.key",
7129 public_key: "testkey.avbpubkey",
7130 private_key: "testkey.pem",
7131 }
7132 `)
7133}
7134
satayevb98371c2021-06-15 16:49:50 +01007135func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7136 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7137 apex {
7138 name: "myapex",
7139 key: "myapex.key",
7140 systemserverclasspath_fragments: [
7141 "mysystemserverclasspathfragment",
7142 ],
7143 min_sdk_version: "29",
7144 updatable: true,
7145 }
7146
7147 apex_key {
7148 name: "myapex.key",
7149 public_key: "testkey.avbpubkey",
7150 private_key: "testkey.pem",
7151 }
7152
7153 java_library {
7154 name: "foo",
7155 srcs: ["b.java"],
7156 min_sdk_version: "29",
7157 installable: true,
7158 apex_available: [
7159 "myapex",
7160 ],
7161 }
7162
7163 systemserverclasspath_fragment {
7164 name: "mysystemserverclasspathfragment",
7165 generate_classpaths_proto: false,
7166 contents: [
7167 "foo",
7168 ],
7169 apex_available: [
7170 "myapex",
7171 ],
7172 }
satayevabcd5972021-08-06 17:49:46 +01007173 `,
7174 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7175 )
satayevb98371c2021-06-15 16:49:50 +01007176}
7177
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007178func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007179 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7180 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7181 // modules to be included in the BootJars.
7182 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7183 return android.GroupFixturePreparers(
7184 dexpreopt.FixtureSetBootJars(bootJars...),
7185 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7186 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7187 }),
7188 )
7189 }
7190
7191 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7192 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7193 // specified in the ArtApexJars configuration.
7194 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7195 return android.GroupFixturePreparers(
7196 dexpreopt.FixtureSetArtBootJars(bootJars...),
7197 dexpreopt.FixtureSetBootJars(bootJars...),
7198 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7199 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7200 }),
7201 )
7202 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007203
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007204 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007205 preparer := android.GroupFixturePreparers(
7206 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7207 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7208 )
7209 fragments := []java.ApexVariantReference{
7210 {
7211 Apex: proptools.StringPtr("com.android.art.debug"),
7212 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7213 },
7214 {
7215 Apex: proptools.StringPtr("some-non-updatable-apex"),
7216 Module: proptools.StringPtr("some-non-updatable-fragment"),
7217 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007218 }
satayevabcd5972021-08-06 17:49:46 +01007219 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007220 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007221
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007222 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007223 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7224 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01007225 preparer := android.GroupFixturePreparers(
7226 prepareSetBootJars("com.android.art.debug:some-art-lib"),
7227 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7228 )
Paul Duffin60264a02021-04-12 20:02:36 +01007229 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007230 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007231
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007232 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 +01007233 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 +01007234 // Update the dexpreopt ArtApexJars directly.
7235 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7236 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007237 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007238
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007239 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 +01007240 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 +01007241 // Update the dexpreopt ArtApexJars directly.
7242 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
7243 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007244 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007245
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007246 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 +01007247 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
satayevabcd5972021-08-06 17:49:46 +01007248 preparer := android.GroupFixturePreparers(
7249 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
7250 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7251 )
Paul Duffin60264a02021-04-12 20:02:36 +01007252 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007253 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007254
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007255 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007256 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007257 fragment := java.ApexVariantReference{
7258 Apex: proptools.StringPtr("some-non-updatable-apex"),
7259 Module: proptools.StringPtr("some-non-updatable-fragment"),
7260 }
7261 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007262 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007263
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007264 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007265 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007266 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7267 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007268 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007269
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007270 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007271 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007272 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7273 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007274 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007275
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007276 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007277 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01007278 // Update the dexpreopt ArtApexJars directly.
7279 preparer := prepareSetArtJars("platform:some-platform-lib")
7280 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007281 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007282
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007283 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007284 preparer := android.GroupFixturePreparers(
7285 java.FixtureConfigureBootJars("platform:some-platform-lib"),
7286 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7287 )
7288 fragments := []java.ApexVariantReference{
7289 {
7290 Apex: proptools.StringPtr("some-non-updatable-apex"),
7291 Module: proptools.StringPtr("some-non-updatable-fragment"),
7292 },
7293 }
7294 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007295 })
Paul Duffin064b70c2020-11-02 17:32:38 +00007296}
7297
7298func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007299 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00007300 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01007301 fragment := java.ApexVariantReference{
7302 Apex: proptools.StringPtr("myapex"),
7303 Module: proptools.StringPtr("my-bootclasspath-fragment"),
7304 }
7305
Paul Duffin064b70c2020-11-02 17:32:38 +00007306 testDexpreoptWithApexes(t, `
7307 prebuilt_apex {
7308 name: "myapex" ,
7309 arch: {
7310 arm64: {
7311 src: "myapex-arm64.apex",
7312 },
7313 arm: {
7314 src: "myapex-arm.apex",
7315 },
7316 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007317 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7318 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007319
Paul Duffin89f570a2021-06-16 01:42:33 +01007320 prebuilt_bootclasspath_fragment {
7321 name: "my-bootclasspath-fragment",
7322 contents: ["libfoo"],
7323 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01007324 hidden_api: {
7325 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
7326 metadata: "my-bootclasspath-fragment/metadata.csv",
7327 index: "my-bootclasspath-fragment/index.csv",
7328 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
7329 all_flags: "my-bootclasspath-fragment/all-flags.csv",
7330 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007331 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007332
Paul Duffin89f570a2021-06-16 01:42:33 +01007333 java_import {
7334 name: "libfoo",
7335 jars: ["libfoo.jar"],
7336 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01007337 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007338 }
7339 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00007340 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007341}
7342
satayevd604b212021-07-21 14:23:52 +01007343func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01007344 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01007345 bp += `
7346 apex_key {
7347 name: "myapex.key",
7348 public_key: "testkey.avbpubkey",
7349 private_key: "testkey.pem",
7350 }`
Paul Duffin45338f02021-03-30 23:07:52 +01007351 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01007352 "lib1/src/A.java": nil,
7353 "lib2/src/B.java": nil,
7354 "system/sepolicy/apex/myapex-file_contexts": nil,
7355 }
7356
Paul Duffin45338f02021-03-30 23:07:52 +01007357 errorHandler := android.FixtureExpectsNoErrors
7358 if errmsg != "" {
7359 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07007360 }
Colin Crossae8600b2020-10-29 17:09:13 -07007361
Paul Duffin45338f02021-03-30 23:07:52 +01007362 android.GroupFixturePreparers(
7363 android.PrepareForTestWithAndroidBuildComponents,
7364 java.PrepareForTestWithJavaBuildComponents,
7365 PrepareForTestWithApexBuildComponents,
7366 android.PrepareForTestWithNeverallowRules(rules),
7367 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01007368 apexBootJars := make([]string, 0, len(bootJars))
7369 for _, apexBootJar := range bootJars {
7370 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01007371 }
satayevd604b212021-07-21 14:23:52 +01007372 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01007373 }),
7374 fs.AddToFixture(),
7375 ).
7376 ExtendWithErrorHandler(errorHandler).
7377 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01007378}
7379
7380func TestApexPermittedPackagesRules(t *testing.T) {
7381 testcases := []struct {
7382 name string
7383 expectedError string
7384 bp string
7385 bootJars []string
7386 modulesPackages map[string][]string
7387 }{
7388
7389 {
7390 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
7391 expectedError: "",
7392 bp: `
7393 java_library {
7394 name: "bcp_lib1",
7395 srcs: ["lib1/src/*.java"],
7396 permitted_packages: ["foo.bar"],
7397 apex_available: ["myapex"],
7398 sdk_version: "none",
7399 system_modules: "none",
7400 }
7401 java_library {
7402 name: "nonbcp_lib2",
7403 srcs: ["lib2/src/*.java"],
7404 apex_available: ["myapex"],
7405 permitted_packages: ["a.b"],
7406 sdk_version: "none",
7407 system_modules: "none",
7408 }
7409 apex {
7410 name: "myapex",
7411 key: "myapex.key",
7412 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007413 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007414 }`,
7415 bootJars: []string{"bcp_lib1"},
7416 modulesPackages: map[string][]string{
7417 "myapex": []string{
7418 "foo.bar",
7419 },
7420 },
7421 },
7422 {
7423 name: "Bootclasspath apex jar not satisfying allowed module packages.",
7424 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.`,
7425 bp: `
7426 java_library {
7427 name: "bcp_lib1",
7428 srcs: ["lib1/src/*.java"],
7429 apex_available: ["myapex"],
7430 permitted_packages: ["foo.bar"],
7431 sdk_version: "none",
7432 system_modules: "none",
7433 }
7434 java_library {
7435 name: "bcp_lib2",
7436 srcs: ["lib2/src/*.java"],
7437 apex_available: ["myapex"],
7438 permitted_packages: ["foo.bar", "bar.baz"],
7439 sdk_version: "none",
7440 system_modules: "none",
7441 }
7442 apex {
7443 name: "myapex",
7444 key: "myapex.key",
7445 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007446 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007447 }
7448 `,
7449 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7450 modulesPackages: map[string][]string{
7451 "myapex": []string{
7452 "foo.bar",
7453 },
7454 },
7455 },
7456 }
7457 for _, tc := range testcases {
7458 t.Run(tc.name, func(t *testing.T) {
7459 rules := createApexPermittedPackagesRules(tc.modulesPackages)
7460 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
7461 })
7462 }
7463}
7464
Jiyong Park62304bb2020-04-13 16:19:48 +09007465func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007466 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09007467 apex {
7468 name: "myapex",
7469 key: "myapex.key",
7470 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007471 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09007472 }
7473
7474 apex_key {
7475 name: "myapex.key",
7476 public_key: "testkey.avbpubkey",
7477 private_key: "testkey.pem",
7478 }
7479
7480 cc_library {
7481 name: "mylib",
7482 srcs: ["mylib.cpp"],
7483 system_shared_libs: [],
7484 stl: "none",
7485 stubs: {
7486 versions: ["1"],
7487 },
7488 apex_available: ["myapex"],
7489 }
7490
7491 cc_library {
7492 name: "myprivlib",
7493 srcs: ["mylib.cpp"],
7494 system_shared_libs: [],
7495 stl: "none",
7496 apex_available: ["myapex"],
7497 }
7498
7499
7500 cc_test {
7501 name: "mytest",
7502 gtest: false,
7503 srcs: ["mylib.cpp"],
7504 system_shared_libs: [],
7505 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09007506 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09007507 test_for: ["myapex"]
7508 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007509
7510 cc_library {
7511 name: "mytestlib",
7512 srcs: ["mylib.cpp"],
7513 system_shared_libs: [],
7514 shared_libs: ["mylib", "myprivlib"],
7515 stl: "none",
7516 test_for: ["myapex"],
7517 }
7518
7519 cc_benchmark {
7520 name: "mybench",
7521 srcs: ["mylib.cpp"],
7522 system_shared_libs: [],
7523 shared_libs: ["mylib", "myprivlib"],
7524 stl: "none",
7525 test_for: ["myapex"],
7526 }
Jiyong Park62304bb2020-04-13 16:19:48 +09007527 `)
7528
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007529 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007530 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007531 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7532 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7533 }
7534
7535 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09007536 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007537 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7538 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7539 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7540}
Jiyong Park46a512f2020-12-04 18:02:13 +09007541
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007542func TestIndirectTestFor(t *testing.T) {
7543 ctx := testApex(t, `
7544 apex {
7545 name: "myapex",
7546 key: "myapex.key",
7547 native_shared_libs: ["mylib", "myprivlib"],
7548 updatable: false,
7549 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007550
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007551 apex_key {
7552 name: "myapex.key",
7553 public_key: "testkey.avbpubkey",
7554 private_key: "testkey.pem",
7555 }
7556
7557 cc_library {
7558 name: "mylib",
7559 srcs: ["mylib.cpp"],
7560 system_shared_libs: [],
7561 stl: "none",
7562 stubs: {
7563 versions: ["1"],
7564 },
7565 apex_available: ["myapex"],
7566 }
7567
7568 cc_library {
7569 name: "myprivlib",
7570 srcs: ["mylib.cpp"],
7571 system_shared_libs: [],
7572 stl: "none",
7573 shared_libs: ["mylib"],
7574 apex_available: ["myapex"],
7575 }
7576
7577 cc_library {
7578 name: "mytestlib",
7579 srcs: ["mylib.cpp"],
7580 system_shared_libs: [],
7581 shared_libs: ["myprivlib"],
7582 stl: "none",
7583 test_for: ["myapex"],
7584 }
7585 `)
7586
7587 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007588 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007589 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7590 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7591 }
7592
7593 // The platform variant of mytestlib links to the platform variant of the
7594 // internal myprivlib.
7595 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
7596
7597 // The platform variant of myprivlib links to the platform variant of mylib
7598 // and bypasses its stubs.
7599 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 +09007600}
7601
Martin Stjernholmec009002021-03-27 15:18:31 +00007602func TestTestForForLibInOtherApex(t *testing.T) {
7603 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
7604 _ = testApex(t, `
7605 apex {
7606 name: "com.android.art",
7607 key: "myapex.key",
7608 native_shared_libs: ["mylib"],
7609 updatable: false,
7610 }
7611
7612 apex {
7613 name: "com.android.art.debug",
7614 key: "myapex.key",
7615 native_shared_libs: ["mylib", "mytestlib"],
7616 updatable: false,
7617 }
7618
7619 apex_key {
7620 name: "myapex.key",
7621 public_key: "testkey.avbpubkey",
7622 private_key: "testkey.pem",
7623 }
7624
7625 cc_library {
7626 name: "mylib",
7627 srcs: ["mylib.cpp"],
7628 system_shared_libs: [],
7629 stl: "none",
7630 stubs: {
7631 versions: ["1"],
7632 },
7633 apex_available: ["com.android.art", "com.android.art.debug"],
7634 }
7635
7636 cc_library {
7637 name: "mytestlib",
7638 srcs: ["mylib.cpp"],
7639 system_shared_libs: [],
7640 shared_libs: ["mylib"],
7641 stl: "none",
7642 apex_available: ["com.android.art.debug"],
7643 test_for: ["com.android.art"],
7644 }
7645 `,
7646 android.MockFS{
7647 "system/sepolicy/apex/com.android.art-file_contexts": nil,
7648 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
7649 }.AddToFixture())
7650}
7651
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007652// TODO(jungjw): Move this to proptools
7653func intPtr(i int) *int {
7654 return &i
7655}
7656
7657func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007658 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007659 apex_set {
7660 name: "myapex",
7661 set: "myapex.apks",
7662 filename: "foo_v2.apex",
7663 overrides: ["foo"],
7664 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007665 `,
7666 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7667 variables.Platform_sdk_version = intPtr(30)
7668 }),
7669 android.FixtureModifyConfig(func(config android.Config) {
7670 config.Targets[android.Android] = []android.Target{
7671 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7672 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7673 }
7674 }),
7675 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007676
Paul Duffin24704672021-04-06 16:09:30 +01007677 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007678
7679 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01007680 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007681 actual := extractedApex.Args["abis"]
7682 expected := "ARMEABI_V7A,ARM64_V8A"
7683 if actual != expected {
7684 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7685 }
7686 actual = extractedApex.Args["sdk-version"]
7687 expected = "30"
7688 if actual != expected {
7689 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7690 }
7691
Paul Duffin6717d882021-06-15 19:09:41 +01007692 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007693 a := m.Module().(*ApexSet)
7694 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007695 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007696 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7697 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7698 }
7699}
7700
Jiyong Park7d95a512020-05-10 15:16:24 +09007701func TestNoStaticLinkingToStubsLib(t *testing.T) {
7702 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7703 apex {
7704 name: "myapex",
7705 key: "myapex.key",
7706 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007707 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007708 }
7709
7710 apex_key {
7711 name: "myapex.key",
7712 public_key: "testkey.avbpubkey",
7713 private_key: "testkey.pem",
7714 }
7715
7716 cc_library {
7717 name: "mylib",
7718 srcs: ["mylib.cpp"],
7719 static_libs: ["otherlib"],
7720 system_shared_libs: [],
7721 stl: "none",
7722 apex_available: [ "myapex" ],
7723 }
7724
7725 cc_library {
7726 name: "otherlib",
7727 srcs: ["mylib.cpp"],
7728 system_shared_libs: [],
7729 stl: "none",
7730 stubs: {
7731 versions: ["1", "2", "3"],
7732 },
7733 apex_available: [ "myapex" ],
7734 }
7735 `)
7736}
7737
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007738func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007739 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007740 apex {
7741 name: "myapex",
7742 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007743 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007744 }
7745
7746 apex_key {
7747 name: "myapex.key",
7748 public_key: "testkey.avbpubkey",
7749 private_key: "testkey.pem",
7750 }
7751
7752 prebuilt_apex {
7753 name: "myapex",
7754 prefer: true,
7755 arch: {
7756 arm64: {
7757 src: "myapex-arm64.apex",
7758 },
7759 arm: {
7760 src: "myapex-arm.apex",
7761 },
7762 },
7763 }
7764
7765 apex_set {
7766 name: "myapex_set",
7767 set: "myapex.apks",
7768 filename: "myapex_set.apex",
7769 overrides: ["myapex"],
7770 }
7771 `)
7772
7773 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7774 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7775 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 +09007776 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 +09007777}
7778
Jooyung Han938b5932020-06-20 12:47:47 +09007779func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007780 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007781 apex {
7782 name: "myapex",
7783 key: "myapex.key",
7784 apps: ["app"],
7785 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007786 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007787 }
7788
7789 apex_key {
7790 name: "myapex.key",
7791 public_key: "testkey.avbpubkey",
7792 private_key: "testkey.pem",
7793 }
7794
7795 android_app {
7796 name: "app",
7797 srcs: ["foo/bar/MyClass.java"],
7798 package_name: "foo",
7799 sdk_version: "none",
7800 system_modules: "none",
7801 apex_available: [ "myapex" ],
7802 }
7803 `, withFiles(map[string][]byte{
7804 "sub/Android.bp": []byte(`
7805 override_apex {
7806 name: "override_myapex",
7807 base: "myapex",
7808 apps: ["override_app"],
7809 allowed_files: ":allowed",
7810 }
7811 // Overridable "path" property should be referenced indirectly
7812 filegroup {
7813 name: "allowed",
7814 srcs: ["allowed.txt"],
7815 }
7816 override_android_app {
7817 name: "override_app",
7818 base: "app",
7819 package_name: "bar",
7820 }
7821 `),
7822 }))
7823
7824 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7825 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7826 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7827 }
7828
7829 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7830 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7831 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7832 }
7833}
7834
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007835func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007836 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007837 apex {
7838 name: "myapex",
7839 key: "myapex.key",
7840 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007841 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007842 }
7843
7844 apex_key {
7845 name: "myapex.key",
7846 public_key: "testkey.avbpubkey",
7847 private_key: "testkey.pem",
7848 }
7849
7850 cc_library {
7851 name: "mylib",
7852 srcs: ["mylib.cpp"],
7853 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007854 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007855 },
7856 apex_available: ["myapex"],
7857 }
7858
7859 cc_prebuilt_library_shared {
7860 name: "mylib",
7861 prefer: false,
7862 srcs: ["prebuilt.so"],
7863 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007864 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007865 },
7866 apex_available: ["myapex"],
7867 }
7868 `)
7869}
7870
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007871func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007872 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007873 apex {
7874 name: "myapex",
7875 key: "myapex.key",
7876 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007877 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007878 }
7879 apex_key {
7880 name: "myapex.key",
7881 public_key: "testkey.avbpubkey",
7882 private_key: "testkey.pem",
7883 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007884 `,
7885 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7886 variables.CompressedApex = proptools.BoolPtr(true)
7887 }),
7888 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007889
7890 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7891 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7892
7893 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7894 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7895
7896 // Make sure output of bundle is .capex
7897 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7898 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7899
7900 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007901 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007902 var builder strings.Builder
7903 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7904 androidMk := builder.String()
7905 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7906}
7907
Martin Stjernholm2856c662020-12-02 15:03:42 +00007908func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007909 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007910 apex {
7911 name: "myapex",
7912 key: "myapex.key",
7913 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007914 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007915 }
7916
7917 apex_key {
7918 name: "myapex.key",
7919 public_key: "testkey.avbpubkey",
7920 private_key: "testkey.pem",
7921 }
7922
7923 cc_library {
7924 name: "mylib",
7925 srcs: ["mylib.cpp"],
7926 apex_available: ["myapex"],
7927 shared_libs: ["otherlib"],
7928 system_shared_libs: [],
7929 }
7930
7931 cc_library {
7932 name: "otherlib",
7933 srcs: ["mylib.cpp"],
7934 stubs: {
7935 versions: ["current"],
7936 },
7937 }
7938
7939 cc_prebuilt_library_shared {
7940 name: "otherlib",
7941 prefer: true,
7942 srcs: ["prebuilt.so"],
7943 stubs: {
7944 versions: ["current"],
7945 },
7946 }
7947 `)
7948
7949 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007950 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007951 var builder strings.Builder
7952 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7953 androidMk := builder.String()
7954
7955 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7956 // a thing there.
7957 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7958}
7959
Jiyong Parke3867542020-12-03 17:28:25 +09007960func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007961 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007962 apex {
7963 name: "myapex",
7964 key: "myapex.key",
7965 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007966 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007967 }
7968
7969 apex_key {
7970 name: "myapex.key",
7971 public_key: "testkey.avbpubkey",
7972 private_key: "testkey.pem",
7973 }
7974
7975 cc_library {
7976 name: "mylib",
7977 srcs: ["mylib.cpp"],
7978 system_shared_libs: [],
7979 stl: "none",
7980 apex_available: ["myapex"],
7981 shared_libs: ["mylib2"],
7982 target: {
7983 apex: {
7984 exclude_shared_libs: ["mylib2"],
7985 },
7986 },
7987 }
7988
7989 cc_library {
7990 name: "mylib2",
7991 srcs: ["mylib.cpp"],
7992 system_shared_libs: [],
7993 stl: "none",
7994 }
7995 `)
7996
7997 // Check if mylib is linked to mylib2 for the non-apex target
7998 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7999 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8000
8001 // Make sure that the link doesn't occur for the apex target
8002 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8003 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8004
8005 // It shouldn't appear in the copy cmd as well.
8006 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8007 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8008}
8009
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008010func TestPrebuiltStubLibDep(t *testing.T) {
8011 bpBase := `
8012 apex {
8013 name: "myapex",
8014 key: "myapex.key",
8015 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008016 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008017 }
8018 apex_key {
8019 name: "myapex.key",
8020 public_key: "testkey.avbpubkey",
8021 private_key: "testkey.pem",
8022 }
8023 cc_library {
8024 name: "mylib",
8025 srcs: ["mylib.cpp"],
8026 apex_available: ["myapex"],
8027 shared_libs: ["stublib"],
8028 system_shared_libs: [],
8029 }
8030 apex {
8031 name: "otherapex",
8032 enabled: %s,
8033 key: "myapex.key",
8034 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008035 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008036 }
8037 `
8038
8039 stublibSourceBp := `
8040 cc_library {
8041 name: "stublib",
8042 srcs: ["mylib.cpp"],
8043 apex_available: ["otherapex"],
8044 system_shared_libs: [],
8045 stl: "none",
8046 stubs: {
8047 versions: ["1"],
8048 },
8049 }
8050 `
8051
8052 stublibPrebuiltBp := `
8053 cc_prebuilt_library_shared {
8054 name: "stublib",
8055 srcs: ["prebuilt.so"],
8056 apex_available: ["otherapex"],
8057 stubs: {
8058 versions: ["1"],
8059 },
8060 %s
8061 }
8062 `
8063
8064 tests := []struct {
8065 name string
8066 stublibBp string
8067 usePrebuilt bool
8068 modNames []string // Modules to collect AndroidMkEntries for
8069 otherApexEnabled []string
8070 }{
8071 {
8072 name: "only_source",
8073 stublibBp: stublibSourceBp,
8074 usePrebuilt: false,
8075 modNames: []string{"stublib"},
8076 otherApexEnabled: []string{"true", "false"},
8077 },
8078 {
8079 name: "source_preferred",
8080 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8081 usePrebuilt: false,
8082 modNames: []string{"stublib", "prebuilt_stublib"},
8083 otherApexEnabled: []string{"true", "false"},
8084 },
8085 {
8086 name: "prebuilt_preferred",
8087 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8088 usePrebuilt: true,
8089 modNames: []string{"stublib", "prebuilt_stublib"},
8090 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8091 },
8092 {
8093 name: "only_prebuilt",
8094 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8095 usePrebuilt: true,
8096 modNames: []string{"stublib"},
8097 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8098 },
8099 }
8100
8101 for _, test := range tests {
8102 t.Run(test.name, func(t *testing.T) {
8103 for _, otherApexEnabled := range test.otherApexEnabled {
8104 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008105 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008106
8107 type modAndMkEntries struct {
8108 mod *cc.Module
8109 mkEntries android.AndroidMkEntries
8110 }
8111 entries := []*modAndMkEntries{}
8112
8113 // Gather shared lib modules that are installable
8114 for _, modName := range test.modNames {
8115 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8116 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8117 continue
8118 }
8119 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008120 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008121 continue
8122 }
Colin Crossaa255532020-07-03 13:18:24 -07008123 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008124 if ent.Disabled {
8125 continue
8126 }
8127 entries = append(entries, &modAndMkEntries{
8128 mod: mod,
8129 mkEntries: ent,
8130 })
8131 }
8132 }
8133 }
8134
8135 var entry *modAndMkEntries = nil
8136 for _, ent := range entries {
8137 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
8138 if entry != nil {
8139 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
8140 } else {
8141 entry = ent
8142 }
8143 }
8144 }
8145
8146 if entry == nil {
8147 t.Errorf("AndroidMk entry for \"stublib\" missing")
8148 } else {
8149 isPrebuilt := entry.mod.Prebuilt() != nil
8150 if isPrebuilt != test.usePrebuilt {
8151 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
8152 }
8153 if !entry.mod.IsStubs() {
8154 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
8155 }
8156 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
8157 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
8158 }
Jiyong Park892a98f2020-12-14 09:20:00 +09008159 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09008160 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09008161 if !android.InList(expected, cflags) {
8162 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
8163 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008164 }
8165 })
8166 }
8167 })
8168 }
8169}
8170
Martin Stjernholmdf298b32021-05-21 20:57:29 +01008171func TestHostApexInHostOnlyBuild(t *testing.T) {
8172 testApex(t, `
8173 apex {
8174 name: "myapex",
8175 host_supported: true,
8176 key: "myapex.key",
8177 updatable: false,
8178 payload_type: "zip",
8179 }
8180 apex_key {
8181 name: "myapex.key",
8182 public_key: "testkey.avbpubkey",
8183 private_key: "testkey.pem",
8184 }
8185 `,
8186 android.FixtureModifyConfig(func(config android.Config) {
8187 // We may not have device targets in all builds, e.g. in
8188 // prebuilts/build-tools/build-prebuilts.sh
8189 config.Targets[android.Android] = []android.Target{}
8190 }))
8191}
8192
Colin Crossc33e5212021-05-25 18:16:02 -07008193func TestApexJavaCoverage(t *testing.T) {
8194 bp := `
8195 apex {
8196 name: "myapex",
8197 key: "myapex.key",
8198 java_libs: ["mylib"],
8199 bootclasspath_fragments: ["mybootclasspathfragment"],
8200 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8201 updatable: false,
8202 }
8203
8204 apex_key {
8205 name: "myapex.key",
8206 public_key: "testkey.avbpubkey",
8207 private_key: "testkey.pem",
8208 }
8209
8210 java_library {
8211 name: "mylib",
8212 srcs: ["mylib.java"],
8213 apex_available: ["myapex"],
8214 compile_dex: true,
8215 }
8216
8217 bootclasspath_fragment {
8218 name: "mybootclasspathfragment",
8219 contents: ["mybootclasspathlib"],
8220 apex_available: ["myapex"],
8221 }
8222
8223 java_library {
8224 name: "mybootclasspathlib",
8225 srcs: ["mybootclasspathlib.java"],
8226 apex_available: ["myapex"],
8227 compile_dex: true,
8228 }
8229
8230 systemserverclasspath_fragment {
8231 name: "mysystemserverclasspathfragment",
8232 contents: ["mysystemserverclasspathlib"],
8233 apex_available: ["myapex"],
8234 }
8235
8236 java_library {
8237 name: "mysystemserverclasspathlib",
8238 srcs: ["mysystemserverclasspathlib.java"],
8239 apex_available: ["myapex"],
8240 compile_dex: true,
8241 }
8242 `
8243
8244 result := android.GroupFixturePreparers(
8245 PrepareForTestWithApexBuildComponents,
8246 prepareForTestWithMyapex,
8247 java.PrepareForTestWithJavaDefaultModules,
8248 android.PrepareForTestWithAndroidBuildComponents,
8249 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01008250 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
8251 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Colin Crossc33e5212021-05-25 18:16:02 -07008252 android.FixtureMergeEnv(map[string]string{
8253 "EMMA_INSTRUMENT": "true",
8254 }),
8255 ).RunTest(t)
8256
8257 // Make sure jacoco ran on both mylib and mybootclasspathlib
8258 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8259 t.Errorf("Failed to find jacoco rule for mylib")
8260 }
8261 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8262 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
8263 }
8264 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8265 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
8266 }
8267}
8268
Jiyong Park192600a2021-08-03 07:52:17 +00008269func TestProhibitStaticExecutable(t *testing.T) {
8270 testApexError(t, `executable mybin is static`, `
8271 apex {
8272 name: "myapex",
8273 key: "myapex.key",
8274 binaries: ["mybin"],
8275 min_sdk_version: "29",
8276 }
8277
8278 apex_key {
8279 name: "myapex.key",
8280 public_key: "testkey.avbpubkey",
8281 private_key: "testkey.pem",
8282 }
8283
8284 cc_binary {
8285 name: "mybin",
8286 srcs: ["mylib.cpp"],
8287 relative_install_path: "foo/bar",
8288 static_executable: true,
8289 system_shared_libs: [],
8290 stl: "none",
8291 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09008292 min_sdk_version: "29",
8293 }
8294 `)
8295
8296 testApexError(t, `executable mybin.rust is static`, `
8297 apex {
8298 name: "myapex",
8299 key: "myapex.key",
8300 binaries: ["mybin.rust"],
8301 min_sdk_version: "29",
8302 }
8303
8304 apex_key {
8305 name: "myapex.key",
8306 public_key: "testkey.avbpubkey",
8307 private_key: "testkey.pem",
8308 }
8309
8310 rust_binary {
8311 name: "mybin.rust",
8312 srcs: ["foo.rs"],
8313 static_executable: true,
8314 apex_available: ["myapex"],
8315 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00008316 }
8317 `)
8318}
8319
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008320func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01008321 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008322}